Skip to content

Commit

Permalink
Share code between temporal schema keys
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd committed Mar 9, 2018
1 parent c6023ce commit 72c8972
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 150 deletions.
Expand Up @@ -35,30 +35,6 @@ class DateSchemaKey extends NativeSchemaKey


long epochDay; long epochDay;


@Override
void from( Value... values )
{
assertValidValue( values ).writeTo( this );
}

private DateValue assertValidValue( Value... values )
{
if ( values.length > 1 )
{
throw new IllegalArgumentException( "Tried to create composite key with non-composite schema key layout" );
}
if ( values.length < 1 )
{
throw new IllegalArgumentException( "Tried to create key without value" );
}
if ( !(values[0] instanceof DateValue) )
{
throw new IllegalArgumentException(
"Key layout does only support DateValue, tried to create key from " + values[0] );
}
return (DateValue) values[0];
}

@Override @Override
public Value asValue() public Value asValue()
{ {
Expand Down Expand Up @@ -100,4 +76,14 @@ public void writeDate( long epochDay )
{ {
this.epochDay = epochDay; this.epochDay = epochDay;
} }

@Override
protected void assertCorrectType( Value value )
{
if ( !(value instanceof DateValue) )
{
throw new IllegalArgumentException(
"Key layout does only support DateValue, tried to create key from " + value );
}
}
} }
Expand Up @@ -37,30 +37,6 @@ class LocalDateTimeSchemaKey extends NativeSchemaKey
int nanoOfSecond; int nanoOfSecond;
long epochSecond; long epochSecond;


@Override
public void from( Value... values )
{
assertValidValue( values ).writeTo( this );
}

private LocalDateTimeValue assertValidValue( Value... values )
{
if ( values.length > 1 )
{
throw new IllegalArgumentException( "Tried to create composite key with non-composite schema key layout" );
}
if ( values.length < 1 )
{
throw new IllegalArgumentException( "Tried to create key without value" );
}
if ( !(values[0] instanceof LocalDateTimeValue) )
{
throw new IllegalArgumentException(
"Key layout does only support LocalDateTimeValue, tried to create key from " + values[0] );
}
return (LocalDateTimeValue) values[0];
}

@Override @Override
public Value asValue() public Value asValue()
{ {
Expand Down Expand Up @@ -101,7 +77,8 @@ int compareValueTo( LocalDateTimeSchemaKey other )
@Override @Override
public String toString() public String toString()
{ {
return format( "value=%s,entityId=%d,nanoOfDay=%s", asValue(), getEntityId(), nanoOfSecond ); return format( "value=%s,entityId=%d,epochSecond=%d,nanoOfSecond=%d",
asValue(), getEntityId(), epochSecond, nanoOfSecond );
} }


@Override @Override
Expand All @@ -110,4 +87,14 @@ public void writeLocalDateTime( long epochSecond, int nano )
this.nanoOfSecond = nano; this.nanoOfSecond = nano;
this.epochSecond = epochSecond; this.epochSecond = epochSecond;
} }

@Override
protected void assertCorrectType( Value value )
{
if ( !(value instanceof LocalDateTimeValue) )
{
throw new IllegalArgumentException(
"Key layout does only support LocalDateTimeValue, tried to create key from " + value );
}
}
} }
Expand Up @@ -35,30 +35,6 @@ class LocalTimeSchemaKey extends NativeSchemaKey


long nanoOfDay; long nanoOfDay;


@Override
public void from( Value... values )
{
assertValidValue( values ).writeTo( this );
}

private LocalTimeValue assertValidValue( Value... values )
{
if ( values.length > 1 )
{
throw new IllegalArgumentException( "Tried to create composite key with non-composite schema key layout" );
}
if ( values.length < 1 )
{
throw new IllegalArgumentException( "Tried to create key without value" );
}
if ( !(values[0] instanceof LocalTimeValue) )
{
throw new IllegalArgumentException(
"Key layout does only support LocalTimeValue, tried to create key from " + values[0] );
}
return (LocalTimeValue) values[0];
}

@Override @Override
public Value asValue() public Value asValue()
{ {
Expand Down Expand Up @@ -100,4 +76,14 @@ public void writeLocalTime( long nanoOfDay )
{ {
this.nanoOfDay = nanoOfDay; this.nanoOfDay = nanoOfDay;
} }

@Override
protected void assertCorrectType( Value value )
{
if ( !(value instanceof LocalTimeValue) )
{
throw new IllegalArgumentException(
"Key layout does only support LocalTimeValue, tried to create key from " + value );
}
}
} }
Expand Up @@ -66,10 +66,26 @@ final void from( long entityId, Value... values )
{ {
compareId = DEFAULT_COMPARE_ID; compareId = DEFAULT_COMPARE_ID;
this.entityId = entityId; this.entityId = entityId;
from( values ); // copy value state and store in this key instance
assertValidValue( values ).writeTo( this );
} }


abstract void from( Value[] values ); private Value assertValidValue( Value... values )
{
if ( values.length > 1 )
{
throw new IllegalArgumentException( "Tried to create composite key with non-composite schema key layout" );
}
if ( values.length < 1 )
{
throw new IllegalArgumentException( "Tried to create key without value" );
}
Value value = values[0];
assertCorrectType( value );
return value;
}

protected abstract void assertCorrectType( Value value );


