Berkeley DB
version 5.3.21

com.sleepycat.db
Class DatabaseEntry

java.lang.Object
  extended by com.sleepycat.db.DatabaseEntry
Direct Known Subclasses:
MultipleEntry

public class DatabaseEntry
extends Object

Encodes database key and data items as a byte array.

Storage and retrieval for the Database and Cursor methods are based on key/data pairs. Both key and data items are represented by DatabaseEntry objects. Key and data byte arrays may refer to arrays of zero length up to arrays of essentially unlimited length.

The DatabaseEntry class provides simple access to an underlying object whose elements can be examined or changed. DatabaseEntry objects can be subclassed, providing a way to associate with it additional data or references to other structures.

Access to DatabaseEntry objects is not re-entrant. In particular, if multiple threads simultaneously access the same DatabaseEntry object using Database or Cursor methods, the results are undefined.

DatabaseEntry objects may be used in conjunction with the object mapping support provided in the com.sleepycat.bind package.

Input and Output Parameters

DatabaseEntry objects are used for both input data (when writing to a database or specifying a search parameter) and output data (when reading from a database). For certain methods, one parameter may be an input parameter and another may be an output parameter. For example, the Database.get(com.sleepycat.db.Transaction, com.sleepycat.db.DatabaseEntry, com.sleepycat.db.DatabaseEntry, com.sleepycat.db.LockMode) method has an input key parameter and an output data parameter. The documentation for each method describes whether its parameters are input or output parameters.

For DatabaseEntry input parameters, the caller is responsible for initializing the data array of the DatabaseEntry. For DatabaseEntry output parameters, the method called will initialize the data array.

For DatabaseEntry output parameters, by default the method called will reuse the byte array in the DatabaseEntry, if the data returned fits in the byte array. This behavior can be configured with setReuseBuffer(boolean) or setUserBuffer(int, boolean). If an entry is configured to reuse the byte array (the default behavior), the length of the underlying byte array should not be used to determine the amount of data returned each time the entry is used as an output parameter, rather the getSize() call should be used. If an entry is configured to not reuse the byte array, a new array is allocated each time the entry is used as an output parameter, so the application can safely keep a reference to the byte array returned by getData() without danger that the array will be overwritten in a subsequent call.

Offset and Size Properties

By default the Offset property is zero and the Size property is the length of the byte array. However, to allow for optimizations involving the partial use of a byte array, the Offset and Size may be set to non-default values.

For DatabaseEntry output parameters, the Size will always be set to the length of the returned data and the Offset will always be set to zero.

However, for DatabaseEntry input parameters the Offset and Size are set to non-default values by the built-in tuple and serial bindings. For example, with a tuple or serial binding the byte array is grown dynamically as data is output, and the Size is set to the number of bytes actually used. For a serial binding, the Offset is set to a non-zero value in order to implement an optimization having to do with the serialization stream header.

Therefore, for output DatabaseEntry parameters the application can assume that the Offset is zero and the Size is the length of the byte array. However, for input DatabaseEntry parameters the application should not make this assumption. In general, it is safest for the application to always honor the Size and Offset properties, rather than assuming they have default values.

Partial Offset and Length Properties

By default the specified data (byte array, offset and size) corresponds to the full stored key or data item. Optionally, the Partial property can be set to true, and the PartialOffset and PartialLength properties are used to specify the portion of the key or data item to be read or written. For details, see the setPartial(int,int,boolean) method.

Note that the Partial properties are set only by the caller. They will never be set by a Database or Cursor method, nor will they every be set by bindings. Therefore, the application can assume that the Partial properties are not set, unless the application itself sets them explicitly.


Constructor Summary
DatabaseEntry()
          Construct a DatabaseEntry with null data.
DatabaseEntry(byte[] data)
          Construct a DatabaseEntry with a given byte array.
DatabaseEntry(byte[] data, int offset, int size)
          Constructs a DatabaseEntry with a given byte array, offset and size.
DatabaseEntry(ByteBuffer data)
          Construct a DatabaseEntry with a given native I/O buffer.
 
