Search in sources :

Example 16 with PrimitiveLongSet

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());
}
Also used : NodeUpdates(org.neo4j.kernel.api.index.NodeUpdates) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DirectIndexUpdates(org.neo4j.kernel.impl.transaction.state.DirectIndexUpdates) IndexUpdates(org.neo4j.kernel.impl.transaction.state.IndexUpdates) PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) BoundedIterable(org.neo4j.helpers.collection.BoundedIterable) Iterators.asCollection(org.neo4j.helpers.collection.Iterators.asCollection) Collection(java.util.Collection) Test(org.junit.Test)

Example 17 with PrimitiveLongSet

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())));
        }
    }
}
Also used : MutableLong(org.apache.commons.lang3.mutable.MutableLong) PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) IOException(java.io.IOException) Test(org.junit.Test)

Example 18 with PrimitiveLongSet

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());
}
Also used : PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) NeoStores(org.neo4j.kernel.impl.store.NeoStores) SchemaRule(org.neo4j.storageengine.api.schema.SchemaRule) NodePropertyCommandsExtractor(org.neo4j.kernel.impl.api.index.NodePropertyCommandsExtractor) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation) Test(org.junit.Test)

Example 19 with PrimitiveLongSet

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);
}
Also used : PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) Test(org.junit.Test)

Example 20 with PrimitiveLongSet

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;
}
Also used : PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) CCVar(apoc.algo.wcc.CCVar)

Aggregations

PrimitiveLongSet (org.neo4j.collection.primitive.PrimitiveLongSet)36 Test (org.junit.Test)27 PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)5 IndexQuery (org.neo4j.kernel.api.schema_new.IndexQuery)5 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)4 ReadOperations (org.neo4j.kernel.api.ReadOperations)4 Statement (org.neo4j.kernel.api.Statement)4 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)4 IOException (java.io.IOException)3 CCVar (apoc.algo.wcc.CCVar)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CCResult (apoc.result.CCResult)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StringReader (java.io.StringReader)1 java.util (java.util)1 Collection (java.util.Collection)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1