Skip to content

Commit

Permalink
Worked on Indices & Streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
JanWiemer committed Aug 23, 2024
1 parent 5d50390 commit f90f268
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 32 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.9
version=2.1.10
#=format for release: 2.1.25
#=format for snapshot: 2.0.26-2023-01-01 (on the way to 2.0.26)
#
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/org/jacis/index/JacisIndexRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ <IK> Stream<K> nonDistinctStreamFromNonUniqueIndexPrimaryKeys(JacisNonUniqueInde
if (regTxView != null) {
Set<K> add = regTxView.getPrimaryKeysAddedForNonUniqueIndex(indexName, ik);
Set<K> del = regTxView.getPrimaryKeysDeletedForNonUniqueIndex(indexName, ik);
resultStream = resultStream.filter(del::contains);
resultStream = resultStream.filter(key -> !del.contains(key));
resultStream = Stream.concat(resultStream, add.stream());
}
return resultStream;
Expand Down Expand Up @@ -406,7 +406,7 @@ <IK> Stream<K> nonDistinctStreamFromNonUniqueMultiIndexPrimaryKeys(JacisNonUniqu
if (regTxView != null) {
Set<K> add = regTxView.getPrimaryKeysAddedForNonUniqueIndex(indexName, indexKey);
Set<K> del = regTxView.getPrimaryKeysDeletedForNonUniqueIndex(indexName, indexKey);
resultStream = resultStream.filter(del::contains);
resultStream = resultStream.filter(key -> !del.contains(key));
resultStream = Stream.concat(resultStream, add.stream());
}
return resultStream;
Expand Down Expand Up @@ -523,7 +523,7 @@ <IK> Stream<K> streamFromUniqueIndexPrimaryKeys(JacisUniqueIndex<IK, K, TV> inde
if (indexKeys == null) {
return Stream.of();
}
return indexKeys.stream().map(key -> getFromUniqueIndexPrimaryKey(index, key)).distinct();
return indexKeys.stream().map(key -> getFromUniqueIndexPrimaryKey(index, key)).filter(Objects::nonNull).distinct();
}

<IK> Set<K> multiGetFromUniqueIndexPrimaryKeys(JacisUniqueIndex<IK, K, TV> index, Collection<IK> indexKeys) {
Expand All @@ -532,7 +532,10 @@ <IK> Set<K> multiGetFromUniqueIndexPrimaryKeys(JacisUniqueIndex<IK, K, TV> index
}
Set<K> res = new HashSet<>();
for (IK indexKey : indexKeys) {
res.add(getFromUniqueIndexPrimaryKey(index, indexKey));
K key = getFromUniqueIndexPrimaryKey(index, indexKey);
if (key != null) {
res.add(key);
}
}
return res;
}
Expand All @@ -543,14 +546,17 @@ <IK> TV getFromUniqueIndex(JacisUniqueIndex<IK, K, TV> index, IK indexKey) {
}

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

<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());
for (K primaryKey : primaryKeys) {
res.add(store.get(primaryKey));
TV obj = store.get(primaryKey);
if (obj != null) {
res.add(obj);
}
}
return res;
}
Expand All @@ -561,14 +567,17 @@ <IK> TV getFromUniqueIndexReadOnly(JacisUniqueIndex<IK, K, TV> index, IK indexKe
}

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

