Skip to content

Commit

Permalink
Share code between temporal layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd committed Mar 9, 2018
1 parent 72c8972 commit c416f1e
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 213 deletions.
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.index.schema;

import org.neo4j.index.internal.gbptree.Layout;

/**
* Simple GBPTree Layout with no values.
*
* @param <KEY> the key type
*/
abstract class BaseLayout<KEY extends ComparableNativeSchemaKey<KEY>> extends SchemaLayout<KEY>
{
BaseLayout( String layoutName, int majorVersion, int minorVersion )
{
super( Layout.namedIdentifier( layoutName, NativeSchemaValue.SIZE ), majorVersion, minorVersion );
}

@Override
int compareValue( KEY o1, KEY o2 )
{
return o1.compareValueTo( o2 );
}

}
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.index.schema;

import java.util.Comparator;

/**
* Adds comparability to NativeSchemaKey.
*
* @param <SELF> the type of the concrete implementing subclass
*/
abstract class ComparableNativeSchemaKey<SELF extends ComparableNativeSchemaKey> extends NativeSchemaKey
{

/**
* Compares the value of this key to that of another key.
* This method is expected to be called in scenarios where inconsistent reads may happen (and later retried).
*
* @param other the {@link LocalTimeSchemaKey} to compare to.
* @return comparison against the {@code other} {@link LocalTimeSchemaKey}.
*/
abstract int compareValueTo( SELF other );

static <T extends ComparableNativeSchemaKey<T>> Comparator<T> UNIQUE()
{
return ( o1, o2 ) -> {
int comparison = o1.compareValueTo( o2 );
if ( comparison == 0 )
{
// This is a special case where we need also compare entityId to support inclusive/exclusive
if ( o1.getCompareId() || o2.getCompareId() )
{
return Long.compare( o1.getEntityId(), o2.getEntityId() );
}
}
return comparison;
};
}

static <T extends ComparableNativeSchemaKey<T>> Comparator<T> NON_UNIQUE()
{
return ( o1, o2 ) -> {
int comparison = o1.compareValueTo( o2 );
return comparison != 0 ? comparison : Long.compare( o1.getEntityId(), o2.getEntityId() );
};
}
}
Expand Up @@ -26,22 +26,19 @@
/**
* {@link Layout} for dates.
*/
class DateLayout extends SchemaLayout<DateSchemaKey>
class DateLayout extends BaseLayout<DateSchemaKey>
{
public static Layout<DateSchemaKey,NativeSchemaValue> of( IndexDescriptor descriptor )
{
return descriptor.type() == IndexDescriptor.Type.UNIQUE ? DateLayout.UNIQUE : DateLayout.NON_UNIQUE;
}

private static final long UNIQUE_LAYOUT_IDENTIFIER = Layout.namedIdentifier( "UTda", NativeSchemaValue.SIZE );
public static DateLayout UNIQUE = new DateLayout( UNIQUE_LAYOUT_IDENTIFIER, 0, 1 );
private static DateLayout UNIQUE = new DateLayout( "UTda", 0, 1 );
private static DateLayout NON_UNIQUE = new DateLayout( "NTda", 0, 1 );

private static final long NON_UNIQUE_LAYOUT_IDENTIFIER = Layout.namedIdentifier( "NTda", NativeSchemaValue.SIZE );
public static DateLayout NON_UNIQUE = new DateLayout( NON_UNIQUE_LAYOUT_IDENTIFIER, 0, 1 );

DateLayout( long identifier, int majorVersion, int minorVersion )
private DateLayout( String layoutName, int majorVersion, int minorVersion )
{
super( identifier, majorVersion, minorVersion );
super( layoutName, majorVersion, minorVersion );
}

@Override
Expand Down Expand Up @@ -78,10 +75,4 @@ public void readKey( PageCursor cursor, DateSchemaKey into, int keySize )
into.epochDay = cursor.getLong();
into.setEntityId( cursor.getLong() );
}

@Override
int compareValue( DateSchemaKey o1, DateSchemaKey o2 )
{
return o1.compareValueTo( o2 );
}
}
Expand Up @@ -27,7 +27,7 @@
/**
* Includes value and entity id (to be able to handle non-unique values). A value can be any {@link DateValue}.
*/
class DateSchemaKey extends NativeSchemaKey
class DateSchemaKey extends ComparableNativeSchemaKey<DateSchemaKey>
{
static final int SIZE =
Long.BYTES + /* epochDay */
Expand All @@ -53,14 +53,8 @@ void initValueAsHighest()
epochDay = Long.MAX_VALUE;
}

