use of org.neo4j.helpers.collection.PrefetchingIterator in project neo4j by neo4j.
the class SchemaStorage method loadAllSchemaRules.
<ReturnType extends SchemaRule> Iterator<ReturnType> loadAllSchemaRules(final Predicate<ReturnType> predicate, final Class<ReturnType> returnType, final boolean ignoreMalformed) {
return new PrefetchingIterator<ReturnType>() {
private final long highestId = schemaStore.getHighestPossibleIdInUse();
private long currentId = 1;
/*record 0 contains the block size*/
private final byte[] scratchData = newRecordBuffer();
private final DynamicRecord record = schemaStore.newRecord();
@Override
protected ReturnType fetchNextOrNull() {
while (currentId <= highestId) {
long id = currentId++;
schemaStore.getRecord(id, record, RecordLoad.FORCE);
if (record.inUse() && record.isStartRecord()) {
try {
SchemaRule schemaRule = loadSingleSchemaRuleViaBuffer(id, scratchData);
if (returnType.isInstance(schemaRule)) {
ReturnType returnRule = returnType.cast(schemaRule);
if (predicate.test(returnRule)) {
return returnRule;
}
}
} catch (MalformedSchemaRuleException e) {
if (!ignoreMalformed) {
throw new RuntimeException(e);
}
}
}
}
return null;
}
};
}
use of org.neo4j.helpers.collection.PrefetchingIterator in project neo4j by neo4j.
the class RecordAccessStub method rawPropertyChain.
@Override
public Iterator<PropertyRecord> rawPropertyChain(final long firstId) {
return new PrefetchingIterator<PropertyRecord>() {
private long next = firstId;
@Override
protected PropertyRecord fetchNextOrNull() {
if (Record.NO_NEXT_PROPERTY.is(next)) {
return null;
}
PropertyRecord record = reference(properties, next, Version.LATEST).record();
next = record.getNextProp();
return record;
}
};
}
use of org.neo4j.helpers.collection.PrefetchingIterator in project neo4j by neo4j.
the class DirectRecordAccess method rawPropertyChain.
@Override
public Iterator<PropertyRecord> rawPropertyChain(final long firstId) {
return new PrefetchingIterator<PropertyRecord>() {
private long next = firstId;
@Override
protected PropertyRecord fetchNextOrNull() {
if (Record.NO_NEXT_PROPERTY.is(next)) {
return null;
}
PropertyRecord record = referenceTo(access.getPropertyStore(), next).record();
next = record.getNextProp();
return record;
}
};
}
use of org.neo4j.helpers.collection.PrefetchingIterator in project graphdb by neo4j-attic.
the class Service method ourOwnLoader.
private static <T> Iterable<T> ourOwnLoader(final Class<T> type) {
Collection<URL> urls = new HashSet<URL>();
try {
Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources("META-INF/services/" + type.getName());
while (resources.hasMoreElements()) {
urls.add(resources.nextElement());
}
} catch (IOException e) {
return null;
}
return new NestingIterable<T, BufferedReader>(FilteringIterable.notNull(new IterableWrapper<BufferedReader, URL>(urls) {
@Override
protected BufferedReader underlyingObjectToObject(URL url) {
try {
return new BufferedReader(new InputStreamReader(url.openStream()));
} catch (IOException e) {
return null;
}
}
})) {
@Override
protected Iterator<T> createNestedIterator(final BufferedReader input) {
return new PrefetchingIterator<T>() {
@Override
protected T fetchNextOrNull() {
try {
String line;
while (null != (line = input.readLine())) {
try {
return type.cast(Class.forName(line).newInstance());
} catch (Exception e) {
} catch (LinkageError e) {
}
}
input.close();
return null;
} catch (IOException e) {
return null;
}
}
/* Finalizer - close the input stream.
* Prevent leakage of open files. Finalizers impact GC performance,
* but there are expected to be few of these objects.
*/
@Override
protected void finalize() throws Throwable {
input.close();
}
};
}
};
}
use of org.neo4j.helpers.collection.PrefetchingIterator in project neo4j-mobile-android by neo4j-contrib.
the class Service method ourOwnLoader.
private static <T> Iterable<T> ourOwnLoader(final Class<T> type) {
Collection<URL> urls = new HashSet<URL>();
try {
Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources("META-INF/services/" + type.getName());
while (resources.hasMoreElements()) {
urls.add(resources.nextElement());
}
} catch (IOException e) {
return null;
}
return new NestingIterable<T, BufferedReader>(FilteringIterable.notNull(new IterableWrapper<BufferedReader, URL>(urls) {
@Override
protected BufferedReader underlyingObjectToObject(URL url) {
try {
return new BufferedReader(new InputStreamReader(url.openStream()));
} catch (IOException e) {
return null;
}
}
})) {
@Override
protected Iterator<T> createNestedIterator(final BufferedReader input) {
return new PrefetchingIterator<T>() {
@Override
protected T fetchNextOrNull() {
try {
String line;
while (null != (line = input.readLine())) {
try {
return type.cast(Class.forName(line).newInstance());
} catch (Exception e) {
} catch (LinkageError e) {
}
}
input.close();
return null;
} catch (IOException e) {
return null;
}
}
/* Finalizer - close the input stream.
* Prevent leakage of open files. Finalizers impact GC performance,
* but there are expected to be few of these objects.
*/
@Override
protected void finalize() throws Throwable {
input.close();
}
};
}
};
}
Aggregations