org.archive.util
Class ObjectIdentityBdbCache<V>

java.lang.Object
  extended by org.archive.util.ObjectIdentityBdbCache<V>
All Implemented Interfaces:
java.io.Closeable, java.io.Serializable, ObjectIdentityCache<java.lang.String,V>

public class ObjectIdentityBdbCache<V>
extends java.lang.Object
implements ObjectIdentityCache<java.lang.String,V>, java.io.Closeable, java.io.Serializable

A BDB JE backed object cache. Soft references to previously-instantiated objects are held so that unless/until an object is garbage collected, subsequent get()s will return the exact same object. (If all outside references are lost, when the soft reference is broken, the object state -- still accessible to this class via reflective access to a phantom referent --is flushed to disk. The next get() will reconsitute a new object, from the disk state.)

The backing disk is only guaranteed to be up-to-date after a flush of all in-memory values to disk, as can be forced by sync().

To ensure that changes/mutations to values in this map are coherent and consistent at the application level, it is assumed that the application level only mutates values that are in this map and does not retain references to values longer than necessary. This allows mappings to be persisted during GC without explicit transactions or write operations.

Based on the earlier CachedBdbMap.

Author:
John Erik Halse, stack, gojomo, paul baclace (conversion to ConcurrentMap)
See Also:
Serialized Form

Nested Class Summary
protected  class ObjectIdentityBdbCache.LowMemoryCanary
           
 
Field Summary
protected  java.lang.ref.SoftReference<ObjectIdentityBdbCache.LowMemoryCanary> canary
           
protected  java.util.concurrent.atomic.AtomicLong count
           
protected  com.sleepycat.je.Database db
          The BDB JE database used for this instance.
protected  com.sleepycat.collections.StoredSortedMap<java.lang.String,V> diskMap
          The Collection view of the BDB JE database used for this instance.
protected  java.util.concurrent.ConcurrentHashMap<java.lang.String,org.archive.util.ObjectIdentityBdbCache.SoftEntry<V>> memMap
          in-memory map of new/recent/still-referenced-elsewhere instances
protected static java.lang.reflect.Field referentField
          Reference to the Reference#referent Field.
protected  java.lang.ref.ReferenceQueue<V> refQueue
           
 
Constructor Summary
ObjectIdentityBdbCache()
          Constructor.
 
Method Summary
 void close()
          close/release any associated resources
protected  com.sleepycat.collections.StoredSortedMap<java.lang.String,V> createDiskMap(com.sleepycat.je.Database database, com.sleepycat.bind.serial.StoredClassCatalog classCatalog, java.lang.Class valueClass)
           
protected  void finalize()
           
 V get(java.lang.String key)
          get the object under the given key/name
protected  java.lang.String getDatabaseName()
           
 V getOrUse(java.lang.String key, Supplier<V> supplierOrNull)
          get the object under the given key/name, using (and remembering) the object supplied by the supplier if no prior mapping exists
 void initialize(com.sleepycat.je.Environment env, java.lang.String dbName, java.lang.Class valueClass, com.sleepycat.bind.serial.StoredClassCatalog classCatalog)
          Call this method when you have an instance when you used the default constructor or when you have a deserialized instance that you want to reconnect with an extant bdbje environment.
 java.util.Set<java.lang.String> keySet()
          set of all keys
protected  com.sleepycat.je.Database openDatabase(com.sleepycat.je.Environment environment, java.lang.String dbName)
           
(package private)  void pageOutStaleEntries()
          An incremental, poll-based expunger.
 int size()
          count of name-to-object contained
 void sync()
          Sync all in-memory map entries to backing disk store.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

db

protected transient com.sleepycat.je.Database db
The BDB JE database used for this instance.


memMap

protected transient java.util.concurrent.ConcurrentHashMap<java.lang.String,org.archive.util.ObjectIdentityBdbCache.SoftEntry<V>> memMap
in-memory map of new/recent/still-referenced-elsewhere instances


refQueue

protected transient java.lang.ref.ReferenceQueue<V> refQueue

diskMap

protected transient com.sleepycat.collections.StoredSortedMap<java.lang.String,V> diskMap
The Collection view of the BDB JE database used for this instance.


count

protected java.util.concurrent.atomic.AtomicLong count

referentField

protected static java.lang.reflect.Field referentField
Reference to the Reference#referent Field.


canary

protected transient java.lang.ref.SoftReference<ObjectIdentityBdbCache.LowMemoryCanary> canary
Constructor Detail

ObjectIdentityBdbCache

public ObjectIdentityBdbCache()
Constructor. You must call #initialize(Environment, Class, Class, StoredClassCatalog) to finish construction. Construction is two-stepped to support reconnecting a deserialized CachedBdbMap with its backing bdbje database.

Parameters:
dbName - Name of the backing db this instance should use.
Method Detail

initialize

public void initialize(com.sleepycat.je.Environment env,
                       java.lang.String dbName,
                       java.lang.Class valueClass,
                       com.sleepycat.bind.serial.StoredClassCatalog classCatalog)
                throws com.sleepycat.je.DatabaseException
Call this method when you have an instance when you used the default constructor or when you have a deserialized instance that you want to reconnect with an extant bdbje environment. Do not call this method if you used the #CachedBdbMap(File, String, Class, Class) constructor.

Parameters:
env -
keyClass -
valueClass -
classCatalog -
Throws:
com.sleepycat.je.DatabaseException

createDiskMap

protected com.sleepycat.collections.StoredSortedMap<java.lang.String,V> createDiskMap(com.sleepycat.je.Database database,
                                                                                      com.sleepycat.bind.serial.StoredClassCatalog classCatalog,
                                                                                      java.lang.Class valueClass)

openDatabase

protected com.sleepycat.je.Database openDatabase(com.sleepycat.je.Environment environment,
                                                 java.lang.String dbName)
                                          throws com.sleepycat.je.DatabaseException
Throws:
com.sleepycat.je.DatabaseException

close

public void close()
Description copied from interface: ObjectIdentityCache
close/release any associated resources

Specified by:
close in interface java.io.Closeable
Specified by:
close in interface ObjectIdentityCache<java.lang.String,V>

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

get

public V get(java.lang.String key)
Description copied from interface: ObjectIdentityCache
get the object under the given key/name

Specified by:
get in interface ObjectIdentityCache<java.lang.String,V>

getOrUse

public V getOrUse(java.lang.String key,
                  Supplier<V> supplierOrNull)
Description copied from interface: ObjectIdentityCache
get the object under the given key/name, using (and remembering) the object supplied by the supplier if no prior mapping exists

Specified by:
getOrUse in interface ObjectIdentityCache<java.lang.String,V>

keySet

public java.util.Set<java.lang.String> keySet()
Description copied from interface: ObjectIdentityCache
set of all keys

Specified by:
keySet in interface ObjectIdentityCache<java.lang.String,V>

size

public int size()
Description copied from interface: ObjectIdentityCache
count of name-to-object contained

Specified by:
size in interface ObjectIdentityCache<java.lang.String,V>

getDatabaseName

protected java.lang.String getDatabaseName()

sync

public void sync()
Sync all in-memory map entries to backing disk store.

Specified by:
sync in interface ObjectIdentityCache<java.lang.String,V>

pageOutStaleEntries

void pageOutStaleEntries()
An incremental, poll-based expunger. Package-protected for unit-test visibility.



Copyright © 2003-2011 Internet Archive. All Rights Reserved.