/**
* Compares the value of this key to that of another key.
* This method is expected to be called in scenarios where inconsistent reads may happen (and later retried).
*
* @param other the {@link DateSchemaKey} to compare to.
* @return comparison against the {@code other} {@link DateSchemaKey}.
*/
int compareValueTo( DateSchemaKey other )
@Override
public int compareValueTo( DateSchemaKey other )
{
return Long.compare( epochDay, other.epochDay );
}
Expand Down
Expand Up @@ -26,78 +26,20 @@
/**
* {@link Layout} for local date times.
*/
abstract class LocalDateTimeLayout extends Layout.Adapter<LocalDateTimeSchemaKey,NativeSchemaValue>
class LocalDateTimeLayout extends BaseLayout<LocalDateTimeSchemaKey>
{
public static Layout<LocalDateTimeSchemaKey,NativeSchemaValue> of( IndexDescriptor descriptor )
{
return descriptor.type() == IndexDescriptor.Type.UNIQUE ? LocalDateTimeLayout.UNIQUE : LocalDateTimeLayout.NON_UNIQUE;
}

private static final long UNIQUE_LAYOUT_IDENTIFIER = Layout.namedIdentifier( "UTld", NativeSchemaValue.SIZE );
public static LocalDateTimeLayout UNIQUE = new LocalDateTimeLayout()
{
@Override
public long identifier()
{
return UNIQUE_LAYOUT_IDENTIFIER;
}

@Override
public int majorVersion()
{
return 0;
}

@Override
public int minorVersion()
{
return 1;
}
private static LocalDateTimeLayout UNIQUE = new LocalDateTimeLayout( "UTld", 0, 1 );
private static LocalDateTimeLayout NON_UNIQUE = new LocalDateTimeLayout( "NTld", 0, 1 );

@Override
public int compare( LocalDateTimeSchemaKey o1, LocalDateTimeSchemaKey o2 )
{
int comparison = o1.compareValueTo( o2 );
if ( comparison == 0 )
{
// This is a special case where we need also compare entityId to support inclusive/exclusive
if ( o1.getCompareId() || o2.getCompareId() )
{
return Long.compare( o1.getEntityId(), o2.getEntityId() );
}
}
return comparison;
}
};

private static final long NON_UNIQUE_LAYOUT_IDENTIFIER = Layout.namedIdentifier( "NTld", NativeSchemaValue.SIZE );
public static LocalDateTimeLayout NON_UNIQUE = new LocalDateTimeLayout()
private LocalDateTimeLayout( String layoutName, int majorVersion, int minorVersion )
{
@Override
public long identifier()
{
return NON_UNIQUE_LAYOUT_IDENTIFIER;
}

@Override
public int majorVersion()
{
return 0;
}

@Override
public int minorVersion()
{
return 1;
}

@Override
public int compare( LocalDateTimeSchemaKey o1, LocalDateTimeSchemaKey o2 )
{
int comparison = o1.compareValueTo( o2 );
return comparison != 0 ? comparison : Long.compare( o1.getEntityId(), o2.getEntityId() );
}
};
super( layoutName, majorVersion, minorVersion );
}

@Override
public LocalDateTimeSchemaKey newKey()
Expand All @@ -115,24 +57,12 @@ public LocalDateTimeSchemaKey copyKey( LocalDateTimeSchemaKey key, LocalDateTime
return into;
}

@Override
public NativeSchemaValue newValue()
{
return NativeSchemaValue.INSTANCE;
}

@Override
public int keySize( LocalDateTimeSchemaKey key )
{
return LocalDateTimeSchemaKey.SIZE;
}

@Override
public int valueSize( NativeSchemaValue value )
{
return NativeSchemaValue.SIZE;
}

@Override
public void writeKey( PageCursor cursor, LocalDateTimeSchemaKey key )
{
Expand All @@ -141,11 +71,6 @@ public void writeKey( PageCursor cursor, LocalDateTimeSchemaKey key )
cursor.putLong( key.getEntityId() );
}

@Override
public void writeValue( PageCursor cursor, NativeSchemaValue value )
{
}

@Override
public void readKey( PageCursor cursor, LocalDateTimeSchemaKey into, int keySize )
{
Expand All @@ -154,11 +79,6 @@ public void readKey( PageCursor cursor, LocalDateTimeSchemaKey into, int keySize
into.setEntityId( cursor.getLong() );
}

@Override
public void readValue( PageCursor cursor, NativeSchemaValue into, int valueSize )
{
}

@Override
public boolean fixedSize()
{
Expand Down
Expand Up @@ -27,7 +27,7 @@
/**
* Includes value and entity id (to be able to handle non-unique values). A value can be any {@link LocalDateTimeValue}.
*/
class LocalDateTimeSchemaKey extends NativeSchemaKey
class LocalDateTimeSchemaKey extends ComparableNativeSchemaKey<LocalDateTimeSchemaKey>
{
static final int SIZE =
Long.BYTES + /* epochSecond */
Expand Down Expand Up @@ -57,14 +57,8 @@ public void initValueAsHighest()
nanoOfSecond = Integer.MAX_VALUE;
}

/**
* Compares the value of this key to that of another key.
* This method is expected to be called in scenarios where inconsistent reads may happen (and later retried).
*
* @param other the {@link LocalDateTimeSchemaKey} to compare to.
* @return comparison against the {@code other} {@link LocalDateTimeSchemaKey}.
*/
int compareValueTo( LocalDateTimeSchemaKey other )
@Override
public int compareValueTo( LocalDateTimeSchemaKey other )
{
int compare = Long.compare( epochSecond, other.epochSecond );
if ( compare == 0 )
Expand Down

0 comments on commit c416f1e

Please sign in to comment.