Skip to content

Commit

Permalink
Separate cursor close and release phase
Browse files Browse the repository at this point in the history
The close() call now only stops further use of the cursor until reopened,
and hands it back to the pool. PageCursors and other resources are not
released until calling release() though, which means that they may be
reused via the instance cache.
  • Loading branch information
fickludd committed Mar 1, 2018
1 parent 221af0f commit e476d09
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 199 deletions.

Large diffs are not rendered by default.

Expand Up @@ -20,7 +20,6 @@
package org.neo4j.kernel.impl.newapi; package org.neo4j.kernel.impl.newapi;


import java.util.Set; import java.util.Set;
import java.util.function.Consumer;


import org.neo4j.collection.primitive.Primitive; import org.neo4j.collection.primitive.Primitive;
import org.neo4j.collection.primitive.PrimitiveIntSet; import org.neo4j.collection.primitive.PrimitiveIntSet;
Expand Down Expand Up @@ -48,9 +47,9 @@ class DefaultNodeCursor extends NodeRecord implements NodeCursor
private HasChanges hasChanges = HasChanges.MAYBE; private HasChanges hasChanges = HasChanges.MAYBE;
private Set<Long> addedNodes; private Set<Long> addedNodes;


private final Consumer<DefaultNodeCursor> pool; private final DefaultCursors pool;


DefaultNodeCursor( Consumer<DefaultNodeCursor> pool ) DefaultNodeCursor( DefaultCursors pool )
{ {
super( NO_ID ); super( NO_ID );
this.pool = pool; this.pool = pool;
Expand Down Expand Up @@ -246,21 +245,12 @@ public boolean shouldRetry()
@Override @Override
public void close() public void close()
{ {
read = null; if ( !isClosed() )
hasChanges = HasChanges.MAYBE;
addedNodes = emptySet();
reset();

if ( labelCursor != null )
{ {
labelCursor.close(); read = null;
labelCursor = null; hasChanges = HasChanges.MAYBE;
} addedNodes = emptySet();

reset();
if ( pageCursor != null )
{
pageCursor.close();
pageCursor = null;


pool.accept( this ); pool.accept( this );
} }
Expand All @@ -269,7 +259,7 @@ public void close()
@Override @Override
public boolean isClosed() public boolean isClosed()
{ {
return pageCursor == null; return read == null;
} }


/** /**
Expand Down Expand Up @@ -337,4 +327,19 @@ public String toString()
return "NodeCursor[id=" + getId() + ", open state with: highMark=" + highMark + ", next=" + next + ", underlying record=" + super.toString() + " ]"; return "NodeCursor[id=" + getId() + ", open state with: highMark=" + highMark + ", next=" + next + ", underlying record=" + super.toString() + " ]";
} }
} }

void release()
{
if ( labelCursor != null )
{
labelCursor.close();
labelCursor = null;
}

if ( pageCursor != null )
{
pageCursor.close();
pageCursor = null;
}
}
} }
Expand Up @@ -19,8 +19,6 @@
*/ */
package org.neo4j.kernel.impl.newapi; package org.neo4j.kernel.impl.newapi;


import java.util.function.Consumer;

import org.neo4j.internal.kernel.api.NodeCursor; import org.neo4j.internal.kernel.api.NodeCursor;
import org.neo4j.storageengine.api.schema.IndexProgressor.ExplicitClient; import org.neo4j.storageengine.api.schema.IndexProgressor.ExplicitClient;


Expand All @@ -34,9 +32,9 @@ class DefaultNodeExplicitIndexCursor extends IndexCursor<ExplicitIndexProgressor
private long node; private long node;
private float score; private float score;


private final Consumer<DefaultNodeExplicitIndexCursor> pool; private final DefaultCursors pool;


DefaultNodeExplicitIndexCursor( Consumer<DefaultNodeExplicitIndexCursor> pool ) DefaultNodeExplicitIndexCursor( DefaultCursors pool )
{ {
this.pool = pool; this.pool = pool;
node = NO_ID; node = NO_ID;
Expand Down Expand Up @@ -126,4 +124,9 @@ public String toString()
", underlying record=" + super.toString() + " ]"; ", underlying record=" + super.toString() + " ]";
} }
} }

public void release()
{
// nothing to do
}
} }
Expand Up @@ -21,7 +21,6 @@


import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer;


import org.neo4j.collection.primitive.PrimitiveLongCollections; import org.neo4j.collection.primitive.PrimitiveLongCollections;
import org.neo4j.collection.primitive.PrimitiveLongIterator; import org.neo4j.collection.primitive.PrimitiveLongIterator;
Expand All @@ -44,9 +43,9 @@ class DefaultNodeLabelIndexCursor extends IndexCursor<LabelScanValueIndexProgres
private PrimitiveLongIterator added; private PrimitiveLongIterator added;
private Set<Long> removed; private Set<Long> removed;


