Skip to content

Commit

Permalink
Update DumpCountsStore to print human readable values
Browse files Browse the repository at this point in the history
  • Loading branch information
thobe committed Feb 11, 2015
1 parent 9a8d310 commit 9d68332
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 67 deletions.
Expand Up @@ -33,8 +33,10 @@
import org.neo4j.kernel.impl.store.counts.keys.CountsKeyFactory;
import org.neo4j.kernel.impl.store.kvstore.AbstractKeyValueStore;
import org.neo4j.kernel.impl.store.kvstore.CollectedMetadata;
import org.neo4j.kernel.impl.store.kvstore.MetadataVisitor;
import org.neo4j.kernel.impl.store.kvstore.ReadableBuffer;
import org.neo4j.kernel.impl.store.kvstore.Rotation;
import org.neo4j.kernel.impl.store.kvstore.UnknownKey;
import org.neo4j.kernel.impl.store.kvstore.WritableBuffer;
import org.neo4j.kernel.impl.util.StringLogger;
import org.neo4j.register.Register;
Expand Down Expand Up @@ -165,22 +167,19 @@ public void accept( final CountsVisitor visitor )
{
try
{
visitAll( new Visitor()
{
@Override
protected boolean visitKeyValuePair( CountsKey key, ReadableBuffer value )
{
key.accept( visitor, value.getLong( 0 ), value.getLong( 8 ) );
return true;
}
} );
visitAll( new DelegatingVisitor( visitor ) );
}
catch ( IOException e )
{
throw new UnderlyingStorageException( e );
}
}

void visitFile( File path, CountsVisitor visitor ) throws IOException
{
super.visitFile( path, new DelegatingVisitor( visitor ) );
}

@Override
protected Metadata initialMetadata()
{
Expand Down Expand Up @@ -216,7 +215,7 @@ protected void writeKey( CountsKey key, final WritableBuffer buffer )
}

@Override
protected CountsKey readKey( ReadableBuffer key )
protected CountsKey readKey( ReadableBuffer key ) throws UnknownKey
{
switch ( key.getByte( 0 ) )
{
Expand All @@ -233,7 +232,7 @@ protected CountsKey readKey( ReadableBuffer key )
return CountsKeyFactory.indexSampleKey( key.getInt( 4 ), key.getInt( 8 ) );
}
default:
throw new IllegalArgumentException( "Unknown key type: " + key );
throw new UnknownKey( "Unknown key type: " + key );
}
}

Expand Down Expand Up @@ -294,6 +293,46 @@ protected boolean hasMetadataChanges( Metadata metadata, Metadata.Diff diff )
return metadata != null && metadata.txId != diff.txId;
}

private class DelegatingVisitor extends Visitor implements MetadataVisitor<Metadata>
{
private final CountsVisitor visitor;

public DelegatingVisitor( CountsVisitor visitor )
{
this.visitor = visitor;
}

@Override
protected boolean visitKeyValuePair( CountsKey key, ReadableBuffer value )
{
key.accept( visitor, value.getLong( 0 ), value.getLong( 8 ) );
return true;
}

@SuppressWarnings("unchecked")
@Override
public void visitMetadata( File path, Metadata metadata, int entryCount )
{
if ( visitor instanceof MetadataVisitor<?> )
{
((MetadataVisitor<Metadata>) visitor).visitMetadata( path, metadata, entryCount );
}
}

@Override
protected boolean visitUnknownKey( UnknownKey exception, ReadableBuffer key, ReadableBuffer value )
{
if ( visitor instanceof UnknownKey.Visitor )
{
return ((UnknownKey.Visitor) visitor).visitUnknownKey( key, value );
}
else
{
return super.visitUnknownKey( exception, key, value );
}
}
}