<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());
for (K primaryKey : primaryKeys) {
res.add(store.getReadOnly(primaryKey));
TV obj = store.getReadOnly(primaryKey);
if (obj != null) {
res.add(obj);
}
}
return res;
}
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/org/jacis/index/JacisNonUniqueIndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
Expand Down Expand Up @@ -58,7 +59,23 @@ public void testNonUniqueIndexSimpleAccess() {
assertEquals(1, index.getReadOnly("IDX-3").size());
assertEquals("A2", index.getReadOnly("IDX-2").iterator().next().getName());
assertEquals("A3", index.getReadOnly("IDX-3").iterator().next().getName());
assertEquals(2, index.streamReadOnly("IDX-1").count());
assertEquals(1, index.streamReadOnly("IDX-2").count());
assertEquals(1, index.streamReadOnly("IDX-3").count());
assertEquals(0, index.multiGetReadOnly(Arrays.asList("IDX-0")).size());
assertEquals(2, index.multiGetReadOnly(Arrays.asList("IDX-1")).size());
assertEquals(1, index.multiGetReadOnly(Arrays.asList("IDX-2")).size());
assertEquals(1, index.multiGetReadOnly(Arrays.asList("IDX-3")).size());
assertEquals(3, index.multiGetReadOnly(Arrays.asList("IDX-1", "IDX-2")).size());
assertEquals(4, index.multiGetReadOnly(Arrays.asList("IDX-1", "IDX-2", "IDX-3")).size());
assertEquals(4, index.multiGetReadOnly(Arrays.asList("IDX-0", "IDX-1", "IDX-2", "IDX-3")).size());
assertEquals(0, index.streamReadOnly(Arrays.asList("IDX-0")).count());
assertEquals(2, index.streamReadOnly(Arrays.asList("IDX-1")).count());
assertEquals(1, index.streamReadOnly(Arrays.asList("IDX-2")).count());
assertEquals(1, index.streamReadOnly(Arrays.asList("IDX-3")).count());
assertEquals(3, index.streamReadOnly(Arrays.asList("IDX-1", "IDX-2")).count());
assertEquals(4, index.streamReadOnly(Arrays.asList("IDX-1", "IDX-2", "IDX-3")).count());
assertEquals(4, index.streamReadOnly(Arrays.asList("IDX-0", "IDX-1", "IDX-2", "IDX-3")).count());
log.info("IDX-1: {}", index.getReadOnly("IDX-1"));
log.info("IDX-2: {}", index.getReadOnly("IDX-2"));
log.info("IDX-3: {}", index.getReadOnly("IDX-3"));
Expand All @@ -68,7 +85,23 @@ public void testNonUniqueIndexSimpleAccess() {
assertEquals(1, index.get("IDX-3").size());
assertEquals("A2", index.get("IDX-2").iterator().next().getName());
assertEquals("A3", index.get("IDX-3").iterator().next().getName());
assertEquals(2, index.stream("IDX-1").count());
assertEquals(1, index.stream("IDX-2").count());
assertEquals(1, index.stream("IDX-3").count());
assertEquals(0, index.multiGet(Arrays.asList("IDX-0")).size());
assertEquals(2, index.multiGet(Arrays.asList("IDX-1")).size());
assertEquals(1, index.multiGet(Arrays.asList("IDX-2")).size());
assertEquals(1, index.multiGet(Arrays.asList("IDX-3")).size());
assertEquals(3, index.multiGet(Arrays.asList("IDX-1", "IDX-2")).size());
assertEquals(4, index.multiGet(Arrays.asList("IDX-1", "IDX-2", "IDX-3")).size());
assertEquals(4, index.multiGet(Arrays.asList("IDX-0", "IDX-1", "IDX-2", "IDX-3")).size());
assertEquals(0, index.stream(Arrays.asList("IDX-0")).count());
assertEquals(2, index.stream(Arrays.asList("IDX-1")).count());
assertEquals(1, index.stream(Arrays.asList("IDX-2")).count());
assertEquals(1, index.stream(Arrays.asList("IDX-3")).count());
assertEquals(3, index.stream(Arrays.asList("IDX-1", "IDX-2")).count());
assertEquals(4, index.stream(Arrays.asList("IDX-1", "IDX-2", "IDX-3")).count());
assertEquals(4, index.stream(Arrays.asList("IDX-0", "IDX-1", "IDX-2", "IDX-3")).count());
});
}

Expand All @@ -85,6 +118,7 @@ public void testNonUniqueIndexTrackNullKeys() {
store.update("4", new TestObject("A3").setValue(5).setStrValue("IDX-3"));
});
assertEquals(2, index.getReadOnly(null).size());
assertEquals(2, index.streamReadOnly(Collections.singletonList((String) null)).count());
}

@Test
Expand All @@ -100,4 +134,29 @@ public void testNonUniqueIndexClearedDuringClear() {
assertEquals(0, idx.getReadOnly("IDX-1").size());
}

