com.sleepycat.je.dbi
Class DatabaseImpl

java.lang.Object
  extended by com.sleepycat.je.dbi.DatabaseImpl
All Implemented Interfaces:
Loggable, java.lang.Cloneable

public class DatabaseImpl
extends java.lang.Object
implements Loggable, java.lang.Cloneable

The underlying object for a given database.


Field Summary
static boolean forceTreeWalkForTruncateAndRemove
          For unit testing, setting this field to true will force a walk of the tree to count utilization during truncate/remove, rather than using the per-database info.
 
Constructor Summary
DatabaseImpl()
          Create an empty database object for initialization from the log.
DatabaseImpl(java.lang.String dbName, DatabaseId id, EnvironmentImpl envImpl, DatabaseConfig dbConfig)
          Create a database object for a new database.
 
Method Summary
 void addReferringHandle(Database db)
          Add a referring handle
static java.util.Comparator<byte[]> bytesToComparator(byte[] comparatorBytes, java.lang.String comparatorType)
          Converts an arbitrary string of bytes to a comparator.
 void checkIsDeleted(java.lang.String operation)
           
 void clearKeyPrefixing()
           
 void clearUtilizationRepairDone()
          Is public for unit testing.
 DatabaseImpl cloneDatabase()
          Create a clone of this database that can be used as the new empty database when truncating this database.
 long count()
          Count entries in the database including dups, but don't dirty the cache.
 void countObsoleteDb(BaseUtilizationTracker tracker, long mapLnLsn)
          Counts all active LSNs in a database as obsolete.
 void dumpLog(java.lang.StringBuffer sb, boolean verbose)
          Write the object into the string buffer for log dumping.
 java.lang.String dumpString(int nSpaces)
           
 Database findPrimaryDatabase()
          For this secondary database return the primary that it is associated with, or null if not associated with any primary.
 void finishDeleteProcessing()
          Release the INs for the deleted database, count all log entries for this database as obsolete, delete the MapLN, and set the state to DELETED.
 int getAdditionalTreeMemorySize()
          Returns the tree memory size that should be added to MAPLN_OVERHEAD.
 int getBinDeltaPercent()
           
 int getBinMaxDeltas()
           
 java.util.Comparator<byte[]> getBtreeComparator()
           
 boolean getBtreeComparatorByClass()
           
 EnvironmentImpl getDbEnvironment()
           
 DbFileSummaryMap getDbFileSummaries()
          For unit testing.
 DbFileSummary getDbFileSummary(java.lang.Long fileNum, boolean willModify)
          Returns the DbFileSummary for the given file, allocates it if necessary and budgeted memory for any changes.
 java.lang.String getDebugName()
           
 java.util.Comparator<byte[]> getDuplicateComparator()
           
 boolean getDuplicateComparatorByClass()
           
 DatabaseStats getEmptyStats()
           
 long getEofNodeId()
           
 DatabaseId getId()
           
 boolean getKeyPrefixing()
           
 int getLogSize()
          This log entry type is configured to perform marshaling (getLogSize and writeToLog) under the write log mutex.
 java.lang.String getName()
           
 int getNodeMaxDupTreeEntries()
           
 int getNodeMaxEntries()
           
 ReplicationContext getRepContext()
          Return a ReplicationContext that will indicate if this operation should broadcast data records for this database as part the replication stream.
 boolean getSortedDuplicates()
           
static boolean getSortedDuplicates(byte flagVal)
           
 long getTransactionId()
           
 Tree getTree()
           
 long getTreeAdminMemory()
          Figure out how much memory is used by the DbFileSummaryMap.
 boolean getUtilizationRepairDone()
          Is public for unit testing.
 void handleClosed(boolean doSyncDw)
          Called after a handle onto this DB is closed.
 boolean hasOpenHandles()
          Returns whether one or more handles are open.
static java.util.Comparator<byte[]> instantiateComparator(java.lang.Class<? extends java.util.Comparator<byte[]>> comparatorClass, java.lang.String comparatorType)
          Used both to read from the log and to validate a comparator when set in DatabaseConfig.