private static class KeyFormat implements CountsVisitor
{
private static final byte NODE_COUNT = 1, RELATIONSHIP_COUNT = 2, INDEX = 127, INDEX_STATS = 1, INDEX_SAMPLE = 2;
Expand Down
Expand Up @@ -24,18 +24,23 @@

import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.impl.api.CountsVisitor;
import org.neo4j.kernel.impl.pagecache.StandalonePageCache;
import org.neo4j.kernel.impl.store.StoreFactory;
import org.neo4j.kernel.impl.store.counts.keys.CountsKey;
import org.neo4j.kernel.impl.store.kvstore.AbstractKeyValueVisitor;
import org.neo4j.kernel.impl.store.kvstore.MetadataVisitor;
import org.neo4j.kernel.impl.store.kvstore.ReadableBuffer;
import org.neo4j.kernel.impl.store.kvstore.UnknownKey;
import org.neo4j.kernel.lifecycle.Lifespan;
import org.neo4j.kernel.monitoring.Monitors;

import static org.neo4j.kernel.impl.pagecache.StandalonePageCacheFactory.createPageCache;
import static org.neo4j.kernel.impl.store.counts.keys.CountsKeyFactory.indexSampleKey;
import static org.neo4j.kernel.impl.store.counts.keys.CountsKeyFactory.indexStatisticsKey;
import static org.neo4j.kernel.impl.store.counts.keys.CountsKeyFactory.nodeKey;
import static org.neo4j.kernel.impl.store.counts.keys.CountsKeyFactory.relationshipKey;
import static org.neo4j.kernel.impl.util.StringLogger.DEV_NULL;

public class DumpCountsStore implements AbstractKeyValueVisitor<CountsKey, Metadata>
public class DumpCountsStore implements CountsVisitor, MetadataVisitor<Metadata>, UnknownKey.Visitor
{
public static void main( String[] args ) throws IOException
{
Expand All @@ -53,7 +58,7 @@ public static void main( String[] args ) throws IOException
if ( fs.isDirectory( path ) )
{
life.add( new StoreFactory( fs, path, pages, DEV_NULL, new Monitors() ).newCountsStore() )
.visitFile( new DumpCountsStore() );
.accept( new DumpCountsStore() );
}
else
{
Expand All @@ -64,7 +69,7 @@ public static void main( String[] args ) throws IOException
}
else
{
life.add( tracker ).visitFile( new DumpCountsStore() );
life.add( tracker ).accept( new DumpCountsStore() );
}
}
}
Expand All @@ -73,16 +78,43 @@ public static void main( String[] args ) throws IOException
@Override
public void visitMetadata( File path, Metadata metadata, int entryCount )
{
System.out.println( "Counts Store: " + path );
System.out.println( "\ttxId: " + metadata.txId );
System.out.println( "\tminor version: " + metadata.minorVersion );
System.out.println( "\tentries: " + entryCount );
System.out.println( "Counts Store:\t" + path );
System.out.println( "\ttxId:\t" + metadata.txId );
System.out.println( "\tminor version:\t" + metadata.minorVersion );
System.out.println( "\tentries:\t" + entryCount );
System.out.println( "Entries:" );
}

@Override
public void visitData( CountsKey key, ReadableBuffer value )
public void visitNodeCount( int labelId, long count )
{
System.out.println( "\t" + key + ": " + value );
System.out.println( "\t" + nodeKey( labelId ) + ":\t" + count );
}

@Override
public void visitRelationshipCount( int startLabelId, int typeId, int endLabelId, long count )
{
System.out.println( "\t" + relationshipKey( startLabelId, typeId, endLabelId ) + ":\t" + count );
}

@Override
public void visitIndexStatistics( int labelId, int propertyKeyId, long updates, long size )
{
System.out.println( "\t" + indexStatisticsKey( labelId, propertyKeyId ) +
":\tupdates=" + updates + ", size=" + size );
}

@Override
public void visitIndexSample( int labelId, int propertyKeyId, long unique, long size )
{
System.out.println( "\t" + indexSampleKey( labelId, propertyKeyId ) +
":\tunique=" + unique + ", size=" + size );
}

@Override
public boolean visitUnknownKey( ReadableBuffer key, ReadableBuffer value )
{
System.out.println( "\t" + key + ":\t" + value );
return true;
}
}
Expand Up @@ -73,13 +73,37 @@ protected final <Value> Value lookup( Key key, Reader<Value> lookup ) throws IOE
/** Introspective feature, not thread safe. */
protected final void visitAll( Visitor visitor ) throws IOException
{
KeyValueStoreState<Key, MetaData> state = this.state;
if ( visitor instanceof MetadataVisitor<?> )
{
@SuppressWarnings("unchecked")
MetadataVisitor<MetaData> metadataVisitor = (MetadataVisitor<MetaData>) visitor;
metadataVisitor.visitMetadata( state.file(), metadata(), state.totalRecordsStored() );
}
try ( DataProvider provider = state.dataProvider() )
{
transfer( provider, visitor );
}
}

