Skip to content

Commit

Permalink
Included feedback from Pontus
Browse files Browse the repository at this point in the history
- Revert change to toString of Point (for now)
- Added SortedLabels to circumvent sorting all LabelSets
- Other small refactorings
  • Loading branch information
SaschaPeukert committed Jul 26, 2018
1 parent efa2215 commit 4a2b3bc
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 203 deletions.
Expand Up @@ -19,7 +19,6 @@
*/ */
package org.neo4j.internal.kernel.api; package org.neo4j.internal.kernel.api;


import java.util.Arrays;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;


/** /**
Expand Down Expand Up @@ -69,15 +68,15 @@ public long[] all()
@Override @Override
public int hashCode() public int hashCode()
{ {
return Arrays.hashCode( EMPTY ); return 1;
} }


@Override @Override
public boolean equals( Object obj ) public boolean equals( Object obj )
{ {
if ( obj instanceof LabelSet ) if ( obj instanceof LabelSet )
{ {
return ((LabelSet) obj).all().length == 0; return ((LabelSet) obj).numberOfLabels() == 0;
} }
return false; return false;
} }
Expand Down
Expand Up @@ -19,7 +19,6 @@
*/ */
package org.neo4j.internal.kernel.api.helpers; package org.neo4j.internal.kernel.api.helpers;


import java.util.Arrays;
import java.util.Map; import java.util.Map;


import org.neo4j.internal.kernel.api.LabelSet; import org.neo4j.internal.kernel.api.LabelSet;
Expand All @@ -33,20 +32,13 @@ class NodeData


NodeData( long id, long[] labels, Map<Integer,Value> properties ) NodeData( long id, long[] labels, Map<Integer,Value> properties )
{ {
if ( labels == null )
{
throw new IllegalArgumentException();
}

this.id = id; this.id = id;
this.labels = labels; this.labels = labels;
Arrays.sort( labels ); // needed for quick equality check, most of the time, its already sorted anyway
this.properties = properties; this.properties = properties;
} }


LabelSet labelSet() LabelSet labelSet()
{ {

return new LabelSet() return new LabelSet()
{ {
@Override @Override
Expand Down Expand Up @@ -79,33 +71,6 @@ public long[] all()
{ {
return labels; return labels;
} }

@Override
public int hashCode()
{
return Arrays.hashCode( labels );
}

@Override
public boolean equals( Object obj )
{
if ( obj instanceof LabelSet )
{
long[] input = ((LabelSet) obj).all();

if ( labels == input )
{
return true;
}
if ( input.length != labels.length )
{
return false;
}

return Arrays.equals( labels, input );
}
return false;
}
}; };
} }
} }

This file was deleted.

Expand Up @@ -32,7 +32,6 @@


import org.neo4j.helpers.collection.Pair; import org.neo4j.helpers.collection.Pair;
import org.neo4j.internal.kernel.api.CursorFactory; import org.neo4j.internal.kernel.api.CursorFactory;
import org.neo4j.internal.kernel.api.LabelSet;
import org.neo4j.internal.kernel.api.NamedToken; import org.neo4j.internal.kernel.api.NamedToken;
import org.neo4j.internal.kernel.api.NodeCursor; import org.neo4j.internal.kernel.api.NodeCursor;
import org.neo4j.internal.kernel.api.PropertyCursor; import org.neo4j.internal.kernel.api.PropertyCursor;
Expand All @@ -52,34 +51,39 @@