static java.util.Comparator<byte[]> instantiateComparator(java.util.Comparator<byte[]> comparator, java.lang.String comparatorType)
          Used to validate a comparator when set in DatabaseConfig.
 boolean isCheckpointNeeded()
          Returns whether this database's MapLN must be flushed during a checkpoint.
 boolean isDeferredWriteMode()
           
 boolean isDeleted()
           
 boolean isDeleteFinished()
           
 boolean isDirtyUtilization()
          Returns whether this database has new (unflushed) utilization info.
 boolean isDurableDeferredWrite()
           
 boolean isInUse()
          Returns whether this DB is in use and cannot be evicted.
 boolean isReplicated()
           
 boolean isTemporary()
           
static boolean isTemporary(byte flagVal)
           
 boolean isTransactional()
           
 boolean logicalEquals(Loggable other)
           
static byte[] objectToBytes(java.lang.Object obj, java.lang.String comparatorType)
          Converts an arbitrary object to a serialized byte array.
 PreloadStats preload(PreloadConfig config)
          Preload the cache, using up to maxBytes bytes or maxMillsecs msec.
 void readFromLog(java.nio.ByteBuffer itemBuffer, byte entryVersion)
          Initialize this object from the data in itemBuf.
 void releaseTreeAdminMemory()
          Update memory budgets when this databaseImpl is closed and will never be accessed again or when it is still open when its owning MapLN will be garbage collected, due to eviction or recovery.
 boolean removeDbFileSummary(java.lang.Long fileNum)
          Removes the DbFileSummary for the given file.
 void removeReferringHandle(Database db)
          Decrement the reference count.
 boolean setBtreeComparator(java.util.Comparator<byte[]> comparator, boolean byClassName)
          Set the btree comparison function for this database.
 void setDebugDatabaseName(java.lang.String debugName)
           
 void setDeferredWrite(boolean durableDeferredWrite)
          Sets the deferred write property for the first opened handle.
 void setDirtyUtilization()
          Sets utilization dirty in order to force the MapLN to be flushed later.
 boolean setDuplicateComparator(java.util.Comparator<byte[]> comparator, boolean byClassName)
          Set the duplicate comparison function for this database.
 void setEnvironmentImpl(EnvironmentImpl envImpl)
          Set the db environment after reading in the DatabaseImpl from the log.
 void setKeyPrefixing()
           
 void setPendingDeletedHook(TestHook hook)
           
 void setSortedDuplicates()
           
 void setTransactional(boolean transactional)
          Sets the transactional property for the first opened handle.
 void startAndFinishDelete()
          Perform the entire two-step database deletion.
 void startDeleteProcessing()
          The delete cleanup is starting.
 DatabaseStats stat(StatsConfig config)
           
 void sync(boolean flushLog)
          Flush all dirty nodes for this database to disk.
 boolean unknownReplicated()
           
 boolean verify(VerifyConfig config, DatabaseStats emptyStats)
           
 void writeToLog(java.nio.ByteBuffer logBuffer)
          Serialize this object into the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

forceTreeWalkForTruncateAndRemove

public static boolean forceTreeWalkForTruncateAndRemove
For unit testing, setting this field to true will force a walk of the tree to count utilization during truncate/remove, rather than using the per-database info. This is used to test the "old technique" for counting utilization, which is now used only if the database was created prior to log version 6.

Constructor Detail

DatabaseImpl

public DatabaseImpl(java.lang.String dbName,
                    DatabaseId id,
                    EnvironmentImpl envImpl,
                    DatabaseConfig dbConfig)
             throws DatabaseException
Create a database object for a new database.

Throws:
DatabaseException

DatabaseImpl

public DatabaseImpl()
             throws DatabaseException
Create an empty database object for initialization from the log. Note that the rest of the initialization comes from readFromLog(), except for the debugDatabaseName, which is set by the caller.

Throws:
DatabaseException
Method Detail

setDebugDatabaseName

public void setDebugDatabaseName(java.lang.String debugName)

getDebugName

public java.lang.String getDebugName()

setPendingDeletedHook

public void setPendingDeletedHook(TestHook hook)

cloneDatabase

public DatabaseImpl cloneDatabase()
Create a clone of this database that can be used as the new empty database when truncating this database. setId and setTree must be called on the returned database.


getTree

public Tree getTree()
Returns:
the database tree.

getId

public DatabaseId getId()
Returns:
the database id.

getEofNodeId

public long getEofNodeId()

isTransactional

public boolean isTransactional()
Returns:
true if this database is transactional.

setTransactional

public void setTransactional(boolean transactional)
Sets the transactional property for the first opened handle.


