use of org.neo4j.collection.primitive.PrimitiveLongSet in project neo4j by neo4j.
the class IndexingServiceTest method recoveredUpdatesShouldBeApplied.
@SuppressWarnings("unchecked")
@Test
public void recoveredUpdatesShouldBeApplied() throws Exception {
// Given
final long nodeId1 = 1;
final long nodeId2 = 2;
final PrimitiveLongSet nodeIds = setOf(nodeId1, nodeId2);
final NodeUpdates nodeUpdate1 = addNodeUpdate(nodeId1, "foo");
final NodeUpdates nodeUpdate2 = addNodeUpdate(nodeId2, "bar");
final Set<NodeUpdates> nodeUpdates = asSet(nodeUpdate1, nodeUpdate2);
final AtomicBoolean applyingRecoveredDataCalled = new AtomicBoolean();
final AtomicBoolean appliedRecoveredDataCalled = new AtomicBoolean();
// Mockito not used here because it does not work well with mutable objects (set of recovered node ids in
// this case, which is cleared at the end of recovery).
// See https://code.google.com/p/mockito/issues/detail?id=126 and
// https://groups.google.com/forum/#!topic/mockito/_A4BpsEAY9s
IndexingService.Monitor monitor = new IndexingService.MonitorAdapter() {
@Override
public void applyingRecoveredData(PrimitiveLongSet recoveredNodeIds) {
assertEquals(nodeIds, recoveredNodeIds);
applyingRecoveredDataCalled.set(true);
}
@Override
public void appliedRecoveredData(Iterable<NodeUpdates> updates) {
assertEquals(nodeUpdates, Iterables.asSet(updates));
appliedRecoveredDataCalled.set(true);
}
};
IndexingService indexing = newIndexingServiceWithMockedDependencies(populator, accessor, withData(), monitor);
doAnswer(nodeUpdatesAnswer(nodeUpdate1)).when(storeView).nodeAsUpdates(eq(nodeId1), any(Collection.class));
doAnswer(nodeUpdatesAnswer(nodeUpdate2)).when(storeView).nodeAsUpdates(eq(nodeId2), any(Collection.class));
// When
life.init();
IndexUpdates updates = nodeIdsAsIndexUpdates(nodeIds);
indexing.apply(updates);
life.start();
// Then
assertTrue("applyingRecoveredData was not called", applyingRecoveredDataCalled.get());
assertTrue("appliedRecoveredData was not called", appliedRecoveredDataCalled.get());
}
use of org.neo4j.collection.primitive.PrimitiveLongSet in project neo4j by neo4j.
the class GBPTreeTest method shouldSplitCorrectly.
/* Randomized tests */
@Test
public void shouldSplitCorrectly() throws Exception {
// GIVEN
GBPTree<MutableLong, MutableLong> index = createIndex(256);
// WHEN
int count = 1_000;
PrimitiveLongSet seen = Primitive.longSet(count);
try (Writer<MutableLong, MutableLong> writer = index.writer()) {
for (int i = 0; i < count; i++) {
MutableLong key;
do {
key = new MutableLong(random.nextInt(100_000));
} while (!seen.add(key.longValue()));
MutableLong value = new MutableLong(i);
writer.put(key, value);
seen.add(key.longValue());
}
}
// THEN
try (RawCursor<Hit<MutableLong, MutableLong>, IOException> cursor = index.seek(new MutableLong(0), new MutableLong(Long.MAX_VALUE))) {
long prev = -1;
while (cursor.next()) {
MutableLong hit = cursor.get().key();
if (hit.longValue() < prev) {
fail(hit + " smaller than prev " + prev);
}
prev = hit.longValue();
assertTrue(seen.remove(hit.longValue()));
}
if (!seen.isEmpty()) {
fail("expected hits " + Arrays.toString(PrimitiveLongCollections.asArray(seen.iterator())));
}
}
}
use of org.neo4j.collection.primitive.PrimitiveLongSet in project neo4j by neo4j.
the class TransactionRecordStateTest method shouldCreateEqualNodePropertyUpdatesOnRecoveryOfCreatedNode.
@Test
public void shouldCreateEqualNodePropertyUpdatesOnRecoveryOfCreatedNode() throws Exception {
/* There was an issue where recovering a tx where a node with a label and a property
* was created resulted in two exact copies of NodePropertyUpdates. */
// GIVEN
NeoStores neoStores = neoStoresRule.open();
long nodeId = 0;
int labelId = 5, propertyKeyId = 7;
// -- an index
long ruleId = 0;
TransactionRecordState recordState = newTransactionRecordState(neoStores);
SchemaRule rule = indexRule(ruleId, forLabel(labelId, propertyKeyId), PROVIDER_DESCRIPTOR);
recordState.createSchemaRule(rule);
apply(neoStores, recordState);
// -- and a tx creating a node with that label and property key
recordState = newTransactionRecordState(neoStores);
recordState.nodeCreate(nodeId);
recordState.addLabelToNode(labelId, nodeId);
recordState.nodeAddProperty(nodeId, propertyKeyId, "Neo");
// WHEN
PhysicalTransactionRepresentation transaction = transactionRepresentationOf(recordState);
NodePropertyCommandsExtractor extractor = new NodePropertyCommandsExtractor();
transaction.accept(extractor);
// THEN
// -- later recovering that tx, there should be only one update
assertTrue(extractor.containsAnyNodeOrPropertyUpdate());
PrimitiveLongSet recoveredNodeIds = Primitive.longSet();
recoveredNodeIds.addAll(extractor.nodeCommandsById().iterator());
recoveredNodeIds.addAll(extractor.propertyCommandsByNodeIds().iterator());
assertEquals(1, recoveredNodeIds.size());
assertEquals(nodeId, recoveredNodeIds.iterator().next());
}
use of org.neo4j.collection.primitive.PrimitiveLongSet in project neo4j by neo4j.
the class StringEncoderTest method shouldEncodeStringWithAnyLength.
@Test
public void shouldEncodeStringWithAnyLength() throws Exception {
// GIVEN
Encoder encoder = new StringEncoder();
// WHEN
PrimitiveLongSet encoded = Primitive.longSet();
int total = 1_000, duplicates = 0;
for (int i = 0; i < total; i++) {
// THEN
long encode = encoder.encode(abcStringOfLength(i));
assertTrue(encode != 0);
if (!encoded.add(encode)) {
duplicates++;
}
}
assertTrue(((float) duplicates / (float) total) < 0.01f);
}
use of org.neo4j.collection.primitive.PrimitiveLongSet in project neo4j-apoc-procedures by neo4j-contrib.
the class WeaklyConnectedComponents method go.
private PrimitiveLongSet go(Node node, Direction direction, List<CCVar> result) {
PrimitiveLongSet visitedIDs = Primitive.longSet(0);
Stack<Node> frontierList = new Stack<Node>();
frontierList.push(node);
visitedIDs.add(node.getId());
result.add(new CCVar(node.getId() + "", node.getLabels().iterator().next().name()));
while (!frontierList.isEmpty()) {
node = frontierList.pop();
Iterator<Relationship> itR = node.getRelationships(direction).iterator();
while (itR.hasNext()) {
Node child = itR.next().getOtherNode(node);
if (visitedIDs.contains(child.getId())) {
continue;
}
visitedIDs.add(child.getId());
frontierList.push(child);
result.add(new CCVar(child.getId() + "", child.getLabels().iterator().next().name()));
}
}
return visitedIDs;
}
Aggregations