Skip to content

Commit

Permalink
Make sure cursors always get closed
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaPeukert committed Jul 26, 2018
1 parent 02be46b commit d3d2595
Showing 1 changed file with 65 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,86 +185,90 @@ private void calculateSchema()

private void scanEverythingBelongingToRelationships( Read dataRead, CursorFactory cursors )
{
RelationshipScanCursor relationshipScanCursor = cursors.allocateRelationshipScanCursor();
PropertyCursor propertyCursor = cursors.allocatePropertyCursor();
dataRead.allRelationshipsScan( relationshipScanCursor );
while ( relationshipScanCursor.next() )
try ( RelationshipScanCursor relationshipScanCursor = cursors.allocateRelationshipScanCursor();
PropertyCursor propertyCursor = cursors.allocatePropertyCursor() )
{
int typeId = relationshipScanCursor.type();
relationshipScanCursor.properties( propertyCursor );
MutableIntSet propertyIds = IntSets.mutable.empty();

while ( propertyCursor.next() )
dataRead.allRelationshipsScan( relationshipScanCursor );
while ( relationshipScanCursor.next() )
{
int propertyKey = propertyCursor.propertyKey();
int typeId = relationshipScanCursor.type();
relationshipScanCursor.properties( propertyCursor );
MutableIntSet propertyIds = IntSets.mutable.empty();

Value currentValue = propertyCursor.propertyValue();
Pair<Integer,Integer> key = Pair.of( typeId, propertyKey );
updateValueTypeInMapping( currentValue, key, relationshipTypeIdANDPropertyTypeIdToValueTypeMapping );
while ( propertyCursor.next() )
{
int propertyKey = propertyCursor.propertyKey();

propertyIds.add( propertyKey );
}
propertyCursor.close();
Value currentValue = propertyCursor.propertyValue();
Pair<Integer,Integer> key = Pair.of( typeId, propertyKey );
updateValueTypeInMapping( currentValue, key, relationshipTypeIdANDPropertyTypeIdToValueTypeMapping );

MutableIntSet oldPropertyKeySet = relationshipTypeIdToPropertyKeysMapping.getOrDefault( typeId, emptyPropertyIdSet );
propertyIds.add( propertyKey );
}
propertyCursor.close();

// find out which old properties we did not visited and mark them as nullable
if ( !(oldPropertyKeySet == emptyPropertyIdSet) )
{
// we can and need to skip this if we found the empty set
oldPropertyKeySet.removeAll( propertyIds );
oldPropertyKeySet.forEach( id -> {
Pair<Integer,Integer> key = Pair.of( typeId, id );
relationshipTypeIdANDPropertyTypeIdToValueTypeMapping.get( key ).setNullable();
} );
}
MutableIntSet oldPropertyKeySet = relationshipTypeIdToPropertyKeysMapping.getOrDefault( typeId, emptyPropertyIdSet );

propertyIds.addAll( oldPropertyKeySet );
relationshipTypeIdToPropertyKeysMapping.put( typeId, propertyIds );
// find out which old properties we did not visited and mark them as nullable
if ( !(oldPropertyKeySet == emptyPropertyIdSet) )
{
// we can and need to skip this if we found the empty set
oldPropertyKeySet.removeAll( propertyIds );
oldPropertyKeySet.forEach( id -> {
Pair<Integer,Integer> key = Pair.of( typeId, id );
relationshipTypeIdANDPropertyTypeIdToValueTypeMapping.get( key ).setNullable();
} );
}

propertyIds.addAll( oldPropertyKeySet );
relationshipTypeIdToPropertyKeysMapping.put( typeId, propertyIds );
}
relationshipScanCursor.close();
}
relationshipScanCursor.close();
}

private void scanEverythingBelongingToNodes( Read dataRead, CursorFactory cursors )
{
NodeCursor nodeCursor = cursors.allocateNodeCursor();
PropertyCursor propertyCursor = cursors.allocatePropertyCursor();
dataRead.allNodesScan( nodeCursor );
while ( nodeCursor.next() )
try ( NodeCursor nodeCursor = cursors.allocateNodeCursor();
PropertyCursor propertyCursor = cursors.allocatePropertyCursor() )
{
// each node
LabelSet labels = nodeCursor.labels();
nodeCursor.properties( propertyCursor );
MutableIntSet propertyIds = IntSets.mutable.empty();

while ( propertyCursor.next() )
dataRead.allNodesScan( nodeCursor );
while ( nodeCursor.next() )
{
Value currentValue = propertyCursor.propertyValue();
int propertyKeyId = propertyCursor.propertyKey();
Pair<LabelSet,Integer> key = Pair.of( labels, propertyKeyId );
updateValueTypeInMapping( currentValue, key, labelSetANDNodePropertyKeyIdToValueTypeMapping );
// each node
LabelSet labels = nodeCursor.labels();
nodeCursor.properties( propertyCursor );
MutableIntSet propertyIds = IntSets.mutable.empty();

propertyIds.add( propertyKeyId );
}
propertyCursor.close();
while ( propertyCursor.next() )
{
Value currentValue = propertyCursor.propertyValue();
int propertyKeyId = propertyCursor.propertyKey();
Pair<LabelSet,Integer> key = Pair.of( labels, propertyKeyId );
updateValueTypeInMapping( currentValue, key, labelSetANDNodePropertyKeyIdToValueTypeMapping );

MutableIntSet oldPropertyKeySet = labelSetToPropertyKeysMapping.getOrDefault( labels, emptyPropertyIdSet );
propertyIds.add( propertyKeyId );
}
propertyCursor.close();

// find out which old properties we did not visited and mark them as nullable
if ( !(oldPropertyKeySet == emptyPropertyIdSet) )
{
// we can and need (!) to skip this if we found the empty set
oldPropertyKeySet.removeAll( propertyIds );
oldPropertyKeySet.forEach( id -> {
Pair<LabelSet,Integer> key = Pair.of( labels, id );
labelSetANDNodePropertyKeyIdToValueTypeMapping.get( key ).setNullable();
} );
}
MutableIntSet oldPropertyKeySet = labelSetToPropertyKeysMapping.getOrDefault( labels, emptyPropertyIdSet );

propertyIds.addAll( oldPropertyKeySet );
labelSetToPropertyKeysMapping.put( labels, propertyIds );
// find out which old properties we did not visited and mark them as nullable
if ( !(oldPropertyKeySet == emptyPropertyIdSet) )
{
// we can and need (!) to skip this if we found the empty set
oldPropertyKeySet.removeAll( propertyIds );
oldPropertyKeySet.forEach( id -> {
Pair<LabelSet,Integer> key = Pair.of( labels, id );
labelSetANDNodePropertyKeyIdToValueTypeMapping.get( key ).setNullable();
} );
}

propertyIds.addAll( oldPropertyKeySet );
labelSetToPropertyKeysMapping.put( labels, propertyIds );
}
nodeCursor.close();
}
nodeCursor.close();
}

private <X, Y> void updateValueTypeInMapping( Value currentValue, Pair<X,Y> key, Map<Pair<X,Y>,ValueTypeDecider> mappingToUpdate )
Expand Down

0 comments on commit d3d2595

Please sign in to comment.