Skip to content

Commit

Permalink
Add zoned date time layout
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd committed Mar 9, 2018
1 parent aeb1e27 commit 4294a62
Show file tree
Hide file tree
Showing 16 changed files with 278 additions and 20 deletions.
Expand Up @@ -72,12 +72,13 @@ public void writeDate( long epochDay )
} }


@Override @Override
protected void assertCorrectType( Value value ) protected Value assertCorrectType( Value value )
{ {
if ( !(value instanceof DateValue) ) if ( !(value instanceof DateValue) )
{ {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Key layout does only support DateValue, tried to create key from " + value ); "Key layout does only support DateValue, tried to create key from " + value );
} }
return value;
} }
} }
Expand Up @@ -83,12 +83,13 @@ public void writeLocalDateTime( long epochSecond, int nano )
} }


@Override @Override
protected void assertCorrectType( Value value ) protected Value assertCorrectType( Value value )
{ {
if ( !(value instanceof LocalDateTimeValue) ) if ( !(value instanceof LocalDateTimeValue) )
{ {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Key layout does only support LocalDateTimeValue, tried to create key from " + value ); "Key layout does only support LocalDateTimeValue, tried to create key from " + value );
} }
return value;
} }
} }
Expand Up @@ -72,12 +72,13 @@ public void writeLocalTime( long nanoOfDay )
} }


@Override @Override
protected void assertCorrectType( Value value ) protected Value assertCorrectType( Value value )
{ {
if ( !(value instanceof LocalTimeValue) ) if ( !(value instanceof LocalTimeValue) )
{ {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Key layout does only support LocalTimeValue, tried to create key from " + value ); "Key layout does only support LocalTimeValue, tried to create key from " + value );
} }
return value;
} }
} }
Expand Up @@ -80,12 +80,10 @@ private Value assertValidValue( Value... values )
{ {
throw new IllegalArgumentException( "Tried to create key without value" ); throw new IllegalArgumentException( "Tried to create key without value" );
} }
Value value = values[0]; return assertCorrectType( values[0] );
assertCorrectType( value );
return value;
} }


protected abstract void assertCorrectType( Value value ); protected abstract Value assertCorrectType( Value value );


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


@Override @Override
protected void assertCorrectType( Value value ) protected Value assertCorrectType( Value value )
{ {
if ( !Values.isNumberValue( value ) ) if ( !Values.isNumberValue( value ) )
{ {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Key layout does only support numbers, tried to create key from " + value ); "Key layout does only support numbers, tried to create key from " + value );
} }
return value;
} }


@Override @Override
Expand Down
Expand Up @@ -96,13 +96,14 @@ int compareValueTo( SpatialSchemaKey other )
} }


@Override @Override
protected void assertCorrectType( Value value ) protected Value assertCorrectType( Value value )
{ {
if ( !Values.isGeometryValue( value ) ) if ( !Values.isGeometryValue( value ) )
{ {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Key layout does only support geometries, tried to create key from " + value ); "Key layout does only support geometries, tried to create key from " + value );
} }
return value;
} }