public class SchemaCalculator public class SchemaCalculator
{ {
private org.neo4j.internal.kernel.api.Transaction ktx; private Map<SortedLabels,MutableIntSet> labelSetToPropertyKeysMapping;

private Map<Pair<SortedLabels,Integer>,ValueTypeDecider> labelSetANDNodePropertyKeyIdToValueTypeMapping;
private Map<LabelSet,MutableIntSet> labelSetToPropertyKeysMapping;
private Map<Pair<LabelSet,Integer>,ValueTypeDecider> labelSetANDNodePropertyKeyIdToValueTypeMapping;
private Map<Integer,String> labelIdToLabelNameMapping; private Map<Integer,String> labelIdToLabelNameMapping;
private Map<Integer,String> propertyIdToPropertylNameMapping; private Map<Integer,String> propertyIdToPropertylNameMapping;
private Map<Integer,String> relationshipTypIdToRelationshipNameMapping; private Map<Integer,String> relationshipTypIdToRelationshipNameMapping;
private Map<Integer,MutableIntSet> relationshipTypeIdToPropertyKeysMapping; private Map<Integer,MutableIntSet> relationshipTypeIdToPropertyKeysMapping;
private Map<Pair<Integer,Integer>,ValueTypeDecider> relationshipTypeIdANDPropertyTypeIdToValueTypeMapping; private Map<Pair<Integer,Integer>,ValueTypeDecider> relationshipTypeIdANDPropertyTypeIdToValueTypeMapping;


private final MutableIntSet emptyPropertyIdSet = IntSets.mutable.empty(); private final MutableIntSet emptyPropertyIdSet = IntSets.mutable.empty();
private final String ANYVALUE = "ANY"; private static final String NODE = "Node";
private final String INTEGRAL = "INTEGRAL"; private static final String RELATIONSHIP = "Relationship";
private final String INTEGRAL_ARRAY = "INTEGRALARRAY"; private static final String NULLABLE = "?";
private final String FLOATING_POINT = "FLOATINGPOINT";
private final String FLOATING_POINT_ARRAY = "FLOATINGPOINTARRAY"; private final Read dataRead;
private final String NULLABLE = "?"; private final TokenRead tokenRead;
private final String NULLABLE_ANYVALUE = ANYVALUE + NULLABLE; private final CursorFactory cursors;
private final String NULLABLE_INTEGRAL = INTEGRAL + NULLABLE;
private final String NULLABLE_INTEGRAL_ARRAY = INTEGRAL_ARRAY + NULLABLE;
private final String NULLABLE_FLOATING_POINT_ARRAY = FLOATING_POINT_ARRAY + NULLABLE;
private final String NULLABLE_FLOATING_POINT = FLOATING_POINT + NULLABLE;
private final String NODE = "Node";
private final String RELATIONSHIP = "Relationship";


SchemaCalculator( Transaction ktx ) SchemaCalculator( Transaction ktx )
{ {
this.ktx = ktx; this.dataRead = ktx.dataRead();
this.tokenRead = ktx.tokenRead();
this.cursors = ktx.cursors();

// setup mappings
int labelCount = tokenRead.labelCount();
int relationshipTypeCount = tokenRead.relationshipTypeCount();
labelSetToPropertyKeysMapping = new HashMap<>( labelCount );
labelIdToLabelNameMapping = new HashMap<>( labelCount );
propertyIdToPropertylNameMapping = new HashMap<>( tokenRead.propertyKeyCount() );
relationshipTypIdToRelationshipNameMapping = new HashMap<>( relationshipTypeCount );
relationshipTypeIdToPropertyKeysMapping = new HashMap<>( relationshipTypeCount );
labelSetANDNodePropertyKeyIdToValueTypeMapping = new HashMap<>();
relationshipTypeIdANDPropertyTypeIdToValueTypeMapping = new HashMap<>();
} }


public Stream<SchemaInfoResult> calculateTabularResultStream() public Stream<SchemaInfoResult> calculateTabularResultStream()
Expand Down Expand Up @@ -123,7 +127,7 @@ private List<SchemaInfoResult> produceResultsForRelationships()
private List<SchemaInfoResult> produceResultsForNodes() private List<SchemaInfoResult> produceResultsForNodes()
{ {
List<SchemaInfoResult> results = new ArrayList<>(); List<SchemaInfoResult> results = new ArrayList<>();
for ( LabelSet labelSet : labelSetToPropertyKeysMapping.keySet() ) for ( SortedLabels labelSet : labelSetToPropertyKeysMapping.keySet() )
{ {
// lookup label names and produce list of names // lookup label names and produce list of names
List<String> labelNames = new ArrayList<>(); List<String> labelNames = new ArrayList<>();
Expand Down Expand Up @@ -155,24 +159,8 @@ private List<SchemaInfoResult> produceResultsForNodes()
//TODO: If we would have this schema information in the count store (or somewhere), this could be super fast //TODO: If we would have this schema information in the count store (or somewhere), this could be super fast
private void calculateSchema() private void calculateSchema()
{ {
// this one does most of the work scanEverythingBelongingToNodes( );
Read dataRead = ktx.dataRead(); scanEverythingBelongingToRelationships( );
TokenRead tokenRead = ktx.tokenRead();
CursorFactory cursors = ktx.cursors();

// setup mappings
int labelCount = tokenRead.labelCount();
int relationshipTypeCount = tokenRead.relationshipTypeCount();
labelSetToPropertyKeysMapping = new HashMap<>( labelCount );
labelIdToLabelNameMapping = new HashMap<>( labelCount );
propertyIdToPropertylNameMapping = new HashMap<>( tokenRead.propertyKeyCount() );
relationshipTypIdToRelationshipNameMapping = new HashMap<>( relationshipTypeCount );
relationshipTypeIdToPropertyKeysMapping = new HashMap<>( relationshipTypeCount );
labelSetANDNodePropertyKeyIdToValueTypeMapping = new HashMap<>();
relationshipTypeIdANDPropertyTypeIdToValueTypeMapping = new HashMap<>();

scanEverythingBelongingToNodes( dataRead, cursors );
scanEverythingBelongingToRelationships( dataRead, cursors );


// OTHER: // OTHER:
// go through all labels // go through all labels
Expand All @@ -183,7 +171,7 @@ private void calculateSchema()
addNamesToCollection( tokenRead.relationshipTypesGetAllTokens(), relationshipTypIdToRelationshipNameMapping ); addNamesToCollection( tokenRead.relationshipTypesGetAllTokens(), relationshipTypIdToRelationshipNameMapping );
} }


private void scanEverythingBelongingToRelationships( Read dataRead, CursorFactory cursors ) private void scanEverythingBelongingToRelationships( )
{ {
try ( RelationshipScanCursor relationshipScanCursor = cursors.allocateRelationshipScanCursor(); try ( RelationshipScanCursor relationshipScanCursor = cursors.allocateRelationshipScanCursor();
PropertyCursor propertyCursor = cursors.allocatePropertyCursor() ) PropertyCursor propertyCursor = cursors.allocatePropertyCursor() )
Expand Down Expand Up @@ -227,7 +215,7 @@ private void scanEverythingBelongingToRelationships( Read dataRead, CursorFactor
} }
} }


private void scanEverythingBelongingToNodes( Read dataRead, CursorFactory cursors ) private void scanEverythingBelongingToNodes( )
{ {
try ( NodeCursor nodeCursor = cursors.allocateNodeCursor(); try ( NodeCursor nodeCursor = cursors.allocateNodeCursor();
PropertyCursor propertyCursor = cursors.allocatePropertyCursor() ) PropertyCursor propertyCursor = cursors.allocatePropertyCursor() )
Expand All @@ -236,15 +224,15 @@ private void scanEverythingBelongingToNodes( Read dataRead, CursorFactory cursor
while ( nodeCursor.next() ) while ( nodeCursor.next() )
{ {
// each node // each node
LabelSet labels = nodeCursor.labels(); SortedLabels labels = SortedLabels.from( nodeCursor.labels() );
nodeCursor.properties( propertyCursor ); nodeCursor.properties( propertyCursor );
MutableIntSet propertyIds = IntSets.mutable.empty(); MutableIntSet propertyIds = IntSets.mutable.empty();


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


propertyIds.add( propertyKeyId ); propertyIds.add( propertyKeyId );
Expand All @@ -259,7 +247,7 @@ private void scanEverythingBelongingToNodes( Read dataRead, CursorFactory cursor
// we can and need (!) to skip this if we found the empty set // we can and need (!) to skip this if we found the empty set
oldPropertyKeySet.removeAll( propertyIds ); oldPropertyKeySet.removeAll( propertyIds );
oldPropertyKeySet.forEach( id -> { oldPropertyKeySet.forEach( id -> {
Pair<LabelSet,Integer> key = Pair.of( labels, id ); Pair<SortedLabels,Integer> key = Pair.of( labels, id );
labelSetANDNodePropertyKeyIdToValueTypeMapping.get( key ).setNullable(); labelSetANDNodePropertyKeyIdToValueTypeMapping.get( key ).setNullable();
} ); } );
} }
Expand Down Expand Up @@ -359,33 +347,33 @@ String getCypherTypeString()
{ {
if ( isIntegral ) if ( isIntegral )
{ {
return isNullable ? NULLABLE_INTEGRAL return isNullable ? ValueName.NULLABLE_INTEGRAL.asString()
: INTEGRAL; : ValueName.INTEGRAL.asString();
} }
else else
{ {
return isNullable ? NULLABLE_FLOATING_POINT return isNullable ? ValueName.NULLABLE_FLOATING_POINT.asString()
: FLOATING_POINT; : ValueName.FLOATING_POINT.asString();
} }
} }
// NUMBER_ARRAY // NUMBER_ARRAY
if ( isIntegral ) if ( isIntegral )
{ {
return isNullable ? NULLABLE_INTEGRAL_ARRAY return isNullable ? ValueName.NULLABLE_INTEGRAL_ARRAY.asString()
: INTEGRAL_ARRAY; : ValueName.INTEGRAL_ARRAY.asString();
} }
else else
{ {
return isNullable ? NULLABLE_FLOATING_POINT_ARRAY return isNullable ? ValueName.NULLABLE_FLOATING_POINT_ARRAY.asString()
: FLOATING_POINT_ARRAY; : ValueName.FLOATING_POINT_ARRAY.asString();
} }


case VALUE_GROUP: case VALUE_GROUP:
return isNullable ? valueGroup.name() + NULLABLE return isNullable ? valueGroup.name() + NULLABLE
: valueGroup.name(); : valueGroup.name();
case ANY: case ANY:
return isNullable ? NULLABLE_ANYVALUE return isNullable ? ValueName.NULLABLE_ANYVALUE.asString()
: ANYVALUE; : ValueName.ANYVALUE.asString();
default: default:
throw new IllegalStateException( "Did not recognize ValueStatus" ); throw new IllegalStateException( "Did not recognize ValueStatus" );
} }
Expand Down Expand Up @@ -485,4 +473,30 @@ enum ValueStatus
VALUE_GROUP, VALUE_GROUP,
ANY ANY
} }

enum ValueName
{
ANYVALUE( "ANY" ),
INTEGRAL( "INTEGRAL" ),
INTEGRAL_ARRAY( "INTEGRALARRAY" ),
FLOATING_POINT( "FLOATINGPOINT" ),
FLOATING_POINT_ARRAY( "FLOATINGPOINTARRAY" ),
NULLABLE_ANYVALUE( ANYVALUE.asString() + NULLABLE ),
NULLABLE_INTEGRAL( INTEGRAL.asString() + NULLABLE ),
NULLABLE_INTEGRAL_ARRAY( INTEGRAL_ARRAY.asString() + NULLABLE ),
NULLABLE_FLOATING_POINT_ARRAY( FLOATING_POINT_ARRAY.asString() + NULLABLE ),
NULLABLE_FLOATING_POINT( FLOATING_POINT.asString() + NULLABLE );

private final String textRepresentation;

ValueName( String textRepresentation )
{
this.textRepresentation = textRepresentation;
}

String asString()
{
return textRepresentation;
}
}
} }

0 comments on commit 4a2b3bc

Please sign in to comment.