Skip to content

Commit

Permalink
Merge pull request #6482 from chrisvest/3.0-metadatastore-scan
Browse files Browse the repository at this point in the history
Add support for scanAllRecords to MetaDataStore
  • Loading branch information
tinwelint committed Feb 25, 2016
2 parents 8cc630a + 37d089f commit d5af4cb
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -514,14 +514,9 @@ long getRecordValue( PageCursor cursor, Position position )

private void refreshFields()
{
scanAllFields( PF_SHARED_READ_LOCK, new Visitor<PageCursor,IOException>()
{
@Override
public boolean visit( PageCursor element ) throws IOException
{
readAllFields( element );
return false;
}
scanAllFields( PF_SHARED_READ_LOCK, element -> {
readAllFields( element );
return false;
} );
}

Expand Down Expand Up @@ -801,23 +796,18 @@ public boolean closedTransactionIdIsOnParWithOpenedTransactionId()

public void logRecords( final Logger msgLog )
{
scanAllFields( PF_SHARED_READ_LOCK, new Visitor<PageCursor,IOException>()
{
@Override
public boolean visit( PageCursor element ) throws IOException
scanAllFields( PF_SHARED_READ_LOCK, element -> {
for ( Position position : Position.values() )
{
for ( Position position : Position.values() )
long value;
do
{
long value;
do
{
value = getRecordValue( element, position );
}
while ( element.shouldRetry() );
msgLog.log( position.name() + " (" + position.description() + "): " + value );
value = getRecordValue( element, position );
}
return false;
while ( element.shouldRetry() );
msgLog.log( position.name() + " (" + position.description() + "): " + value );
}
return false;
} );
}

Expand All @@ -838,7 +828,17 @@ public <FAILURE extends Exception> void accept(
@Override
protected void readRecord( PageCursor cursor, NeoStoreActualRecord record, RecordLoad mode )
{
throw new UnsupportedOperationException();
int id = record.getIntId();
Position[] values = Position.values();
if ( id >= values.length )
{
record.initialize( false, FIELD_NOT_PRESENT );
return;
}

long value = getRecordValue( cursor, values[id] );
boolean inUse = value != FIELD_NOT_INITIALIZED && value != FIELD_NOT_PRESENT;
record.initialize( inUse, value );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
package org.neo4j.kernel.impl.store;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
Expand All @@ -35,12 +37,15 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

import org.neo4j.function.ThrowingAction;
import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.io.pagecache.PagedFile;
import org.neo4j.kernel.impl.store.record.NeoStoreActualRecord;
import org.neo4j.kernel.impl.store.record.RecordLoad;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.test.EphemeralFileSystemRule;
import org.neo4j.test.PageCacheRule;
Expand All @@ -63,12 +68,28 @@ public class MetaDataStoreTest
@Rule
public final PageCacheRule pageCacheRule = new PageCacheRule( false );

private EphemeralFileSystemAbstraction fs;
private PageCache pageCache;

@AfterClass
public static void shutDownExecutor()
{
executor.shutdown();
}

@Before
public void setUp()
{
fs = fsRule.get();
pageCache = pageCacheRule.getPageCache( fs );
}

private MetaDataStore newMetaDataStore() throws IOException
{
StoreFactory storeFactory = new StoreFactory( fs, STORE_DIR, pageCache, NullLogProvider.getInstance() );
return storeFactory.openNeoStores( true, StoreType.META_DATA ).getMetaDataStore();
}

@Test
public void getCreationTimeShouldFailWhenStoreIsClosed() throws IOException
{
Expand Down Expand Up @@ -293,14 +314,6 @@ public void transactionCommittedShouldFailWhenStoreIsClosed() throws IOException
}
}

private MetaDataStore newMetaDataStore() throws IOException
{
EphemeralFileSystemAbstraction fs = fsRule.get();
PageCache pageCache = pageCacheRule.getPageCache( fs );
StoreFactory storeFactory = new StoreFactory( fs, STORE_DIR, pageCache, NullLogProvider.getInstance() );
return storeFactory.openNeoStores( true, StoreType.META_DATA ).getMetaDataStore();
}

@Test
public void testRecordTransactionClosed() throws Exception
{
Expand Down Expand Up @@ -560,4 +573,67 @@ private static void assertLogVersionEqualsByteOffset( long logVersion, long byte
" should be identical" );
}
}

@Test
public void mustSupportScanningAllRecords() throws Exception
{
File file = new File( STORE_DIR, MetaDataStore.DEFAULT_NAME );
fs.mkdir( STORE_DIR );
fs.create( file ).close();
MetaDataStore.Position[] positions = MetaDataStore.Position.values();
for ( MetaDataStore.Position position : positions )
{
MetaDataStore.setRecord( pageCache, file, position, position.ordinal() + 1 );
}

List<Long> actualValues = new ArrayList<>();
try ( MetaDataStore store = newMetaDataStore() )
{
store.scanAllRecords( record -> {
actualValues.add( record.getValue() );
return false;
} );
}

List<Long> expectedValues = Arrays.stream( positions ).map(
p -> p.ordinal() + 1L ).collect( Collectors.toList() );

assertThat( actualValues, is( expectedValues ) );
}

@Test
public void mustSupportScanningAllRecordsWithRecordCursor() throws Exception
{
File file = new File( STORE_DIR, MetaDataStore.DEFAULT_NAME );
fs.mkdir( STORE_DIR );
fs.create( file ).close();
MetaDataStore.Position[] positions = MetaDataStore.Position.values();
for ( MetaDataStore.Position position : positions )
{
MetaDataStore.setRecord( pageCache, file, position, position.ordinal() + 1 );
}

List<Long> actualValues = new ArrayList<>();
try ( MetaDataStore store = newMetaDataStore() )
{
NeoStoreActualRecord record = store.newRecord();
try ( RecordCursor<NeoStoreActualRecord> cursor = store.newRecordCursor( record ) )
{
store.placeRecordCursor( 0, cursor, RecordLoad.NORMAL );
long highId = store.getHighId();
for ( long id = 0; id < highId; id++ )
{
if ( cursor.next( id ) )
{
actualValues.add( record.getValue() );
}
}
}
}

List<Long> expectedValues = Arrays.stream( positions ).map(
p -> p.ordinal() + 1L ).collect( Collectors.toList() );

assertThat( actualValues, is( expectedValues ) );
}
}

0 comments on commit d5af4cb

Please sign in to comment.