diff --git a/src/main/java/org/neo4j/index/bdbje/BerkeleyDbBatchInserterIndex.java b/src/main/java/org/neo4j/index/bdbje/BerkeleyDbBatchInserterIndex.java index ef5d3e5..8a09d84 100644 --- a/src/main/java/org/neo4j/index/bdbje/BerkeleyDbBatchInserterIndex.java +++ b/src/main/java/org/neo4j/index/bdbje/BerkeleyDbBatchInserterIndex.java @@ -46,22 +46,22 @@ public class BerkeleyDbBatchInserterIndex implements BatchInserterIndex { - + // private final boolean createdNow; // private Map>> cache; - + private final Map dbs = new HashMap(); - + private final Pair storeDir; - + private final IndexIdentifier identifier; - - + + BerkeleyDbBatchInserterIndex( BerkeleyDbBatchInserterIndexProvider provider, BatchInserter inserter, IndexIdentifier identifier, Map config ) { System.err.println( this.getClass() + " initing with id=" + identifier + " config=" + config.get( "provider" ) ); - + this.identifier = identifier; String dbStoreDir = ( (BatchInserterImpl)inserter ).getStore(); storeDir = @@ -69,8 +69,8 @@ public class BerkeleyDbBatchInserterIndex implements BatchInserterIndex { + identifier.indexName ); // this.createdNow = storeDir.other(); } - - + + @Override public void add( long entityId, Map properties ) { try { @@ -88,15 +88,15 @@ public void add( long entityId, Map properties ) { dos.writeLong( entityId ); dos.flush(); db.put( null, valueEntry, new DatabaseEntry( baus.toByteArray() ) ); - + dos.close(); } } catch ( Exception e ) { throw new RuntimeException( e ); } } - - + + public Database createDB( String key ) { try { EnvironmentConfig environmentConfig = new EnvironmentConfig(); @@ -116,9 +116,9 @@ public Database createDB( String key ) { throw new RuntimeException( e ); } } - - - + + + @Override public void updateOrAdd( long entityId, Map properties ) { try { @@ -127,9 +127,9 @@ public void updateOrAdd( long entityId, Map properties ) { throw new RuntimeException( e ); } } - - - + + + @Override public IndexHits get( String key, Object value ) { ArrayList resultList = new ArrayList(); @@ -156,8 +156,8 @@ public IndexHits get( String key, Object value ) { } return new IndexHitsImpl( resultList, resultList.size() ); } - - + + public void shutdown() { System.err.println( "shutting down batch inserter index: " + identifier ); for ( Database db : dbs.values() ) { @@ -168,32 +168,32 @@ public void shutdown() { } } } - - + + @Override public void flush() { // writerModified = true; } - - + + @Override public IndexHits query( String key, Object queryOrQueryObject ) { // TODO Auto-generated method stub return null; } - - + + @Override public IndexHits query( Object queryOrQueryObject ) { // TODO Auto-generated method stub return null; } - - + + @Override public void setCacheCapacity( String key, int size ) { // TODO Auto-generated method stub - + } - + } diff --git a/src/main/java/org/neo4j/index/bdbje/BerkeleyDbBatchInserterIndexProvider.java b/src/main/java/org/neo4j/index/bdbje/BerkeleyDbBatchInserterIndexProvider.java index dfb3000..387b7a8 100644 --- a/src/main/java/org/neo4j/index/bdbje/BerkeleyDbBatchInserterIndexProvider.java +++ b/src/main/java/org/neo4j/index/bdbje/BerkeleyDbBatchInserterIndexProvider.java @@ -41,27 +41,27 @@ * are compatible with {@link Index}s from {@link LuceneIndexProvider}. */ public class BerkeleyDbBatchInserterIndexProvider implements BatchInserterIndexProvider { - + private final BatchInserter inserter; private final Map indexes = new HashMap(); final IndexStore indexStore; - - + + public BerkeleyDbBatchInserterIndexProvider( final BatchInserter inserter ) { this.inserter = inserter; indexStore = ( (BatchInserterImpl)inserter ).getIndexStore(); - + } - - + + @Override public BatchInserterIndex nodeIndex( String indexName, Map config ) { config( Node.class, indexName, config ); return index( new IndexIdentifier( Node.class, indexName ), config ); } - - + + private Map config( Class cls, String indexName, Map config ) { // TODO Doesn't look right if ( config != null ) { @@ -74,15 +74,15 @@ private Map config( Class cls, Stri return indexStore.get( cls, indexName ); } } - - + + @Override public BatchInserterIndex relationshipIndex( String indexName, Map config ) { config( Relationship.class, indexName, config ); return index( new IndexIdentifier( Relationship.class, indexName ), config ); } - - + + private BatchInserterIndex index( IndexIdentifier identifier, Map config ) { // We don't care about threads here... c'mon... it's a // single-threaded batch inserter @@ -93,8 +93,8 @@ private BatchInserterIndex index( IndexIdentifier identifier, Map creationConfig = null; if ( commandType == CREATE_COMMAND ) { creationConfig = IoPrimitiveUtils.readMap( channel, buffer ); } - + IndexIdentifier identifier = new IndexIdentifier( itemsClass, indexName ); switch ( commandType ) { diff --git a/src/main/java/org/neo4j/index/bdbje/BerkeleyDbDataSource.java b/src/main/java/org/neo4j/index/bdbje/BerkeleyDbDataSource.java index c679783..4bcbac6 100644 --- a/src/main/java/org/neo4j/index/bdbje/BerkeleyDbDataSource.java +++ b/src/main/java/org/neo4j/index/bdbje/BerkeleyDbDataSource.java @@ -67,7 +67,7 @@ public class BerkeleyDbDataSource extends LogBackedXaDataSource { /** * Constructs this data source. - * + * * @param params * XA parameters. * @throws InstantiationException diff --git a/src/main/java/org/neo4j/index/bdbje/BerkeleyDbTransaction.java b/src/main/java/org/neo4j/index/bdbje/BerkeleyDbTransaction.java index 6c9ab9b..a38b5c2 100644 --- a/src/main/java/org/neo4j/index/bdbje/BerkeleyDbTransaction.java +++ b/src/main/java/org/neo4j/index/bdbje/BerkeleyDbTransaction.java @@ -44,7 +44,7 @@ class BerkeleydbTransaction extends XaTransaction new HashMap(); private final BerkeleyDbDataSource dataSource; - private final Map> commandMap = + private final Map> commandMap = new HashMap>(); BerkeleydbTransaction( int identifier, XaLogicalLog xaLog, @@ -60,13 +60,13 @@ void add( BerkeleyDbIndex index, T entity, TxDataBoth data = getTxData( index, true ); insert( index, entity, key, value, data.added( true ), data.removed( false ) ); } - + private long getEntityId( PropertyContainer entity ) { return entity instanceof Node ? ((Node) entity).getId() : ((Relationship) entity).getId(); } - + TxDataBoth getTxData( BerkeleyDbIndex index, boolean createIfNotExists ) { @@ -86,7 +86,7 @@ void remove( BerkeleyDbIndex index, T entity, TxDataBoth data = getTxData( index, true ); insert( index, entity, key, value, data.removed( true ), data.added( false ) ); } - + private void queueCommand( BerkeleyDbCommand command ) { IndexIdentifier indexId = command.indexId; @@ -98,7 +98,7 @@ private void queueCommand( BerkeleyDbCommand command ) } commands.add( command ); } - + private void insert( BerkeleyDbIndex index, T entity, String key, Object value, TxData insertInto, TxData removeFrom ) { @@ -121,7 +121,7 @@ Set getRemovedIds( BerkeleyDbIndex index, Set ids = removed.getEntityIds( key, value ); return ids != null ? ids : Collections.emptySet(); } - + Set getAddedIds( BerkeleyDbIndex index, String key, Object value ) { @@ -133,7 +133,7 @@ Set getAddedIds( BerkeleyDbIndex index, Set ids = added.getEntityIds( key, value ); return ids != null ? ids : Collections.emptySet(); } - + private TxData addedTxDataOrNull( BerkeleyDbIndex index ) { TxDataBoth data = getTxData( index, false ); @@ -143,7 +143,7 @@ private TxData addedTxDataOrNull( BerkeleyDbIndex< } return data.added( false ); } - + private TxData removedTxDataOrNull( BerkeleyDbIndex index ) { TxDataBoth data = getTxData( index, false ); @@ -153,12 +153,12 @@ private TxData removedTxDataOrNull( BerkeleyDbInde } return data.removed( false ); } - + @Override protected void doAddCommand( XaCommand command ) { // we override inject command and manage our own in memory command list } - + @Override protected void injectCommand( XaCommand command ) { @@ -184,7 +184,7 @@ protected void doCommit() ((CreateCommand) command).config ); continue; } - + long[] entityIds = command.entityIds; String key = command.key; String value = command.value; @@ -253,7 +253,7 @@ protected void doPrepare() } } } - + // TODO Fix duplicate code for-loop for ( TxDataBoth txData : this.txData.values() ) { @@ -296,7 +296,7 @@ public boolean isReadOnly() } return true; } - + // Bad name private class TxDataBoth { @@ -304,13 +304,13 @@ private class TxDataBoth private TxData remove; @SuppressWarnings("unchecked") private final BerkeleyDbIndex index; - + @SuppressWarnings("unchecked") public TxDataBoth( BerkeleyDbIndex index ) { this.index = index; } - + TxData added( boolean createIfNotExists ) { if ( this.add == null && createIfNotExists ) @@ -319,7 +319,7 @@ TxData added( boolean createIfNotExists ) } return this.add; } - + TxData removed( boolean createIfNotExists ) { if ( this.remove == null && createIfNotExists ) @@ -328,7 +328,7 @@ TxData removed( boolean createIfNotExists ) } return this.remove; } - + void close() { safeClose( add ); diff --git a/src/main/java/org/neo4j/index/bdbje/BerkeleyDbXaConnection.java b/src/main/java/org/neo4j/index/bdbje/BerkeleyDbXaConnection.java index 50ee62b..0c71128 100644 --- a/src/main/java/org/neo4j/index/bdbje/BerkeleyDbXaConnection.java +++ b/src/main/java/org/neo4j/index/bdbje/BerkeleyDbXaConnection.java @@ -37,36 +37,36 @@ class BerkeleyDbXaConnection extends IndexXaConnection { private final BerkeleyDbXaResource xaResource; - BerkeleyDbXaConnection( Object identifier, XaResourceManager xaRm, + BerkeleyDbXaConnection( Object identifier, XaResourceManager xaRm, byte[] branchId ) { super( xaRm ); xaResource = new BerkeleyDbXaResource( identifier, xaRm, branchId ); } - + @Override public XAResource getXaResource() { return xaResource; } - + private static class BerkeleyDbXaResource extends XaResourceHelpImpl { private final Object identifier; - - BerkeleyDbXaResource( Object identifier, XaResourceManager xaRm, + + BerkeleyDbXaResource( Object identifier, XaResourceManager xaRm, byte[] branchId ) { super( xaRm, branchId ); this.identifier = identifier; } - + @Override public boolean isSameRM( XAResource xares ) { if ( xares instanceof BerkeleyDbXaResource ) { - return identifier.equals( + return identifier.equals( ((BerkeleyDbXaResource) xares).identifier ); } return false; @@ -74,7 +74,7 @@ public boolean isSameRM( XAResource xares ) } private BerkeleydbTransaction tx; - + BerkeleydbTransaction getTx() { if ( tx == null ) @@ -90,13 +90,13 @@ BerkeleydbTransaction getTx() } return tx; } - + void add( BerkeleyDbIndex index, T entity, String key, Object value ) { getTx().add( index, entity, key, value ); } - + void remove( BerkeleyDbIndex index, T entity, String key, Object value ) { diff --git a/src/main/java/org/neo4j/index/bdbje/IndexHitsImpl.java b/src/main/java/org/neo4j/index/bdbje/IndexHitsImpl.java index 9f25090..9b1de15 100644 --- a/src/main/java/org/neo4j/index/bdbje/IndexHitsImpl.java +++ b/src/main/java/org/neo4j/index/bdbje/IndexHitsImpl.java @@ -37,7 +37,7 @@ public class IndexHitsImpl implements IndexHits with a known size. - * + * * @param hits the hits to iterate through. * @param size the size of the iteration. */ @@ -48,7 +48,7 @@ public IndexHitsImpl( Iterable hits, int size ) /** * Wraps an Iterator with a known size. - * + * * @param hits the hits to iterate through. * @param size the size of the iteration. */ diff --git a/src/main/java/org/neo4j/index/bdbje/IndexIdentifier.java b/src/main/java/org/neo4j/index/bdbje/IndexIdentifier.java index 76432e2..0278247 100644 --- a/src/main/java/org/neo4j/index/bdbje/IndexIdentifier.java +++ b/src/main/java/org/neo4j/index/bdbje/IndexIdentifier.java @@ -25,13 +25,13 @@ class IndexIdentifier { final Class itemClass; final String indexName; - + IndexIdentifier( Class cls, String name ) { this.itemClass = cls; this.indexName = name; } - + @Override public boolean equals( Object o ) { @@ -43,7 +43,7 @@ public boolean equals( Object o ) return itemClass.equals( i.itemClass ) && indexName.equals( i.indexName ); } - + @Override public int hashCode() { diff --git a/src/test/java/org/neo4j/index/bdbje/Neo4jTestCase.java b/src/test/java/org/neo4j/index/bdbje/Neo4jTestCase.java index ded6091..c067922 100644 --- a/src/test/java/org/neo4j/index/bdbje/Neo4jTestCase.java +++ b/src/test/java/org/neo4j/index/bdbje/Neo4jTestCase.java @@ -39,13 +39,13 @@ public abstract class Neo4jTestCase { - + private static File basePath = new File( "target/var" ); private static File dbPath = new File( basePath, "neo4j-db" ); private static GraphDatabaseService graphDb = null; private Transaction tx; - - + + @BeforeClass public static void setUpDb() throws Exception { // System.err.println( "@BeforeClass" ); @@ -53,8 +53,8 @@ public static void setUpDb() throws Exception { deleteFileOrDirectory( dbPath ); graphDb = new EmbeddedGraphDatabase( dbPath.getAbsolutePath() ); } - - + + @AfterClass public static void tearDownDb() throws Exception { // System.err.println( "@AfterClass" ); @@ -62,15 +62,15 @@ public static void tearDownDb() throws Exception { graphDb = null; deleteFileOrDirectory( dbPath ); } - - + + @Before public void setUpTest() { // System.err.println( "@Before " + this.getClass() ); tx = graphDb.beginTx(); } - - + + @After public void tearDownTest() { // System.err.println( "@After " + this.getClass() ); @@ -78,49 +78,49 @@ public void tearDownTest() { finishTx( true ); } } - - + + protected boolean manageMyOwnTxFinish() { return false; } - - + + protected void finishTx( boolean commit ) { if ( tx == null ) { return; } - + if ( commit ) { tx.success(); } tx.finish(); tx = null; } - - + + protected Transaction beginTx() { if ( tx == null ) { tx = graphDb.beginTx(); } return tx; } - - + + protected File getBasePath() { return basePath; } - - + + protected File getDbPath() { return dbPath; } - - + + public static void deleteFileOrDirectory( File file ) { if ( !file.exists() ) { return; } - + if ( file.isDirectory() ) { for ( File child : file.listFiles() ) { deleteFileOrDirectory( child ); @@ -132,13 +132,13 @@ public static void deleteFileOrDirectory( File file ) { assert ( ret ) || ( ( !ret ) && ( file.isDirectory() || file.getName().equals( "messages.log" ) ) ) : "failed to delete file: " + file.getAbsoluteFile(); } - - + + protected void restartTx() { restartTx( true ); } - - + + protected void restartTx( boolean success ) { if ( success ) { tx.success(); @@ -148,13 +148,13 @@ protected void restartTx( boolean success ) { tx.finish(); tx = graphDb.beginTx(); } - - + + protected static GraphDatabaseService graphDb() { return graphDb; } - - + + public static void assertContains( Collection collection, T... expectedItems ) { String collectionString = join( ", ", collection.toArray() ); assertEquals( collectionString, expectedItems.length, collection.size() ); @@ -162,13 +162,13 @@ public static void assertContains( Collection collection, T... expectedIt assertTrue( collection.contains( item ) ); } } - - + + public static void assertContains( Iterable items, T... expectedItems ) { assertContains( asCollection( items ), expectedItems ); } - - + + public static void assertContainsInOrder( Collection collection, T... expectedItems ) { String collectionString = join( ", ", collection.toArray() ); assertEquals( collectionString, expectedItems.length, collection.size() ); @@ -177,13 +177,13 @@ public static void assertContainsInOrder( Collection collection, T... exp assertEquals( expectedItems[i], itr.next() ); } } - - + + public static void assertContainsInOrder( Iterable collection, T... expectedItems ) { assertContainsInOrder( asCollection( collection ), expectedItems ); } - - + + public static Collection asCollection( Iterable iterable ) { List list = new ArrayList(); for ( T item : iterable ) { @@ -191,8 +191,8 @@ public static Collection asCollection( Iterable iterable ) { } return list; } - - + + public static String join( String delimiter, T... items ) { StringBuffer buffer = new StringBuffer(); for ( T item : items ) { @@ -203,8 +203,8 @@ public static String join( String delimiter, T... items ) { } return buffer.toString(); } - - + + protected int countIterable( Iterable iterable ) { int counter = 0; Iterator itr = iterable.iterator(); diff --git a/src/test/java/org/neo4j/index/bdbje/RawBDBSpeed.java b/src/test/java/org/neo4j/index/bdbje/RawBDBSpeed.java index 2256375..0ebcb41 100644 --- a/src/test/java/org/neo4j/index/bdbje/RawBDBSpeed.java +++ b/src/test/java/org/neo4j/index/bdbje/RawBDBSpeed.java @@ -35,16 +35,16 @@ public class RawBDBSpeed { - + private static final String BDB = "target/bdb"; private static final String DB_NAME = "testdb"; private static Database bdb; - - + + public static void main( String[] args ) throws Exception { System.err.println("main in "+RawBDBSpeed.class); File bdbDir = new File( BDB ); - + deleteFileOrDirectory( bdbDir ); EnvironmentConfig environmentConfig = new EnvironmentConfig(); environmentConfig.setAllowCreate( true ); @@ -56,7 +56,7 @@ public static void main( String[] args ) throws Exception { databaseConfig.setAllowCreate( true ); // perform other database configurations bdb = environment.openDatabase( null, DB_NAME, databaseConfig ); - + long t = System.currentTimeMillis(); int max = 50000000; for ( int i = 0; i < max; i++ ) { @@ -73,7 +73,7 @@ public static void main( String[] args ) throws Exception { } } System.out.println( "insert time:" + ( System.currentTimeMillis() - t ) ); - + t = System.currentTimeMillis(); for ( int i = 0; i < 1000; i++ ) { DatabaseEntry theData = new DatabaseEntry(); @@ -88,11 +88,11 @@ public static void main( String[] args ) throws Exception { // fastToLong( array ); } System.out.println( "1000 lookups:" + ( System.currentTimeMillis() - t ) ); - + bdb.close(); } - - + + private static byte[] fastToBytes( long value ) throws IOException { byte[] array = new byte[8]; for ( int i = 0; i < 8; i++ ) { @@ -100,8 +100,8 @@ private static byte[] fastToBytes( long value ) throws IOException { } return array; } - - + + private static long fastToLong( byte[] array ) throws IOException { long value = 0; for ( int i = 0; i < array.length; i++ ) { @@ -110,21 +110,21 @@ private static long fastToLong( byte[] array ) throws IOException { } return value; } - - + + private static byte[] lookupKey( String key, Object value ) { return String.valueOf( key + "|" + value ).getBytes(); } - - + + private static byte[] key( long id, String key, Object value ) { return String.valueOf( key + "|" + value ).getBytes(); } - - + + public static void deleteFileOrDirectory( File file ) { if ( file.exists() ) { - + if ( file.isDirectory() ) { for ( File child : file.listFiles() ) { deleteFileOrDirectory( child ); diff --git a/src/test/java/org/neo4j/index/bdbje/TestBerkeleyBatchInsert.java b/src/test/java/org/neo4j/index/bdbje/TestBerkeleyBatchInsert.java index 9e8bbd0..9570472 100644 --- a/src/test/java/org/neo4j/index/bdbje/TestBerkeleyBatchInsert.java +++ b/src/test/java/org/neo4j/index/bdbje/TestBerkeleyBatchInsert.java @@ -49,28 +49,28 @@ public class TestBerkeleyBatchInsert extends Neo4jTestCase { - + private static final String PATH = "target/var/batch"; private static final int MAX = 1000000; private BatchInserter inserter; - - + + @Before public void cleanDirectory() { Neo4jTestCase.deleteFileOrDirectory( new File( PATH ) ); inserter = new BatchInserterImpl( PATH ); } - - + + @After public void tearDown() { inserter.shutdown(); } - - + + @Test public void testSome() throws Exception { - + BatchInserterIndexProvider provider = new BerkeleyDbBatchInserterIndexProvider( inserter ); try { BatchInserterIndex index = provider.nodeIndex( "users", BerkeleyDbIndexImplementation.DEFAULT_CONFIG ); @@ -83,7 +83,7 @@ public void testSome() throws Exception { index.add( i, MapUtil.map( "name", "Joe" + i, "other", "Schmoe" ) ); // ids.put( i, id ); } - + for ( int i = 0; i < MAX; i++ ) { if ( i % 100000 == 0 ) { // restartTx(); @@ -96,7 +96,7 @@ public void testSome() throws Exception { provider.shutdown(); inserter.shutdown(); } - + GraphDatabaseService db = new EmbeddedGraphDatabase( PATH ); try { assertTrue( db.index().existsForNodes( "users" ) ); @@ -108,8 +108,8 @@ public void testSome() throws Exception { db.shutdown(); } } - - + + // @Ignore @Test public void testInsertionSpeed() { @@ -126,7 +126,7 @@ public void testInsertionSpeed() { } System.out.println( "insert:" + ( System.currentTimeMillis() - t ) ); index.flush(); - + t = System.currentTimeMillis(); for ( int i = 0; i < 1000000; i++ ) { IteratorUtil.count( (Iterator)index.get( "key", "value" + i ) ); @@ -136,8 +136,8 @@ public void testInsertionSpeed() { provider.shutdown(); } } - - + + @Test public void testFindCreatedIndex() { String indexName = "persons"; @@ -167,8 +167,8 @@ public void testFindCreatedIndex() { graphDb.shutdown(); } } - - + + @Ignore @Test public void testCanIndexRelationships() { @@ -176,22 +176,22 @@ public void testCanIndexRelationships() { BatchInserterIndexProvider indexProvider = new BerkeleyDbBatchInserterIndexProvider( inserter ); BatchInserterIndex edgesIndex = indexProvider.relationshipIndex( "edgeIndex", MapUtil.stringMap( "provider", "lucene", "type", "exact" ) ); - + long nodeId1 = inserter.createNode( MapUtil.map( "ID", "1" ) ); long nodeId2 = inserter.createNode( MapUtil.map( "ID", "2" ) ); long relationshipId = inserter.createRelationship( nodeId1, nodeId2, EdgeType.KNOWS, null ); - + edgesIndex.add( relationshipId, MapUtil.map( "EDGE_TYPE", EdgeType.KNOWS.name() ) ); edgesIndex.flush(); - + assertEquals( String.format( "Should return relationship id" ), new Long( relationshipId ), edgesIndex.query( "EDGE_TYPE", EdgeType.KNOWS.name() ).getSingle() ); // FIXME: fails on above line - + indexProvider.shutdown(); inserter.shutdown(); } - + private enum EdgeType implements RelationshipType { KNOWS }