protected abstract Key readKey( ReadableBuffer key );
protected final void visitFile( File path, Visitor visitor ) throws IOException
{
try ( KeyValueStoreFile<MetaData> file = state.openStoreFile( path ) )
{
if ( visitor instanceof MetadataVisitor<?> )
{
@SuppressWarnings("unchecked")
MetadataVisitor<MetaData> metadataVisitor = (MetadataVisitor<MetaData>) visitor;
metadataVisitor.visitMetadata( path, file.metadata(), file.recordCount() );
}
try ( DataProvider provider = file.dataProvider() )
{
transfer( provider, visitor );
}
}
}

protected abstract Key readKey( ReadableBuffer key ) throws UnknownKey;

protected abstract void writeKey( Key key, WritableBuffer buffer );

Expand Down Expand Up @@ -146,28 +170,6 @@ protected final void apply( Update<Key> update ) throws IOException
}
}

public void visitFile( AbstractKeyValueVisitor<Key, MetaData> visitor ) throws IOException
{
KeyValueStoreState<Key, MetaData> state = this.state;
visitor.visitMetadata( state.file(), metadata(), state.totalRecordsStored() );
try ( DataProvider provider = state.dataProvider() )
{
transfer( provider, new DelegatingKeyValueVisitor( visitor ) );
}
}

public void visitFile( File path, AbstractKeyValueVisitor<Key, MetaData> visitor ) throws IOException
{
try ( KeyValueStoreFile<MetaData> file = state.openStoreFile( path ) )
{
visitor.visitMetadata( path, file.metadata(), file.recordCount() );
try ( DataProvider provider = file.dataProvider() )
{
transfer( provider, new DelegatingKeyValueVisitor( visitor ) );
}
}
}

protected void failedToOpenStoreFile( File path, Exception error )
{
// override to implement logging
Expand Down Expand Up @@ -280,27 +282,22 @@ public abstract class Visitor implements KeyValueVisitor
@Override
public boolean visit( ReadableBuffer key, ReadableBuffer value )
{
return visitKeyValuePair( readKey( key ), value );
try
{
return visitKeyValuePair( readKey( key ), value );
}
catch ( UnknownKey e )
{
return visitUnknownKey( e, key, value );
}
}

protected abstract boolean visitKeyValuePair( Key key, ReadableBuffer value );
}

private class DelegatingKeyValueVisitor extends Visitor
{
private final AbstractKeyValueVisitor<Key, MetaData> visitor;

DelegatingKeyValueVisitor( AbstractKeyValueVisitor<Key, MetaData> visitor )
protected boolean visitUnknownKey( UnknownKey exception, ReadableBuffer key, ReadableBuffer value )
{
this.visitor = visitor;
throw new IllegalArgumentException( exception.getMessage(), exception );
}

@Override
protected boolean visitKeyValuePair( Key key, ReadableBuffer value )
{
visitor.visitData( key, value );
return true;
}
protected abstract boolean visitKeyValuePair( Key key, ReadableBuffer value );
}

private final class Format extends ProgressiveFormat<MetaData> implements KeyFormat<Key>
Expand Down Expand Up @@ -362,9 +359,16 @@ public boolean visit( WritableBuffer key, WritableBuffer value ) throws IOExcept
{
while ( provider.visit( key, value ) )
{
if ( include( readKey( key ), value ) )
try
{
if ( include( readKey( key ), value ) )
{
return true;
}
}
catch ( UnknownKey e )
{
return true;
throw new IllegalArgumentException( e.getMessage(), e );
}
}
return false;
Expand Down
Expand Up @@ -17,14 +17,11 @@
* 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.store.kvstore;

import java.io.File;

public interface AbstractKeyValueVisitor<Key, Meta>
public interface MetadataVisitor<Meta>
{
void visitMetadata( File path, Meta metadata, int entryCount );

void visitData( Key key, ReadableBuffer value );
}
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2002-2015 "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.store.kvstore;

public class UnknownKey extends Exception
{
public interface Visitor
{
boolean visitUnknownKey( ReadableBuffer key, ReadableBuffer value );
}

public UnknownKey( String message )
{
super( message );
}
}

0 comments on commit 9d68332

Please sign in to comment.