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.");
}
}
}
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);
}
}
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());
}
}
}
}
Aggregations