org.apache.lucene.store.jdbc
Class JdbcDirectory

java.lang.Object
  extended by org.apache.lucene.store.Directory
      extended by org.apache.lucene.store.jdbc.JdbcDirectory
All Implemented Interfaces:
MultiDeleteDirectory

public class JdbcDirectory
extends Directory
implements MultiDeleteDirectory

A Jdbc based implementation of a Lucene Directory allowing the storage of a Lucene index within a database. Uses a jdbc DataSource, Dialect specific for the database used, and an optional JdbcDirectorySettings and JdbcTable for configuration.

The directory works against a single table, where the binary data is stored in Blob. Each "file" has an entry in the database, and different FileEntryHandler can be defines for different files (or files groups).

Most of the files will not be deleted from the database when the directory delete method is called, but will only be marked to be deleted (see MarkDeleteFileEntryHandler. It is done since other readers or searchers might be working with the database, and still use the files. The ability to purge mark deleted files based on a "delta" is acheived using deleteMarkDeleted() and deleteMarkDeleted(long). Note, the purging process is not called by the directory code, so it will have to be managed by the application using the jdbc directory.

For transaction management, all the operations performed against the database do not call commit or rollback. They simply open a connection (using DataSourceUtils.getConnection(javax.sql.DataSource) ), and close it using DataSourceUtils.releaseConnection(java.sql.Connection)). This results in the fact that transcation management is simple and wraps the directory operations, allowing it to span as many operations as needed.

For none managed applications (i.e. applications that do not use JTA or Spring transaction manager), the jdbc directory implementation comes with TransactionAwareDataSourceProxy which wraps a DataSource (should be a pooled one, like Jakartat DBCP). Using it with the DataSourceUtils, or the provided DirectoryTemplate should make integrating or using jdbc directory simple.

Also, for none managed applications, there is an option working with autoCommit=true mode. The system will work much slower, and it is only supported on a portion of the databases, but any existing code that uses Lucene with any other Directory implemenation should work as is.

If working within managed environments, an external transaction management should be performed (using JTA for example). Simple solutions can be using CMT or Spring Framework abstraction of transaction managers. Currently, the jdbc directory implementation does not implement a transaction management abstraction, since there is a very good solution out there already (Spring and JTA). Note, when using Spring and the DataSourceTransactionManager, to provide the jdbc directory with a Spring's TransactionAwareDataSourceProxy.

Author:
kimchy

Field Summary
 
Fields inherited from class org.apache.lucene.store.Directory
lockFactory
 
Constructor Summary
JdbcDirectory(DataSource dataSource, Dialect dialect, JdbcDirectorySettings settings, String tableName)
          Creates a new jdbc directory.
JdbcDirectory(DataSource dataSource, Dialect dialect, String tableName)
          Creates a new jdbc directory.
JdbcDirectory(DataSource dataSource, JdbcDirectorySettings settings, String tableName)
          Creates a new jdbc directory.
JdbcDirectory(DataSource dataSource, JdbcTable table)
          Creates a new jdbc directory.
JdbcDirectory(DataSource dataSource, String tableName)
          Creates a new jdbc directory.
 
Method Summary
 void close()
          Closes the directory.
 void create()
          Creates a new database table.
protected  Lock createLock()
           
 IndexOutput createOutput(String name)
           
 void delete()
          Deletes the database table (drops it) from the database.
 void deleteContent()
          Deletes the contents of the database, except for the commit and write lock.
 void deleteFile(String name)
           
 List deleteFiles(List names)
          Deletes the given file names.
 void deleteMarkDeleted()
          Delets all the file entries that are marked to be deleted, and they were marked "delta" time ago (base on database time, if possible by dialect).
 void deleteMarkDeleted(long delta)
          Delets all the file entries that are marked to be deleted, and they were marked "delta" time ago (base on database time, if possible by dialect).
 boolean fileExists(String name)
           
 long fileLength(String name)
           
 long fileModified(String name)
           
 void forceDeleteFile(String name)
           
 DataSource getDataSource()
           
 Dialect getDialect()
           
protected  FileEntryHandler getFileEntryHandler(String name)
           
 JdbcTemplate getJdbcTemplate()
           
 JdbcDirectorySettings getSettings()
           
 JdbcTable getTable()
           
 String[] list()
           
 Lock makeLock(String name)
           
 IndexInput openInput(String name)
           
 void renameFile(String from, String to)
           
 boolean tableExists()
          Returns true if the database table exists.
 void touchFile(String name)
           
 
Methods inherited from class org.apache.lucene.store.Directory
clearLock, copy, ensureOpen, getLockFactory, getLockID, openInput, setLockFactory, sync
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JdbcDirectory

public JdbcDirectory(DataSource dataSource,
                     String tableName)
              throws JdbcStoreException