/** /**
Expand Down
Expand Up @@ -47,13 +47,14 @@ int size()
} }


@Override @Override
protected void assertCorrectType( Value value ) protected Value assertCorrectType( Value value )
{ {
if ( !Values.isTextValue( value ) ) if ( !Values.isTextValue( value ) )
{ {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Key layout does only support strings, tried to create key from " + value ); "Key layout does only support strings, tried to create key from " + value );
} }
return value;
} }


@Override @Override
Expand Down
Expand Up @@ -242,7 +242,7 @@ public PartAccessor<?> newLocalDateTime() throws IOException
@Override @Override
public PartAccessor<?> newZonedDateTime() throws IOException public PartAccessor<?> newZonedDateTime() throws IOException
{ {
throw new UnsupportedOperationException( "no comprende" ); return createPartAccessor( temporalIndexFiles.zonedDateTime() );
} }


@Override @Override
Expand Down
Expand Up @@ -34,7 +34,7 @@ class TemporalIndexFiles
private final FileSystemAbstraction fs; private final FileSystemAbstraction fs;
private FileLayout<DateSchemaKey> date; private FileLayout<DateSchemaKey> date;
private FileLayout<LocalDateTimeSchemaKey> localDateTime; private FileLayout<LocalDateTimeSchemaKey> localDateTime;
private FileLayout zonedDateTime; private FileLayout<ZonedDateTimeSchemaKey> zonedDateTime;
private FileLayout<LocalTimeSchemaKey> localTime; private FileLayout<LocalTimeSchemaKey> localTime;
private FileLayout zonedTime; private FileLayout zonedTime;
private FileLayout duration; private FileLayout duration;
Expand All @@ -44,8 +44,9 @@ class TemporalIndexFiles
this.fs = fs; this.fs = fs;
File indexDirectory = directoryStructure.directoryForIndex( indexId ); File indexDirectory = directoryStructure.directoryForIndex( indexId );
this.date = new FileLayout<>( new File( indexDirectory, "date" ), DateLayout.of( descriptor ), ValueGroup.DATE ); this.date = new FileLayout<>( new File( indexDirectory, "date" ), DateLayout.of( descriptor ), ValueGroup.DATE );
this.localTime = new FileLayout<>( new File( indexDirectory, "time" ), LocalTimeLayout.of( descriptor ), ValueGroup.LOCAL_TIME ); this.localTime = new FileLayout<>( new File( indexDirectory, "localTime" ), LocalTimeLayout.of( descriptor ), ValueGroup.LOCAL_TIME );
this.localDateTime = new FileLayout<>( new File( indexDirectory, "datetime" ), LocalDateTimeLayout.of( descriptor ), ValueGroup.LOCAL_DATE_TIME ); this.localDateTime = new FileLayout<>( new File( indexDirectory, "localDateTime" ), LocalDateTimeLayout.of( descriptor ), ValueGroup.LOCAL_DATE_TIME );
this.zonedDateTime = new FileLayout<>( new File( indexDirectory, "zonedDateTime" ), ZonedDateTimeLayout.of( descriptor ), ValueGroup.ZONED_DATE_TIME );
} }


Iterable<FileLayout> existing() Iterable<FileLayout> existing()
Expand Down Expand Up @@ -83,6 +84,11 @@ FileLayout<LocalDateTimeSchemaKey> localDateTime()
return localDateTime; return localDateTime;
} }


FileLayout<ZonedDateTimeSchemaKey> zonedDateTime()
{
return zonedDateTime;
}

private void addIfExists( List<FileLayout> existing, FileLayout fileLayout ) private void addIfExists( List<FileLayout> existing, FileLayout fileLayout )
{ {
if ( exists( fileLayout ) ) if ( exists( fileLayout ) )
Expand Down
Expand Up @@ -104,7 +104,7 @@ public IndexUpdater newLocalDateTime() throws IOException
@Override @Override
public IndexUpdater newZonedDateTime() throws IOException public IndexUpdater newZonedDateTime() throws IOException
{ {
throw new UnsupportedOperationException( "too tired to write" ); return populator.zonedDateTime().newPopulatingUpdater( propertyAccessor );
} }


@Override @Override
Expand Down
Expand Up @@ -256,7 +256,7 @@ public PartPopulator<?> newLocalDateTime() throws IOException
@Override @Override
public PartPopulator<?> newZonedDateTime() throws IOException public PartPopulator<?> newZonedDateTime() throws IOException
{ {
throw new UnsupportedOperationException( "not implementedur still" ); return create( temporalIndexFiles.zonedDateTime() );
} }


@Override @Override
Expand Down
Expand Up @@ -151,7 +151,7 @@ public TemporalIndexPartReader<?> newLocalDateTime()
@Override @Override
public TemporalIndexPartReader<?> newZonedDateTime() public TemporalIndexPartReader<?> newZonedDateTime()
{ {
throw new UnsupportedOperationException( "Illiterate" ); return accessor.selectOrElse( ValueGroup.ZONED_DATE_TIME, TemporalIndexAccessor.PartAccessor::newReader, null );
} }


@Override @Override
Expand Down
Expand Up @@ -97,7 +97,7 @@ public NativeSchemaIndexUpdater<?, NativeSchemaValue> newLocalDateTime() throws
@Override @Override
public NativeSchemaIndexUpdater<?, NativeSchemaValue> newZonedDateTime() throws IOException public NativeSchemaIndexUpdater<?, NativeSchemaValue> newZonedDateTime() throws IOException
{ {
throw new UnsupportedOperationException( "ma-a-da dayo" ); return accessor.zonedDateTime().newUpdater( mode );
} }


@Override @Override
Expand Down
@@ -0,0 +1,114 @@
/*
* 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;

import org.neo4j.index.internal.gbptree.Layout;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;

/**
* {@link Layout} for absolute date times.
*/
class ZonedDateTimeLayout extends BaseLayout<ZonedDateTimeSchemaKey>
{
private static final int ZONE_ID_FLAG = 0x0100_0000;
private static final int NO_ZONE_ID_FLAG = 0x0000_FFFF;

public static Layout<ZonedDateTimeSchemaKey,NativeSchemaValue> of( IndexDescriptor descriptor )
{
return descriptor.type() == IndexDescriptor.Type.UNIQUE ? ZonedDateTimeLayout.UNIQUE : ZonedDateTimeLayout.NON_UNIQUE;
}

private static final ZonedDateTimeLayout UNIQUE = new ZonedDateTimeLayout( "UTdt", 0, 1 );
private static final ZonedDateTimeLayout NON_UNIQUE = new ZonedDateTimeLayout( "NTdt", 0, 1 );

private ZonedDateTimeLayout(
String layoutName, int majorVersion, int minorVersion )
{
super( layoutName, majorVersion, minorVersion );
}

@Override
public ZonedDateTimeSchemaKey newKey()
{
return new ZonedDateTimeSchemaKey();
}

@Override
public ZonedDateTimeSchemaKey copyKey( ZonedDateTimeSchemaKey key, ZonedDateTimeSchemaKey into )
{
into.epochSecondUTC = key.epochSecondUTC;
into.nanoOfSecond = key.nanoOfSecond;
into.zoneId = key.zoneId;
into.zoneOffsetSeconds = key.zoneOffsetSeconds;
into.setEntityId( key.getEntityId() );
into.setCompareId( key.getCompareId() );
return into;
}

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

@Override
public void writeKey( PageCursor cursor, ZonedDateTimeSchemaKey key )
{
cursor.putLong( key.epochSecondUTC );
cursor.putInt( key.nanoOfSecond );
if ( key.zoneId >= 0 )
{
cursor.putInt( key.zoneId | ZONE_ID_FLAG );
}
else
{
cursor.putInt( key.zoneOffsetSeconds );
}
cursor.putLong( key.getEntityId() );
}

@Override
public void readKey( PageCursor cursor, ZonedDateTimeSchemaKey into, int keySize )
{
into.epochSecondUTC = cursor.getLong();
into.nanoOfSecond = cursor.getInt();
int encodedZone = cursor.getInt();
if ( ( encodedZone & ZONE_ID_FLAG ) != 0 )
{
into.zoneId = (short) ( encodedZone & NO_ZONE_ID_FLAG );
into.zoneOffsetSeconds = 0;
}
else
{
into.zoneId = -1;
into.zoneOffsetSeconds = encodedZone;
}
into.setEntityId( cursor.getLong() );
}

@Override
public boolean fixedSize()
{
return true;
}
}

0 comments on commit 4294a62

Please sign in to comment.