fr.gouv.culture.sdx.utils.database
Class LuceneDatabase

java.lang.Object
  extended by fr.gouv.culture.sdx.utils.AbstractSdxObject
      extended by fr.gouv.culture.sdx.utils.lucene.LuceneDataStore
          extended by fr.gouv.culture.sdx.utils.database.LuceneDatabase
All Implemented Interfaces:
Database, Describable, Encodable, Identifiable, Localizable, Saveable, SdxObject, org.apache.avalon.framework.configuration.Configurable, org.apache.avalon.framework.context.Contextualizable, org.apache.avalon.framework.logger.LogEnabled, org.apache.avalon.framework.service.Serviceable, org.apache.excalibur.xml.sax.XMLizable

public class LuceneDatabase
extends LuceneDataStore
implements Database

An implementation of database operations using Lucene.

For a detailed description of the database abstraction in SDX, please see the Database interface.

When creating a LuceneDatabase object, these steps should be followed :

  1. Create the object with a File object pointing at the directory where the index is or will be.
  2. Use the enableLogging method to enable logging of errors/events.
  3. Use the init() method to make sure that the database is initialized and usable.


Nested Class Summary
 
Nested classes/interfaces inherited from interface fr.gouv.culture.sdx.utils.SdxObject
SdxObject.ConfigurationNode
 
Field Summary
protected  java.lang.String DATABASE_DIR_NAME
           
protected  java.lang.String id
           
 
Fields inherited from class fr.gouv.culture.sdx.utils.lucene.LuceneDataStore
ALL_FIELD, ALL_VALUE, analyzer, fsd, fsdFile, ID_FIELD, searcher, useCompoundFiles, XML_LANG_FIELD
 
Fields inherited from class fr.gouv.culture.sdx.utils.AbstractSdxObject
_configuration, _context, _description, _encoding, _id, _locale, _logger, _manager, _xmlizable_objects, _xmlLang, isToSaxInitialized
 
Fields inherited from interface fr.gouv.culture.sdx.utils.database.Database
CLASS_NAME_SUFFIX, DATABASE_DIR_PATH, PACKAGE_QUALNAME, SEARCH_MODE_AND, SEARCH_MODE_NOT, SEARCH_MODE_OR
 
Fields inherited from interface fr.gouv.culture.sdx.utils.Encodable
DEFAULT_ENCODING
 
Fields inherited from interface fr.gouv.culture.sdx.utils.save.Saveable
ALL_SAVE_ATTRIB, PATH_ATTRIB, SAVE_DIRECTORY_PARAM
 
Constructor Summary
LuceneDatabase()
           
LuceneDatabase(java.io.File dir)
          Gets or creates a _database stored at a given dir.
 
Method Summary
 void addProperty(java.lang.String entityId, java.lang.String propertyName, java.lang.String propertyValue)
          If the entity does not exist the method fails silently
 void backup(SaveParameters save_config)
          Save Lucene files
 void configure(org.apache.avalon.framework.configuration.Configuration configuration)
           
 void delete(DatabaseEntity ent)
          Deletes an entity from the database.
 void delete(DatabaseEntity[] entities)
           
 void empty()
          Empties the _database.
 boolean entityExists(java.lang.String id)
          Checks whether an entity with the provided id exists within this database
protected  java.lang.String getClassNameSuffix()
          Sub classes should element this method to return their class name suffix for SAX output
 DatabaseConnection getConnection()
           
 java.lang.String getDatabaseDirectoryName()
          If the database is a directory based file system implemenation like lucene this method should return a valid name for a directory which may be created to house the database, or in the case of hsql it will return a valid table name
 DatabaseEntity[] getEntities()
          Returns the list of entities within the database.
protected  DatabaseEntity getEntity(org.apache.lucene.document.Document ldoc)
          Builds an entity from a Lucene document.
 DatabaseEntity getEntity(java.lang.String id)
          Returns an entity for a given id.
 java.lang.String getIndexPath()
          Return's the absolute path for the directory in which the database resides
 Property[] getProperties(java.lang.String entityId)
          Returns all properties from an entity in the database.
 java.lang.String getProperty(java.lang.String documentId, java.lang.String property)
          Searches database for a given document id, and then returns the content of the specified field.
 java.lang.String getPropertyValue(java.lang.String entityId, java.lang.String name)
          Returns a property value from an entity in the database.
 java.lang.String[] getPropertyValues(java.lang.String entityId, java.lang.String propertyName)
          Returns a repeatable property from an entity in the database.
 java.lang.String getWildcardSearchToken()
          Returns a String representing the appropriate wildcard search token for the implementation
 void init()
          Initializes the Lucene database.
