Skip to content

Commit

Permalink
Javadoc and naming fixes for fulltext addon
Browse files Browse the repository at this point in the history
  • Loading branch information
ragadeeshu committed Sep 12, 2017
1 parent 7395af8 commit 60f6a60
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 72 deletions.
2 changes: 1 addition & 1 deletion enterprise/fulltext-addon/pom.xml
Expand Up @@ -14,7 +14,7 @@
<version>3.3.0-SNAPSHOT</version>

<name>Neo4j - Fulltext add-on</name>
<description>Fulltext helper addon for neo4j</description>
<description>Fulltext index addon for neo4j</description>
<packaging>jar</packaging>

<scm>
Expand Down
Expand Up @@ -62,13 +62,13 @@ public FulltextFactory( FileSystemAbstraction fileSystem, File storeDir, Analyze

/**
* Creates an instance of {@link LuceneFulltext} and registers it with the supplied {@link FulltextProvider}.
* @param identifier The identifier of the new fulltext helper
* @param type The type of the new fulltext helper
* @param identifier The identifier of the new fulltext index
* @param type The type of the new fulltext index
* @param properties The properties to index
* @param provider The provider to register with
* @throws IOException
*/
public void createFulltextHelper( String identifier, FulltextProvider.FULLTEXT_HELPER_TYPE type, String[] properties, FulltextProvider provider )
public void createFulltextIndex( String identifier, FulltextProvider.FULLTEXT_INDEX_TYPE type, String[] properties, FulltextProvider provider )
throws IOException
{
LuceneIndexStorageBuilder storageBuilder = LuceneIndexStorageBuilder.create();
Expand Down
Expand Up @@ -28,6 +28,9 @@

import org.neo4j.graphdb.GraphDatabaseService;

/**
* Provider class that manages and provides fulltext indices. This is the main entry point for the fulltext addon.
*/
public class FulltextProvider implements AutoCloseable
{
private static FulltextProvider instance;
Expand All @@ -51,6 +54,11 @@ private FulltextProvider( GraphDatabaseService db )
relationshipIndices = new HashMap<>();
}

/**
* Fetch the current instance of the provider. If there is none, create one associated with the given database.
* @param db Database used for eventual creation of the provider.
* @return
*/
public static synchronized FulltextProvider instance( GraphDatabaseService db )
{
if ( instance == null || instance.closed )
Expand All @@ -60,29 +68,32 @@ public static synchronized FulltextProvider instance( GraphDatabaseService db )
return instance;
}

/**
* Closes the provider and all associated resources.
*/
@Override
public synchronized void close()
{
if ( !closed )
{
closed = true;
db.unregisterTransactionEventHandler( fulltextTransactionEventUpdater );
nodeIndices.values().forEach( luceneFulltextHelper ->
nodeIndices.values().forEach( luceneFulltextIndex ->
{
try
{
luceneFulltextHelper.close();
luceneFulltextIndex.close();
}
catch ( IOException e )
{
e.printStackTrace();
}
} );
relationshipIndices.values().forEach( luceneFulltextHelper ->
relationshipIndices.values().forEach( luceneFulltextIndex ->
{
try
{
luceneFulltextHelper.close();
luceneFulltextIndex.close();
}
catch ( IOException e )
{
Expand All @@ -92,18 +103,18 @@ public synchronized void close()
}
}

synchronized void register( LuceneFulltext fulltextHelper ) throws IOException
synchronized void register( LuceneFulltext fulltextIndex ) throws IOException
{
fulltextHelper.open();
if ( fulltextHelper.getType() == FULLTEXT_HELPER_TYPE.NODES )
fulltextIndex.open();
if ( fulltextIndex.getType() == FULLTEXT_INDEX_TYPE.NODES )
{
nodeIndices.put( fulltextHelper.getIdentifier(), fulltextHelper );
nodeProperties.addAll( fulltextHelper.getProperties() );
nodeIndices.put( fulltextIndex.getIdentifier(), fulltextIndex );
nodeProperties.addAll( fulltextIndex.getProperties() );
}
else
{
relationshipIndices.put( fulltextHelper.getIdentifier(), fulltextHelper );
relationshipProperties.addAll( fulltextHelper.getProperties() );
relationshipIndices.put( fulltextIndex.getIdentifier(), fulltextIndex );
relationshipProperties.addAll( fulltextIndex.getProperties() );
}
}

Expand All @@ -127,9 +138,16 @@ Set<WritableFulltext> writableRelationshipIndices()
return relationshipIndices.values().stream().map( WritableFulltext::new ).collect( Collectors.toSet() );
}

public ReadOnlyFulltext getReader( String identifier, FULLTEXT_HELPER_TYPE type ) throws IOException
/**
* Returns a reader for the specified index.
* @param identifier Identifier for the index.
* @param type Type of the index.
* @return A {@link ReadOnlyFulltext} for the index, or null if no such index is found.
* @throws IOException
*/
public ReadOnlyFulltext getReader( String identifier, FULLTEXT_INDEX_TYPE type ) throws IOException
{
if ( type == FULLTEXT_HELPER_TYPE.NODES )
if ( type == FULLTEXT_INDEX_TYPE.NODES )
{
return nodeIndices.get( identifier ).getIndexReader();
}
Expand All @@ -139,7 +157,10 @@ public ReadOnlyFulltext getReader( String identifier, FULLTEXT_HELPER_TYPE type
}
}

public enum FULLTEXT_HELPER_TYPE
/**
* Fulltext index type.
*/
public enum FULLTEXT_INDEX_TYPE
{
NODES
{
Expand Down
Expand Up @@ -105,7 +105,7 @@ public void afterCommit( TransactionData data, Object state )
}
catch ( IOException e )
{
throw new RuntimeException( "Unable to update fulltext helper index", e );
throw new RuntimeException( "Unable to update fulltext index", e );
}
}

Expand Down
Expand Up @@ -48,12 +48,12 @@ class LuceneFulltext extends AbstractLuceneIndex
private static final Map<String,String> ONLINE_COMMIT_USER_DATA = singletonMap( KEY_STATUS, ONLINE );
private final Analyzer analyzer;
private final String identifier;
private final FulltextProvider.FULLTEXT_HELPER_TYPE type;
private final FulltextProvider.FULLTEXT_INDEX_TYPE type;
private final TaskCoordinator taskCoordinator = new TaskCoordinator( 10, TimeUnit.MILLISECONDS );
private final Set<String> properties;

LuceneFulltext( PartitionedIndexStorage indexStorage, IndexPartitionFactory partitionFactory, String[] properties, Analyzer analyzer,
String identifier, FulltextProvider.FULLTEXT_HELPER_TYPE type )
String identifier, FulltextProvider.FULLTEXT_INDEX_TYPE type )
{
super( indexStorage, partitionFactory );
this.properties = Collections.unmodifiableSet( new HashSet<>( Arrays.asList( properties ) ) );
Expand Down Expand Up @@ -104,7 +104,7 @@ ReadOnlyFulltext getIndexReader() throws IOException
return hasSinglePartition( partitions ) ? createSimpleReader( partitions ) : createPartitionedReader( partitions );
}

FulltextProvider.FULLTEXT_HELPER_TYPE getType()
FulltextProvider.FULLTEXT_INDEX_TYPE getType()
{
return type;
}
Expand Down
Expand Up @@ -24,15 +24,15 @@
public interface ReadOnlyFulltext extends AutoCloseable
{
/**
* Searches the fulltext helper for any exact match of any of the given terms against any token in any of the indexed properties.
* Searches the fulltext index for any exact match of any of the given terms against any token in any of the indexed properties.
*
* @param terms The terms to query for.
* @return An iterator over the matching entityIDs, ordered by lucene scoring of the match.
*/
PrimitiveLongIterator query( String... terms );

/**
* Searches the fulltext helper for any fuzzy match of any of the given terms against any token in any of the indexed properties.
* Searches the fulltext index for any fuzzy match of any of the given terms against any token in any of the indexed properties.
*
* @param terms The terms to query for.
* @return An iterator over the matching entityIDs, ordered by lucene scoring of the match.
Expand Down
Expand Up @@ -39,7 +39,7 @@
import static org.neo4j.kernel.api.impl.schema.LuceneDocumentStructure.NODE_ID_KEY;

/**
* Schema index reader that is able to read/sample a single partition of a partitioned Lucene index.
* Lucene index reader that is able to read/sample a single partition of a partitioned Lucene index.
*
* @see PartitionedFulltextReader
*/
Expand Down
Expand Up @@ -68,12 +68,12 @@ public void init() throws IOException, ProcedureException
FulltextProvider provider = FulltextProvider.instance( db );
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemAbstraction, storeDir, analyzer );
String bloomNodes = "bloomNodes";
fulltextFactory.createFulltextHelper( bloomNodes, FulltextProvider.FULLTEXT_HELPER_TYPE.NODES, properties, provider );
fulltextFactory.createFulltextIndex( bloomNodes, FulltextProvider.FULLTEXT_INDEX_TYPE.NODES, properties, provider );
String bloomRelationships = "bloomRelationships";
fulltextFactory.createFulltextHelper( bloomRelationships, FulltextProvider.FULLTEXT_HELPER_TYPE.RELATIONSHIPS, properties, provider );
fulltextFactory.createFulltextIndex( bloomRelationships, FulltextProvider.FULLTEXT_INDEX_TYPE.RELATIONSHIPS, properties, provider );

procedures.register( new BloomProcedure( FulltextProvider.FULLTEXT_HELPER_TYPE.NODES, bloomNodes, provider ) );
procedures.register( new BloomProcedure( FulltextProvider.FULLTEXT_HELPER_TYPE.RELATIONSHIPS, bloomRelationships, provider ) );
procedures.register( new BloomProcedure( FulltextProvider.FULLTEXT_INDEX_TYPE.NODES, bloomNodes, provider ) );
procedures.register( new BloomProcedure( FulltextProvider.FULLTEXT_INDEX_TYPE.RELATIONSHIPS, bloomRelationships, provider ) );
}

@Override
Expand Down
Expand Up @@ -44,15 +44,15 @@ public class BloomProcedure extends CallableProcedure.BasicProcedure
private static final String[] PROCEDURE_NAMESPACE = {"db", "fulltext"};
private final String identifier;
private final FulltextProvider provider;
private FulltextProvider.FULLTEXT_HELPER_TYPE type;
private FulltextProvider.FULLTEXT_INDEX_TYPE type;

/**
* Creates a procedure for querying the bloom fulltext addon.
* @param type The type of fulltext helper to be queried.
* @param identifier The identifier of the fulltext helper.
* @param provider The provider of the fulltext helper.
* @param type The type of fulltext index to be queried.
* @param identifier The identifier of the fulltext index.
* @param provider The provider of the fulltext index.
*/
public BloomProcedure( FulltextProvider.FULLTEXT_HELPER_TYPE type, String identifier, FulltextProvider provider )
public BloomProcedure( FulltextProvider.FULLTEXT_INDEX_TYPE type, String identifier, FulltextProvider provider )
{
super( procedureSignature( new QualifiedName( PROCEDURE_NAMESPACE, PROCEDURE_NAME + type ) ).in( "terms",
Neo4jTypes.NTList( Neo4jTypes.NTString ) ).out( OUTPUT_NAME, Neo4jTypes.NTInteger ).description(
Expand Down
Expand Up @@ -37,7 +37,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.neo4j.kernel.api.impl.fulltext.FulltextProvider.FULLTEXT_HELPER_TYPE;
import static org.neo4j.kernel.api.impl.fulltext.FulltextProvider.FULLTEXT_INDEX_TYPE;

public class FulltextAnalyzerTest
{
Expand All @@ -56,7 +56,7 @@ public void shouldBeAbleToSpecifyEnglishAnalyzer() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), new EnglishAnalyzer() );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextHelper( "bloomNodes", FULLTEXT_HELPER_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "bloomNodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );

long firstID;
long secondID;
Expand All @@ -72,7 +72,7 @@ public void shouldBeAbleToSpecifyEnglishAnalyzer() throws Exception
tx.success();
}

try ( ReadOnlyFulltext reader = provider.getReader( "bloomNodes", FULLTEXT_HELPER_TYPE.NODES ) )
try ( ReadOnlyFulltext reader = provider.getReader( "bloomNodes", FULLTEXT_INDEX_TYPE.NODES ) )
{

assertFalse( reader.query( "and" ).hasNext() );
Expand All @@ -92,7 +92,7 @@ public void shouldBeAbleToSpecifySwedishAnalyzer() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), new SwedishAnalyzer() );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextHelper( "bloomNodes", FULLTEXT_HELPER_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "bloomNodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );

long firstID;
long secondID;
Expand All @@ -108,7 +108,7 @@ public void shouldBeAbleToSpecifySwedishAnalyzer() throws Exception
tx.success();
}

try ( ReadOnlyFulltext reader = provider.getReader( "bloomNodes", FULLTEXT_HELPER_TYPE.NODES ) )
try ( ReadOnlyFulltext reader = provider.getReader( "bloomNodes", FULLTEXT_INDEX_TYPE.NODES ) )
{
assertEquals( firstID, reader.query( "and" ).next() );
assertEquals( firstID, reader.query( "in" ).next() );
Expand Down

0 comments on commit 60f6a60

Please sign in to comment.