Search in sources :

Example 11 with CountsKey

use of org.neo4j.kernel.impl.store.counts.keys.CountsKey in project neo4j by neo4j.

the class CountsSnapshotSerializer method serialize.

public static void serialize(FlushableChannel channel, CountsSnapshot countsSnapshot) throws IOException {
    channel.putLong(countsSnapshot.getTxId());
    channel.putInt(countsSnapshot.getMap().size());
    for (Map.Entry<CountsKey, long[]> pair : countsSnapshot.getMap().entrySet()) {
        CountsKey key = pair.getKey();
        long[] value = pair.getValue();
        switch(key.recordType()) {
            case ENTITY_NODE:
                if (value.length != 1) {
                    throw new IllegalArgumentException("CountsKey of type " + key.recordType() + " has an unexpected value.");
                }
                NodeKey nodeKey = (NodeKey) key;
                channel.put(ENTITY_NODE.code);
                channel.putInt(nodeKey.getLabelId());
                channel.putLong(value[0]);
                break;
            case ENTITY_RELATIONSHIP:
                if (value.length != 1) {
                    throw new IllegalArgumentException("CountsKey of type " + key.recordType() + " has an unexpected value.");
                }
                RelationshipKey relationshipKey = (RelationshipKey) key;
                channel.put(ENTITY_RELATIONSHIP.code);
                channel.putInt(relationshipKey.getStartLabelId());
                channel.putInt(relationshipKey.getTypeId());
                channel.putInt(relationshipKey.getEndLabelId());
                channel.putLong(value[0]);
                break;
            case INDEX_SAMPLE:
                if (value.length != 2) {
                    throw new IllegalArgumentException("CountsKey of type " + key.recordType() + " has an unexpected value.");
                }
                IndexSampleKey indexSampleKey = (IndexSampleKey) key;
                channel.put(INDEX_SAMPLE.code);
                channel.putLong(indexSampleKey.indexId());
                channel.putLong(value[0]);
                channel.putLong(value[1]);
                break;
            case INDEX_STATISTICS:
                if (value.length != 2) {
                    throw new IllegalArgumentException("CountsKey of type " + key.recordType() + " has an unexpected value.");
                }
                IndexStatisticsKey indexStatisticsKey = (IndexStatisticsKey) key;
                channel.put(INDEX_STATISTICS.code);
                channel.putLong(indexStatisticsKey.indexId());
                channel.putLong(value[0]);
                channel.putLong(value[1]);
                break;
            case EMPTY:
                throw new IllegalArgumentException("CountsKey of type EMPTY cannot be serialized.");
            default:
                throw new IllegalArgumentException("The read CountsKey has an unknown type.");
        }
    }
}
Also used : IndexSampleKey(org.neo4j.kernel.impl.store.counts.keys.IndexSampleKey) IndexStatisticsKey(org.neo4j.kernel.impl.store.counts.keys.IndexStatisticsKey) CountsKey(org.neo4j.kernel.impl.store.counts.keys.CountsKey) RelationshipKey(org.neo4j.kernel.impl.store.counts.keys.RelationshipKey) Map(java.util.Map) NodeKey(org.neo4j.kernel.impl.store.counts.keys.NodeKey)

Example 12 with CountsKey

use of org.neo4j.kernel.impl.store.counts.keys.CountsKey in project neo4j by neo4j.

the class CountsTrackerTest method shouldBeAbleToReadUpToDateValueWhileAnotherThreadIsPerformingRotation.

