org.biojava.bio.program.tagvalue
Class Indexer

java.lang.Object
  extended by org.biojava.bio.program.tagvalue.Indexer
All Implemented Interfaces:
TagValueListener

public class Indexer
extends Object
implements TagValueListener

Listens to tag-value events and passes on indexing events to an IndexStore.

This class is provided to allow the indexing of arbitrary record-based text files. Indexer objects are built for a single file and the indexes are written to a single index store. To keep all of the reader offsets in sync with one another, you will almost certainly wish to use the getReader() method to retrieve a CountedBufferedReader instance if you want to read the byte-offset between calls to Parser.read(). Below is an example of how to index a file.

Note: It is very important to configure the BioStoreFactory instance with all the right keys before hand.

 File fileToIndex; // get this from somewhere
 BioStore store = bsf.createBioStore();
 Indexer indexer = new Indexer(fileToIndex, store);
 indexer.setPrimaryKeyName("foo");
 indexer.addSecondaryKey("bar");
 indexer.addSecondaryKey("baz");

 TagValueParser tvParser; // make this appropriate for your format
 TagValueListener listener; // make this appropriate for your format
                            // and forward all events to changer
 
 Parser parser = new Parser();
 while(
   parser.read(indexer.getReader(), tvParser, listener)
 ) {
   System.out.print(".");
 }
 

Since:
1.2
Author:
Matthew Pocock

Constructor Summary
Indexer(File file, IndexStore indexStore)
          Build a new Indexer.
 
Method Summary
 void addSecondaryKey(String secKeyName)
           Add a secondary key.
 void endRecord()
          The current record has ended.
 void endTag()
          End the current tag.
 String getPrimaryKeyName()
          Retrieve the tag currently used as primary key.
 CountedBufferedReader getReader()
          Retrieve the reader that can be safely used to index this file.
 void removeSecondaryKey(String secKeyName)
          Remove a secondary key.
 void setPrimaryKeyName(String primaryKeyName)
           Set the tag to use as a primary key in the index.
 void startRecord()
          A new record is about to start.
 void startTag(Object tag)
          Start a new tag.
 void value(TagValueContext ctxt, Object value)
          A value has been seen.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Indexer

public Indexer(File file,
               IndexStore indexStore)
        throws FileNotFoundException
Build a new Indexer.

Parameters:
file - the file to be processed
indexStore - the IndexStore to write to
Throws:
FileNotFoundException
Method Detail

getReader

public CountedBufferedReader getReader()
Retrieve the reader that can be safely used to index this file.

Returns:
the CountedBufferedReader that should be processed

setPrimaryKeyName

public void setPrimaryKeyName(String primaryKeyName)

Set the tag to use as a primary key in the index.

Whenever a value for the primary key tag is seen, this is passed to the indexer as the primary key for indexing.

Primary keys must be unique between entries, and each entry must provide exactly one primary key value.

Parameters:
primaryKeyName - the tag to use as primary key

getPrimaryKeyName

public String getPrimaryKeyName()
Retrieve the tag currently used as primary key.

Returns:
a String representing the primary key name

addSecondaryKey

public void addSecondaryKey(String secKeyName)

Add a secondary key.

Secondary keys are potentially non-unique properties of the entries being indexed. Multiple records can use the same secondary key values, and a single record can have multiple values for a secondary key.

Parameters:
secKeyName - the name of the secondary key to add

removeSecondaryKey

public void removeSecondaryKey(String secKeyName)
Remove a secondary key.

Parameters:
secKeyName - the name of the secondary key to remove

startRecord

public void startRecord()
Description copied from interface: TagValueListener
A new record is about to start.

Specified by:
startRecord in interface TagValueListener

startTag

public void startTag(Object tag)
Description copied from interface: TagValueListener
Start a new tag.

Specified by:
startTag in interface TagValueListener
Parameters:
tag - the Object representing the new tag

value

public void value(TagValueContext ctxt,
                  Object value)
Description copied from interface: TagValueListener
A value has been seen.

Specified by:
value in interface TagValueListener
Parameters:
ctxt - a TagValueContext that could be used to push a sub-document
value - the value Object observed

endTag

public void endTag()
Description copied from interface: TagValueListener
End the current tag.

Specified by:
endTag in interface TagValueListener

endRecord

public void endRecord()
               throws ParserException
Description copied from interface: TagValueListener
The current record has ended.

Specified by:
endRecord in interface TagValueListener
Throws:
ParserException - if the record can not be ended