@Test
public void testNonUniqueIndexAddAndDelDuringTx() {
JacisTestHelper testHelper = new JacisTestHelper();
JacisStore<String, TestObject> store = testHelper.createTestStoreWithCloning();
JacisContainer container = store.getContainer();
container.withLocalTx(() -> {
store.update("1", new TestObject("A1").setValue(5).setStrValue("IDX-0"));
store.update("2", new TestObject("A2").setValue(5).setStrValue("IDX-0"));
});
JacisNonUniqueIndex<Object, String, TestObject> index = store.createNonUniqueIndex("IDX-NAME", TestObject::getStrValue);
container.withLocalTx(() -> {
store.update("3", new TestObject("A3").setValue(5).setStrValue("IDX-0"));
assertEquals(3, index.getReadOnly("IDX-0").size());
assertEquals(3, index.streamReadOnly("IDX-0").count());
store.update("3", new TestObject("A3").setValue(5).setStrValue("OTHER"));
assertEquals(2, index.getReadOnly("IDX-0").size());
assertEquals(2, index.streamReadOnly("IDX-0").count());
store.update("3", new TestObject("A3").setValue(5).setStrValue("IDX-0"));
assertEquals(3, index.getReadOnly("IDX-0").size());
assertEquals(3, index.streamReadOnly("IDX-0").count());
});
assertEquals(3, index.getReadOnly("IDX-0").size());
assertEquals(3, index.streamReadOnly("IDX-0").count());
}

}
76 changes: 53 additions & 23 deletions src/test/java/org/jacis/index/JacisUniqueIndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import static org.junit.Assert.*;