isTemporary

public boolean isTemporary()
Returns:
true if this database is temporary.

isTemporary

public static boolean isTemporary(byte flagVal)

isDurableDeferredWrite

public boolean isDurableDeferredWrite()
Returns:
true if this database was user configured for durable deferred write mode.

isDeferredWriteMode

public boolean isDeferredWriteMode()
Returns:
true if write operations are not logged immediately. This is true if the user configured a durable DW database or a temporary database.

setDeferredWrite

public void setDeferredWrite(boolean durableDeferredWrite)
Sets the deferred write property for the first opened handle.


getSortedDuplicates

public boolean getSortedDuplicates()
Returns:
true if duplicates are allowed in this database.

getSortedDuplicates

public static boolean getSortedDuplicates(byte flagVal)

setSortedDuplicates

public void setSortedDuplicates()

getKeyPrefixing

public boolean getKeyPrefixing()
Returns:
true if key prefixing is enabled in this database.

setKeyPrefixing

public void setKeyPrefixing()

clearKeyPrefixing

public void clearKeyPrefixing()

isReplicated

public boolean isReplicated()
Returns:
true if this database is replicated. Note that we only need to check the IS_REPLICATED_BIT, because we require that we never have both IS_REPLICATED and NOT_REPLICATED set at the same time.

unknownReplicated

public boolean unknownReplicated()
Returns:
true if this database is replicated.

getUtilizationRepairDone

public boolean getUtilizationRepairDone()
Is public for unit testing.


clearUtilizationRepairDone

public void clearUtilizationRepairDone()
Is public for unit testing.


getNodeMaxEntries

public int getNodeMaxEntries()

getNodeMaxDupTreeEntries

public int getNodeMaxDupTreeEntries()

getAdditionalTreeMemorySize

public int getAdditionalTreeMemorySize()
Returns the tree memory size that should be added to MAPLN_OVERHEAD. This is a start at budgeting per-Database memory. For future reference, other things that could be budgeted are: - debugDatabaseName as it is set - Database handles as they are added/removed in referringHandles


setDuplicateComparator

public boolean setDuplicateComparator(java.util.Comparator<byte[]> comparator,
                                      boolean byClassName)
                               throws DatabaseException
Set the duplicate comparison function for this database.

Parameters:
duplicateComparator - - The Duplicate Comparison function.
Returns:
true if the comparator was actually changed
Throws:
DatabaseException

setBtreeComparator

public boolean setBtreeComparator(java.util.Comparator<byte[]> comparator,
                                  boolean byClassName)
                           throws DatabaseException
Set the btree comparison function for this database.

Parameters:
btreeComparator - - The btree Comparison function.
Returns:
true if the comparator was actually changed
Throws:
DatabaseException

getBtreeComparator

public java.util.Comparator<byte[]> getBtreeComparator()
Returns:
the btree Comparator object.

getDuplicateComparator

public java.util.Comparator<byte[]> getDuplicateComparator()
Returns:
the duplicate Comparator object.

getBtreeComparatorByClass

public boolean getBtreeComparatorByClass()
Returns:
whether Comparator is set by class name, not by serializable Comparator object.

getDuplicateComparatorByClass

public boolean getDuplicateComparatorByClass()
Returns:
whether Comparator is set by class name, not by serializable Comparator object.

setEnvironmentImpl

public void setEnvironmentImpl(EnvironmentImpl envImpl)
                        throws DatabaseException
Set the db environment after reading in the DatabaseImpl from the log.

Throws:
DatabaseException

getDbEnvironment

public EnvironmentImpl getDbEnvironment()
Returns:
the database environment.

hasOpenHandles

public boolean hasOpenHandles()
Returns whether one or more handles are open.


addReferringHandle

public void addReferringHandle(Database db)
Add a referring handle


removeReferringHandle

public void removeReferringHandle(Database db)
Decrement the reference count.


handleClosed

public void handleClosed(boolean doSyncDw)
                  throws DatabaseException
Called after a handle onto this DB is closed.

Throws:
DatabaseException

getTreeAdminMemory

public long getTreeAdminMemory()
Figure out how much memory is used by the DbFileSummaryMap. Usually this number is built up over time by the DbFileSummaryMap itself and added to the memory budget, but in this case we need to reinitialize it after recovery, when DbFileSummaryMaps may be cut adrift by the process of overlaying new portions of the btree.


