Skip to content

Commit

Permalink
Finish old tests cleanup and restoration. All old tests restored.
Browse files Browse the repository at this point in the history
Rewrite and cleanup couple of old tests that where still commented out, minor code cleanups.
  • Loading branch information
MishaDemianenko committed Jan 21, 2016
1 parent 5c4cde0 commit ce7c4a5
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 300 deletions.
Expand Up @@ -45,6 +45,7 @@
public class PartitionedLuceneLabelScanWriter implements LabelScanWriter public class PartitionedLuceneLabelScanWriter implements LabelScanWriter
{ {


// TODO: Integer.MAX_VALUE usually used as sentinel marker in lucene, test can we use it as max partition size ?
private final Integer MAXIMUM_PARTITION_SIZE = private final Integer MAXIMUM_PARTITION_SIZE =
Integer.getInteger( "labelScanStore.maxPartitionSize", Integer.MAX_VALUE ); Integer.getInteger( "labelScanStore.maxPartitionSize", Integer.MAX_VALUE );


Expand Down
Expand Up @@ -27,7 +27,7 @@
import org.neo4j.kernel.api.impl.index.storage.DirectoryFactory; import org.neo4j.kernel.api.impl.index.storage.DirectoryFactory;
import org.neo4j.kernel.api.impl.index.storage.PartitionedIndexStorage; import org.neo4j.kernel.api.impl.index.storage.PartitionedIndexStorage;


class LuceneIndexStorageBuilder public class LuceneIndexStorageBuilder
{ {
private DirectoryFactory directoryFactory = DirectoryFactory.PERSISTENT; private DirectoryFactory directoryFactory = DirectoryFactory.PERSISTENT;
private FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction(); private FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
Expand All @@ -39,12 +39,12 @@ private LuceneIndexStorageBuilder()
{ {
} }


static LuceneIndexStorageBuilder create() public static LuceneIndexStorageBuilder create()
{ {
return new LuceneIndexStorageBuilder(); return new LuceneIndexStorageBuilder();
} }


PartitionedIndexStorage buildIndexStorage() public PartitionedIndexStorage build()
{ {
if ( indexStorage == null ) if ( indexStorage == null )
{ {
Expand All @@ -58,31 +58,31 @@ PartitionedIndexStorage buildIndexStorage()
return indexStorage; return indexStorage;
} }


LuceneIndexStorageBuilder withIndexIdentifier( String indexIdentifier ) public LuceneIndexStorageBuilder withIndexIdentifier( String indexIdentifier )
{ {
this.indexIdentifier = indexIdentifier; this.indexIdentifier = indexIdentifier;
return this; return this;
} }


LuceneIndexStorageBuilder withDirectoryFactory( DirectoryFactory directoryFactory ) public LuceneIndexStorageBuilder withDirectoryFactory( DirectoryFactory directoryFactory )
{ {
this.directoryFactory = directoryFactory; this.directoryFactory = directoryFactory;
return this; return this;
} }


LuceneIndexStorageBuilder withFileSystem( FileSystemAbstraction fileSystem ) public LuceneIndexStorageBuilder withFileSystem( FileSystemAbstraction fileSystem )
{ {
this.fileSystem = fileSystem; this.fileSystem = fileSystem;
return this; return this;
} }


LuceneIndexStorageBuilder withIndexRootFolder( File indexRootFolder ) public LuceneIndexStorageBuilder withIndexRootFolder( File indexRootFolder )
{ {
this.indexRootFolder = indexRootFolder; this.indexRootFolder = indexRootFolder;
return this; return this;
} }


LuceneIndexStorageBuilder withIndexStorage( PartitionedIndexStorage indexStorage ) public LuceneIndexStorageBuilder withIndexStorage( PartitionedIndexStorage indexStorage )
{ {
this.indexStorage = indexStorage; this.indexStorage = indexStorage;
return this; return this;
Expand Down
Expand Up @@ -47,6 +47,6 @@ public LuceneLabelScanIndexBuilder withDocumentFormat( BitmapDocumentFormat form


public LuceneLabelScanIndex build() public LuceneLabelScanIndex build()
{ {
return new LuceneLabelScanIndex( format, storageBuilder.buildIndexStorage() ); return new LuceneLabelScanIndex( format, storageBuilder.build() );
} }
} }
Expand Up @@ -77,7 +77,7 @@ public LuceneSchemaIndexBuilder uniqueIndex()


public LuceneSchemaIndex build() public LuceneSchemaIndex build()
{ {
return new LuceneSchemaIndex( storageBuilder.buildIndexStorage(), indexConfig, samplingConfig ); return new LuceneSchemaIndex( storageBuilder.build(), indexConfig, samplingConfig );
} }


} }
Expand Up @@ -64,8 +64,6 @@ public void shouldRebuildDeletedLabelScanStoreOnStartup() throws Exception
deleteNode( node2 ); // just to create a hole in the store deleteNode( node2 ); // just to create a hole in the store


// WHEN // WHEN
// TODO how do we make sure it was deleted and then fully rebuilt? I mean if we somehow deleted
// the wrong directory here then it would also work, right?
dbRule.restartDatabase( deleteTheLabelScanStoreIndex() ); dbRule.restartDatabase( deleteTheLabelScanStoreIndex() );


// THEN // THEN
Expand Down
Expand Up @@ -55,13 +55,13 @@ public class IndexConstraintsTest
@Before @Before
public void setup() throws IOException public void setup() throws IOException
{ {
this.graphDb = new TestGraphDatabaseFactory().newImpermanentDatabase(); graphDb = new TestGraphDatabaseFactory().newImpermanentDatabase();
} }


@After @After
public void shutdown() throws IOException public void shutdown() throws IOException
{ {
this.graphDb.shutdown(); graphDb.shutdown();
} }


@Test @Test
Expand All @@ -81,26 +81,22 @@ public void testMultipleCreate() throws InterruptedException
Executors.newFixedThreadPool( numThreads ) ); Executors.newFixedThreadPool( numThreads ) );
for ( int i = 0; i < numThreads; i++ ) for ( int i = 0; i < numThreads; i++ )
{ {
ecs.submit( new Callable<Node>() ecs.submit( () -> {
{ try ( Transaction tx = graphDb.beginTx() )
public Node call() throws Exception
{ {
try ( Transaction tx = graphDb.beginTx() ) final Node node = graphDb.createNode();
// Acquire lock
tx.acquireWriteLock( commonNode );
Index<Node> index = graphDb.index().forNodes( "uuids" );
final Node existing = index.get( "uuid", uuid ).getSingle();
if ( existing != null )
{ {
final Node node = graphDb.createNode(); throw new RuntimeException( "Node already exists" );
// Acquire lock
tx.acquireWriteLock( commonNode );
Index<Node> index = graphDb.index().forNodes( "uuids" );
final Node existing = index.get( "uuid", uuid ).getSingle();
if ( existing != null )
{
throw new RuntimeException( "Node already exists" );
}
node.setProperty( "uuid", uuid );
index.add( node, "uuid", uuid );
tx.success();
return node;
} }
node.setProperty( "uuid", uuid );
index.add( node, "uuid", uuid );
tx.success();
return node;
} }
} ); } );
} }
Expand Down
Expand Up @@ -97,15 +97,15 @@ void restartTx()
beginTx(); beginTx();
} }


protected static abstract interface EntityCreator<T extends PropertyContainer> protected interface EntityCreator<T extends PropertyContainer>
{ {
T create( Object... properties ); T create( Object... properties );


void delete( T entity ); void delete( T entity );
} }


private static final RelationshipType TEST_TYPE = private static final RelationshipType TEST_TYPE = RelationshipType.withName( "TEST_TYPE" );
RelationshipType.withName( "TEST_TYPE" );
protected static final EntityCreator<Node> NODE_CREATOR = new EntityCreator<Node>() protected static final EntityCreator<Node> NODE_CREATOR = new EntityCreator<Node>()
{ {
public Node create( Object... properties ) public Node create( Object... properties )
Expand Down
Expand Up @@ -28,9 +28,8 @@
import org.neo4j.graphdb.ConstraintViolationException; import org.neo4j.graphdb.ConstraintViolationException;
import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction; import org.neo4j.graphdb.Transaction;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.kernel.api.exceptions.schema.UnableToValidateConstraintKernelException; import org.neo4j.kernel.api.exceptions.schema.UnableToValidateConstraintKernelException;
import org.neo4j.kernel.api.impl.index.storage.DirectoryFactory; import org.neo4j.kernel.api.impl.index.builder.LuceneIndexStorageBuilder;
import org.neo4j.kernel.api.impl.index.storage.PartitionedIndexStorage; import org.neo4j.kernel.api.impl.index.storage.PartitionedIndexStorage;
import org.neo4j.test.TargetDirectory; import org.neo4j.test.TargetDirectory;
import org.neo4j.test.TestGraphDatabaseFactory; import org.neo4j.test.TestGraphDatabaseFactory;
Expand Down Expand Up @@ -103,8 +102,9 @@ private void dbWithConstraint()
private void storeIndexFailure( String failure ) throws IOException private void storeIndexFailure( String failure ) throws IOException
{ {
File luceneRootDirectory = new File( storeDir.directory(), "schema/index/lucene" ); File luceneRootDirectory = new File( storeDir.directory(), "schema/index/lucene" );
PartitionedIndexStorage indexStorage = new PartitionedIndexStorage( DirectoryFactory.PERSISTENT, new PartitionedIndexStorage indexStorage = LuceneIndexStorageBuilder.create()
DefaultFileSystemAbstraction(), luceneRootDirectory, "1" ); .withIndexRootFolder( luceneRootDirectory )
.withIndexIdentifier( "1" ).build();
indexStorage.storeIndexFailure( failure ); indexStorage.storeIndexFailure( failure );
} }
} }
Expand Up @@ -81,11 +81,6 @@ public IndexReaderStub( final Map<String,NumericDocValues> ndvs )
}; };
} }


