Skip to content

Commit

Permalink
Extended HexPrinter to handle multiple header style, grouping style.
Browse files Browse the repository at this point in the history
Now we standardize the format to print multiple hex strings:
 - The line numbers are always coded in hex.
 - Each byte is presented in two hex digits and bytes are separated with one space.

However one could freely choose how many digits to use for line numbers, what suffix or prefix around line number digits, how many bytes in each line and each group, and what group separator to use.
  • Loading branch information
Zhen committed Apr 16, 2015
1 parent 07156aa commit 1a7e878
Show file tree
Hide file tree
Showing 10 changed files with 498 additions and 303 deletions.
26 changes: 26 additions & 0 deletions community/kernel/src/main/java/org/neo4j/helpers/Strings.java
Expand Up @@ -190,4 +190,30 @@ public static void escape( Appendable output, String arg ) throws IOException
} }
} }
} }

/**
* Use this to standardize the width of some text output to all be left-justified and space-padded
* on the right side to fill up the given column width.
*
* @param str
* @param columnWidth
* @return
*/
public static String ljust( String str, int columnWidth )
{
return String.format( "%-" + columnWidth + "s", str );
}

/**
* Use this to standardize the width of some text output to all be right-justified and space-padded
* on the left side to fill up the given column width.
*
* @param str
* @param columnWidth
* @return
*/
public static String rjust( String str, int columnWidth )
{
return String.format( "%" + columnWidth + "s", str );
}
} }
Expand Up @@ -35,6 +35,7 @@
import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord; import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord;
import org.neo4j.kernel.impl.store.record.RelationshipRecord; import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.kernel.impl.store.record.TokenRecord; import org.neo4j.kernel.impl.store.record.TokenRecord;
import org.neo4j.kernel.impl.util.HexPrinter;
import org.neo4j.logging.FormattedLogProvider; import org.neo4j.logging.FormattedLogProvider;
import org.neo4j.logging.LogProvider; import org.neo4j.logging.LogProvider;
import org.neo4j.logging.NullLogProvider; import org.neo4j.logging.NullLogProvider;
Expand Down Expand Up @@ -226,10 +227,12 @@ protected Object transform( NodeRecord record ) throws Exception
} }


private final PrintStream out; private final PrintStream out;
private final HexPrinter printer;


protected DumpStore( PrintStream out ) protected DumpStore( PrintStream out )
{ {
this.out = out; this.out = out;
this.printer = new HexPrinter( out ).withBytesGroupingFormat( 16, 4, " " ).withLineNumberDigits( 8 );
} }


public final void dump( STORE store, long[] ids ) throws Exception public final void dump( STORE store, long[] ids ) throws Exception
Expand Down Expand Up @@ -291,64 +294,44 @@ private boolean dumpRecord( STORE store, int size, StoreChannel fileChannel, Byt
out.print( record ); out.print( record );
buffer.clear(); buffer.clear();
fileChannel.read( buffer, id * size ); fileChannel.read( buffer, id * size );
buffer.flip(); dumpHex( record, buffer, id, size );
if ( record.inUse() )
{
dumpHex( buffer, id * size );
}
else if ( allZero( buffer ) )
{
out.printf( ": all zeros @ 0x%x - 0x%x%n", id * size, (id + 1) * size );
}
else
{
dumpHex( buffer, id * size );
}
} }


return record.inUse(); return record.inUse();
} }


private boolean allZero( ByteBuffer buffer ) void dumpHex( RECORD record, ByteBuffer buffer, long id, int size )
{ {
int pos = buffer.position(); printer.withLineNumberOffset( id * size );
try if ( record.inUse() )
{ {
while ( buffer.remaining() > 0 ) printer.append( buffer );
{
if ( buffer.get() != 0 )
{
return false;
}
}
} }
finally else if ( allZero( buffer ) )
{ {
buffer.position( pos ); out.printf( ": all zeros @ 0x%x - 0x%x", id * size, (id + 1) * size );
} }
return true; else
} {

printer.append( buffer );
protected Object transform( RECORD record ) throws Exception }
{ out.printf( "%n" );
return record.inUse() ? record : null;
} }


private void dumpHex( ByteBuffer buffer, long offset ) private boolean allZero( ByteBuffer buffer )
{ {
for ( int count = 0; buffer.remaining() > 0; count++, offset++ ) for ( int i = 0; i < buffer.position(); i++ )
{ {
int b = buffer.get(); if ( buffer.get( i ) != 0 )
if ( count % 16 == 0 )
{ {
out.printf( "%n @ 0x%08x: ", offset ); return false;
} }
else if ( count % 4 == 0 )
{
out.print( " " );
}
out.printf( " %x%x", 0xF & (b >> 4), 0xF & b );
} }
out.println(); return true;
}

protected Object transform( RECORD record ) throws Exception
{
return record.inUse() ? record : null;
} }
} }

0 comments on commit 1a7e878

Please sign in to comment.