Skip to content

Commit

Permalink
improved stream access on indices (-> v.2.1.9)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanWiemer committed Aug 21, 2024
1 parent 80b8cd9 commit 5d50390
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 31 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
############################
# JACIS VERSION:
############################
version=2.1.8
version=2.1.9
#=format for release: 2.1.25
#=format for snapshot: 2.0.26-2023-01-01 (on the way to 2.0.26)
#
Expand Down
97 changes: 88 additions & 9 deletions src/main/java/org/jacis/index/JacisIndexRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,26 @@ void checkUniqueIndexProperty(JacisUniqueIndex<?, K, TV> idx, Object newIndexKey
//----- Access methods for NON-UNIQUE INDEX
//----------------------------------------------------------------------------------------------------------

<IK> Stream<K> nonDistinctStreamFromNonUniqueIndexPrimaryKeys(JacisNonUniqueIndex<IK, K, TV> index, IK indexKey) {
JacisIndexRegistryTxView<K, TV> regTxView = store.getIndexRegistryTransactionView(); // null if no TX
String indexName = index.getIndexName();
Map<Object, Set<K>> indexMap = nonUniqueIndexDataMap.get(indexName);
Object ik = index.wrapIndexKey(indexKey);
Stream<K> resultStream = indexMap.getOrDefault(ik, Collections.emptySet()).stream();
if (regTxView != null) {
Set<K> add = regTxView.getPrimaryKeysAddedForNonUniqueIndex(indexName, ik);
Set<K> del = regTxView.getPrimaryKeysDeletedForNonUniqueIndex(indexName, ik);
resultStream = resultStream.filter(del::contains);
resultStream = Stream.concat(resultStream, add.stream());
}
return resultStream;
}

<IK> Stream<K> streamFromNonUniqueIndexPrimaryKeys(JacisNonUniqueIndex<IK, K, TV> index, IK indexKey) {
return nonDistinctStreamFromNonUniqueIndexPrimaryKeys(index, indexKey).distinct();
}


<IK> Set<K> getFromNonUniqueIndexPrimaryKeys(JacisNonUniqueIndex<IK, K, TV> index, IK indexKey) {
JacisIndexRegistryTxView<K, TV> regTxView = store.getIndexRegistryTransactionView(); // null if no TX
String indexName = index.getIndexName();
Expand All @@ -292,7 +312,7 @@ <IK> Set<K> getFromNonUniqueIndexPrimaryKeys(JacisNonUniqueIndex<IK, K, TV> inde
Set<K> add = regTxView.getPrimaryKeysAddedForNonUniqueIndex(indexName, ik);
Set<K> del = regTxView.getPrimaryKeysDeletedForNonUniqueIndex(indexName, ik);
if (!add.isEmpty() || !del.isEmpty()) {
Set<K> res = new HashSet(indexMap.getOrDefault(ik, Collections.emptySet()));
Set<K> res = new HashSet<>(indexMap.getOrDefault(ik, Collections.emptySet()));
res.removeAll(del);
res.addAll(add);
return res;
Expand All @@ -301,6 +321,13 @@ <IK> Set<K> getFromNonUniqueIndexPrimaryKeys(JacisNonUniqueIndex<IK, K, TV> inde
return indexMap.getOrDefault(ik, Collections.emptySet());
}

<IK> Stream<K> streamFromNonUniqueIndexPrimaryKeys(JacisNonUniqueIndex<IK, K, TV> index, Collection<IK> indexKeys) {
if (indexKeys == null) {
return Stream.of();
}
return indexKeys.stream().flatMap(key -> nonDistinctStreamFromNonUniqueIndexPrimaryKeys(index, key)).distinct();
}

<IK> Set<K> multiGetFromNonUniqueIndexPrimaryKeys(JacisNonUniqueIndex<IK, K, TV> index, Collection<IK> indexKeys) {
if (indexKeys == null || indexKeys.isEmpty()) {
return Collections.emptySet();
Expand All @@ -313,8 +340,7 @@ <IK> Set<K> multiGetFromNonUniqueIndexPrimaryKeys(JacisNonUniqueIndex<IK, K, TV>
}

<IK> Stream<TV> streamFromNonUniqueIndex(JacisNonUniqueIndex<IK, K, TV> index, IK indexKey) {
Set<K> primaryKeys = getFromNonUniqueIndexPrimaryKeys(index, indexKey);
return primaryKeys.stream().map(primaryKey -> store.get(primaryKey));
return streamFromNonUniqueIndexPrimaryKeys(index, indexKey).map(store::get);
}

<IK> Collection<TV> getFromNonUniqueIndex(JacisNonUniqueIndex<IK, K, TV> index, IK indexKey) {
Expand All @@ -326,6 +352,10 @@ <IK> Collection<TV> getFromNonUniqueIndex(JacisNonUniqueIndex<IK, K, TV> index,
return res;
}

<IK> Stream<TV> streamFromNonUniqueIndex(JacisNonUniqueIndex<IK, K, TV> index, Collection<IK> indexKeys) {
return streamFromNonUniqueIndexPrimaryKeys(index, indexKeys).map(store::get);
}

<IK> Collection<TV> multiGetFromNonUniqueIndex(JacisNonUniqueIndex<IK, K, TV> index, Collection<IK> indexKeys) {
Set<K> primaryKeys = multiGetFromNonUniqueIndexPrimaryKeys(index, indexKeys);
Collection<TV> res = new ArrayList<>(primaryKeys.size());
Expand All @@ -336,8 +366,7 @@ <IK> Collection<TV> multiGetFromNonUniqueIndex(JacisNonUniqueIndex<IK, K, TV> in
}

<IK> Stream<TV> streamFromNonUniqueIndexReadOnly(JacisNonUniqueIndex<IK, K, TV> index, IK indexKey) {
Set<K> primaryKeys = getFromNonUniqueIndexPrimaryKeys(index, indexKey);
return primaryKeys.stream().map(primaryKey -> store.getReadOnly(primaryKey));
return streamFromNonUniqueIndexPrimaryKeys(index, indexKey).map(store::getReadOnly);
}

<IK> Collection<TV> getFromNonUniqueIndexReadOnly(JacisNonUniqueIndex<IK, K, TV> index, IK indexKey) {
Expand All @@ -349,6 +378,13 @@ <IK> Collection<TV> getFromNonUniqueIndexReadOnly(JacisNonUniqueIndex<IK, K, TV>
return res;
}

<IK> Stream<TV> streamFromNonUniqueIndexReadOnly(JacisNonUniqueIndex<IK, K, TV> index, Collection<IK> indexKeys) {
if (indexKeys == null) {
return Stream.of();
}
return streamFromNonUniqueIndexPrimaryKeys(index, indexKeys).map(store::getReadOnly);
}

<IK> Collection<TV> multiGetFromNonUniqueIndexReadOnly(JacisNonUniqueIndex<IK, K, TV> index, Collection<IK> indexKeys) {
Set<K> primaryKeys = multiGetFromNonUniqueIndexPrimaryKeys(index, indexKeys);
Collection<TV> res = new ArrayList<>(primaryKeys.size());
Expand All @@ -362,6 +398,24 @@ <IK> Collection<TV> multiGetFromNonUniqueIndexReadOnly(JacisNonUniqueIndex<IK, K
//----- Access methods for NON-UNIQUE MULTI-INDEX
//----------------------------------------------------------------------------------------------------------

<IK> Stream<K> nonDistinctStreamFromNonUniqueMultiIndexPrimaryKeys(JacisNonUniqueMultiIndex<IK, K, TV> index, IK indexKey) {
JacisIndexRegistryTxView<K, TV> regTxView = store.getIndexRegistryTransactionView(); // null if no TX
String indexName = index.getIndexName();
Map<Object, Set<K>> indexMap = nonUniqueIndexDataMap.get(indexName);
Stream<K> resultStream = indexMap.getOrDefault(indexKey, Collections.emptySet()).stream();
if (regTxView != null) {
Set<K> add = regTxView.getPrimaryKeysAddedForNonUniqueIndex(indexName, indexKey);
Set<K> del = regTxView.getPrimaryKeysDeletedForNonUniqueIndex(indexName, indexKey);
resultStream = resultStream.filter(del::contains);
resultStream = Stream.concat(resultStream, add.stream());
}
return resultStream;
}

<IK> Stream<K> streamFromNonUniqueMultiIndexPrimaryKeys(JacisNonUniqueMultiIndex<IK, K, TV> index, IK indexKey) {
return nonDistinctStreamFromNonUniqueMultiIndexPrimaryKeys(index, indexKey).distinct();
}

<IK> Set<K> getFromNonUniqueMultiIndexPrimaryKeys(JacisNonUniqueMultiIndex<IK, K, TV> index, IK indexKey) {
JacisIndexRegistryTxView<K, TV> regTxView = store.getIndexRegistryTransactionView(); // null if no TX
String indexName = index.getIndexName();
Expand All @@ -379,6 +433,10 @@ <IK> Set<K> getFromNonUniqueMultiIndexPrimaryKeys(JacisNonUniqueMultiIndex<IK, K
return indexMap.getOrDefault(indexKey, Collections.emptySet());
}

<IK> Stream<K> streamFromNonUniqueMultiIndexPrimaryKeys(JacisNonUniqueMultiIndex<IK, K, TV> index, Collection<IK> indexKeys) {
return indexKeys.stream().flatMap(key -> nonDistinctStreamFromNonUniqueMultiIndexPrimaryKeys(index, key)).distinct();
}

<IK> Set<K> multiGetFromNonUniqueMultiIndexPrimaryKeys(JacisNonUniqueMultiIndex<IK, K, TV> index, Collection<IK> indexKeys) {
if (indexKeys == null || indexKeys.isEmpty()) {
return Collections.emptySet();
Expand All @@ -391,8 +449,7 @@ <IK> Set<K> multiGetFromNonUniqueMultiIndexPrimaryKeys(JacisNonUniqueMultiIndex<
}

<IK> Stream<TV> streamFromNonUniqueMultiIndex(JacisNonUniqueMultiIndex<IK, K, TV> index, IK indexKey) {
Set<K> primaryKeys = getFromNonUniqueMultiIndexPrimaryKeys(index, indexKey);
return primaryKeys.stream().map(primaryKey -> store.get(primaryKey));
return streamFromNonUniqueMultiIndexPrimaryKeys(index, indexKey).map(store::get);
}

<IK> Collection<TV> getFromNonUniqueMultiIndex(JacisNonUniqueMultiIndex<IK, K, TV> index, IK indexKey) {
Expand All @@ -404,6 +461,10 @@ <IK> Collection<TV> getFromNonUniqueMultiIndex(JacisNonUniqueMultiIndex<IK, K, T
return res;
}

<IK> Stream<TV> streamFromNonUniqueMultiIndex(JacisNonUniqueMultiIndex<IK, K, TV> index, Collection<IK> indexKeys) {
return streamFromNonUniqueMultiIndexPrimaryKeys(index, indexKeys).map(store::get);
}

<IK> Collection<TV> multiGetFromNonUniqueMultiIndex(JacisNonUniqueMultiIndex<IK, K, TV> index, Collection<IK> indexKeys) {
Set<K> primaryKeys = multiGetFromNonUniqueMultiIndexPrimaryKeys(index, indexKeys);
Collection<TV> res = new ArrayList<>(primaryKeys.size());
Expand All @@ -414,8 +475,7 @@ <IK> Collection<TV> multiGetFromNonUniqueMultiIndex(JacisNonUniqueMultiIndex<IK,
}

<IK> Stream<TV> streamFromNonUniqueMultiIndexReadOnly(JacisNonUniqueMultiIndex<IK, K, TV> index, IK indexKey) {
Set<K> primaryKeys = getFromNonUniqueMultiIndexPrimaryKeys(index, indexKey);
return primaryKeys.stream().map(primaryKey -> store.getReadOnly(primaryKey));
return streamFromNonUniqueMultiIndexPrimaryKeys(index, indexKey).map(store::getReadOnly);
}

<IK> Collection<TV> getFromNonUniqueMultiIndexReadOnly(JacisNonUniqueMultiIndex<IK, K, TV> index, IK indexKey) {
Expand All @@ -427,6 +487,10 @@ <IK> Collection<TV> getFromNonUniqueMultiIndexReadOnly(JacisNonUniqueMultiIndex<
return res;
}

<IK> Stream<TV> streamFromNonUniqueMultiIndexReadOnly(JacisNonUniqueMultiIndex<IK, K, TV> index, Collection<IK> indexKeys) {
return streamFromNonUniqueMultiIndexPrimaryKeys(index, indexKeys).map(store::getReadOnly);
}

<IK> Collection<TV> multiGetFromNonUniqueMultiIndexReadOnly(JacisNonUniqueMultiIndex<IK, K, TV> index, Collection<IK> indexKeys) {
Set<K> primaryKeys = multiGetFromNonUniqueMultiIndexPrimaryKeys(index, indexKeys);
Collection<TV> res = new ArrayList<>(primaryKeys.size());
Expand Down Expand Up @@ -455,6 +519,13 @@ <IK> K getFromUniqueIndexPrimaryKey(JacisUniqueIndex<IK, K, TV> index, IK indexK
return indexMap.get(indexKey);
}

<IK> Stream<K> streamFromUniqueIndexPrimaryKeys(JacisUniqueIndex<IK, K, TV> index, Collection<IK> indexKeys) {
if (indexKeys == null) {
return Stream.of();
}
return indexKeys.stream().map(key -> getFromUniqueIndexPrimaryKey(index, key)).distinct();
}

<IK> Set<K> multiGetFromUniqueIndexPrimaryKeys(JacisUniqueIndex<IK, K, TV> index, Collection<IK> indexKeys) {
if (indexKeys == null || indexKeys.isEmpty()) {
return Collections.emptySet();
Expand All @@ -471,6 +542,10 @@ <IK> TV getFromUniqueIndex(JacisUniqueIndex<IK, K, TV> index, IK indexKey) {
return primaryKey == null ? null : store.get(primaryKey);
}

<IK> Stream<TV> streamFromUniqueIndex(JacisUniqueIndex<IK, K, TV> index, Collection<IK> indexKeys) {
return streamFromUniqueIndexPrimaryKeys(index, indexKeys).map(store::get);
}

<IK> Collection<TV> multiGetFromUniqueIndex(JacisUniqueIndex<IK, K, TV> index, Collection<IK> indexKeys) {
Set<K> primaryKeys = multiGetFromUniqueIndexPrimaryKeys(index, indexKeys);
Collection<TV> res = new ArrayList<>(primaryKeys.size());
Expand All @@ -485,6 +560,10 @@ <IK> TV getFromUniqueIndexReadOnly(JacisUniqueIndex<IK, K, TV> index, IK indexKe
return primaryKey == null ? null : store.getReadOnly(primaryKey);
}

<IK> Stream<TV> streamFromUniqueIndexReadOnly(JacisUniqueIndex<IK, K, TV> index, Collection<IK> indexKeys) {
return streamFromUniqueIndexPrimaryKeys(index, indexKeys).map(store::getReadOnly);
}

<IK> Collection<TV> multiGetFromUniqueIndexReadOnly(JacisUniqueIndex<IK, K, TV> index, Collection<IK> indexKeys) {
Set<K> primaryKeys = multiGetFromUniqueIndexPrimaryKeys(index, indexKeys);
Collection<TV> res = new ArrayList<>(primaryKeys.size());
Expand Down
55 changes: 45 additions & 10 deletions src/main/java/org/jacis/index/JacisNonUniqueIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public class JacisNonUniqueIndex<IK, K, TV> extends AbstractJacisIndex<IK, K, TV
super(indexName, indexKeyFunction, indexRegistry);
}

/**
* Returns a stream of the primary keys for the passed index key.
* The primary keys are the keys used to store the object in the store.
*
* @param indexKey The index key of the desired entry.
* @return a stream of the primary keys for the passed index key.
*/
public Stream<K> streamPrimaryKeys(IK indexKey) {
return indexRegistry.streamFromNonUniqueIndexPrimaryKeys(this, indexKey);
}

/**
* Returns the primary keys for the passed index key.
* The primary keys are the keys used to store the object in the store.
Expand All @@ -51,25 +62,25 @@ public Stream<TV> stream(IK indexKey) {
}

/**
* Returns a stream of the read only values for the passed index key.
* For details see the {@link JacisStore#getReadOnly(Object)} method.
* Returns the values for the passed index key.
* For details see the {@link JacisStore#get(Object)} method.
*
* @param indexKey The index key of the desired entry.
* @return a stream of the read only values for the passed key.
* @return the values for the passed index key.
*/
public Stream<TV> streamReadOnly(IK indexKey) {
return indexRegistry.streamFromNonUniqueIndexReadOnly(this, indexKey);
public Collection<TV> get(IK indexKey) {
return indexRegistry.getFromNonUniqueIndex(this, indexKey);
}

/**
* Returns the values for the passed index key.
* For details see the {@link JacisStore#get(Object)} method.
* Returns a stream of the read only values for the passed index key.
* For details see the {@link JacisStore#getReadOnly(Object)} method.
*
* @param indexKey The index key of the desired entry.
* @return the values for the passed index key.
* @return a stream of the read only values for the passed key.
*/
public Collection<TV> get(IK indexKey) {
return indexRegistry.getFromNonUniqueIndex(this, indexKey);
public Stream<TV> streamReadOnly(IK indexKey) {
return indexRegistry.streamFromNonUniqueIndexReadOnly(this, indexKey);
}

/**
Expand All @@ -83,6 +94,18 @@ public Collection<TV> getReadOnly(IK indexKey) {
return indexRegistry.getFromNonUniqueIndexReadOnly(this, indexKey);
}


/**
* Returns a stream of the values for the passed index keys.
* For details see the {@link JacisStore#get(Object)} method.
*
* @param indexKeys The index keys of the desired entries.
* @return a stream of the values for the passed index keys.
*/
public Stream<TV> stream(Collection<IK> indexKeys) {
return indexRegistry.streamFromNonUniqueIndex(this, indexKeys);
}

/**
* Returns the values for the passed index keys.
* For details see the {@link JacisStore#get(Object)} method.
Expand All @@ -94,6 +117,18 @@ public Collection<TV> multiGet(Collection<IK> indexKeys) {
return indexRegistry.multiGetFromNonUniqueIndex(this, indexKeys);
}


/**
* Returns a stream of the read only values for the passed index keys.
* For details see the {@link JacisStore#getReadOnly(Object)} method.
*
* @param indexKeys The index keys of the desired entries.
* @return a stream of the read only values for the passed keys.
*/
public Stream<TV> streamReadOnly(Collection<IK> indexKeys) {
return indexRegistry.streamFromNonUniqueIndexReadOnly(this, indexKeys);
}

/**
* Returns the read only values for the passed index keys.
* For details see the {@link JacisStore#getReadOnly(Object)} method.
Expand Down
Loading

0 comments on commit 5d50390

Please sign in to comment.