Skip to content

Commit

Permalink
Adds more CRS header testing and slight format change
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Sep 5, 2018
1 parent 5c6d46a commit d0f1a89
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 21 deletions.
Expand Up @@ -66,9 +66,4 @@ public SpaceFillingCurveSettings forCRS( CoordinateReferenceSystem crs )
return SpaceFillingCurveSettingsFactory.fromConfig( maxBits, new EnvelopeSettings( crs ) ); return SpaceFillingCurveSettingsFactory.fromConfig( maxBits, new EnvelopeSettings( crs ) );
} }
} }

public int getMaxBits()
{
return maxBits;
}
} }
Expand Up @@ -35,6 +35,11 @@
public class IndexSpecificSpaceFillingCurveSettingsCache public class IndexSpecificSpaceFillingCurveSettingsCache
{ {
private final ConfiguredSpaceFillingCurveSettingsCache globalConfigCache; private final ConfiguredSpaceFillingCurveSettingsCache globalConfigCache;
/**
* Map of settings that are specific to this index, i.e. where there is or have been at least one value of in the index.
* Modifications of this map happen by a single thread at a time, due to index updates being applied using work-sync,
* but there can be concurrent readers at any point.
*/
private final ConcurrentMap<CoordinateReferenceSystem,SpaceFillingCurveSettings> specificIndexConfigCache = new ConcurrentHashMap<>(); private final ConcurrentMap<CoordinateReferenceSystem,SpaceFillingCurveSettings> specificIndexConfigCache = new ConcurrentHashMap<>();


public IndexSpecificSpaceFillingCurveSettingsCache( public IndexSpecificSpaceFillingCurveSettingsCache(
Expand Down Expand Up @@ -70,7 +75,7 @@ public SpaceFillingCurve forCrs( CoordinateReferenceSystem crs, boolean assignTo
} }


// Global config // Global config
SpaceFillingCurveSettings configuredSetting = fromConfig( crs ); SpaceFillingCurveSettings configuredSetting = globalConfigCache.forCRS( crs );
if ( assignToIndexIfNotYetAssigned ) if ( assignToIndexIfNotYetAssigned )
{ {
specificIndexConfigCache.put( crs, configuredSetting ); specificIndexConfigCache.put( crs, configuredSetting );
Expand All @@ -87,18 +92,6 @@ public void visitIndexSpecificSettings( SettingVisitor visitor )
specificIndexConfigCache.forEach( (crs, settings) -> visitor.visit( crs, settings ) ); specificIndexConfigCache.forEach( (crs, settings) -> visitor.visit( crs, settings ) );
} }


private SpaceFillingCurveSettings fromConfig( CoordinateReferenceSystem crs )
{
SpaceFillingCurveSettings global = globalConfigCache.forCRS( crs );
if ( global != null )
{
return global;
}

// Fall back to creating one (TODO cache this?)
return SpaceFillingCurveSettingsFactory.fromConfig( globalConfigCache.getMaxBits(), new EnvelopeSettings( crs ) );
}

public interface SettingVisitor public interface SettingVisitor
{ {
void count( int count ); void count( int count );
Expand Down
Expand Up @@ -23,9 +23,15 @@
import java.util.Map; import java.util.Map;


import org.neo4j.gis.spatial.index.Envelope; import org.neo4j.gis.spatial.index.Envelope;
import org.neo4j.index.internal.gbptree.GBPTree;
import org.neo4j.index.internal.gbptree.Header; import org.neo4j.index.internal.gbptree.Header;
import org.neo4j.values.storable.CoordinateReferenceSystem; import org.neo4j.values.storable.CoordinateReferenceSystem;


/**
* {@link GBPTree} header reader for reading {@link SpaceFillingCurveSettings}.
*
* @see SpaceFillingCurveSettingsWriter
*/
public class SpaceFillingCurveSettingsReader implements Header.Reader public class SpaceFillingCurveSettingsReader implements Header.Reader
{ {
private final Map<CoordinateReferenceSystem,SpaceFillingCurveSettings> settings; private final Map<CoordinateReferenceSystem,SpaceFillingCurveSettings> settings;
Expand All @@ -47,7 +53,7 @@ public void read( ByteBuffer headerBytes )


private void readNext( ByteBuffer headerBytes ) private void readNext( ByteBuffer headerBytes )
{ {
int tableId = headerBytes.getInt(); int tableId = headerBytes.get() & 0xFF;
int code = headerBytes.getInt(); int code = headerBytes.getInt();
CoordinateReferenceSystem crs = CoordinateReferenceSystem.get( tableId, code ); CoordinateReferenceSystem crs = CoordinateReferenceSystem.get( tableId, code );


Expand Down
Expand Up @@ -21,9 +21,15 @@


import java.util.function.Consumer; import java.util.function.Consumer;


import org.neo4j.index.internal.gbptree.GBPTree;
import org.neo4j.io.pagecache.PageCursor; import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.values.storable.CoordinateReferenceSystem; import org.neo4j.values.storable.CoordinateReferenceSystem;


/**
* {@link GBPTree} header writer for {@link SpaceFillingCurveSettings}.
*
* @see SpaceFillingCurveSettingsReader
*/
public class SpaceFillingCurveSettingsWriter implements Consumer<PageCursor> public class SpaceFillingCurveSettingsWriter implements Consumer<PageCursor>
{ {
private final IndexSpecificSpaceFillingCurveSettingsCache settings; private final IndexSpecificSpaceFillingCurveSettingsCache settings;
Expand All @@ -33,6 +39,13 @@ public SpaceFillingCurveSettingsWriter( IndexSpecificSpaceFillingCurveSettingsCa
this.settings = settings; this.settings = settings;
} }


/**
* Given the {@link PageCursor} goes through all {@link SpaceFillingCurveSettings} that are in use in specific index.
* The internal {@link IndexSpecificSpaceFillingCurveSettingsCache} provides those settings, which have been collected
* from geometry values from updates.
*
* @param cursor {@link PageCursor} to write index-specific {@link SpaceFillingCurveSettings} into.
*/
@Override @Override
public void accept( PageCursor cursor ) public void accept( PageCursor cursor )
{ {
Expand All @@ -47,9 +60,14 @@ public void count( int count )
@Override @Override
public void visit( CoordinateReferenceSystem crs, SpaceFillingCurveSettings settings ) public void visit( CoordinateReferenceSystem crs, SpaceFillingCurveSettings settings )
{ {
cursor.putInt( crs.getTable().getTableId() ); // For tableId+code the native layout is even stricter here, but it'd add unnecessary complexity to shave off a couple of more bits
int tableId = crs.getTable().getTableId();
if ( (tableId & ~0xFF) != 0 )
{
throw new IllegalArgumentException( "Invalid table id " + tableId );
}
cursor.putByte( (byte) tableId );
cursor.putInt( crs.getCode() ); cursor.putInt( crs.getCode() );

cursor.putInt( settings.maxLevels ); cursor.putInt( settings.maxLevels );
cursor.putInt( settings.dimensions ); cursor.putInt( settings.dimensions );
double[] min = settings.extents.getMin(); double[] min = settings.extents.getMin();
Expand Down
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.config;

import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

import org.neo4j.kernel.configuration.Config;
import org.neo4j.values.storable.CoordinateReferenceSystem;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.neo4j.values.storable.CoordinateReferenceSystem.Cartesian;
import static org.neo4j.values.storable.CoordinateReferenceSystem.Cartesian_3D;
import static org.neo4j.values.storable.CoordinateReferenceSystem.WGS84;

class IndexSpecificSpaceFillingCurveSettingsCacheTest
{
private final ConfiguredSpaceFillingCurveSettingsCache globalSettings = new ConfiguredSpaceFillingCurveSettingsCache( Config.defaults() );

@Test
void shouldHaveInitialIndexSpecificSetting()
{
// given
Map<CoordinateReferenceSystem,SpaceFillingCurveSettings> initialSettings = new HashMap<>();
initialSettings.put( WGS84, globalSettings.forCRS( WGS84 ) );
initialSettings.put( Cartesian, globalSettings.forCRS( Cartesian ) );
IndexSpecificSpaceFillingCurveSettingsCache indexSettings = new IndexSpecificSpaceFillingCurveSettingsCache( globalSettings, initialSettings );

// when
ToMapSettingVisitor visitor = new ToMapSettingVisitor();
indexSettings.visitIndexSpecificSettings( visitor );

// then
assertEquals( initialSettings, visitor.map );
}

@Test
void shouldHaveInitialIndexSpecificSettingsPlusRequestedOnes()
{
// given
Map<CoordinateReferenceSystem,SpaceFillingCurveSettings> initialSettings = new HashMap<>();
initialSettings.put( WGS84, globalSettings.forCRS( WGS84 ) );
initialSettings.put( Cartesian, globalSettings.forCRS( Cartesian ) );
IndexSpecificSpaceFillingCurveSettingsCache indexSettings = new IndexSpecificSpaceFillingCurveSettingsCache( globalSettings, initialSettings );

// when
indexSettings.forCrs( Cartesian_3D, true );

// then
ToMapSettingVisitor visitor = new ToMapSettingVisitor();
indexSettings.visitIndexSpecificSettings( visitor );
Map<CoordinateReferenceSystem,SpaceFillingCurveSettings> expectedSettings = new HashMap<>( initialSettings );
assertNull( expectedSettings.put( Cartesian_3D, globalSettings.forCRS( Cartesian_3D ) ) );
assertEquals( expectedSettings, visitor.map );
}

@Test
void shouldNotCreateIndexSpecificSettingFOrReadRequest()
{
// given
Map<CoordinateReferenceSystem,SpaceFillingCurveSettings> initialSettings = new HashMap<>();
initialSettings.put( WGS84, globalSettings.forCRS( WGS84 ) );
initialSettings.put( Cartesian, globalSettings.forCRS( Cartesian ) );
IndexSpecificSpaceFillingCurveSettingsCache indexSettings = new IndexSpecificSpaceFillingCurveSettingsCache( globalSettings, initialSettings );

// when
indexSettings.forCrs( Cartesian_3D, false );

// then
ToMapSettingVisitor visitor = new ToMapSettingVisitor();
indexSettings.visitIndexSpecificSettings( visitor );
assertEquals( initialSettings, visitor.map );
}
}
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.config;

import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

import org.neo4j.io.pagecache.ByteArrayPageCursor;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.values.storable.CoordinateReferenceSystem;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.neo4j.io.pagecache.PageCache.PAGE_SIZE;

public class SpaceFillingCurveSettingsHeaderTest
{
private final byte[] data = new byte[PAGE_SIZE];
private final PageCursor pageCursor = ByteArrayPageCursor.wrap( data );

@Test
void shouldWriteAndReadZeroSettings()
{
shouldWriteAndReadSettings();
}

@Test
void shouldWriteAndReadSingleSetting()
{
shouldWriteAndReadSettings( CoordinateReferenceSystem.WGS84 );
}

@Test
void shouldWriteAndReadMultipleSettings()
{
shouldWriteAndReadSettings( CoordinateReferenceSystem.WGS84, CoordinateReferenceSystem.Cartesian, CoordinateReferenceSystem.Cartesian_3D );
}

private void shouldWriteAndReadSettings( CoordinateReferenceSystem... crss )
{
// given
IndexSpecificSpaceFillingCurveSettingsCache indexSettings =
new IndexSpecificSpaceFillingCurveSettingsCache( new ConfiguredSpaceFillingCurveSettingsCache( Config.defaults() ), new HashMap<>() );
for ( CoordinateReferenceSystem crs : crss )
{
indexSettings.forCrs( crs, true );
}
SpaceFillingCurveSettingsWriter writer = new SpaceFillingCurveSettingsWriter( indexSettings );

// when
writer.accept( pageCursor );
pageCursor.rewind();

// then
Map<CoordinateReferenceSystem,SpaceFillingCurveSettings> read = new HashMap<>();
SpaceFillingCurveSettingsReader reader = new SpaceFillingCurveSettingsReader( read );
reader.read( ByteBuffer.wrap( data ) );
assertEquals( asMap( indexSettings ), read );
}

private Map<CoordinateReferenceSystem,SpaceFillingCurveSettings> asMap( IndexSpecificSpaceFillingCurveSettingsCache indexSettings )
{
ToMapSettingVisitor visitor = new ToMapSettingVisitor();
indexSettings.visitIndexSpecificSettings( visitor );
return visitor.map;
}
}
@@ -1,3 +1,22 @@
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.config; package org.neo4j.kernel.impl.index.schema.config;


import org.junit.Test; import org.junit.Test;
Expand Down
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.config;

import java.util.HashMap;
import java.util.Map;

import org.neo4j.values.storable.CoordinateReferenceSystem;

class ToMapSettingVisitor implements IndexSpecificSpaceFillingCurveSettingsCache.SettingVisitor
{
final Map<CoordinateReferenceSystem,SpaceFillingCurveSettings> map = new HashMap<>();

@Override
public void count( int count )
{
}

@Override
public void visit( CoordinateReferenceSystem crs, SpaceFillingCurveSettings settings )
{
map.put( crs, settings );
}
}

0 comments on commit d0f1a89

Please sign in to comment.