Method Summary
 boolean equals(Object o)
          Compares the data of two entries for byte-by-byte equality.
 byte[] getData()
          Return the byte array.
 ByteBuffer getDataNIO()
          Return the java.nio.ByteBuffer.
 int getOffset()
          Return the byte offset into the data array.
 boolean getPartial()
          Return whether this DatabaseEntry is configured to read or write partial records.
 int getPartialLength()
          Return the byte length of the partial record being read or written by the application, in bytes.
 int getPartialOffset()
          Return the offset of the partial record being read or written by the application, in bytes.
 boolean getReadOnly()
          Return whether this DatabaseEntry is configured as read only.
 int getRecordNumber()
          Return the record number encoded in this entry's buffer.
 boolean getReuseBuffer()
          Return true if the whether the entry is configured to reuse the buffer.
 int getSize()
          Return the byte size of the data array.
 boolean getUserBuffer()
          Return true if the whether the buffer in this entry is owned by the application.
 int getUserBufferLength()
          Return the length of the application's buffer.
 int hashCode()
          Returns a hash code based on the data value.
 void setData(byte[] data)
          Sets the byte array.
 void setData(byte[] data, int offset, int size)
          Sets the byte array, offset and size.
 void setDataNIO(ByteBuffer data)
          Sets the java.nio.ByteBuffer.
 void setDataNIO(ByteBuffer data, int offset, int size)
          Sets the java.nio.ByteBuffer (or the backing array if passed a non-direct ByteBuffer,) offset and size.
 void setOffset(int offset)
          Set the byte offset into the data array.
 void setPartial(boolean partial)
          Configure this DatabaseEntry to read or write partial records.
 void setPartial(int doff, int dlen, boolean partial)
          Configures this DatabaseEntry to read or write partial records.
 void setPartialLength(int dlen)
          Set the byte length of the partial record being read or written by the application, in bytes.
 void setPartialOffset(int doff)
          Set the offset of the partial record being read or written by the application, in bytes.
 void setReadOnly(boolean readonly)
          Configure this DatabaseEntry as read only.
 void setRecordNumber(int recno)
          Initialize the entry from a logical record number.
 void setReuseBuffer(boolean reuse)
          Configures the entry to try to reuse the buffer before allocating a new one.
 void setSize(int size)
          Set the byte size of the data array.
 void setUserBuffer(int length, boolean usermem)
          Configures the entry with an application-owned buffer.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseEntry

public DatabaseEntry()
Construct a DatabaseEntry with null data. The offset and size are set to zero.


DatabaseEntry

public DatabaseEntry(byte[] data)
Construct a DatabaseEntry with a given byte array. The offset is set to zero; the size is set to the length of the array, or to zero if null is passed.

Parameters:
data - Byte array wrapped by the DatabaseEntry.

DatabaseEntry

public DatabaseEntry(byte[] data,
                     int offset,
                     int size)
Constructs a DatabaseEntry with a given byte array, offset and size.

Parameters:
data - Byte array wrapped by the DatabaseEntry.
offset - Offset in the first byte in the byte array to be included.
size - Number of bytes in the byte array to be included.

DatabaseEntry

public DatabaseEntry(ByteBuffer data)
Construct a DatabaseEntry with a given native I/O buffer. If the buffer is non-direct, the buffer's backing array is used. If the buffer is direct, the DatabaseEntry is configured with an application-owned buffer whose length is set to the ByteBuffer's capacity less its position. The DatabaseEntry's size is set to the ByteBuffer's limit less its position and the offset is set to buffer's position (adjusted by arrayOffset if the buffer is non-direct.)

Parameters:
data - NIO byte buffer wrapped by the DatabaseEntry.
Method Detail

getData

public byte[] getData()
Return the byte array.

For a DatabaseEntry that is used as an output parameter, the byte array will always be a newly allocated array. The byte array specified by the caller will not be used and may be null.

Returns:
The byte array.

getDataNIO

public ByteBuffer getDataNIO()
Return the java.nio.ByteBuffer.

Used to access the underlying data when the DatabaseEntry is configured to utilize a java.nio.ByteBuffer.

Returns:
The underlying java.nio.ByteBuffer.

setData

public void setData(byte[] data,
                    int offset,
                    int size)
Sets the byte array, offset and size.