protected  boolean initToSax()
          Init the LinkedHashMap _xmlizable_objects with the objects in order to describ them in XML
 void optimize()
          Optimizeds the index for the current FSDirectory.
 void releaseConnection(DatabaseConnection conn)
           
 void removeProperty(java.lang.String propertyName, java.lang.String propertyValue)
          Removes a property with the provided name and value from the and EXISTING entity having the name/value pair It is up to the implementation to determine proper behavior if the entity with the provided id does not exist.
 void removeProperty(java.lang.String entityId, java.lang.String propertyName, java.lang.String propertyValue)
          If the entity does not exist the method fails silently
 void restore(SaveParameters save_config)
          Restore Lucene files
 void save(DatabaseEntity entity)
          Saves an entity within the database.
 java.lang.String[] search(org.apache.avalon.framework.parameters.Parameters params)
          Returns an array of database entity ids based upon the provided search parameters One can used Database.getEntity(id) after retrieving a list of ids with this method.
 java.lang.String[] search(org.apache.avalon.framework.parameters.Parameters params, int mode)
           
 long size()
          Returns the number of entities within this database.
 void update(DatabaseEntity ent)
          Updates an entity.
protected  void write(org.apache.lucene.document.Document lDoc)
          Writes a document to the index
 
Methods inherited from class fr.gouv.culture.sdx.utils.lucene.LuceneDataStore
delete, delete, delete, delete, finalize, freeResources, getFSDirectory, getFSDirectory, getReader, getSearcher, getWriter, getWriter, getWriter, getWriter, init, initVolatileObjectsToSax, isUsingCompoundFiles, recycleSearcher, search, search, verifyIndex, writeLuceneData
 
Methods inherited from class fr.gouv.culture.sdx.utils.AbstractSdxObject
configureDescription, contextualize, enableLogging, getBaseAttributes, getConfiguration, getContext, getDescription, getEncoding, getId, getLocale, getLog, getServiceManager, getXmlLang, service, setDescription, setEncoding, setId, setLocale, setUpSdxObject, setUpSdxObject, setXmlLang, toSAX, verifyConfigurationResources
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface fr.gouv.culture.sdx.utils.SdxObject
getLog
 
Methods inherited from interface org.apache.avalon.framework.logger.LogEnabled
enableLogging
 
Methods inherited from interface org.apache.avalon.framework.context.Contextualizable
contextualize
 
Methods inherited from interface org.apache.avalon.framework.service.Serviceable
service
 
Methods inherited from interface fr.gouv.culture.sdx.utils.Identifiable
getId, setId
 
Methods inherited from interface fr.gouv.culture.sdx.utils.Describable
getDescription, setDescription
 
Methods inherited from interface fr.gouv.culture.sdx.utils.Encodable
getEncoding, setEncoding
 
Methods inherited from interface fr.gouv.culture.sdx.utils.Localizable
getLocale, getXmlLang, setLocale, setXmlLang
 
Methods inherited from interface org.apache.excalibur.xml.sax.XMLizable
toSAX
 

Field Detail

DATABASE_DIR_NAME

protected java.lang.String DATABASE_DIR_NAME

id

protected java.lang.String id
Constructor Detail

LuceneDatabase

public LuceneDatabase()

LuceneDatabase

public LuceneDatabase(java.io.File dir)
               throws SDXException
Gets or creates a _database stored at a given dir.

One should call the enableLogging() method before using this database, otherwise errors won't be logged.

Most of all, you should call the init() method to make sure that the database can be used.

Parameters:
dir - The directory where the Lucene database is stored.
Throws:
SDXException
Method Detail

init

public void init()
          throws SDXException
Initializes the Lucene database.

It the database exists, nothing is done here. If it is doesn't exist, it will be created.

Specified by:
init in interface Database
Throws:
SDXException

getProperty

public java.lang.String getProperty(java.lang.String documentId,
                                    java.lang.String property)
                             throws SDXException
