Skip to content

Commit

Permalink
Moved shared schema key code to NativeSchemaKey
Browse files Browse the repository at this point in the history
This is now especially important since the value of compareId
plays a bigger part in correctness of updates to the tree.
  • Loading branch information
tinwelint committed Mar 8, 2018
1 parent c42b3f9 commit f29bd08
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 192 deletions.
Expand Up @@ -22,52 +22,24 @@
import org.neo4j.index.internal.gbptree.GBPTree;
import org.neo4j.values.storable.DateValue;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueWriter;

import static java.lang.String.format;

/**
* Includes value and entity id (to be able to handle non-unique values). A value can be any {@link String},
* or rather any string that {@link GBPTree} can handle.
*/
class DateSchemaKey extends ValueWriter.Adapter<RuntimeException> implements NativeSchemaKey
class DateSchemaKey extends NativeSchemaKey
{
static final int SIZE =
Long.BYTES + /* raw value bits */
Long.BYTES; /* entityId */

private long entityId;
private boolean compareId;

long epochDay;

public void setCompareId( boolean compareId )
{
this.compareId = compareId;
}

public boolean getCompareId()
{
return compareId;
}

@Override
public long getEntityId()
void from( Value... values )
{
return entityId;
}

@Override
public void setEntityId( long entityId )
{
this.entityId = entityId;
}

@Override
public void from( long entityId, Value... values )
{
this.entityId = entityId;
compareId = false;
assertValidValue( values ).writeTo( this );
}

Expand All @@ -89,32 +61,22 @@ private DateValue assertValidValue( Value... values )
return (DateValue) values[0];
}

@Override
public String propertiesAsString()
{
return asValue().toString();
}

@Override
public Value asValue()
{
return DateValue.epochDate( epochDay );
}

@Override
public void initAsLowest()
void initValueAsLowest()
{
epochDay = Long.MIN_VALUE;
entityId = Long.MIN_VALUE;
compareId = true;
}

@Override
public void initAsHighest()
void initValueAsHighest()
{
epochDay = Long.MAX_VALUE;
entityId = Long.MAX_VALUE;
compareId = true;
}

/**
Expand All @@ -132,7 +94,7 @@ int compareValueTo( DateSchemaKey other )
@Override
public String toString()
{
return format( "value=%s,entityId=%d,epochDay=%s", asValue(), entityId, epochDay );
return format( "value=%s,entityId=%d,epochDay=%s", asValue(), getEntityId(), epochDay );
}

@Override
Expand Down
Expand Up @@ -21,40 +21,78 @@

import org.neo4j.index.internal.gbptree.GBPTree;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueWriter;

/**
* Includes value and entity id (to be able to handle non-unique values).
* This is the abstraction of what NativeSchemaIndex with friends need from a schema key.
* Note that it says nothing about how keys are compared, serialized, read, written, etc. That is the job of Layout.
*/
interface NativeSchemaKey
abstract class NativeSchemaKey extends ValueWriter.Adapter<RuntimeException>
{
static final boolean DEFAULT_COMPARE_ID = true;

private long entityId;
private boolean compareId = DEFAULT_COMPARE_ID;

/**
* Marks that comparisons with this key requires also comparing entityId, this allows functionality
* of inclusive/exclusive bounds of range queries.
* This is because {@link GBPTree} only support from inclusive and to exclusive.
* <p>
* Note that {@code compareId} is only an in memory state.
*/
void setCompareId( boolean compareId );
void setCompareId( boolean compareId )
{
this.compareId = compareId;
}

boolean getCompareId()
{
return compareId;
}

boolean getCompareId();
long getEntityId()
{
return entityId;
}

long getEntityId();
void setEntityId( long entityId )
{
this.entityId = entityId;
}

void setEntityId( long entityId );
final void from( long entityId, Value... values )
{
compareId = DEFAULT_COMPARE_ID;
this.entityId = entityId;
from( values );
}

void from( long entityId, Value... values );
abstract void from( Value[] values );

String propertiesAsString();
String propertiesAsString()
{
return asValue().toString();
}

Value asValue();
abstract Value asValue();

void initAsLowest();
final void initAsLowest()
{
compareId = DEFAULT_COMPARE_ID;
entityId = Long.MIN_VALUE;
initValueAsLowest();
}

void initAsHighest();
abstract void initValueAsLowest();

@Override
String toString();
final void initAsHighest()
{
compareId = DEFAULT_COMPARE_ID;
entityId = Long.MAX_VALUE;
initValueAsHighest();
}

abstract void initValueAsHighest();
}
Expand Up @@ -21,7 +21,6 @@

import org.neo4j.values.storable.NumberValue;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueWriter;
import org.neo4j.values.storable.Values;