Parameters:
data - Byte array wrapped by the DatabaseEntry.
offset - Offset in the first byte in the byte array to be included.
size - Number of bytes in the byte array to be included.

setData

public void setData(byte[] data)
Sets the byte array. The offset is set to zero; the size is set to the length of the array, or to zero if null is passed.

Parameters:
data - Byte array wrapped by the DatabaseEntry.

setDataNIO

public void setDataNIO(ByteBuffer data,
                       int offset,
                       int size)
Sets the java.nio.ByteBuffer (or the backing array if passed a non-direct ByteBuffer,) offset and size. If passed a direct ByteBuffer, the entry is configured with an application-owned buffer whose length is set to the ByteBuffer's capacity less the offset.

Parameters:
data - java.nio.ByteBuffer wrapped by the DatabaseEntry.
offset - int offset into the ByteBuffer where the DatabaseEntry data begins.
size - int size of the ByteBuffer available.

setDataNIO

public void setDataNIO(ByteBuffer data)
Sets the java.nio.ByteBuffer. The offset is set to the ByteBuffer's position; the size is set to the ByteBuffer's limit less its position, or to zero if null is passed. If the ByteBuffer is non-direct, the backing array will be used. If passed a direct ByteBuffer, the entry is configured with an application-owned buffer whose length is set to the ByteBuffer's capacity less its current position. No call to setUserBuffer(int, boolean) is required after setting the ByteBuffer.

Parameters:
data - java.nio.ByteBuffer wrapped by the DatabaseEntry.

getOffset

public int getOffset()
Return the byte offset into the data array.

For a DatabaseEntry that is used as an output parameter, the offset will always be zero.

Returns:
Offset in the first byte in the byte array to be included.

setOffset

public void setOffset(int offset)
Set the byte offset into the data array.

Parameters:
offset - Offset in the first byte in the byte array to be included.

getPartialLength

public int getPartialLength()
Return the byte length of the partial record being read or written by the application, in bytes.

Note that the Partial properties are set only by the caller. They will never be set by a Database or Cursor method.

Returns:
The byte length of the partial record being read or written by the application, in bytes.

See Also:
setPartial(int,int,boolean)

getPartialOffset

public int getPartialOffset()
Return the offset of the partial record being read or written by the application, in bytes.

Note that the Partial properties are set only by the caller. They will never be set by a Database or Cursor method.

Returns:
The offset of the partial record being read or written by the application, in bytes.

See Also:
setPartial(int,int,boolean)

getPartial

public boolean getPartial()
Return whether this DatabaseEntry is configured to read or write partial records.

Note that the Partial properties are set only by the caller. They will never be set by a Database or Cursor method.

Returns:
Whether this DatabaseEntry is configured to read or write partial records.

See Also:
setPartial(int,int,boolean)

setPartialOffset

public void setPartialOffset(int doff)
Set the offset of the partial record being read or written by the application, in bytes.

Note that the Partial properties are set only by the caller. They will never be set by a Database or Cursor method.

Parameters:
doff - The offset of the partial record being read or written by the application, in bytes.

See Also:
setPartial(int,int,boolean)

setPartialLength

public void setPartialLength(int dlen)
Set the byte length of the partial record being read or written by the application, in bytes.

Note that the Partial properties are set only by the caller. They will never be set by a Database or Cursor method.

Parameters:
dlen - The byte length of the partial record being read or written by the

See Also:
application, in bytes.

setPartial

public void setPartial(boolean partial)
Configure this DatabaseEntry to read or write partial records.

Note that the Partial properties are set only by the caller. They will never be set by a Database or Cursor method.

Parameters:
partial - Whether this DatabaseEntry is configured to read or write partial records.

See Also:
setPartial(int,int,boolean)

setPartial

public void setPartial(int doff,
                       int dlen,
                       boolean partial)
Configures this DatabaseEntry to read or write partial records.

Do partial retrieval or storage of an item. If the calling application is doing a retrieval, length bytes specified by dlen, starting at the offset set by doff bytes from the beginning of the retrieved data record are returned as if they comprised the entire record. If any or all of the specified bytes do not exist in the record, the get is successful, and any existing bytes are returned.

For example, if the data portion of a retrieved record was 100 bytes, and a partial retrieval was done using a DatabaseEntry having a partial length of 20 and a partial offset of 85, the retrieval would succeed and the retrieved data would be the last 15 bytes of the record.