releaseTreeAdminMemory

public void releaseTreeAdminMemory()
Update memory budgets when this databaseImpl is closed and will never be accessed again or when it is still open when its owning MapLN will be garbage collected, due to eviction or recovery.


isInUse

public boolean isInUse()
Returns whether this DB is in use and cannot be evicted. Called by MapLN.isEvictable while holding a write-lock on the MapLN and a latch on its parent BIN. [#13415] When isInUse returns false (while holding a write-lock on the MapLN and a latch on the parent BIN), it guarantees that the database object is not in use and cannot be acquired by another thread (via DbTree.createDb/getDb) until both the MapLN lock and BIN latch are released. This guarantee is due to the fact that DbTree.createDb/getDb only increment the use count while holding a read-lock on the MapLN. Therefore, it is safe to evict the MapLN when isInUse returns false. When isInUse returns true, it is possible that another thread may decrement the use count at any time, since no locking or latching is performed when calling DbTree.releaseDb (which calls decrementUseCount). Therefore, it is not guaranteed that the MapLN is in use when isInUse returns true. A true result means: the DB may be in use, so it is not safe to evict it.


sync

public void sync(boolean flushLog)
          throws DatabaseException
Flush all dirty nodes for this database to disk.

Throws:
DatabaseException

findPrimaryDatabase

public Database findPrimaryDatabase()
                             throws DatabaseException
For this secondary database return the primary that it is associated with, or null if not associated with any primary. Note that not all handles need be associated with a primary.

Throws:
DatabaseException

getName

public java.lang.String getName()
                         throws DatabaseException
Throws:
DatabaseException

getDbFileSummary

public DbFileSummary getDbFileSummary(java.lang.Long fileNum,
                                      boolean willModify)
Returns the DbFileSummary for the given file, allocates it if necessary and budgeted memory for any changes.

Must be called under the log write latch.

Parameters:
willModify - if true, the caller will modify the utilization info.

removeDbFileSummary

public boolean removeDbFileSummary(java.lang.Long fileNum)
Removes the DbFileSummary for the given file.

Must be called under the log write latch.

Returns:
whether a DbFileSummary for the given file was present and was removed.

getDbFileSummaries

public DbFileSummaryMap getDbFileSummaries()
For unit testing.


isDirtyUtilization

public boolean isDirtyUtilization()
Returns whether this database has new (unflushed) utilization info.


setDirtyUtilization

public void setDirtyUtilization()
Sets utilization dirty in order to force the MapLN to be flushed later.


isCheckpointNeeded

public boolean isCheckpointNeeded()
Returns whether this database's MapLN must be flushed during a checkpoint.


isDeleted

public boolean isDeleted()
Returns:
true if this database is deleted. Delete cleanup may still be in progress.

isDeleteFinished

public boolean isDeleteFinished()
Returns:
true if this database is deleted and all cleanup is finished.

startDeleteProcessing

public void startDeleteProcessing()
The delete cleanup is starting. Set this before releasing any write locks held for a db operation.


startAndFinishDelete

public void startAndFinishDelete()
                          throws DatabaseException
Perform the entire two-step database deletion. This method is used at non-transactional operation end. When a transaction is used (see Txn), startDeleteProcessing is called at commit before releasing write locks and finishDeleteProcessing is called after releasing write locks.

Throws:
DatabaseException

finishDeleteProcessing

public void finishDeleteProcessing()
                            throws DatabaseException
Release the INs for the deleted database, count all log entries for this database as obsolete, delete the MapLN, and set the state to DELETED. Used at transaction end or non-transactional operation end in these cases: - purge the deleted database after a commit of Environment.removeDatabase - purge the deleted database after a commit of Environment.truncateDatabase - purge the newly created database after an abort of Environment.truncateDatabase Note that the processing of the naming tree means the MapLN is never actually accessible from the current tree, but deleting the MapLN will do two things: (a) mark it properly obsolete (b) null out the database tree, leaving the INList the only reference to the INs.

Throws:
DatabaseException

checkIsDeleted

public void checkIsDeleted(java.lang.String operation)
                    throws DatabaseException
Throws:
DatabaseException

countObsoleteDb

public void countObsoleteDb(BaseUtilizationTracker tracker,
                            long mapLnLsn)
Counts all active LSNs in a database as obsolete.

Parameters:
mapLnLsn - is the LSN of the MapLN when called via recovery, otherwise is NULL_LSN.

Must be called under the log write latch or during recovery.


stat

public DatabaseStats stat(StatsConfig config)
                   throws DatabaseException
Throws:
DatabaseException

verify

public boolean verify(VerifyConfig config,
                      DatabaseStats emptyStats)
               throws DatabaseException
Throws:
DatabaseException

getEmptyStats

public DatabaseStats getEmptyStats()

preload

public PreloadStats preload(PreloadConfig config)
                     throws DatabaseException
Preload the cache, using up to maxBytes bytes or maxMillsecs msec.

Throws:
DatabaseException

count

public long count()
           throws DatabaseException
Count entries in the database including dups, but don't dirty the cache.

Throws:
DatabaseException

dumpString

public java.lang.String dumpString(int nSpaces)

getLogSize

public int getLogSize()
This log entry type is configured to perform marshaling (getLogSize and writeToLog) under the write log mutex. Otherwise, the size could change in between calls to these two methods as the result of utilizaton tracking.

Specified by:
getLogSize in interface Loggable
Returns:
number of bytes used to store this object.
See Also:
Loggable.getLogSize()

writeToLog

public void writeToLog(java.nio.ByteBuffer logBuffer)
Description copied from interface: Loggable
Serialize this object into the buffer.

Specified by:
writeToLog in interface Loggable
Parameters:
logBuffer - is the destination buffer
See Also:
Loggable.writeToLog(java.nio.ByteBuffer)

readFromLog

public void readFromLog(java.nio.ByteBuffer itemBuffer,
                        byte entryVersion)
                 throws LogException
Description copied from interface: Loggable
Initialize this object from the data in itemBuf.

Specified by:
readFromLog in interface Loggable
Throws:
LogException
See Also:
Loggable.readFromLog(java.nio.ByteBuffer, byte)

dumpLog

public void dumpLog(java.lang.StringBuffer sb,
                    boolean verbose)
Description copied from interface: Loggable
Write the object into the string buffer for log dumping. Each object should be dumped without indentation or new lines and should be valid XML.

Specified by:
dumpLog in interface Loggable
Parameters:
sb - destination string buffer
verbose - if true, dump the full, verbose version
See Also:
Loggable.dumpLog(java.lang.StringBuffer, boolean)

getTransactionId

public long getTransactionId()
Specified by:
getTransactionId in interface Loggable
Returns:
the transaction id embedded within this loggable object. Objects that have no transaction id should return 0.
See Also:
Loggable.getTransactionId()

logicalEquals

public boolean logicalEquals(Loggable other)
Specified by:
logicalEquals in interface Loggable
Returns:
true if these two loggable items are logically the same. Used for replication testing.
See Also:
Always return false, this item should never be compared.

instantiateComparator

public static java.util.Comparator<byte[]> instantiateComparator(java.lang.Class<? extends java.util.Comparator<byte[]>> comparatorClass,
                                                                 java.lang.String comparatorType)
                                                          throws LogException
Used both to read from the log and to validate a comparator when set in DatabaseConfig.

Throws:
LogException

instantiateComparator

public static java.util.Comparator<byte[]> instantiateComparator(java.util.Comparator<byte[]> comparator,
                                                                 java.lang.String comparatorType)
                                                          throws DatabaseException
Used to validate a comparator when set in DatabaseConfig.

Throws:
DatabaseException

objectToBytes

public static byte[] objectToBytes(java.lang.Object obj,
                                   java.lang.String comparatorType)
                            throws LogException
Converts an arbitrary object to a serialized byte array. Assumes that the object given is non-null.

Throws:
LogException

bytesToComparator

public static java.util.Comparator<byte[]> bytesToComparator(byte[] comparatorBytes,
                                                             java.lang.String comparatorType)
                                                      throws LogException,
                                                             java.lang.ClassNotFoundException
Converts an arbitrary string of bytes to a comparator. Used to instantiate a comparator on a client node.

Throws:
LogException
java.lang.ClassNotFoundException

getBinDeltaPercent

public int getBinDeltaPercent()

getBinMaxDeltas

public int getBinMaxDeltas()

getRepContext

public ReplicationContext getRepContext()
Return a ReplicationContext that will indicate if this operation should broadcast data records for this database as part the replication stream.