use of org.neo4j.helpers.collection.PrefetchingIterator in project neo4j by neo4j.
the class EncodingIdMapperTest method shouldDetectCorrectDuplicateInputIdsWhereManyAccidentalInManyGroups.
@Test
public void shouldDetectCorrectDuplicateInputIdsWhereManyAccidentalInManyGroups() throws Exception {
// GIVEN
final ControlledEncoder encoder = new ControlledEncoder(new LongEncoder());
IdMapper mapper = mapper(encoder, Radix.LONG, NO_MONITOR);
final int idsPerGroup = 20, groups = 5;
final AtomicReference<Group> group = new AtomicReference<>();
InputIterable<Object> ids = SimpleInputIteratorWrapper.wrap("source", new Iterable<Object>() {
@Override
public Iterator<Object> iterator() {
return new PrefetchingIterator<Object>() {
private int i;
@Override
protected Object fetchNextOrNull() {
// Change group every <idsPerGroup> id
if (i % idsPerGroup == 0) {
int groupId = i / idsPerGroup;
if (groupId == groups) {
return null;
}
group.set(new Group.Adapter(groupId, "Group " + groupId));
}
try {
// i.e. all first 10% in each group collides with all other first 10% in each group
if (i % idsPerGroup < 2) {
// Let these colliding values encode into the same eId as well,
// so that they are definitely marked as collisions
encoder.useThisIdToEncodeNoMatterWhatComesIn(Long.valueOf(1234567));
return Long.valueOf(i % idsPerGroup);
}
// The other 90% will be accidental collisions for something else
encoder.useThisIdToEncodeNoMatterWhatComesIn(Long.valueOf(123456 - group.get().id()));
return Long.valueOf(i);
} finally {
i++;
}
}
};
}
});
// WHEN
long actualId = 0;
for (Object id : ids) {
mapper.put(id, actualId++, group.get());
}
Collector collector = mock(Collector.class);
mapper.prepare(ids, collector, NONE);
// THEN
verifyNoMoreInteractions(collector);
actualId = 0;
for (Object id : ids) {
assertEquals(actualId++, mapper.get(id, group.get()));
}
}
use of org.neo4j.helpers.collection.PrefetchingIterator in project neo4j-mobile-android by neo4j-contrib.
the class LegacyNodeStoreReader method readNodeStore.
public Iterable<NodeRecord> readNodeStore() throws IOException {
final ByteBuffer buffer = ByteBuffer.allocateDirect(RECORD_LENGTH);
return new Iterable<NodeRecord>() {
@Override
public Iterator<NodeRecord> iterator() {
return new PrefetchingIterator<NodeRecord>() {
long id = 0;
@Override
protected NodeRecord fetchNextOrNull() {
NodeRecord nodeRecord = null;
while (nodeRecord == null && id <= maxId) {
buffer.clear();
try {
fileChannel.read(buffer);
} catch (IOException e) {
throw new RuntimeException(e);
}
buffer.flip();
long inUseByte = buffer.get();
boolean inUse = (inUseByte & 0x1) == Record.IN_USE.intValue();
nodeRecord = new NodeRecord(id);
nodeRecord.setInUse(inUse);
if (inUse) {
long nextRel = LegacyStore.getUnsignedInt(buffer);
long nextProp = LegacyStore.getUnsignedInt(buffer);
long relModifier = (inUseByte & 0xEL) << 31;
long propModifier = (inUseByte & 0xF0L) << 28;
nodeRecord.setNextRel(LegacyStore.longFromIntAndMod(nextRel, relModifier));
nodeRecord.setNextProp(LegacyStore.longFromIntAndMod(nextProp, propModifier));
}
id++;
}
return nodeRecord;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
use of org.neo4j.helpers.collection.PrefetchingIterator in project neo4j-mobile-android by neo4j-contrib.
the class ExactDepthPathFinder method paths.
private Iterator<Path> paths(final Node start, final Node end) {
TraversalDescription base = Traversal.description().uniqueness(Uniqueness.RELATIONSHIP_PATH).order(new BranchOrderingPolicy() {
public BranchSelector create(TraversalBranch startSource) {
return new LiteDepthFirstSelector(startSource, startThreshold);
}
});
final int firstHalf = onDepth / 2;
Traverser startTraverser = base.prune(Traversal.pruneAfterDepth(firstHalf)).expand(expander).filter(new Predicate<Path>() {
public boolean accept(Path item) {
return item.length() == firstHalf;
}
}).traverse(start);
final int secondHalf = onDepth - firstHalf;
Traverser endTraverser = base.prune(Traversal.pruneAfterDepth(secondHalf)).expand(expander.reversed()).filter(new Predicate<Path>() {
public boolean accept(Path item) {
return item.length() == secondHalf;
}
}).traverse(end);
final Iterator<Path> startIterator = startTraverser.iterator();
final Iterator<Path> endIterator = endTraverser.iterator();
final Map<Node, Visit> visits = new HashMap<Node, Visit>();
return new PrefetchingIterator<Path>() {
@Override
protected Path fetchNextOrNull() {
Path[] found = null;
while (found == null && (startIterator.hasNext() || endIterator.hasNext())) {
found = goOneStep(start, startIterator, visits);
if (found == null) {
found = goOneStep(end, endIterator, visits);
}
}
return found != null ? toPath(found, start) : null;
}
};
}
use of org.neo4j.helpers.collection.PrefetchingIterator in project graphdb by neo4j-attic.
the class ExactDepthPathFinder method paths.
private Iterator<Path> paths(final Node start, final Node end) {
TraversalDescription base = Traversal.description().uniqueness(Uniqueness.RELATIONSHIP_PATH).order(new BranchOrderingPolicy() {
public BranchSelector create(TraversalBranch startSource) {
return new LiteDepthFirstSelector(startSource, startThreshold);
}
});
final int firstHalf = onDepth / 2;
Traverser startTraverser = base.prune(Traversal.pruneAfterDepth(firstHalf)).expand(expander).filter(new Predicate<Path>() {
public boolean accept(Path item) {
return item.length() == firstHalf;
}
}).traverse(start);
final int secondHalf = onDepth - firstHalf;
Traverser endTraverser = base.prune(Traversal.pruneAfterDepth(secondHalf)).expand(expander.reversed()).filter(new Predicate<Path>() {
public boolean accept(Path item) {
return item.length() == secondHalf;
}
}).traverse(end);
final Iterator<Path> startIterator = startTraverser.iterator();
final Iterator<Path> endIterator = endTraverser.iterator();
final Map<Node, Visit> visits = new HashMap<Node, Visit>();
return new PrefetchingIterator<Path>() {
@Override
protected Path fetchNextOrNull() {
Path[] found = null;
while (found == null && (startIterator.hasNext() || endIterator.hasNext())) {
found = goOneStep(start, startIterator, visits);
if (found == null) {
found = goOneStep(end, endIterator, visits);
}
}
return found != null ? toPath(found, start) : null;
}
};
}
use of org.neo4j.helpers.collection.PrefetchingIterator in project neo4j-mobile-android by neo4j-contrib.
the class LegacyRelationshipStoreReader method readRelationshipStore.
public Iterable<RelationshipRecord> readRelationshipStore() throws IOException {
final ByteBuffer buffer = ByteBuffer.allocateDirect(RECORD_LENGTH);
return new Iterable<RelationshipRecord>() {
@Override
public Iterator<RelationshipRecord> iterator() {
return new PrefetchingIterator<RelationshipRecord>() {
long id = 0;
@Override
protected RelationshipRecord fetchNextOrNull() {
RelationshipRecord record = null;
while (record == null && id <= maxId) {
buffer.clear();
try {
fileChannel.read(buffer);
} catch (IOException e) {
throw new RuntimeException(e);
}
buffer.flip();
long inUseByte = buffer.get();
boolean inUse = (inUseByte & 0x1) == Record.IN_USE.intValue();
if (inUse) {
long firstNode = LegacyStore.getUnsignedInt(buffer);
long firstNodeMod = (inUseByte & 0xEL) << 31;
long secondNode = LegacyStore.getUnsignedInt(buffer);
// [ xxx, ][ , ][ , ][ , ] second node high order bits, 0x70000000
// [ ,xxx ][ , ][ , ][ , ] first prev rel high order bits, 0xE000000
// [ , x][xx , ][ , ][ , ] first next rel high order bits, 0x1C00000
// [ , ][ xx,x ][ , ][ , ] second prev rel high order bits, 0x380000
// [ , ][ , xxx][ , ][ , ] second next rel high order bits, 0x70000
// [ , ][ , ][xxxx,xxxx][xxxx,xxxx] type
long typeInt = buffer.getInt();
long secondNodeMod = (typeInt & 0x70000000L) << 4;
int type = (int) (typeInt & 0xFFFF);
record = new RelationshipRecord(id, LegacyStore.longFromIntAndMod(firstNode, firstNodeMod), LegacyStore.longFromIntAndMod(secondNode, secondNodeMod), type);
record.setInUse(inUse);
long firstPrevRel = LegacyStore.getUnsignedInt(buffer);
long firstPrevRelMod = (typeInt & 0xE000000L) << 7;
record.setFirstPrevRel(LegacyStore.longFromIntAndMod(firstPrevRel, firstPrevRelMod));
long firstNextRel = LegacyStore.getUnsignedInt(buffer);
long firstNextRelMod = (typeInt & 0x1C00000L) << 10;
record.setFirstNextRel(LegacyStore.longFromIntAndMod(firstNextRel, firstNextRelMod));
long secondPrevRel = LegacyStore.getUnsignedInt(buffer);
long secondPrevRelMod = (typeInt & 0x380000L) << 13;
record.setSecondPrevRel(LegacyStore.longFromIntAndMod(secondPrevRel, secondPrevRelMod));
long secondNextRel = LegacyStore.getUnsignedInt(buffer);
long secondNextRelMod = (typeInt & 0x70000L) << 16;
record.setSecondNextRel(LegacyStore.longFromIntAndMod(secondNextRel, secondNextRelMod));
long nextProp = LegacyStore.getUnsignedInt(buffer);
long nextPropMod = (inUseByte & 0xF0L) << 28;
record.setNextProp(LegacyStore.longFromIntAndMod(nextProp, nextPropMod));
} else {
record = new RelationshipRecord(id, -1, -1, -1);
record.setInUse(false);
}
id++;
}
return record;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
Aggregations