Creates a new jdbc directory. Creates new JdbcDirectorySettings using it's default values. Uses DialectResolver to try and automatically reolve the Dialect.

Parameters:
dataSource - The data source to use
tableName - The table name
Throws:
JdbcStoreException

JdbcDirectory

public JdbcDirectory(DataSource dataSource,
                     Dialect dialect,
                     String tableName)
Creates a new jdbc directory. Creates new JdbcDirectorySettings using it's default values.

Parameters:
dataSource - The data source to use
dialect - The dialect
tableName - The table name

JdbcDirectory

public JdbcDirectory(DataSource dataSource,
                     JdbcDirectorySettings settings,
                     String tableName)
              throws JdbcStoreException
Creates a new jdbc directory. Uses DialectResolver to try and automatically reolve the Dialect.

Parameters:
dataSource - The data source to use
settings - The settings to configure the directory
tableName - The table name that will be used
Throws:
JdbcStoreException

JdbcDirectory

public JdbcDirectory(DataSource dataSource,
                     Dialect dialect,
                     JdbcDirectorySettings settings,
                     String tableName)
Creates a new jdbc directory.

Parameters:
dataSource - The data source to use
dialect - The dialect
settings - The settings to configure the directory
tableName - The table name that will be used

JdbcDirectory

public JdbcDirectory(DataSource dataSource,
                     JdbcTable table)
Creates a new jdbc directory.

Parameters:
dataSource - The data source to use
table - The Jdbc table definitions
Method Detail

tableExists

public boolean tableExists()
                    throws IOException,
                           UnsupportedOperationException
Returns true if the database table exists.

Returns:
true if the database table exists, false otherwise
Throws:
IOException
UnsupportedOperationException - If the database dialect does not support it

delete

public void delete()
            throws IOException
Deletes the database table (drops it) from the database.

Throws:
IOException

create

public void create()
            throws IOException
Creates a new database table. Drops it before hand.

Throws:
IOException

deleteContent

public void deleteContent()
                   throws IOException
Deletes the contents of the database, except for the commit and write lock.

Throws:
IOException

deleteMarkDeleted

public void deleteMarkDeleted()
                       throws IOException
Delets all the file entries that are marked to be deleted, and they were marked "delta" time ago (base on database time, if possible by dialect). The delta is taken from JdbcDirectorySettings.getDeleteMarkDeletedDelta().

Throws:
IOException

deleteMarkDeleted

public void deleteMarkDeleted(long delta)
                       throws IOException
Delets all the file entries that are marked to be deleted, and they were marked "delta" time ago (base on database time, if possible by dialect).

Throws:
IOException

list

public String[] list()
              throws IOException
Specified by:
list in class Directory
Throws:
IOException

fileExists

public boolean fileExists(String name)
                   throws IOException
Specified by:
fileExists in class Directory
Throws:
IOException

fileModified

public long fileModified(String name)
                  throws IOException
Specified by:
fileModified in class Directory
Throws:
IOException

touchFile

public void touchFile(String name)
               throws IOException
Specified by:
touchFile in class Directory
Throws:
IOException

deleteFile

public void deleteFile(String name)
                throws IOException
Specified by:
deleteFile in class Directory
Throws:
IOException

forceDeleteFile

public void forceDeleteFile(String name)
                     throws IOException
Throws:
IOException

deleteFiles

public List deleteFiles(List names)
                 throws IOException
Description copied from interface: MultiDeleteDirectory
Deletes the given file names. Returns a list of the ones that could not be deleted.

Specified by:
deleteFiles in interface MultiDeleteDirectory
Throws:
IOException

renameFile

public void renameFile(String from,
                       String to)
                throws IOException
Specified by:
renameFile in class Directory
Throws:
IOException

fileLength

public long fileLength(String name)
                throws IOException
Specified by:
fileLength in class Directory
Throws:
IOException

openInput

public IndexInput openInput(String name)
                     throws IOException
Specified by:
openInput in class Directory
Throws:
IOException

createOutput

public IndexOutput createOutput(String name)
                         throws IOException
Specified by:
createOutput in class Directory
Throws:
IOException

makeLock

public Lock makeLock(String name)
Overrides:
makeLock in class Directory

close

public void close()
           throws IOException
Closes the directory.

Specified by:
close in class Directory
Throws:
IOException

getFileEntryHandler

protected FileEntryHandler getFileEntryHandler(String name)

createLock

protected Lock createLock()
                   throws IOException
Throws:
IOException

getDialect

public Dialect getDialect()

getJdbcTemplate

public JdbcTemplate getJdbcTemplate()

getTable

public JdbcTable getTable()

getSettings

public JdbcDirectorySettings getSettings()

getDataSource

public DataSource getDataSource()


Copyright (c) 2004-2008 The Compass Project.