/*- * See the file LICENSE for redistribution information. * * Copyright (c) 2002, 2010 Oracle and/or its affiliates. All rights reserved. * * $Id$ */ package com.sleepycat.db; import com.sleepycat.db.internal.Db; import com.sleepycat.db.internal.DbConstants; /** A secondary database handle.

Secondary databases are opened with {@link Environment#openSecondaryDatabase Environment.openSecondaryDatabase} and are always associated with a single primary database. The distinguishing characteristics of a secondary database are:

Before opening or creating a secondary database you must implement the {@link SecondaryKeyCreator} interface.

For example, to create a secondary database that supports duplicates:

    Database primaryDb; // The primary database must already be open.
    SecondaryKeyCreator keyCreator; // Your key creator implementation.
    SecondaryConfig secConfig = new SecondaryConfig();
    secConfig.setAllowCreate(true);
    secConfig.setSortedDuplicates(true);
    secConfig.setKeyCreator(keyCreator);
    SecondaryDatabase newDb = env.openSecondaryDatabase(transaction,
                                                        "myDatabaseName",
                                                        primaryDb,
                                                        secConfig)

If a primary database is to be associated with one or more secondary databases, it may not be configured for duplicates.

Note that the associations between primary and secondary databases are not stored persistently. Whenever a primary database is opened for write access by the application, the appropriate associated secondary databases should also be opened by the application. This is necessary to ensure data integrity when changes are made to the primary database. */ public class SecondaryDatabase extends Database { private final Database primaryDatabase; /* package */ SecondaryDatabase(final Db db, final Database primaryDatabase) throws DatabaseException { super(db); this.primaryDatabase = primaryDatabase; } /** Open a database.

The database is represented by the file and database parameters.

The currently supported database file formats (or access methods) are Btree, Hash, Queue, and Recno. The Btree format is a representation of a sorted, balanced tree structure. The Hash format is an extensible, dynamic hashing scheme. The Queue format supports fast access to fixed-length records accessed sequentially or by logical record number. The Recno format supports fixed- or variable-length records, accessed sequentially or by logical record number, and optionally backed by a flat text file.

Storage and retrieval are based on key/data pairs; see {@link com.sleepycat.db.DatabaseEntry DatabaseEntry} for more information.

Opening a database is a relatively expensive operation, and maintaining a set of open databases will normally be preferable to repeatedly opening and closing the database for each new query.

In-memory databases never intended to be preserved on disk may be created by setting both the fileName and databaseName parameters to null. Note that in-memory databases can only ever be shared by sharing the single database handle that created them, in circumstances where doing so is safe. The environment variable TMPDIR may be used as a directory in which to create temporary backing files.

@param fileName The name of an underlying file that will be used to back the database. On Windows platforms, this argument will be interpreted as a UTF-8 string, which is equivalent to ASCII for Latin characters.

@param databaseName An optional parameter that allows applications to have multiple databases in a single file. Although no databaseName parameter needs to be specified, it is an error to attempt to open a second database in a physical file that was not initially created using a databaseName parameter. Further, the databaseName parameter is not supported by the Queue format.

@param primaryDatabase a database handle for the primary database that is to be indexed.

@param config The secondary database open attributes. If null, default attributes are used. */ public SecondaryDatabase(final String fileName, final String databaseName, final Database primaryDatabase, final SecondaryConfig config) throws DatabaseException, java.io.FileNotFoundException { this(SecondaryConfig.checkNull(config).openSecondaryDatabase( null, null, fileName, databaseName, primaryDatabase.db), primaryDatabase); } /** {@inheritDoc} */ public Cursor openCursor(final Transaction txn, final CursorConfig config) throws DatabaseException { return openSecondaryCursor(txn, config); } /** Obtain a cursor on a database, returning a SecondaryCursor. Calling this method is the equivalent of calling {@link #openCursor} and casting the result to {@link SecondaryCursor}.

@param txn To use a cursor for writing to a transactional database, an explicit transaction must be specified. For read-only access to a transactional database, the transaction may be null. For a non-transactional database, the transaction must be null.

To transaction-protect cursor operations, cursors must be opened and closed within the context of a transaction, and the txn parameter specifies the transaction context in which the cursor will be used.

@param config The cursor attributes. If null, default attributes are used.

@return A secondary database cursor.

@throws DatabaseException if a failure occurs. */ public SecondaryCursor openSecondaryCursor(final Transaction txn, final CursorConfig config) throws DatabaseException { return new SecondaryCursor(this, CursorConfig.checkNull(config).openCursor(db, (txn == null) ? null : txn.txn), config); } /** Returns the primary database associated with this secondary database.

@return the primary database associated with this secondary database. */ public Database getPrimaryDatabase() { return primaryDatabase; } /** {@inheritDoc} */ public DatabaseConfig getConfig() throws DatabaseException { return getSecondaryConfig(); } /** Returns a copy of the secondary configuration of this database.

@return a copy of the secondary configuration of this database.

@throws DatabaseException if a failure occurs. */ public SecondaryConfig getSecondaryConfig() throws DatabaseException { return new SecondaryConfig(db); } /** Retrieves the key/data pair with the given key. If the matching key has duplicate values, the first data item in the set of duplicates is returned. Retrieval of duplicates requires the use of {@link Cursor} operations.

@param txn For a transactional database, an explicit transaction may be specified to transaction-protect the operation, or null may be specified to perform the operation without transaction protection. For a non-transactional database, null must be specified.

@param key the secondary key used as input. It must be initialized with a non-null byte array by the caller.

@param pKey the primary key returned as output. Its byte array does not need to be initialized by the caller.

@param data the primary data returned as output. Its byte array does not need to be initialized by the caller.

@param lockMode the locking attributes; if null, default attributes are used.

@return {@link com.sleepycat.db.OperationStatus#NOTFOUND OperationStatus.NOTFOUND} if no matching key/data pair is found; {@link com.sleepycat.db.OperationStatus#KEYEMPTY OperationStatus.KEYEMPTY} if the database is a Queue or Recno database and the specified key exists, but was never explicitly created by the application or was later deleted; otherwise, {@link com.sleepycat.db.OperationStatus#SUCCESS OperationStatus.SUCCESS}.

@throws DeadlockException if the operation was selected to resolve a deadlock.

@throws IllegalArgumentException if an invalid parameter was specified.

@throws DatabaseException if a failure occurs. */ public OperationStatus get(final Transaction txn, final DatabaseEntry key, final DatabaseEntry pKey, final DatabaseEntry data, final LockMode lockMode) throws DatabaseException { return OperationStatus.fromInt( db.pget((txn == null) ? null : txn.txn, key, pKey, data, LockMode.getFlag(lockMode) | ((data == null) ? 0 : data.getMultiFlag()))); } /** Retrieves the key/data pair with the specified secondary and primary key, that is, both the primary and secondary key items must match.

@param txn For a transactional database, an explicit transaction may be specified to transaction-protect the operation, or null may be specified to perform the operation without transaction protection. For a non-transactional database, null must be specified. @param key the secondary key used as input. It must be initialized with a non-null byte array by the caller. @param pKey the primary key used as input. It must be initialized with a non-null byte array by the caller. @param data the primary data returned as output. Its byte array does not need to be initialized by the caller.

@param lockMode the locking attributes; if null, default attributes are used.

@return {@link com.sleepycat.db.OperationStatus#NOTFOUND OperationStatus.NOTFOUND} if no matching key/data pair is found; {@link com.sleepycat.db.OperationStatus#KEYEMPTY OperationStatus.KEYEMPTY} if the database is a Queue or Recno database and the specified key exists, but was never explicitly created by the application or was later deleted; otherwise, {@link com.sleepycat.db.OperationStatus#SUCCESS OperationStatus.SUCCESS}.

@throws DeadlockException if the operation was selected to resolve a deadlock.

@throws IllegalArgumentException if an invalid parameter was specified.

@throws DatabaseException if a failure occurs. */ public OperationStatus getSearchBoth(final Transaction txn, final DatabaseEntry key, final DatabaseEntry pKey, final DatabaseEntry data, final LockMode lockMode) throws DatabaseException { return OperationStatus.fromInt( db.pget((txn == null) ? null : txn.txn, key, pKey, data, DbConstants.DB_GET_BOTH | LockMode.getFlag(lockMode) | ((data == null) ? 0 : data.getMultiFlag()))); } /** Retrieves the key/data pair associated with the specific numbered record of the database.

The data field of the specified key must be a byte array containing a record number, as described in {@link com.sleepycat.db.DatabaseEntry DatabaseEntry}. This determines the record to be retrieved.

For this method to be called, the underlying database must be of type Btree, and it must have been configured to support record numbers.

If this method fails for any reason, the position of the cursor will be unchanged. @throws NullPointerException if a DatabaseEntry parameter is null or does not contain a required non-null byte array.

@throws DeadlockException if the operation was selected to resolve a deadlock.

@throws IllegalArgumentException if an invalid parameter was specified.

@throws DatabaseException if a failure occurs.

@param key the secondary key returned as output. Its byte array does not need to be initialized by the caller. @param pKey the primary key returned as output. Its byte array does not need to be initialized by the caller. @param data the primary data returned as output. Multiple results can be retrieved by passing an object that is a subclass of {@link com.sleepycat.db.MultipleEntry MultipleEntry}, otherwise its byte array does not need to be initialized by the caller. @param lockMode the locking attributes; if null, default attributes are used. @return {@link com.sleepycat.db.OperationStatus#NOTFOUND OperationStatus.NOTFOUND} if no matching key/data pair is found; {@link com.sleepycat.db.OperationStatus#KEYEMPTY OperationStatus.KEYEMPTY} if the database is a Queue or Recno database and the specified key exists, but was never explicitly created by the application or was later deleted; otherwise, {@link com.sleepycat.db.OperationStatus#SUCCESS OperationStatus.SUCCESS}. */ public OperationStatus getSearchRecordNumber(final Transaction txn, final DatabaseEntry key, final DatabaseEntry pKey, final DatabaseEntry data, final LockMode lockMode) throws DatabaseException { return OperationStatus.fromInt( db.pget((txn == null) ? null : txn.txn, key, pKey, data, DbConstants.DB_SET_RECNO | LockMode.getFlag(lockMode) | ((data == null) ? 0 : data.getMultiFlag()))); } }