public void throwOnNextFieldsAccess( IOException throwOnFields )
{
this.throwOnFields = throwOnFields;
}

public void setElements( String[] elements ) public void setElements( String[] elements )
{ {
this.elements = elements; this.elements = elements;
Expand Down Expand Up @@ -148,9 +143,7 @@ public SortedSetDocValues getSortedSetDocValues( String field ) throws IOExcepti
@Override @Override
public Bits getDocsWithField( String field ) throws IOException public Bits getDocsWithField( String field ) throws IOException
{ {
throw new RuntimeException( throw new RuntimeException( "Not yet implemented." );
"org.neo4j.kernel.api.impl.index.LuceneAllDocumentsReaderTest.ReaderStub.getDocsWithField: Not " +
"yet implemented." );
} }


@Override @Override
Expand All @@ -162,9 +155,7 @@ public NumericDocValues getNormValues( String field ) throws IOException
@Override @Override
public FieldInfos getFieldInfos() public FieldInfos getFieldInfos()
{ {
throw new RuntimeException( throw new RuntimeException( "Not yet implemented." );
"org.neo4j.kernel.api.impl.index.LuceneAllDocumentsReaderTest.ReaderStub.getFieldInfos: Not yet " +
"implemented." );
} }


@Override @Override
Expand Down Expand Up @@ -193,15 +184,12 @@ public int length()
@Override @Override
public void checkIntegrity() throws IOException public void checkIntegrity() throws IOException
{ {

} }


@Override @Override
public Fields getTermVectors( int docID ) throws IOException public Fields getTermVectors( int docID ) throws IOException
{ {
throw new RuntimeException( throw new RuntimeException( "Not yet implemented." );
"org.neo4j.kernel.api.impl.index.LuceneAllDocumentsReaderTest.ReaderStub.getTermVectors: Not yet " +
"implemented." );
} }


@Override @Override
Expand All @@ -225,6 +213,5 @@ public void document( int docID, StoredFieldVisitor visitor ) throws IOException
@Override @Override
protected void doClose() throws IOException protected void doClose() throws IOException
{ {

} }
} }
Expand Up @@ -26,73 +26,94 @@
import org.junit.Test; import org.junit.Test;


