FileDocCategorySizeDatePackage
DbDirectory.javaAPI DocApache Lucene 2.1.07682Wed Feb 14 10:46:08 GMT 2007org.apache.lucene.store.db

DbDirectory

public class DbDirectory extends Directory
A DbDirectory is a Berkeley DB 4.3 based implementation of {@link org.apache.lucene.store.Directory Directory}. It uses two {@link com.sleepycat.db.internal.Db Db} database handles, one for storing file records and another for storing file data blocks.
author
Andi Vajda

Fields Summary
protected Set
openFiles
protected com.sleepycat.db.internal.Db
files
protected com.sleepycat.db.internal.Db
blocks
protected com.sleepycat.db.internal.DbTxn
txn
protected int
flags
Constructors Summary
public DbDirectory(com.sleepycat.db.internal.DbTxn txn, com.sleepycat.db.internal.Db files, com.sleepycat.db.internal.Db blocks, int flags)
Instantiate a DbDirectory. The same threading rules that apply to Berkeley DB handles apply to instances of DbDirectory.

param
txn a transaction handle that is going to be used for all db operations done by this instance. This parameter may be null.
param
files a db handle to store file records.
param
blocks a db handle to store file data blocks.
param
flags flags used for db read operations.


                                                                              

            
    
        super();

        this.txn = txn;
        this.files = files;
        this.blocks = blocks;
        this.flags = flags;
    
public DbDirectory(com.sleepycat.db.Transaction txn, com.sleepycat.db.Database files, com.sleepycat.db.Database blocks, int flags)

        super();

        this.txn = txn != null ? DbHandleExtractor.getDbTxn(txn) : null;
        this.files = DbHandleExtractor.getDb(files);
        this.blocks = DbHandleExtractor.getDb(blocks);
        this.flags = flags;
    
public DbDirectory(com.sleepycat.db.Transaction txn, com.sleepycat.db.Database files, com.sleepycat.db.Database blocks)

        this(txn, files, blocks, 0);
    
Methods Summary
public voidclose()

        flush();
    
public org.apache.lucene.store.IndexOutputcreateOutput(java.lang.String name)

        return new DbIndexOutput(this, name, true);
    
public voiddeleteFile(java.lang.String name)

        new File(name).delete(this);
    
public booleanfileExists(java.lang.String name)

        return new File(name).exists(this);
    
public longfileLength(java.lang.String name)

        File file = new File(name);

        if (file.exists(this))
            return file.getLength();

        throw new IOException("File does not exist: " + name);
    
public longfileModified(java.lang.String name)

        File file = new File(name);

        if (file.exists(this))
            return file.getTimeModified();

        throw new IOException("File does not exist: " + name);
    
public voidflush()
Flush the currently open files. After they have been flushed it is safe to commit the transaction without closing this DbDirectory instance first.

see
setTransaction

        Iterator iterator = openFiles.iterator();
        
        while (iterator.hasNext())
            ((IndexOutput) iterator.next()).flush();
    
public java.lang.String[]list()

        Dbc cursor = null;
        List list = new ArrayList();

        try {
            try {
                DatabaseEntry key = new DatabaseEntry(new byte[0]);
                DatabaseEntry data = new DatabaseEntry((byte[]) null);

                data.setPartial(true);

                cursor = files.cursor(txn, flags);

                if (cursor.get(key, data,
                               DbConstants.DB_SET_RANGE | flags) != DbConstants.DB_NOTFOUND)
                {
                    ByteArrayInputStream buffer =
                        new ByteArrayInputStream(key.getData());
                    DataInputStream in = new DataInputStream(buffer);
                    String name = in.readUTF();
                
                    in.close();
                    list.add(name);

                    while (cursor.get(key, data,
                                      DbConstants.DB_NEXT | flags) != DbConstants.DB_NOTFOUND) {
                        buffer = new ByteArrayInputStream(key.getData());
                        in = new DataInputStream(buffer);
                        name = in.readUTF();
                        in.close();

                        list.add(name);
                    }
                }
            } finally {
                if (cursor != null)
                    cursor.close();
            }
        } catch (DatabaseException e) {
            throw new IOException(e.getMessage());
        }

        return (String[]) list.toArray(new String[list.size()]);
    
public org.apache.lucene.store.LockmakeLock(java.lang.String name)

        return new DbLock();
    
public org.apache.lucene.store.IndexInputopenInput(java.lang.String name)

        return new DbIndexInput(this, name);
    
public voidrenameFile(java.lang.String from, java.lang.String to)

        new File(from).rename(this, to);
    
public voidsetTransaction(com.sleepycat.db.Transaction txn)
Once a transaction handle was committed it is no longer valid. In order to continue using this DbDirectory instance after a commit, the transaction handle has to be replaced.

param
txn the new transaction handle to use

        setTransaction(txn != null ? DbHandleExtractor.getDbTxn(txn) : null);
    
public voidsetTransaction(com.sleepycat.db.internal.DbTxn txn)
Once a transaction handle was committed it is no longer valid. In order to continue using this DbDirectory instance after a commit, the transaction handle has to be replaced.

param
txn the new transaction handle to use

        this.txn = txn;
    
public voidtouchFile(java.lang.String name)

        File file = new File(name);
        long length = 0L;

        if (file.exists(this))
            length = file.getLength();

        file.modify(this, length, System.currentTimeMillis());