If the calling application is storing an item, length bytes specified by dlen, starting at the offset set by doff bytes from the beginning of the specified key's data item are replaced by the data specified by the DatabaseEntry. If the partial length is smaller than the data, the record will grow; if the partial length is larger than the data, the record will shrink. If the specified bytes do not exist, the record will be extended using nul bytes as necessary, and the store will succeed.

It is an error to specify a partial key when performing a put operation of any kind.

It is an error to attempt a partial store using the Database.put method in a database that supports duplicate records. Partial stores in databases supporting duplicate records must be done using a cursor method.

Note that the Partial properties are set only by the caller. They will never be set by a Database or Cursor method.

Parameters:
doff - The offset of the partial record being read or written by the application, in bytes.

dlen - The byte length of the partial record being read or written by the application, in bytes.

partial - Whether this DatabaseEntry is configured to read or write partial records.

getReadOnly

public boolean getReadOnly()
Return whether this DatabaseEntry is configured as read only.

Returns:
Whether this DatabaseEntry is configured as read only.

See Also:
setReadOnly(boolean)

setReadOnly

public void setReadOnly(boolean readonly)
Configure this DatabaseEntry as read only.

Parameters:
readonly - Whether this DatabaseEntry is configured as read only.

getRecordNumber

public int getRecordNumber()
Return the record number encoded in this entry's buffer.

This method may be called at any time during the life of the application.

Returns:
The record number encoded in this entry's buffer.

setRecordNumber

public void setRecordNumber(int recno)
Initialize the entry from a logical record number. Record numbers are integer keys starting at 1. When this method is called the data, size and offset fields are implicitly set to hold a byte array representation of the integer key.

Parameters:
recno - the record number to be encoded

getReuseBuffer

public boolean getReuseBuffer()
Return true if the whether the entry is configured to reuse the buffer.

This method may be called at any time during the life of the application.

Returns:
True if the whether the entry is configured to reuse the buffer.

setReuseBuffer

public void setReuseBuffer(boolean reuse)
Configures the entry to try to reuse the buffer before allocating a new one.

Parameters:
reuse - whether to reuse the buffer

getSize

public int getSize()
Return the byte size of the data array.

For a DatabaseEntry that is used as an output parameter, the size will always be the length of the data array.

Returns:
Number of bytes in the byte array to be included.

setSize

public void setSize(int size)
Set the byte size of the data array.

Parameters:
size - Number of bytes in the byte array to be included.

getUserBuffer

public boolean getUserBuffer()
Return true if the whether the buffer in this entry is owned by the application.

This method may be called at any time during the life of the application.

Returns:
True if the whether the buffer in this entry is owned by the application.

getUserBufferLength

public int getUserBufferLength()
Return the length of the application's buffer.

This method may be called at any time during the life of the application.

Returns:
The length of the application's buffer.

setUserBuffer

public void setUserBuffer(int length,
                          boolean usermem)
Configures the entry with an application-owned buffer.

The data field of the entry must refer to a buffer that is at least length bytes in length.

If the length of the requested item is less than or equal to that number of bytes, the item is copied into the memory to which the data field refers. Otherwise, the size field is set to the length needed for the requested item, and a MemoryException is thrown.

Applications can determine the length of a record by setting length to 0 and calling DatabaseEntry.getSize on the return value.

Parameters:
length - the length of the buffer

usermem - whether the buffer is owned by the application

equals

public boolean equals(Object o)
Compares the data of two entries for byte-by-byte equality.

In either entry, if the offset is non-zero or the size is not equal to the data array length, then only the data bounded by these values is compared. The data array length and offset need not be the same in both entries for them to be considered equal.

If the data array is null in one entry, then to be considered equal both entries must have a null data array.

If the partial property is set in either entry, then to be considered equal both entries must have the same partial properties: partial, partialOffset and partialLength.

Overrides:
equals in class Object

hashCode

public int hashCode()
Returns a hash code based on the data value.

Overrides:
hashCode in class Object

Berkeley DB
version 5.3.21

Copyright (c) 1996, 2012 Oracle and/or its affiliates. All rights reserved.