Searches database for a given document id, and then returns the content of the specified field.

Parameters:
documentId - The id of the document to search for.
property - The field name, or the property to search for.
Throws:
SDXException

getEntity

public DatabaseEntity getEntity(java.lang.String id)
                         throws SDXException
Returns an entity for a given id.

Specified by:
getEntity in interface Database
Parameters:
id - The entity's id.
Returns:
The entity, or null if no entity has this id.
Throws:
SDXException

getEntities

public DatabaseEntity[] getEntities()
                             throws SDXException
Returns the list of entities within the database.

Specified by:
getEntities in interface Database
Throws:
SDXException

getEntity

protected DatabaseEntity getEntity(org.apache.lucene.document.Document ldoc)
                            throws SDXException
Builds an entity from a Lucene document.

Parameters:
ldoc - The Lucene document.
Throws:
SDXException

save

public void save(DatabaseEntity entity)
          throws SDXException
Saves an entity within the database.

Specified by:
save in interface Database
Parameters:
entity - The entity to save.
Throws:
SDXException

write

protected void write(org.apache.lucene.document.Document lDoc)
              throws SDXException
Description copied from class: LuceneDataStore
Writes a document to the index

Overrides:
write in class LuceneDataStore
Parameters:
lDoc - The lucene document to add
Throws:
SDXException

getPropertyValue

public java.lang.String getPropertyValue(java.lang.String entityId,
                                         java.lang.String name)
                                  throws SDXException
Returns a property value from an entity in the database.

Specified by:
getPropertyValue in interface Database
Parameters:
entityId - The needed entity's id.
name - The property's name for the desired value.
Returns:
The property if is exists, otherwise null. If the property is defined more than once, the first value is returned.
Throws:
SDXException

getPropertyValues

public java.lang.String[] getPropertyValues(java.lang.String entityId,
                                            java.lang.String propertyName)
                                     throws SDXException
Returns a repeatable property from an entity in the database.

Please note that this method currently doesn't work for Lucene, which still stores (?) and retrieves only the last added repeatable property.TODO:remove this note after we commit our lucene changes

Specified by:
getPropertyValues in interface Database
Parameters:
entityId - The needed entity's name.
propertyName - The needed property's name.
Returns:
An enumeration of all values for this property, null if this property is not defined for this entity.
Throws:
SDXException

getProperties

public Property[] getProperties(java.lang.String entityId)
                         throws SDXException
Returns all properties from an entity in the database.

Specified by:
getProperties in interface Database
Parameters:
entityId - The entity's id.
Returns:
The list of properties, an empty list if no properties are defined or the entity doesn't exist.
Throws:
SDXException

delete

public void delete(DatabaseEntity ent)
            throws SDXException
Deletes an entity from the database.

Specified by:
delete in interface Database
Parameters:
ent - The entity to delete (must have an id). A new DatabaseEntity can be created with the id set to the id of the entity desired for deletion and then this new entity can be passed to this method and the appropriate entity corresponding to that id will be deleted.
Throws:
SDXException

delete

public void delete(DatabaseEntity[] entities)
            throws SDXException
Throws:
SDXException

update

public void update(DatabaseEntity ent)
            throws SDXException
Updates an entity. Deletes the existing entity with the same id and saves the provided entity.

Specified by:
update in interface Database
Parameters:
ent - The entity to update.
Throws:
SDXException
See Also:
delete(fr.gouv.culture.sdx.utils.database.DatabaseEntity), save(fr.gouv.culture.sdx.utils.database.DatabaseEntity)

size

public long size()
Returns the number of entities within this database.

Specified by:
size in interface Database
Overrides:
size in class LuceneDataStore

empty

public void empty()
           throws SDXException
Empties the _database.

Specified by:
empty in interface Database
Throws:
SDXException

getIndexPath

public java.lang.String getIndexPath()
Description copied from class: LuceneDataStore
Return's the absolute path for the directory in which the database resides

Overrides:
getIndexPath in class LuceneDataStore

optimize

public void optimize()
              throws SDXException
Description copied from class: LuceneDataStore
Optimizeds the index for the current FSDirectory.

Specified by:
optimize in interface Database
Overrides:
optimize in class LuceneDataStore
Throws:
SDXException

entityExists

