The main facade for fetching, storing and removing records from a Persistit™ database.
Applications interact with Persistit through instances of this class. A Exchange
has two important associated member objects, a {@link com.persistit.Key} and a {@link com.persistit.Value}. A Key
is a mutable representation of a key, and a Value
is a mutable representation of a value. Applications manipulate these objects and interact with the database through one of the following four general patterns:
- Modify the
Key
, perform a {@link com.persistit.Exchange#fetch fetch} operation, and query the Value
. - Modify the
Key
, modify the Value
, and then perform a {@link com.persistit.Exchange#store store} operation to insert or replacedata in the database. - Modify the
Key
, and then perform a {@link com.persistit.Exchange#remove remove} to remove one or more key/valuepairs. - Optionally modify the
Key
, perform a {@link com.persistit.Exchange#traverse traverse} operation, then query theresulting state of Key
and/or Value
to enumerate key/value pairs currently stored in the database.
Additional methods of Exchange
include {@link #fetchAndStore fetchAndStore} and {@link #fetchAndRemove fetchAndRemove} which atomicallymodify the database and return the former value associated with the current Key
.
Exchange is Not Threadsafe
Important: an
Exchange
and its associated
Key
and
Value
instances are
not thread-safe. Generally each
Thread
should allocate and use its own
Exchange
instances. Were it to occur, modification of the
Key
or
Value
objects associated with an
Exchange
by another thread could cause severe and unpredictable errors, including possible corruption of the underlying data storage. While the methods of one
Exchange
instance are not threadsafe, Persistit is designed to allow multiple threads, using
multiple Exchange
instances, to access and update the underlying database in a highly concurrent fashion.
Exchange Pools
Normally each thread should allocate its own
Exchange
instances. However, depending on the garbage collection performance characteristics of a particular JVM it may be desirable to maintain a pool of
Exchange
s available for reuse, thereby reducing the frequency with which
Exchange
s need to be constructed and then garbage collected. An application may get an Exchange using {@link Persistit#getExchange(String,String,boolean)} or{@link Persistit#getExchange(Volume,String,boolean)}. These methods reuse a previously constructed
Exchange
if one is available in a pool; otherwise they construct methods construct a new one. Applications using the Exchange pool should call {@link Persistit#releaseExchange(Exchange,boolean)} to relinquish an
Exchange
once it is no longer needed, thereby placing it in the pool for subsequent reuse.
@version 1.0