import static java.lang.String.format;
Expand All @@ -34,7 +33,7 @@
* Distinction between double and float exists because coersions between each other and long may differ.
* TODO this should be figured out and potentially reduced to long, double types only.
*/
class NumberSchemaKey extends ValueWriter.Adapter<RuntimeException> implements NativeSchemaKey
class NumberSchemaKey extends NativeSchemaKey
{
static final int SIZE =
Byte.BYTES + /* type of value */
Expand All @@ -43,41 +42,13 @@ class NumberSchemaKey extends ValueWriter.Adapter<RuntimeException> implements N
// TODO this could use 6 bytes instead and have the highest 2 bits stored in the type byte
Long.BYTES; /* entityId */

private long entityId;
private boolean compareId = true;

byte type;
long rawValueBits;

public void setCompareId( boolean compareId )
{
this.compareId = compareId;
}

public boolean getCompareId()
{
return compareId;
}

@Override
public long getEntityId()
{
return entityId;
}

@Override
public void setEntityId( long entityId )
{
this.entityId = entityId;
compareId = true;
}

@Override
public void from( long entityId, Value... values )
void from( Value... values )
{
extractRawBitsAndType( assertValidValue( values ) );
this.entityId = entityId;
compareId = true;
}

private NumberValue assertValidValue( Value... values )
Expand All @@ -100,31 +71,21 @@ private NumberValue assertValidValue( Value... values )
}

@Override
public String propertiesAsString()
{
return asValue().toString();
}

@Override
public NumberValue asValue()
NumberValue asValue()
{
return RawBits.asNumberValue( rawValueBits, type );
}

@Override
public void initAsLowest()
void initValueAsLowest()
{
writeFloatingPoint( Double.NEGATIVE_INFINITY );
entityId = Long.MIN_VALUE;
compareId = true;
}

@Override
public void initAsHighest()
void initValueAsHighest()
{
writeFloatingPoint( Double.POSITIVE_INFINITY );
entityId = Long.MAX_VALUE;
compareId = true;
}

/**
Expand Down Expand Up @@ -152,7 +113,7 @@ private void extractRawBitsAndType( NumberValue value )
@Override
public String toString()
{
return format( "type=%d,rawValue=%d,value=%s,entityId=%d", type, rawValueBits, asValue(), entityId );
return format( "type=%d,rawValue=%d,value=%s,entityId=%d", type, rawValueBits, asValue(), getEntityId() );
}

@Override
Expand Down
Expand Up @@ -34,15 +34,12 @@
* Includes value and entity id (to be able to handle non-unique values).
* A value can be any {@link PointValue} and is represented as a {@code long} to store the 1D mapped version
*/
class SpatialSchemaKey implements NativeSchemaKey
class SpatialSchemaKey extends NativeSchemaKey
{
static final int SIZE =
Long.BYTES + /* raw value bits */
Long.BYTES; /* entityId */

private long entityId;
private boolean compareId;

long rawValueBits;
CoordinateReferenceSystem crs;
SpaceFillingCurve curve;
Expand All @@ -54,41 +51,9 @@ class SpatialSchemaKey implements NativeSchemaKey
}

@Override
public void setCompareId( boolean entityIdIsSpecialTieBreaker )
{
this.compareId = entityIdIsSpecialTieBreaker;
}

@Override
public boolean getCompareId()
{
return compareId;
}

@Override
public long getEntityId()
{
return entityId;
}

@Override
public void setEntityId( long entityId )
{
this.entityId = entityId;
}

@Override
public void from( long entityId, Value... values )
void from( Value... values )
{
extractRawBits( assertValidValue( values ) );
this.entityId = entityId;
compareId = false;
}

@Override
public String propertiesAsString()
{
return asValue().toString();
}

@Override
Expand All @@ -102,32 +67,28 @@ public NumberValue asValue()
}

@Override
public void initAsLowest()
void initValueAsLowest()
{
double[] limit = new double[crs.getDimension()];
Arrays.fill(limit, Double.NEGATIVE_INFINITY);
writePoint( limit );
entityId = Long.MIN_VALUE;
compareId = true;
}

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

public void fromDerivedValue( long entityId, long derivedValue )
{
rawValueBits = derivedValue;
this.entityId = entityId;
this.compareId = false;
setEntityId( entityId );
setCompareId( DEFAULT_COMPARE_ID );
}

/**
Expand Down Expand Up @@ -175,6 +136,6 @@ private void writePoint( double[] coordinate )
@Override
public String toString()
{
return format( "rawValue=%d,value=%s,entityId=%d", rawValueBits, "unknown", entityId );
return format( "rawValue=%d,value=%s,entityId=%d", rawValueBits, "unknown", getEntityId() );
}
}

0 comments on commit f29bd08

Please sign in to comment.