private final Consumer<DefaultNodeLabelIndexCursor> pool; private final DefaultCursors pool;


DefaultNodeLabelIndexCursor( Consumer<DefaultNodeLabelIndexCursor> pool ) DefaultNodeLabelIndexCursor( DefaultCursors pool )
{ {
this.pool = pool; this.pool = pool;
node = NO_ID; node = NO_ID;
Expand Down Expand Up @@ -180,4 +179,9 @@ private boolean isRemoved( long reference )
{ {
return removed != null && removed.contains( reference ); return removed != null && removed.contains( reference );
} }

public void release()
{
// nothing to do
}
} }
Expand Up @@ -20,7 +20,6 @@
package org.neo4j.kernel.impl.newapi; package org.neo4j.kernel.impl.newapi;


import java.util.Arrays; import java.util.Arrays;
import java.util.function.Consumer;


import org.neo4j.collection.primitive.PrimitiveLongCollections; import org.neo4j.collection.primitive.PrimitiveLongCollections;
import org.neo4j.collection.primitive.PrimitiveLongIterator; import org.neo4j.collection.primitive.PrimitiveLongIterator;
Expand Down Expand Up @@ -52,9 +51,9 @@ final class DefaultNodeValueIndexCursor extends IndexCursor<IndexProgressor>
private PrimitiveLongIterator added = emptyIterator(); private PrimitiveLongIterator added = emptyIterator();
private PrimitiveLongSet removed = emptySet(); private PrimitiveLongSet removed = emptySet();
private boolean needsValues; private boolean needsValues;
private final Consumer<DefaultNodeValueIndexCursor> pool; private final DefaultCursors pool;


DefaultNodeValueIndexCursor( Consumer<DefaultNodeValueIndexCursor> pool ) DefaultNodeValueIndexCursor( DefaultCursors pool )
{ {
this.pool = pool; this.pool = pool;
node = NO_ID; node = NO_ID;
Expand Down Expand Up @@ -312,4 +311,9 @@ private PrimitiveLongSet removed( TransactionState txState, PrimitiveLongReadabl
longSet.addAll( changes.getRemoved().iterator() ); longSet.addAll( changes.getRemoved().iterator() );
return longSet; return longSet;
} }

public void release()
{
// nothing to do
}
} }
Expand Up @@ -21,7 +21,6 @@


import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Iterator; import java.util.Iterator;
import java.util.function.Consumer;
import java.util.regex.Pattern; import java.util.regex.Pattern;


import org.neo4j.internal.kernel.api.PropertyCursor; import org.neo4j.internal.kernel.api.PropertyCursor;
Expand Down Expand Up @@ -66,9 +65,9 @@ public class DefaultPropertyCursor extends PropertyRecord implements PropertyCur
private Iterator<StorageProperty> txStateChangedProperties; private Iterator<StorageProperty> txStateChangedProperties;
private StorageProperty txStateValue; private StorageProperty txStateValue;
private AssertOpen assertOpen; private AssertOpen assertOpen;
private final Consumer<DefaultPropertyCursor> pool; private final DefaultCursors pool;


public DefaultPropertyCursor( Consumer<DefaultPropertyCursor> pool ) public DefaultPropertyCursor( DefaultCursors pool )
{ {
super( NO_ID ); super( NO_ID );
this.pool = pool; this.pool = pool;
Expand Down Expand Up @@ -233,26 +232,13 @@ public boolean shouldRetry()
@Override @Override
public void close() public void close()
{ {
propertiesState = null; if ( !isClosed() )
txStateChangedProperties = null;
txStateValue = null;
read = null;
clear();

if ( stringPage != null )
{ {
stringPage.close(); propertiesState = null;
stringPage = null; txStateChangedProperties = null;
} txStateValue = null;
if ( arrayPage != null ) read = null;
{ clear();
arrayPage.close();
arrayPage = null;
}
if ( page != null )
{
page.close();
page = null;


pool.accept( this ); pool.accept( this );
} }
Expand Down Expand Up @@ -558,7 +544,7 @@ public boolean valueLessThanOrEqualTo( double number )
@Override @Override
public boolean isClosed() public boolean isClosed()
{ {
return page == null; return read == null;
} }


@Override @Override
Expand All @@ -574,4 +560,23 @@ public String toString()
", underlying record=" + super.toString() + " ]"; ", underlying record=" + super.toString() + " ]";
} }
} }

public void release()
{
if ( stringPage != null )
{
stringPage.close();
stringPage = null;
}
if ( arrayPage != null )
{
arrayPage.close();
arrayPage = null;
}
if ( page != null )
{
page.close();
page = null;
}
}
} }
Expand Up @@ -19,8 +19,6 @@
*/ */
package org.neo4j.kernel.impl.newapi; package org.neo4j.kernel.impl.newapi;