public class JacisUniqueIndexTest {
Expand Down Expand Up @@ -57,11 +59,36 @@ public void testUniqueIndexSimpleAccess() {
assertEquals("A2", index.getReadOnly("IDX-2").getName());
assertEquals("A3", index.getReadOnly("IDX-3").getName());
assertEquals("A4", index.getReadOnly("IDX-4").getName());
assertEquals(0, index.multiGetReadOnly(List.of("IDX-0")).size());
assertEquals(1, index.multiGetReadOnly(List.of("IDX-1")).size());
assertEquals(1, index.multiGetReadOnly(List.of("IDX-2")).size());
assertEquals(1, index.multiGetReadOnly(List.of("IDX-3")).size());
assertEquals(1, index.multiGetReadOnly(List.of("IDX-4")).size());
assertEquals(4, index.multiGetReadOnly(List.of("IDX-1", "IDX-2", "IDX-3", "IDX-4")).size());
assertEquals(0, index.streamReadOnly(List.of("IDX-0")).count());
assertEquals(1, index.streamReadOnly(List.of("IDX-1")).count());
assertEquals(1, index.streamReadOnly(List.of("IDX-2")).count());
assertEquals(1, index.streamReadOnly(List.of("IDX-3")).count());
assertEquals(1, index.streamReadOnly(List.of("IDX-4")).count());
assertEquals(4, index.streamReadOnly(List.of("IDX-1", "IDX-2", "IDX-3", "IDX-4")).count());
container.withLocalTx(() -> {
assertEquals("A1", index.get("IDX-1").getName());
assertEquals("A2", index.get("IDX-2").getName());
assertEquals("A3", index.get("IDX-3").getName());
assertEquals("A4", index.get("IDX-4").getName());
assertEquals(0, index.multiGet(List.of("IDX-0")).size());
assertEquals(1, index.multiGet(List.of("IDX-1")).size());
assertEquals(1, index.multiGet(List.of("IDX-2")).size());
assertEquals(1, index.multiGet(List.of("IDX-3")).size());
assertEquals(1, index.multiGet(List.of("IDX-4")).size());
assertEquals(4, index.multiGet(List.of("IDX-1", "IDX-2", "IDX-3", "IDX-4")).size());

assertEquals(0, index.stream(List.of("IDX-0")).count());
assertEquals(1, index.stream(List.of("IDX-1")).count());
assertEquals(1, index.stream(List.of("IDX-2")).count());
assertEquals(1, index.stream(List.of("IDX-3")).count());
assertEquals(1, index.stream(List.of("IDX-4")).count());
assertEquals(4, index.stream(List.of("IDX-1", "IDX-2", "IDX-3", "IDX-4")).count());
});
}

Expand Down Expand Up @@ -105,20 +132,45 @@ public void testUniqueIndexWhileModifiedInTransaction() {
assertEquals("A1", index.getReadOnly("IDX-1a").getName());
assertNull(index.getReadOnly("IDX-1b"));
assertNull(index.getReadOnly("IDX-1c"));
assertEquals(1, index.multiGetReadOnly(List.of("IDX-1a", "IDX-1b", "IDX-1c")).size());
assertEquals(1, index.multiGetReadOnly(List.of("IDX-1a")).size());
assertEquals(0, index.multiGetReadOnly(List.of("IDX-1b", "IDX-1c")).size());
assertEquals(1, index.streamReadOnly(List.of("IDX-1a", "IDX-1b", "IDX-1c")).count());
assertEquals(1, index.streamReadOnly(List.of("IDX-1a")).count());
assertEquals(0, index.streamReadOnly(List.of("IDX-1b", "IDX-1c")).count());
//
store.update("1", new TestObject("A1").setStrValue("IDX-1b"));
assertEquals("A1", index.getReadOnly("IDX-1b").getName());
assertNull(index.getReadOnly("IDX-1a"));
assertEquals("A1", index.getReadOnly("IDX-1b").getName());
assertNull(index.getReadOnly("IDX-1c"));
assertEquals(1, index.multiGetReadOnly(List.of("IDX-1a", "IDX-1b", "IDX-1c")).size());
assertEquals(1, index.multiGetReadOnly(List.of("IDX-1b")).size());
assertEquals(0, index.multiGetReadOnly(List.of("IDX-1a", "IDX-1c")).size());
assertEquals(1, index.streamReadOnly(List.of("IDX-1a", "IDX-1b", "IDX-1c")).count());
assertEquals(1, index.streamReadOnly(List.of("IDX-1b")).count());
assertEquals(0, index.streamReadOnly(List.of("IDX-1a", "IDX-1c")).count());
//
store.update("1", new TestObject("A1").setStrValue("IDX-1c"));
assertNull(index.getReadOnly("IDX-1a"));
assertNull(index.getReadOnly("IDX-1b"));
assertEquals("A1", index.getReadOnly("IDX-1c").getName());
assertEquals(1, index.multiGetReadOnly(List.of("IDX-1a", "IDX-1b", "IDX-1c")).size());
assertEquals(1, index.multiGetReadOnly(List.of("IDX-1c")).size());
assertEquals(0, index.multiGetReadOnly(List.of("IDX-1a", "IDX-1b")).size());
assertEquals(1, index.streamReadOnly(List.of("IDX-1a", "IDX-1b", "IDX-1c")).count());
assertEquals(1, index.streamReadOnly(List.of("IDX-1c")).count());
assertEquals(0, index.streamReadOnly(List.of("IDX-1a", "IDX-1b")).count());
//
store.update("1", new TestObject("A1").setStrValue("IDX-1b"));
assertNull(index.getReadOnly("IDX-1a"));
assertEquals("A1", index.getReadOnly("IDX-1b").getName());
assertNull(index.getReadOnly("IDX-1c"));
assertEquals(1, index.multiGetReadOnly(List.of("IDX-1a", "IDX-1b", "IDX-1c")).size());
assertEquals(1, index.multiGetReadOnly(List.of("IDX-1b")).size());
assertEquals(0, index.multiGetReadOnly(List.of("IDX-1a", "IDX-1c")).size());
assertEquals(1, index.streamReadOnly(List.of("IDX-1a", "IDX-1b", "IDX-1c")).count());
assertEquals(1, index.streamReadOnly(List.of("IDX-1b")).count());
assertEquals(0, index.streamReadOnly(List.of("IDX-1a", "IDX-1c")).count());
});
assertNull(index.getReadOnly("IDX-1a"));
assertEquals("A1", index.getReadOnly("IDX-1b").getName());
Expand Down Expand Up @@ -303,26 +355,4 @@ public void testUniqueIndexAddByChangeDuringTx() {
assertEquals(3, index.getReadOnly("IDX-0").size());
}

@Test
public void testUniqueIndexAddAndDelDuringTx() {
JacisTestHelper testHelper = new JacisTestHelper();
JacisStore<String, TestObject> store = testHelper.createTestStoreWithCloning();
JacisContainer container = store.getContainer();
container.withLocalTx(() -> {
store.update("1", new TestObject("A1").setValue(5).setStrValue("IDX-0"));
store.update("2", new TestObject("A2").setValue(5).setStrValue("IDX-0"));
});
JacisNonUniqueIndex<Object, String, TestObject> index = store.createNonUniqueIndex("IDX-NAME", TestObject::getStrValue);
container.withLocalTx(() -> {
store.update("3", new TestObject("A3").setValue(5).setStrValue("IDX-0"));
assertEquals(3, index.getReadOnly("IDX-0").size());
store.update("3", new TestObject("A3").setValue(5).setStrValue("OTHER"));
assertEquals(2, index.getReadOnly("IDX-0").size());
store.update("3", new TestObject("A3").setValue(5).setStrValue("IDX-0"));
assertEquals(3, index.getReadOnly("IDX-0").size());
});
assertEquals(3, index.getReadOnly("IDX-0").size());
}


}

0 comments on commit f90f268

Please sign in to comment.