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

java.lang.Object
  extended byfr.gouv.culture.sdx.utils.SdxObjectImpl
      extended byfr.gouv.culture.sdx.utils.lucene.LuceneDataStore
          extended byfr.gouv.culture.sdx.utils.database.LuceneDatabase
All Implemented Interfaces:
org.apache.avalon.framework.component.Composable, org.apache.avalon.framework.configuration.Configurable, Database, org.apache.avalon.framework.logger.LogEnabled, SdxObject

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.


Field Summary
protected  java.lang.String DATABASE_DIR_NAME
           
protected  java.lang.String id
           
protected  org.apache.avalon.framework.component.ComponentManager manager
          Hashtable of property values
protected  java.util.Hashtable props
           
 
Fields inherited from class fr.gouv.culture.sdx.utils.lucene.LuceneDataStore
ALL_FIELD, ALL_VALUE, analyzer, fsd, fsdFile, ID_FIELD, LUCENE_CHECK_FILE, searcher, XML_LANG_FIELD
 
Fields inherited from class fr.gouv.culture.sdx.utils.SdxObjectImpl
encoding, logger
 
Fields inherited from interface fr.gouv.culture.sdx.utils.database.Database
DATABASE_DIR_PATH, SEARCH_MODE_AND, SEARCH_MODE_NOT, SEARCH_MODE_OR
 
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 compose(org.apache.avalon.framework.component.ComponentManager manager)
          Compose the object so that we get the Components we need from the ComponentManager.
 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
 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 getId()
           
 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.
 void optimize()
          Utility method for database maintenance, normally used to optimize indices of Lucene based repositories It is the responsibility of the Repository to ensure that unnecessary optimizations are not performed when this method is called.
 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 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.
 void setId(java.lang.String id)
           
 void setProperties(java.util.Hashtable props)
          Provides a list of properties of this class which can be used at configuration and/or initialization time
 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, getReader, getSearcher, getWriter, getWriter, init, recycleSearcher, search, search, verifyIndex, writeLuceneData
 
Methods inherited from class fr.gouv.culture.sdx.utils.SdxObjectImpl
enableLogging, getChildLogger, setEncoding
 
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
setEncoding
 
Methods inherited from interface org.apache.avalon.framework.logger.LogEnabled
enableLogging
 

Field Detail

manager

protected org.apache.avalon.framework.component.ComponentManager manager
Hashtable of property values


props

protected java.util.Hashtable props

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.
Method Detail

compose

public void compose(org.apache.avalon.framework.component.ComponentManager manager)
             throws org.apache.avalon.framework.component.ComponentException
Compose the object so that we get the Components we need from the ComponentManager.

Specified by:
compose in interface org.apache.avalon.framework.component.Composable
Parameters:
manager - The component manager from Cocoon that allow us to acquire a database selector component.
Throws:
org.apache.avalon.framework.component.ComponentException

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 interface: Database
Utility method for database maintenance, normally used to optimize indices of Lucene based repositories It is the responsibility of the Repository to ensure that unnecessary optimizations are not performed when this method is called.

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

setProperties

public void setProperties(java.util.Hashtable props)
Description copied from interface: Database
Provides a list of properties of this class which can be used at configuration and/or initialization time

Specified by:
setProperties in interface Database
Parameters:
props -

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

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
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)

setId

public void setId(java.lang.String id)
Specified by:
setId in interface Database

getId

public java.lang.String getId()
Specified by:
getId in interface Database

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

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


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