import java.util.function.Consumer;

import org.neo4j.internal.kernel.api.NodeCursor; import org.neo4j.internal.kernel.api.NodeCursor;
import org.neo4j.internal.kernel.api.RelationshipExplicitIndexCursor; import org.neo4j.internal.kernel.api.RelationshipExplicitIndexCursor;
import org.neo4j.internal.kernel.api.RelationshipScanCursor; import org.neo4j.internal.kernel.api.RelationshipScanCursor;
Expand All @@ -36,9 +34,9 @@ class DefaultRelationshipExplicitIndexCursor extends IndexCursor<ExplicitIndexPr
private long relationship; private long relationship;
private float score; private float score;


private final Consumer<DefaultRelationshipExplicitIndexCursor> pool; private final DefaultCursors pool;


DefaultRelationshipExplicitIndexCursor( Consumer<DefaultRelationshipExplicitIndexCursor> pool ) DefaultRelationshipExplicitIndexCursor( DefaultCursors pool )
{ {
this.pool = pool; this.pool = pool;
} }
Expand Down Expand Up @@ -157,4 +155,9 @@ public String toString()
" ,underlying record=" + super.toString() + " ]"; " ,underlying record=" + super.toString() + " ]";
} }
} }

public void release()
{
// nothing to do
}
} }
Expand Up @@ -19,8 +19,6 @@
*/ */
package org.neo4j.kernel.impl.newapi; package org.neo4j.kernel.impl.newapi;


import java.util.function.Consumer;

import org.neo4j.collection.primitive.Primitive; import org.neo4j.collection.primitive.Primitive;
import org.neo4j.collection.primitive.PrimitiveIntIterator; import org.neo4j.collection.primitive.PrimitiveIntIterator;
import org.neo4j.collection.primitive.PrimitiveIntObjectMap; import org.neo4j.collection.primitive.PrimitiveIntObjectMap;
Expand All @@ -46,7 +44,7 @@ class DefaultRelationshipGroupCursor extends RelationshipGroupRecord implements
{ {
private Read read; private Read read;
private final RelationshipRecord edge = new RelationshipRecord( NO_ID ); private final RelationshipRecord edge = new RelationshipRecord( NO_ID );
private final Consumer<DefaultRelationshipGroupCursor> pool; private final DefaultCursors pool;


private BufferedGroup bufferedGroup; private BufferedGroup bufferedGroup;
private PageCursor page; private PageCursor page;
Expand All @@ -55,7 +53,7 @@ class DefaultRelationshipGroupCursor extends RelationshipGroupRecord implements
private final PrimitiveIntSet txTypes = Primitive.intSet(); private final PrimitiveIntSet txTypes = Primitive.intSet();
private PrimitiveIntIterator txTypeIterator; private PrimitiveIntIterator txTypeIterator;


DefaultRelationshipGroupCursor( Consumer<DefaultRelationshipGroupCursor> pool ) DefaultRelationshipGroupCursor( DefaultCursors pool )
{ {
super( NO_ID ); super( NO_ID );
this.pool = pool; this.pool = pool;
Expand Down Expand Up @@ -243,23 +241,17 @@ public boolean shouldRetry()
@Override @Override
public void close() public void close()
{ {
bufferedGroup = null; if ( !isClosed() )
read = null;
setId( NO_ID );
clear();

if ( edgePage != null )
{ {
edgePage.close(); bufferedGroup = null;
edgePage = null; read = null;
} setId( NO_ID );
clear();


if ( page != null ) if ( pool != null )
{ {
page.close(); pool.accept( this );
page = null; }

pool.accept( this );
} }
} }


Expand Down Expand Up @@ -410,7 +402,7 @@ public long loopsReference()
@Override @Override
public boolean isClosed() public boolean isClosed()
{ {
return page == null && bufferedGroup == null; return read == null && bufferedGroup == null;
} }


@Override @Override
Expand Down Expand Up @@ -470,6 +462,21 @@ private long encodeRelationshipReference( long relationshipId )
return isBuffered() ? encodeForFiltering( relationshipId ) : encodeForTxStateFiltering( relationshipId ); return isBuffered() ? encodeForFiltering( relationshipId ) : encodeForTxStateFiltering( relationshipId );
} }


public void release()
{
if ( edgePage != null )
{
edgePage.close();
edgePage = null;
}

if ( page != null )
{
page.close();
page = null;
}
}

static class BufferedGroup static class BufferedGroup
{ {
final int label; final int label;
Expand Down

0 comments on commit e476d09

Please sign in to comment.