public boolean entityExists(java.lang.String id)
Description copied from interface: Database
Checks whether an entity with the provided id exists within this database

Specified by:
entityExists in interface Database
Parameters:
id - _identifier of the entity

search

public java.lang.String[] search(org.apache.avalon.framework.parameters.Parameters params)
                          throws SDXException
Description copied from interface: Database
Returns an array of database entity ids based upon the provided search parameters One can used Database.getEntity(id) after retrieving a list of ids with this method.

Specified by:
search in interface Database
Parameters:
params - search params
Throws:
SDXException

search

public java.lang.String[] search(org.apache.avalon.framework.parameters.Parameters params,
                                 int mode)
                          throws SDXException
Specified by:
search in interface Database
Throws:
SDXException

configure

public void configure(org.apache.avalon.framework.configuration.Configuration configuration)
               throws org.apache.avalon.framework.configuration.ConfigurationException
Specified by:
configure in interface org.apache.avalon.framework.configuration.Configurable
Overrides:
configure in class AbstractSdxObject
Throws:
org.apache.avalon.framework.configuration.ConfigurationException

getDatabaseDirectoryName

public java.lang.String getDatabaseDirectoryName()
Description copied from interface: Database
If the database is a directory based file system implemenation like lucene this method should return a valid name for a directory which may be created to house the database, or in the case of hsql it will return a valid table name

Specified by:
getDatabaseDirectoryName in interface Database
Returns:
A directory name (not a full path)

getConnection

public DatabaseConnection getConnection()
                                 throws SDXException
Specified by:
getConnection in interface Database
Throws:
SDXException

releaseConnection

public void releaseConnection(DatabaseConnection conn)
                       throws SDXException
Specified by:
releaseConnection in interface Database
Throws:
SDXException

addProperty

public void addProperty(java.lang.String entityId,
                        java.lang.String propertyName,
                        java.lang.String propertyValue)
                 throws SDXException
If the entity does not exist the method fails silently

Specified by:
addProperty in interface Database
Parameters:
entityId -
propertyName -
propertyValue -
Throws:
SDXException

removeProperty

public void removeProperty(java.lang.String entityId,
                           java.lang.String propertyName,
                           java.lang.String propertyValue)
                    throws SDXException
If the entity does not exist the method fails silently

Specified by:
removeProperty in interface Database
Parameters:
entityId -
propertyName -
propertyValue -
Throws:
SDXException

removeProperty

public void removeProperty(java.lang.String propertyName,
                           java.lang.String propertyValue)
                    throws SDXException
Description copied from interface: Database
Removes a property with the provided name and value from the and EXISTING entity having the name/value pair It is up to the implementation to determine proper behavior if the entity with the provided id does not exist.

Specified by:
removeProperty in interface Database
Parameters:
propertyName - The property to remove
propertyValue - The property value
Throws:
SDXException

getClassNameSuffix

protected java.lang.String getClassNameSuffix()
Description copied from class: AbstractSdxObject
Sub classes should element this method to return their class name suffix for SAX output

Specified by:
getClassNameSuffix in class AbstractSdxObject

getWildcardSearchToken

public java.lang.String getWildcardSearchToken()
Description copied from interface: Database
Returns a String representing the appropriate wildcard search token for the implementation

Specified by:
getWildcardSearchToken in interface Database

initToSax

protected boolean initToSax()
Description copied from class: AbstractSdxObject
Init the LinkedHashMap _xmlizable_objects with the objects in order to describ them in XML

Overrides:
initToSax in class LuceneDataStore

backup

public void backup(SaveParameters save_config)
            throws SDXException
Description copied from class: LuceneDataStore
Save Lucene files

Specified by:
backup in interface Saveable
Overrides:
backup in class LuceneDataStore
Throws:
SDXException
See Also:
Saveable.backup(fr.gouv.culture.sdx.utils.save.SaveParameters)

restore

public void restore(SaveParameters save_config)
             throws SDXException
Description copied from class: LuceneDataStore
Restore Lucene files

Specified by:
restore in interface Saveable
Overrides:
restore in class LuceneDataStore
Throws:
SDXException
See Also:
Saveable.restore(fr.gouv.culture.sdx.utils.save.SaveParameters)


Copyright © 2000-2010 Ministere de la culture et de la communication / AJLSM. All Rights Reserved.