Skip to content

Commit

Permalink
Small cleanups around cursors, couple tests added
Browse files Browse the repository at this point in the history
Co-authored-by: @burqen
  • Loading branch information
lutovich committed May 12, 2016
1 parent a093aef commit 0c16c0f
Show file tree
Hide file tree
Showing 17 changed files with 278 additions and 107 deletions.
69 changes: 52 additions & 17 deletions community/io/src/main/java/org/neo4j/io/IOUtils.java
Expand Up @@ -20,6 +20,7 @@
package org.neo4j.io;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.Collection;

/**
Expand Down Expand Up @@ -75,7 +76,43 @@ public static <T extends AutoCloseable> void closeAllSilently( Collection<T> clo
@SafeVarargs
public static <T extends AutoCloseable> void closeAll( T... closeables ) throws IOException
{
Exception closeException = null;
closeAll( IOException.class, closeables );
}

/**
* Closes given array of {@link AutoCloseable closeables} ignoring all exceptions.
*
* @param closeables the closeables to close
* @param <T> the type of closeable
*/
@SafeVarargs
public static <T extends AutoCloseable> void closeAllSilently( T... closeables )
{
try
{
closeAll( closeables );
}
catch ( IOException ignored )
{
}
}

/**
* Close all given closeables and if something goes wrong throw exception of the given type.
* Exception class should have a public constructor that accepts {@link String} and {@link Throwable} like
* {@link RuntimeException#RuntimeException(String, Throwable)}
*
* @param throwableClass exception type to throw in case of failure
* @param closeables the closeables to close
* @param <T> the type of closeable
* @param <E> the type of exception
* @throws E when any {@link AutoCloseable#close()} throws exception
*/
@SafeVarargs
public static <T extends AutoCloseable, E extends Throwable> void closeAll( Class<E> throwableClass,
T... closeables ) throws E
{
Throwable closeThrowable = null;
for ( T closeable : closeables )
{
if ( closeable != null )
Expand All @@ -84,40 +121,38 @@ public static <T extends AutoCloseable> void closeAll( T... closeables ) throws
{
closeable.close();
}
catch ( Exception e )
catch ( Throwable t )
{
if ( closeException == null )
if ( closeThrowable == null )
{
closeException = e;
closeThrowable = t;
}
else
{
closeException.addSuppressed( e );
closeThrowable.addSuppressed( t );
}
}
}
}
if ( closeException != null )
if ( closeThrowable != null )
{
throw new IOException( "Exception closing multiple resources", closeException );
throw newThrowable( throwableClass, "Exception closing multiple resources", closeThrowable );
}
}

