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 fdba154 commit 096204a
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 197 deletions.
Expand Up @@ -19,7 +19,6 @@
*/
package org.neo4j.internal.kernel.api;

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

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

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

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

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

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

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

Expand Down Expand Up @@ -78,33 +72,6 @@ public long[] all()
{
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 @@ -31,7 +31,6 @@

import org.neo4j.helpers.collection.Pair;
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.NodeCursor;
import org.neo4j.internal.kernel.api.PropertyCursor;
Expand All @@ -51,34 +50,39 @@

public class SchemaCalculator
{
private org.neo4j.internal.kernel.api.Transaction ktx;

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

private final Set<Integer> emptyPropertyIdSet = Collections.unmodifiableSet( Collections.emptySet() );
private final String ANYVALUE = "ANY";
private final String INTEGRAL = "INTEGRAL";
private final String INTEGRAL_ARRAY = "INTEGRALARRAY";
private final String FLOATING_POINT = "FLOATINGPOINT";
private final String FLOATING_POINT_ARRAY = "FLOATINGPOINTARRAY";
private final String NULLABLE = "?";
private final String NULLABLE_ANYVALUE = ANYVALUE + NULLABLE;
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";
private static final String NODE = "Node";
private static final String RELATIONSHIP = "Relationship";
private static final String NULLABLE = "?";

private final Read dataRead;
private final TokenRead tokenRead;
private final CursorFactory cursors;

SchemaCalculator( Transaction ktx )
{
this.ktx = ktx;
dataRead = ktx.dataRead();
tokenRead = ktx.tokenRead();
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()
Expand Down Expand Up @@ -122,7 +126,7 @@ private List<SchemaInfoResult> produceResultsForRelationships()
private List<SchemaInfoResult> produceResultsForNodes()
{
List<SchemaInfoResult> results = new ArrayList<>();
for ( LabelSet labelSet : labelSetToPropertyKeysMapping.keySet() )
for ( SortedLabels labelSet : labelSetToPropertyKeysMapping.keySet() )
{
// lookup label names and produce list of names
List<String> labelNames = new ArrayList<>();
Expand Down Expand Up @@ -154,24 +158,8 @@ private List<SchemaInfoResult> produceResultsForNodes()
//TODO: If we would have this schema information in the count store (or somewhere), this could be super fast
private void calculateSchema()
{
// this one does most of the work
Read dataRead = ktx.dataRead();
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 );
scanEverythingBelongingToNodes();
scanEverythingBelongingToRelationships();

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

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

private void scanEverythingBelongingToNodes( Read dataRead, CursorFactory cursors )
private void scanEverythingBelongingToNodes()
{
try ( NodeCursor nodeCursor = cursors.allocateNodeCursor();
PropertyCursor propertyCursor = cursors.allocatePropertyCursor() )
Expand All @@ -235,15 +223,15 @@ private void scanEverythingBelongingToNodes( Read dataRead, CursorFactory cursor
while ( nodeCursor.next() )
{
// each node
LabelSet labels = nodeCursor.labels();
SortedLabels labels = SortedLabels.from( nodeCursor.labels() );
nodeCursor.properties( propertyCursor );
Set<Integer> propertyIds = new HashSet<>();

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

propertyIds.add( propertyKeyId );
Expand All @@ -258,7 +246,7 @@ private void scanEverythingBelongingToNodes( Read dataRead, CursorFactory cursor
// 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 );
Pair<SortedLabels,Integer> key = Pair.of( labels, id );
labelSetANDNodePropertyKeyIdToValueTypeMapping.get( key ).setNullable();
} );
}
Expand Down Expand Up @@ -358,33 +346,33 @@ String getCypherTypeString()
{
if ( isIntegral )
{
return isNullable ? NULLABLE_INTEGRAL
: INTEGRAL;
return isNullable ? ValueName.NULLABLE_INTEGRAL.asString()
: ValueName.INTEGRAL.asString();
}
else
{
return isNullable ? NULLABLE_FLOATING_POINT
: FLOATING_POINT;
return isNullable ? ValueName.NULLABLE_FLOATING_POINT.asString()
: ValueName.FLOATING_POINT.asString();
}
}
// NUMBER_ARRAY
if ( isIntegral )
{
return isNullable ? NULLABLE_INTEGRAL_ARRAY
: INTEGRAL_ARRAY;
return isNullable ? ValueName.NULLABLE_INTEGRAL_ARRAY.asString()
: ValueName.INTEGRAL_ARRAY.asString();
}
else
{
return isNullable ? NULLABLE_FLOATING_POINT_ARRAY
: FLOATING_POINT_ARRAY;
return isNullable ? ValueName.NULLABLE_FLOATING_POINT_ARRAY.asString()
: ValueName.FLOATING_POINT_ARRAY.asString();
}

case VALUE_GROUP:
return isNullable ? valueGroup.name() + NULLABLE
: valueGroup.name();
case ANY:
return isNullable ? NULLABLE_ANYVALUE
: ANYVALUE;
return isNullable ? ValueName.NULLABLE_ANYVALUE.asString()
: ValueName.ANYVALUE.asString();
default:
throw new IllegalStateException( "Did not recognize ValueStatus" );
}
Expand Down Expand Up @@ -484,4 +472,30 @@ enum ValueStatus
VALUE_GROUP,
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 096204a

Please sign in to comment.