String propertiesAsString() String propertiesAsString()
{ {
Expand Down
Expand Up @@ -46,28 +46,13 @@ class NumberSchemaKey extends NativeSchemaKey
long rawValueBits; long rawValueBits;


@Override @Override
void from( Value... values ) protected void assertCorrectType( Value value )
{ {
extractRawBitsAndType( assertValidValue( values ) ); if ( !Values.isNumberValue( value ) )
}

private NumberValue assertValidValue( Value... values )
{
// TODO: support multiple values, right?
if ( values.length > 1 )
{
throw new IllegalArgumentException( "Tried to create composite key with non-composite schema key layout" );
}
if ( values.length < 1 )
{
throw new IllegalArgumentException( "Tried to create key without value" );
}
if ( !Values.isNumberValue( values[0] ) )
{ {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Key layout does only support numbers, tried to create key from " + values[0] ); "Key layout does only support numbers, tried to create key from " + value );
} }
return (NumberValue) values[0];
} }


@Override @Override
Expand Down Expand Up @@ -100,16 +85,6 @@ int compareValueTo( NumberSchemaKey other )
return RawBits.compare( rawValueBits, type, other.rawValueBits, other.type ); return RawBits.compare( rawValueBits, type, other.rawValueBits, other.type );
} }


/**
* Extracts raw bits and type from a {@link NumberValue} and store as state of this {@link NumberSchemaKey} instance.
*
* @param value actual {@link NumberValue} value.
*/
private void extractRawBitsAndType( NumberValue value )
{
value.writeTo( this );
}

@Override @Override
public String toString() public String toString()
{ {
Expand Down
Expand Up @@ -50,12 +50,6 @@ class SpatialSchemaKey extends NativeSchemaKey
this.curve = curve; this.curve = curve;
} }


@Override
void from( Value... values )
{
extractRawBits( assertValidValue( values ) );
}

@Override @Override
public NumberValue asValue() public NumberValue asValue()
{ {
Expand All @@ -71,17 +65,17 @@ void initValueAsLowest()
{ {
double[] limit = new double[crs.getDimension()]; double[] limit = new double[crs.getDimension()];
Arrays.fill(limit, Double.NEGATIVE_INFINITY); Arrays.fill(limit, Double.NEGATIVE_INFINITY);
writePoint( limit ); writePoint( crs, limit );
} }


@Override @Override
void initValueAsHighest() void initValueAsHighest()
{ {
// These coords will generate the largest value on the spacial curve // These coordinates will generate the largest value on the spacial curve
double[] limit = new double[crs.getDimension()]; double[] limit = new double[crs.getDimension()];
Arrays.fill(limit, Double.NEGATIVE_INFINITY); Arrays.fill(limit, Double.NEGATIVE_INFINITY);
limit[0] = Double.POSITIVE_INFINITY; limit[0] = Double.POSITIVE_INFINITY;
writePoint( limit ); writePoint( crs, limit );
} }


public void fromDerivedValue( long entityId, long derivedValue ) public void fromDerivedValue( long entityId, long derivedValue )
Expand All @@ -101,34 +95,21 @@ int compareValueTo( SpatialSchemaKey other )
return Long.compare( rawValueBits, other.rawValueBits ); return Long.compare( rawValueBits, other.rawValueBits );
} }


private PointValue assertValidValue( Value... values ) @Override
protected void assertCorrectType( Value value )
{ {
// TODO: support multiple values, right? if ( !Values.isGeometryValue( value ) )
if ( values.length > 1 )
{
throw new IllegalArgumentException( "Tried to create composite key with non-composite schema key layout" );
}
if ( values.length < 1 )
{
throw new IllegalArgumentException( "Tried to create key without value" );
}
if ( !Values.isGeometryValue( values[0] ) )
{ {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Key layout does only support geometries, tried to create key from " + values[0] ); "Key layout does only support geometries, tried to create key from " + value );
} }
return (PointValue) values[0];
}

private void extractRawBits( PointValue value )
{
writePoint( value.coordinate() );
} }


/** /**
* Extracts raw bits from a {@link PointValue} and store as state of this {@link SpatialSchemaKey} instance. * Extracts raw bits from a {@link PointValue} and store as state of this {@link SpatialSchemaKey} instance.
*/ */
private void writePoint( double[] coordinate ) @Override
public void writePoint( CoordinateReferenceSystem crs, double[] coordinate )
{ {
rawValueBits = curve.derivedValueFor( coordinate ); rawValueBits = curve.derivedValueFor( coordinate );
} }
Expand Down
Expand Up @@ -23,7 +23,6 @@


import org.neo4j.index.internal.gbptree.GBPTree; import org.neo4j.index.internal.gbptree.GBPTree;
import org.neo4j.string.UTF8; import org.neo4j.string.UTF8;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.storable.Value; import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.Values; import org.neo4j.values.storable.Values;


Expand All @@ -48,27 +47,13 @@ int size()
} }


@Override @Override
void from( Value... values ) protected void assertCorrectType( Value value )
{ {
assertValidValue( values ).writeTo( this ); if ( !Values.isTextValue( value ) )
}

private TextValue assertValidValue( Value... values )
{
if ( values.length > 1 )
{
throw new IllegalArgumentException( "Tried to create composite key with non-composite schema key layout" );
}
if ( values.length < 1 )
{
throw new IllegalArgumentException( "Tried to create key without value" );
}
if ( !Values.isTextValue( values[0] ) )
{ {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Key layout does only support strings, tried to create key from " + values[0] ); "Key layout does only support strings, tried to create key from " + value );
} }
return (TextValue) values[0];
} }


@Override @Override
Expand Down
Expand Up @@ -120,7 +120,7 @@ public boolean hasFullValuePrecision( IndexQuery... predicates )


private boolean validPredicates( IndexQuery[] predicates ) private boolean validPredicates( IndexQuery[] predicates )
{ {
return predicates[0] instanceof ExactPredicate || predicates[0] instanceof GeometryRangePredicate; return predicates[0] instanceof ExactPredicate;
} }


/** /**
Expand Down

0 comments on commit 72c8972

Please sign in to comment.