/**
* Closes given array of {@link AutoCloseable closeables} ignoring all exceptions.
*
* @param closeables the closeables to close
* @param <T> the type of closeable
*/
@SafeVarargs
public static <T extends AutoCloseable> void closeAllSilently( T... closeables )
private static <E extends Throwable> E newThrowable( Class<E> throwableClass, String message, Throwable cause )
{
try
{
closeAll( closeables );
Constructor<E> constructor = throwableClass.getConstructor( String.class, Throwable.class );
return constructor.newInstance( message, cause );
}
catch ( IOException ignored )
catch ( Throwable t )
{
RuntimeException runtimeException = new RuntimeException(
"Unable to create exception to throw. Original message: " + message, t );
runtimeException.addSuppressed( cause );
throw runtimeException;
}
}
}
Expand Up @@ -108,27 +108,26 @@ protected StoreSingleLabelCursor create()
@Override
protected StoreNodeRelationshipCursor create()
{
return new StoreNodeRelationshipCursor( relationshipStore.newRecord(), neoStores,
relationshipGroupStore.newRecord(), storeStatement, this, lockService,
cursors );
return new StoreNodeRelationshipCursor( relationshipStore.newRecord(),
relationshipGroupStore.newRecord(), this, lockService, cursors );
}
};
singlePropertyCursor = new InstanceCache<StoreSinglePropertyCursor>()
{
@Override
protected StoreSinglePropertyCursor create()
{
return new StoreSinglePropertyCursor( cursors.property(),
cursors.propertyString(), cursors.propertyArray(), this );
return new StoreSinglePropertyCursor( cursors.property(), cursors.propertyString(),
cursors.propertyArray(), this );
}
};
allPropertyCursor = new InstanceCache<StorePropertyCursor>()
{
@Override
protected StorePropertyCursor create()
{
return new StorePropertyCursor( cursors.property(),
cursors.propertyString(), cursors.propertyArray(), allPropertyCursor );
return new StorePropertyCursor( cursors.property(), cursors.propertyString(), cursors.propertyArray(),
allPropertyCursor );
}
};
}
Expand Down
Expand Up @@ -23,13 +23,9 @@
import org.neo4j.kernel.api.cursor.EntityItemHelper;
import org.neo4j.kernel.impl.locking.Lock;
import org.neo4j.kernel.impl.locking.LockService;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.RecordCursor;
import org.neo4j.kernel.impl.store.RecordCursors;
import org.neo4j.kernel.impl.store.RecordStore;
import org.neo4j.kernel.impl.store.RelationshipStore;
import org.neo4j.kernel.impl.store.record.Record;
import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.kernel.impl.util.InstanceCache;
import org.neo4j.storageengine.api.PropertyItem;
Expand All @@ -45,26 +41,19 @@ public abstract class StoreAbstractRelationshipCursor extends EntityItemHelper
implements Cursor<RelationshipItem>, RelationshipItem
{
protected final RelationshipRecord relationshipRecord;
protected final RelationshipStore relationshipStore;
protected final RecordStore<RelationshipGroupRecord> relationshipGroupStore;
protected final RecordCursor<RelationshipRecord> relationshipRecordCursor;
private final LockService lockService;
protected StoreStatement storeStatement;

private InstanceCache<StoreSinglePropertyCursor> singlePropertyCursor;
private InstanceCache<StorePropertyCursor> allPropertyCursor;
private final InstanceCache<StoreSinglePropertyCursor> singlePropertyCursor;
private final InstanceCache<StorePropertyCursor> allPropertyCursor;

public StoreAbstractRelationshipCursor( RelationshipRecord relationshipRecord, final NeoStores neoStores,
StoreStatement storeStatement, LockService lockService, RecordCursors cursors )
public StoreAbstractRelationshipCursor( RelationshipRecord relationshipRecord, LockService lockService,
RecordCursors cursors )
{
this.lockService = lockService;
this.relationshipRecordCursor = cursors.relationship();
this.relationshipStore = neoStores.getRelationshipStore();
this.relationshipGroupStore = neoStores.getRelationshipGroupStore();
this.relationshipRecord = relationshipRecord;

this.storeStatement = storeStatement;

singlePropertyCursor = new InstanceCache<StoreSinglePropertyCursor>()
{
@Override
Expand Down
Expand Up @@ -22,7 +22,6 @@
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.graphdb.Resource;
import org.neo4j.kernel.impl.locking.LockService;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.RecordCursors;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.kernel.impl.util.InstanceCache;
Expand All @@ -38,11 +37,10 @@ public class StoreIteratorRelationshipCursor extends StoreAbstractRelationshipCu
private final InstanceCache<StoreIteratorRelationshipCursor> instanceCache;

public StoreIteratorRelationshipCursor( RelationshipRecord relationshipRecord,
NeoStores neoStores,
StoreStatement storeStatement, InstanceCache<StoreIteratorRelationshipCursor> instanceCache,
InstanceCache<StoreIteratorRelationshipCursor> instanceCache,
LockService lockService, RecordCursors cursors )
{
super( relationshipRecord, neoStores, storeStatement, lockService, cursors );
super( relationshipRecord, lockService, cursors );
this.instanceCache = instanceCache;
}

Expand Down
Expand Up @@ -34,7 +34,7 @@ public class StoreLabelCursor implements Cursor<LabelItem>, LabelItem
private long[] labels;
private int index;
private int currentLabel;
private Consumer<StoreLabelCursor> instanceCache;
private final Consumer<StoreLabelCursor> instanceCache;

public StoreLabelCursor( Consumer<StoreLabelCursor> instanceCache )
{
Expand Down
Expand Up @@ -23,7 +23,6 @@

import org.neo4j.kernel.impl.locking.LockService;
import org.neo4j.kernel.impl.store.InvalidRecordException;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.RecordCursors;
import org.neo4j.kernel.impl.store.record.Record;
import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord;
Expand Down Expand Up @@ -53,14 +52,12 @@ public class StoreNodeRelationshipCursor extends StoreAbstractRelationshipCursor
private final RecordCursors cursors;

public StoreNodeRelationshipCursor( RelationshipRecord relationshipRecord,
NeoStores neoStores,
RelationshipGroupRecord groupRecord,
StoreStatement storeStatement,
Consumer<StoreNodeRelationshipCursor> instanceCache,
LockService lockService,
RecordCursors cursors )
{
super( relationshipRecord, neoStores, storeStatement, lockService, cursors );
super( relationshipRecord, lockService, cursors );
this.groupRecord = groupRecord;
this.instanceCache = instanceCache;
this.cursors = cursors;
Expand Down
Expand Up @@ -21,7 +21,6 @@

import org.neo4j.kernel.api.StatementConstants;
import org.neo4j.kernel.impl.locking.LockService;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.RecordCursors;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.kernel.impl.util.InstanceCache;
Expand All @@ -36,11 +35,10 @@ public class StoreSingleRelationshipCursor extends StoreAbstractRelationshipCurs
private long relationshipId = -1;
private final InstanceCache<StoreSingleRelationshipCursor> instanceCache;

public StoreSingleRelationshipCursor( RelationshipRecord relationshipRecord, NeoStores neoStores,
StoreStatement storeStatement, InstanceCache<StoreSingleRelationshipCursor> instanceCache,
LockService lockService, RecordCursors cursors )
public StoreSingleRelationshipCursor( RelationshipRecord relationshipRecord,
InstanceCache<StoreSingleRelationshipCursor> instanceCache, LockService lockService, RecordCursors cursors )
{
super( relationshipRecord, neoStores, storeStatement, lockService, cursors );
super( relationshipRecord, lockService, cursors );
this.instanceCache = instanceCache;
}

Expand Down
Expand Up @@ -33,7 +33,6 @@
import org.neo4j.kernel.impl.store.NodeStore;
import org.neo4j.kernel.impl.store.RecordCursors;
import org.neo4j.kernel.impl.store.RelationshipStore;
import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.kernel.impl.util.InstanceCache;
import org.neo4j.storageengine.api.NodeItem;
import org.neo4j.storageengine.api.RelationshipItem;
Expand All @@ -47,7 +46,6 @@
* The cursors call the release methods, so there is no need for manual release, only
* closing those cursor.
* <p/>
* {@link NeoStores} caches one of these per thread, so that they can be reused between statements/transactions.
*/
public class StoreStatement implements StorageStatement
{
Expand All @@ -60,14 +58,16 @@ public class StoreStatement implements StorageStatement
private final RelationshipStore relationshipStore;
private final Supplier<IndexReaderFactory> indexReaderFactorySupplier;
private final RecordCursors recordCursors;
private IndexReaderFactory indexReaderFactory;
private final Supplier<LabelScanReader> labelScanStore;

private IndexReaderFactory indexReaderFactory;
private LabelScanReader labelScanReader;
private boolean acquired, closed;

public StoreStatement( final NeoStores neoStores, final LockService lockService,
Supplier<IndexReaderFactory> indexReaderFactory,
Supplier<LabelScanReader> labelScanReaderSupplier )
private boolean acquired;
private boolean closed;

public StoreStatement( NeoStores neoStores, LockService lockService,
Supplier<IndexReaderFactory> indexReaderFactory, Supplier<LabelScanReader> labelScanReaderSupplier )
{
this.neoStores = neoStores;
this.indexReaderFactorySupplier = indexReaderFactory;
Expand All @@ -81,7 +81,7 @@ public StoreStatement( final NeoStores neoStores, final LockService lockService,
@Override
protected StoreSingleNodeCursor create()
{
return new StoreSingleNodeCursor( new NodeRecord( -1 ), neoStores, StoreStatement.this, this,
return new StoreSingleNodeCursor( nodeStore.newRecord(), neoStores, StoreStatement.this, this,
lockService, recordCursors );
}
};
Expand All @@ -100,7 +100,7 @@ protected StoreIteratorNodeCursor create()
protected StoreSingleRelationshipCursor create()
{
return new StoreSingleRelationshipCursor( relationshipStore.newRecord(),
neoStores, StoreStatement.this, this, lockService, recordCursors );
this, lockService, recordCursors );
}
};
iteratorRelationshipCursor = new InstanceCache<StoreIteratorRelationshipCursor>()
Expand All @@ -109,14 +109,15 @@ protected StoreSingleRelationshipCursor create()
protected StoreIteratorRelationshipCursor create()
{
return new StoreIteratorRelationshipCursor( relationshipStore.newRecord(),
neoStores, StoreStatement.this, this, lockService, recordCursors );
this, lockService, recordCursors );
}
};
}

@Override
public void acquire()
{
assert !closed;
assert !acquired;
this.acquired = true;
}
Expand Down Expand Up @@ -164,12 +165,22 @@ public Cursor<RelationshipItem> relationshipsGetAllCursor()
@Override
public void release()
{
assert !closed;
assert acquired;
doRelease();
closeSchemaResources();
acquired = false;
}

private void doRelease()
@Override
public void close()
{
assert !closed;
closeSchemaResources();
recordCursors.close();
closed = true;
}

private void closeSchemaResources()
{
if ( indexReaderFactory != null )
{
Expand All @@ -183,22 +194,13 @@ private void doRelease()
}
}

@Override
public void close()
{
doRelease();
assert !closed;
recordCursors.close();
closed = true;
}

private class AllStoreIdIterator extends PrimitiveLongCollections.PrimitiveLongBaseIterator
private static class AllStoreIdIterator extends PrimitiveLongCollections.PrimitiveLongBaseIterator
{
private final CommonAbstractStore store;
private final CommonAbstractStore<?,?> store;
private long highId;
private long currentId;

public AllStoreIdIterator( CommonAbstractStore store )
AllStoreIdIterator( CommonAbstractStore<?,?> store )
{
this.store = store;
highId = store.getHighestPossibleIdInUse();
Expand Down

0 comments on commit 0c16c0f

Please sign in to comment.