Skip to content

Commit

Permalink
Fix dump store tool.
Browse files Browse the repository at this point in the history
Use correct store directory and correct mapping from file name to a
database file. Add a test to check that tool can dump store file.
  • Loading branch information
MishaDemianenko committed Sep 26, 2018
1 parent 71ca380 commit 886220c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
24 changes: 24 additions & 0 deletions community/io/src/main/java/org/neo4j/io/layout/DatabaseFile.java
Expand Up @@ -21,9 +21,12 @@

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.neo4j.util.Preconditions;

import static java.util.Objects.requireNonNull;

/**
* Enumeration of storage implementation specific files for particular database.
* Any internal details of this enumeration is hidden and should not be visible to anyone except of implementation of specific database layout.
Expand Down Expand Up @@ -101,4 +104,25 @@ boolean hasIdFile()
{
return hasIdFile;
}

/**
* Determine database file for provided file name.
*
* @param name - database file name to map
* @return an {@link Optional} that wraps the matching database file that matches to the specified name,
* or {@link Optional#empty()} if the given file name does not match to any of database files.
*/
public static Optional<DatabaseFile> fileOf( String name )
{
requireNonNull( name );
DatabaseFile[] databaseFiles = DatabaseFile.values();
for ( DatabaseFile databaseFile : databaseFiles )
{
if ( databaseFile.names.contains( name ) )
{
return Optional.of( databaseFile );
}
}
return Optional.empty();
}
}
13 changes: 9 additions & 4 deletions tools/src/main/java/org/neo4j/tools/dump/DumpStore.java
Expand Up @@ -26,6 +26,7 @@
import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.util.function.Function;
import java.util.function.Supplier;

import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.layout.DatabaseFile;
Expand Down Expand Up @@ -102,7 +103,7 @@ public static void main( String... args ) throws Exception
PageCache pageCache = createPageCache( fs, createInitialisedScheduler() ) )
{
final DefaultIdGeneratorFactory idGeneratorFactory = new DefaultIdGeneratorFactory( fs );
Function<File,StoreFactory> createStoreFactory = file -> new StoreFactory( DatabaseLayout.of( file ),
Function<File,StoreFactory> createStoreFactory = file -> new StoreFactory( DatabaseLayout.of( file.getParentFile() ),
Config.defaults(), idGeneratorFactory, pageCache, fs, logProvider(), EmptyVersionContextSupplier.EMPTY );

for ( String arg : args )
Expand Down Expand Up @@ -142,9 +143,8 @@ else if ( !file.isDirectory() && file.getName().indexOf( ':' ) != -1 )
throw new IllegalArgumentException( "No such file: " + fileName );
}
}
DatabaseFile databaseFile = DatabaseFile.valueOf( file.getName() );
StoreType storeType = StoreType.typeOf( databaseFile ).orElseThrow(
() -> new IllegalArgumentException( "Not a store file: " + fileName ) );
DatabaseFile databaseFile = DatabaseFile.fileOf( file.getName() ).orElseThrow( illegalArgumentExceptionSupplier( fileName ) );
StoreType storeType = StoreType.typeOf( databaseFile ).orElseThrow( illegalArgumentExceptionSupplier( fileName ) );
try ( NeoStores neoStores = createStoreFactory.apply( file ).openNeoStores( storeType ) )
{
switch ( storeType )
Expand Down Expand Up @@ -182,6 +182,11 @@ else if ( !file.isDirectory() && file.getName().indexOf( ':' ) != -1 )
}
}

private static Supplier<IllegalArgumentException> illegalArgumentExceptionSupplier( String fileName )
{
return () -> new IllegalArgumentException( "Not a store file: " + fileName );
}

private static void dumpMetaDataStore( NeoStores neoStores )
{
neoStores.getMetaDataStore().logRecords( new PrintStreamLogger( System.out ) );
Expand Down
27 changes: 20 additions & 7 deletions tools/src/test/java/org/neo4j/tools/dump/DumpStoreTest.java
Expand Up @@ -22,21 +22,26 @@
*/
package org.neo4j.tools.dump;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.nio.ByteBuffer;

import org.neo4j.kernel.impl.store.record.AbstractBaseRecord;
import org.neo4j.test.extension.SuppressOutputExtension;

import static org.junit.Assert.assertEquals;
import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DumpStoreTest
@ExtendWith( SuppressOutputExtension.class )
class DumpStoreTest
{
@Test
public void dumpStoreShouldPrintBufferWithContent()
void dumpStoreShouldPrintBufferWithContent()
{
// Given
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
Expand All @@ -56,11 +61,11 @@ public void dumpStoreShouldPrintBufferWithContent()
dumpStore.dumpHex( record, buffer, 2, 4 );

// Then
assertEquals( String.format( "@ 0x00000008: 00 01 02 03 04 05 06 07 08 09%n" ), outStream.toString() );
assertEquals( format( "@ 0x00000008: 00 01 02 03 04 05 06 07 08 09%n" ), outStream.toString() );
}

@Test
public void dumpStoreShouldPrintShorterMessageForAllZeroBuffer()
void dumpStoreShouldPrintShorterMessageForAllZeroBuffer()
{
// Given
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
Expand All @@ -74,6 +79,14 @@ public void dumpStoreShouldPrintShorterMessageForAllZeroBuffer()
dumpStore.dumpHex( record, buffer, 2, 4 );

// Then
assertEquals( String.format( ": all zeros @ 0x8 - 0xc%n" ), outStream.toString() );
assertEquals( format( ": all zeros @ 0x8 - 0xc%n" ), outStream.toString() );
}

@Test
void canDumpNeoStoreFileContent() throws Exception
{
URL neostore = getClass().getClassLoader().getResource( "neostore" );
String neostoreFile = neostore.getFile();
DumpStore.main( neostoreFile );
}
}
Binary file added tools/src/test/resources/neostore
Binary file not shown.

0 comments on commit 886220c

Please sign in to comment.