import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.Arrays;
import java.util.Iterator;
import java.util.List; import java.util.List;


import org.neo4j.helpers.collection.IteratorUtil; import org.neo4j.helpers.collection.Iterables;
import org.neo4j.kernel.api.impl.index.partition.PartitionSearcher;


import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


public class LuceneAllDocumentsReaderTest public class LuceneAllDocumentsReaderTest
{ {

private final PartitionSearcher partitionSearcher1 = createPartitionSearcher( 1, 0, 2 );
private final PartitionSearcher partitionSearcher2 = createPartitionSearcher( 2, 1, 2 );

public LuceneAllDocumentsReaderTest() throws IOException
{
}

@Test @Test
public void shouldIterateOverAllLuceneDocumentsWhereNoDocumentsHaveBeenDeleted() throws Exception public void allDocumentsMaxCount()
{ {
// given LuceneAllDocumentsReader allDocumentsReader = createAllDocumentsReader();
String[] elements = {"A", "B", "C"}; assertEquals( 3, allDocumentsReader.maxCount());

// LuceneAllDocumentsReader reader =
// new LuceneAllDocumentsReader(
// new SearcherManagerStub( new SearcherStub( new IndexReaderStub( false, elements ), elements )
// ) );
//
// // when
// Iterator<Document> iterator = reader.iterator();
// List<Document> actualDocuments = new ArrayList<>( IteratorUtil.asCollection( iterator ) );
//
// // then
// for ( int i = 0; i < elements.length; i++ )
// {
// assertEquals( elements[i], actualDocuments.get( i ).get( "element" ) );
// }
} }


@Test @Test
public void shouldFindNoDocumentsIfTheyHaveAllBeenDeleted() throws Exception public void closeCorrespondingSearcherOnClose() throws IOException
{ {
// given LuceneAllDocumentsReader allDocumentsReader = createAllDocumentsReader();
// final String[] elements = {"A", "B", "C"}; allDocumentsReader.close();
//
// LuceneAllDocumentsReader reader = verify( partitionSearcher1 ).close();
// new LuceneAllDocumentsReader( verify( partitionSearcher2 ).close();
// new SearcherManagerStub( new SearcherStub( new IndexReaderStub( true, elements ), elements )
// ) );
//
// // when
// Iterator<Document> iterator = reader.iterator();
//
// // then
// assertFalse( iterator.hasNext() );
} }


private static class SearcherStub extends IndexSearcher @Test
public void readAllDocuments()
{ {
private final String[] elements; LuceneAllDocumentsReader allDocumentsReader = createAllDocumentsReader();

List<Document> documents = Iterables.toList( allDocumentsReader.iterator() );
public SearcherStub( IndexReader r, String[] elements )
{ assertEquals( "Should have 1 document from first partition and 2 from second one.", 3, documents.size() );
super( r ); assertEquals( "1", documents.get( 0 ).getField( "value" ).stringValue() );
this.elements = elements; assertEquals( "3", documents.get( 1 ).getField( "value" ).stringValue() );
} assertEquals( "4", documents.get( 2 ).getField( "value" ).stringValue() );

}
@Override
public Document doc( int docID ) throws IOException private LuceneAllDocumentsReader createAllDocumentsReader()
{ {
Document document = new Document(); return new LuceneAllDocumentsReader(createPartitionReaders());
document.add( new StoredField( "element", elements[docID] ) ); }
return document;
} private List<LucenePartitionAllDocumentsReader> createPartitionReaders()
{
LucenePartitionAllDocumentsReader reader1 = new LucenePartitionAllDocumentsReader( partitionSearcher1 );
LucenePartitionAllDocumentsReader reader2 = new LucenePartitionAllDocumentsReader( partitionSearcher2 );
return Arrays.asList(reader1, reader2);
}

private static PartitionSearcher createPartitionSearcher( int maxDoc, int partition, int maxSize )
throws IOException
{
PartitionSearcher partitionSearcher = mock( PartitionSearcher.class );
IndexSearcher indexSearcher = mock( IndexSearcher.class );
IndexReader indexReader = mock( IndexReader.class );

when(partitionSearcher.getIndexSearcher()).thenReturn( indexSearcher );
when( indexSearcher.getIndexReader() ).thenReturn( indexReader );
when( indexReader.maxDoc() ).thenReturn( maxDoc );

when( indexSearcher.doc( 0 ) ).thenReturn( createDocument( uniqueDocValue( 1, partition, maxSize ) ) );
when( indexSearcher.doc( 1 ) ).thenReturn( createDocument( uniqueDocValue( 2, partition, maxSize ) ) );
when( indexSearcher.doc( 2 ) ).thenReturn( createDocument( uniqueDocValue( 3, partition, maxSize ) ) );

return partitionSearcher;
}

private static String uniqueDocValue(int value, int partition, int maxSize )
{
return String.valueOf( value + (partition * maxSize) );
}

private static Document createDocument(String value) {
Document document = new Document();
document.add( new StoredField( "value", value) );
return document;
} }
} }

0 comments on commit ce7c4a5

Please sign in to comment.