Skip to content

Commit

Permalink
Removed lots of duplicated Layout code
Browse files Browse the repository at this point in the history
And uses one single version of a couple of critical methods.
  • Loading branch information
tinwelint committed Mar 8, 2018
1 parent 5c2f3ed commit bb21dac
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 329 deletions.
Expand Up @@ -23,81 +23,23 @@
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;

/**
* {@link Layout} for numbers where numbers doesn't need to be unique.
*/
abstract class DateLayout extends Layout.Adapter<DateSchemaKey,NativeSchemaValue>
class DateLayout extends SchemaLayout<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()
{
@Override
public long identifier()
{
return UNIQUE_LAYOUT_IDENTIFIER;
}

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

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

@Override
public int compare( DateSchemaKey o1, DateSchemaKey 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;
}
};
public static DateLayout UNIQUE = new DateLayout( UNIQUE_LAYOUT_IDENTIFIER, 0, 1 );

private static final long NON_UNIQUE_LAYOUT_IDENTIFIER = Layout.namedIdentifier( "NTda", NativeSchemaValue.SIZE );
public static DateLayout NON_UNIQUE = new DateLayout()
{
@Override
public long identifier()
{
return NON_UNIQUE_LAYOUT_IDENTIFIER;
}

@Override
public int majorVersion()
{
return 0;
}
public static DateLayout NON_UNIQUE = new DateLayout( NON_UNIQUE_LAYOUT_IDENTIFIER, 0, 1 );

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

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

@Override
public DateSchemaKey newKey()
Expand All @@ -114,36 +56,19 @@ public DateSchemaKey copyKey( DateSchemaKey key, DateSchemaKey into )
return into;
}

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

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

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

@Override
public void writeKey( PageCursor cursor, DateSchemaKey key )
{
cursor.putLong( key.epochDay );
cursor.putLong( key.getEntityId() );
}

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

@Override
public void readKey( PageCursor cursor, DateSchemaKey into, int keySize )
{
Expand All @@ -152,13 +77,8 @@ public void readKey( PageCursor cursor, DateSchemaKey into, int keySize )
}

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

@Override
public boolean fixedSize()
int compareValue( DateSchemaKey o1, DateSchemaKey o2 )
{
return true;
return o1.compareValueTo( o2 );
}
}
Expand Up @@ -25,8 +25,13 @@
/**
* {@link Layout} for numbers where numbers doesn't need to be unique.
*/
abstract class NumberLayout extends Layout.Adapter<NumberSchemaKey,NativeSchemaValue>
abstract class NumberLayout extends SchemaLayout<NumberSchemaKey>
{
NumberLayout( long identifier, int majorVersion, int minorVersion )
{
super( identifier, majorVersion, minorVersion );
}

@Override
public NumberSchemaKey newKey()
{
Expand All @@ -43,24 +48,12 @@ public NumberSchemaKey copyKey( NumberSchemaKey key, NumberSchemaKey into )
return into;
}

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

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

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

@Override
public void writeKey( PageCursor cursor, NumberSchemaKey key )
{
Expand All @@ -69,11 +62,6 @@ public void writeKey( PageCursor cursor, NumberSchemaKey key )
cursor.putLong( key.getEntityId() );
}

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

@Override
public void readKey( PageCursor cursor, NumberSchemaKey into, int keySize )
{
Expand All @@ -83,28 +71,8 @@ public void readKey( PageCursor cursor, NumberSchemaKey into, int keySize )
}

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

@Override
public int compare( NumberSchemaKey o1, NumberSchemaKey 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;
}

@Override
public boolean fixedSize()
int compareValue( NumberSchemaKey o1, NumberSchemaKey o2 )
{
return true;
return o1.compareValueTo( o2 );
}
}
Expand Up @@ -21,28 +21,15 @@

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

public class NumberLayoutNonUnique extends NumberLayout
class NumberLayoutNonUnique extends NumberLayout
{
private static final String IDENTIFIER_NAME = "NUNI";
static final int MAJOR_VERSION = 0;
static final int MINOR_VERSION = 1;
static long IDENTIFIER = Layout.namedIdentifier( IDENTIFIER_NAME, NativeSchemaValue.SIZE );
private static final int MAJOR_VERSION = 0;
private static final int MINOR_VERSION = 1;
private static long IDENTIFIER = Layout.namedIdentifier( IDENTIFIER_NAME, NativeSchemaValue.SIZE );

@Override
public long identifier()
NumberLayoutNonUnique()
{
return IDENTIFIER;
}

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

@Override
public int minorVersion()
{
return MINOR_VERSION;
super( IDENTIFIER, MAJOR_VERSION, MINOR_VERSION );
}
}
Expand Up @@ -27,26 +27,12 @@
class NumberLayoutUnique extends NumberLayout
{
private static final String IDENTIFIER_NAME = "UNI";
static final int MAJOR_VERSION = 0;
static final int MINOR_VERSION = 1;
static long IDENTIFIER = Layout.namedIdentifier( IDENTIFIER_NAME, NumberSchemaKey.SIZE );
private static final int MAJOR_VERSION = 0;
private static final int MINOR_VERSION = 1;
private static long IDENTIFIER = Layout.namedIdentifier( IDENTIFIER_NAME, NumberSchemaKey.SIZE );

@Override
public long identifier()
NumberLayoutUnique()
{
// todo Is Number.Value.SIZE a good checksum?
return IDENTIFIER;
}

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

@Override
public int minorVersion()
{
return MINOR_VERSION;
super( IDENTIFIER, MAJOR_VERSION, MINOR_VERSION );
}
}
@@ -0,0 +1,102 @@
/*
* 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;
import org.neo4j.io.pagecache.PageCursor;

abstract class SchemaLayout<KEY extends NativeSchemaKey> extends Layout.Adapter<KEY,NativeSchemaValue>
{
private final long identifier;
private final int majorVersion;
private final int minorVersion;

SchemaLayout( long identifier, int majorVersion, int minorVersion )
{
this.identifier = identifier;
this.majorVersion = majorVersion;
this.minorVersion = minorVersion;
}

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

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

@Override
public void writeValue( PageCursor cursor, NativeSchemaValue nativeSchemaValue )
{
// nothing to write
}

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

@Override
public boolean fixedSize()
{
return true; // for the most case
}

@Override
public long identifier()
{
return identifier;
}

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

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

@Override
public final int compare( KEY o1, KEY o2 )
{
int valueComparison = compareValue( o1, o2 );
if ( valueComparison == 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 valueComparison;
}

abstract int compareValue( KEY o1, KEY o2 );
}

0 comments on commit bb21dac

Please sign in to comment.