@Test
public void shouldBeAbleToReadUpToDateValueWhileAnotherThreadIsPerformingRotation() throws Exception {
    // given
    CountsOracle oracle = someData();
    final int firstTransaction = 2, secondTransaction = 3;
    try (Lifespan life = new Lifespan()) {
        CountsTracker tracker = life.add(newTracker());
        oracle.update(tracker, firstTransaction);
        tracker.rotate(firstTransaction);
    }
    // when
    final CountsOracle delta = new CountsOracle();
    {
        CountsOracle.Node n1 = delta.node(1);
        // Label 4 has not been used before...
        CountsOracle.Node n2 = delta.node(1, 4);
        delta.relationship(n1, 1, n2);
        // relationshipType 2 has not been used before...
        delta.relationship(n2, 2, n1);
    }
    delta.update(oracle);
    try (Lifespan life = new Lifespan()) {
        final Barrier.Control barrier = new Barrier.Control();
        CountsTracker tracker = life.add(new CountsTracker(resourceManager.logProvider(), resourceManager.fileSystem(), resourceManager.pageCache(), Config.empty(), resourceManager.testPath()) {

            @Override
            protected boolean include(CountsKey countsKey, ReadableBuffer value) {
                barrier.reached();
                return super.include(countsKey, value);
            }
        });
        Future<Void> task = threading.execute((ThrowingFunction<CountsTracker, Void, RuntimeException>) t -> {
            try {
                delta.update(t, secondTransaction);
                t.rotate(secondTransaction);
            } catch (IOException e) {
                throw new AssertionError(e);
            }
            return null;
        }, tracker);
        // then
        barrier.await();
        oracle.verify(tracker);
        barrier.release();
        task.get();
        oracle.verify(tracker);
    }
}
Also used : CountsKey(org.neo4j.kernel.impl.store.counts.keys.CountsKey) Registers(org.neo4j.register.Registers) Assert.assertSame(org.junit.Assert.assertSame) Future(java.util.concurrent.Future) FakeClock(org.neo4j.time.FakeClock) CountsVisitor(org.neo4j.kernel.impl.api.CountsVisitor) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) CountsKeyFactory(org.neo4j.kernel.impl.store.counts.keys.CountsKeyFactory) DebugUtil.classNameContains(org.neo4j.kernel.impl.util.DebugUtil.classNameContains) Assert.fail(org.junit.Assert.fail) ReadableBuffer(org.neo4j.kernel.impl.store.kvstore.ReadableBuffer) STARTED(org.neo4j.test.rule.Resources.InitialLifecycle.STARTED) Config(org.neo4j.kernel.configuration.Config) CountsOracle(org.neo4j.kernel.impl.store.CountsOracle) Barrier(org.neo4j.test.Barrier) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Predicate(java.util.function.Predicate) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Register(org.neo4j.register.Register) IOFunction(org.neo4j.function.IOFunction) ThrowingFunction(org.neo4j.function.ThrowingFunction) File(java.io.File) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) DataInitializer(org.neo4j.kernel.impl.store.kvstore.DataInitializer) FILE_IN_EXISTING_DIRECTORY(org.neo4j.test.rule.Resources.TestPath.FILE_IN_EXISTING_DIRECTORY) DebugUtil.stackTraceContains(org.neo4j.kernel.impl.util.DebugUtil.stackTraceContains) RotationTimeoutException(org.neo4j.kernel.impl.store.kvstore.RotationTimeoutException) Rule(org.junit.Rule) CountsAccessor(org.neo4j.kernel.impl.api.CountsAccessor) Resources(org.neo4j.test.rule.Resources) ThreadingRule(org.neo4j.test.rule.concurrent.ThreadingRule) Clock(java.time.Clock) DebugUtil.methodIs(org.neo4j.kernel.impl.util.DebugUtil.methodIs) GraphDatabaseSettings(org.neo4j.graphdb.factory.GraphDatabaseSettings) Clocks(org.neo4j.time.Clocks) Assert.assertEquals(org.junit.Assert.assertEquals) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Predicates.all(org.neo4j.function.Predicates.all) Mockito.mock(org.mockito.Mockito.mock) Barrier(org.neo4j.test.Barrier) CountsKey(org.neo4j.kernel.impl.store.counts.keys.CountsKey) IOException(java.io.IOException) ReadableBuffer(org.neo4j.kernel.impl.store.kvstore.ReadableBuffer) CountsOracle(org.neo4j.kernel.impl.store.CountsOracle) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.Test)

Example 13 with CountsKey

use of org.neo4j.kernel.impl.store.counts.keys.CountsKey in project neo4j by neo4j.

the class InMemoryCountsStoreCountsSnapshotSerializerIntegrationTest method largeWorkloadOnPhysicalLogTest.

@Test
public void largeWorkloadOnPhysicalLogTest() throws IOException {
    //GIVEN
    try (FileSystemAbstraction fs = new DefaultFileSystemAbstraction()) {
        File tempFile = new File(testDir.directory(), "temp");
        StoreChannel rawChannel = fs.create(tempFile);
        Map<CountsKey, long[]> map = CountsStoreMapGenerator.simpleCountStoreMap(100000);
        CountsSnapshot countsSnapshot = new CountsSnapshot(1, map);
        CountsSnapshot recovered;
        //WHEN
        try (FlushableChannel tempChannel = new PhysicalFlushableChannel(rawChannel)) {
            serialize(tempChannel, countsSnapshot);
        }
        // close() here is necessary to flush the temp buffer into the channel so we can read it next
        // The try-with-resources closes the channel, need to reopen
        rawChannel = fs.open(tempFile, "r");
        try (ReadAheadChannel<StoreChannel> readAheadChannel = new ReadAheadChannel<>(rawChannel)) {
            recovered = deserialize(readAheadChannel);
            //THEN
            Assert.assertEquals(countsSnapshot.getTxId(), recovered.getTxId());
            for (Map.Entry<CountsKey, long[]> pair : countsSnapshot.getMap().entrySet()) {
                long[] value = recovered.getMap().get(pair.getKey());
                Assert.assertNotNull(value);
                Assert.assertArrayEquals(value, pair.getValue());
            }
            for (Map.Entry<CountsKey, long[]> pair : recovered.getMap().entrySet()) {
                long[] value = countsSnapshot.getMap().get(pair.getKey());
                Assert.assertNotNull(value);
                Assert.assertArrayEquals(value, pair.getValue());
            }
        }
    }
}
Also used : DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) PhysicalFlushableChannel(org.neo4j.kernel.impl.transaction.log.PhysicalFlushableChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) CountsKey(org.neo4j.kernel.impl.store.counts.keys.CountsKey) FlushableChannel(org.neo4j.kernel.impl.transaction.log.FlushableChannel) PhysicalFlushableChannel(org.neo4j.kernel.impl.transaction.log.PhysicalFlushableChannel) File(java.io.File) Map(java.util.Map) ReadAheadChannel(org.neo4j.kernel.impl.transaction.log.ReadAheadChannel) Test(org.junit.Test)

Aggregations

CountsKey (org.neo4j.kernel.impl.store.counts.keys.CountsKey)13 Test (org.junit.Test)9 HashMap (java.util.HashMap)7 NodeKey (org.neo4j.kernel.impl.store.counts.keys.NodeKey)6 Map (java.util.Map)4 File (java.io.File)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CountsVisitor (org.neo4j.kernel.impl.api.CountsVisitor)2 InMemoryClosableChannel (org.neo4j.kernel.impl.transaction.log.InMemoryClosableChannel)2 IOException (java.io.IOException)1 Clock (java.time.Clock)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)1 SECONDS (java.util.concurrent.TimeUnit.SECONDS)1 Predicate (java.util.function.Predicate)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotEquals (org.junit.Assert.assertNotEquals)1 Assert.assertSame (org.junit.Assert.assertSame)1