org.mapdb
Class Caches.HashTable

java.lang.Object
  extended by org.mapdb.EngineWrapper
      extended by org.mapdb.Caches.HashTable
All Implemented Interfaces:
Engine
Enclosing class:
Caches

public static class Caches.HashTable
extends EngineWrapper
implements Engine

Fixed size cache which uses hash table. Is thread-safe and requires only minimal locking. Items are randomly removed and replaced by hash collisions.

This is simple, concurrent, small-overhead, random cache.

Author:
Jan Kotek

Nested Class Summary
 
Nested classes/interfaces inherited from class org.mapdb.EngineWrapper
EngineWrapper.CloseOnJVMShutdown, EngineWrapper.ImmutabilityCheckEngine, EngineWrapper.ReadOnlyEngine, EngineWrapper.SerializerCheckEngineWrapper, EngineWrapper.SynchronizedEngineWrapper
 
Field Summary
protected  int cacheMaxSize
           
protected  int cacheMaxSizeMask
           
protected  long hashSalt
          Salt added to keys before hashing, so it is harder to trigger hash collision attack.
protected  org.mapdb.Caches.HashTable.HashItem[] items
           
protected  ReentrantLock[] locks
           
 
Fields inherited from class org.mapdb.EngineWrapper
CLOSED
 
Fields inherited from interface org.mapdb.Engine
CATALOG_RECID, CHECK_RECORD, CLASS_INFO_RECID, LAST_RESERVED_RECID
 
Constructor Summary
Caches.HashTable(Engine engine, int cacheMaxSize, boolean disableLocks)
           
 
Method Summary
 void clearCache()
          clears any underlying cache
 void close()
          Close store/cache.
<A> boolean
compareAndSwap(long recid, A expectedOldValue, A newValue, Serializer<A> serializer)
          Updates existing record in atomic (Compare And Swap) manner.
<A> void
delete(long recid, Serializer<A> serializer)
          Remove existing record from store/cache

Recid must be a number returned by 'put' method.

<A> A
get(long recid, Serializer<A> serializer)
          Get existing record.
<A> long
put(A value, Serializer<A> serializer)
          Insert new record.
 void rollback()
          Undoes all changes made in the current transaction.
<A> void
update(long recid, A value, Serializer<A> serializer)
          Update existing record with new value.
 
Methods inherited from class org.mapdb.EngineWrapper
canRollback, canSnapshot, checkClosed, closeListenerRegister, closeListenerUnregister, commit, compact, getSerializerPojo, getWrappedEngine, isClosed, isReadOnly, preallocate, preallocate, snapshot
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.mapdb.Engine
canRollback, canSnapshot, closeListenerRegister, closeListenerUnregister, commit, compact, getSerializerPojo, isClosed, isReadOnly, preallocate, preallocate, snapshot
 

Field Detail

locks

protected final ReentrantLock[] locks

items

protected org.mapdb.Caches.HashTable.HashItem[] items

cacheMaxSize

protected final int cacheMaxSize

cacheMaxSizeMask

protected final int cacheMaxSizeMask

hashSalt

protected final long hashSalt
Salt added to keys before hashing, so it is harder to trigger hash collision attack.

Constructor Detail

Caches.HashTable

public Caches.HashTable(Engine engine,
                        int cacheMaxSize,
                        boolean disableLocks)
Method Detail

put

public <A> long put(A value,
                    Serializer<A> serializer)
Description copied from interface: Engine
Insert new record.

Specified by:
put in interface Engine
Overrides:
put in class EngineWrapper
Parameters:
value - records to be added
serializer - used to convert record into/from binary form
Returns:
recid (record identifier) under which record is stored.

get

public <A> A get(long recid,
                 Serializer<A> serializer)
Description copied from interface: Engine
Get existing record.

Recid must be a number returned by 'put' method. Behaviour for invalid recid (random number or already deleted record) is not defined, typically it returns null or throws 'EndOfFileException'

Specified by:
get in interface Engine
Overrides:
get in class EngineWrapper
Parameters:
recid - (record identifier) under which record was persisted
serializer - used to deserialize record from binary form
Returns:
record matching given recid, or null if record is not found under given recid.

update

public <A> void update(long recid,
                       A value,
                       Serializer<A> serializer)
Description copied from interface: Engine
Update existing record with new value.

Recid must be a number returned by 'put' method. Behaviour for invalid recid (random number or already deleted record) is not defined, typically it throws 'EndOfFileException', but it may also corrupt store.

Specified by:
update in interface Engine
Overrides:
update in class EngineWrapper
Parameters:
recid - (record identifier) under which record was persisted.
value - new record value to be stored
serializer - used to serialize record into binary form

compareAndSwap

public <A> boolean compareAndSwap(long recid,
                                  A expectedOldValue,
                                  A newValue,
                                  Serializer<A> serializer)
Description copied from interface: Engine
Updates existing record in atomic (Compare And Swap) manner. Value is modified only if old value matches expected value. There are three ways to match values, MapDB may use any of them:
  1. Equality check oldValue==expectedOldValue when old value is found in instance cache
  2. Deserializing oldValue using serializer and checking oldValue.equals(expectedOldValue)
  3. Serializing expectedOldValue using serializer and comparing binary array with already serialized oldValue

Recid must be a number returned by 'put' method. Behaviour for invalid recid (random number or already deleted record) is not defined, typically it throws 'EndOfFileException', but it may also corrupt store.

Specified by:
compareAndSwap in interface Engine
Overrides:
compareAndSwap in class EngineWrapper
Parameters:
recid - (record identifier) under which record was persisted.
expectedOldValue - old value to be compared with existing record
newValue - to be written if values are matching
serializer - used to serialize record into binary form
Returns:
true if values matched and newValue was written

delete

public <A> void delete(long recid,
                       Serializer<A> serializer)
Description copied from interface: Engine
Remove existing record from store/cache

Recid must be a number returned by 'put' method. Behaviour for invalid recid (random number or already deleted record) is not defined, typically it throws 'EndOfFileException', but it may also corrupt store.

Specified by:
delete in interface Engine
Overrides:
delete in class EngineWrapper
Parameters:
recid - (record identifier) under which was record persisted
serializer - which may be used in some circumstances to deserialize and store old object

close

public void close()
Description copied from interface: Engine
Close store/cache. This method must be called before JVM exits to flush all caches and prevent store corruption. Also it releases resources used by MapDB (disk, memory..).

Engine can no longer be used after this method was called. If Engine is used after closing, it may throw any exception including NullPointerException

There is an configuration option DBMaker.closeOnJvmShutdown() which uses shutdown hook to automatically close Engine when JVM shutdowns.

Specified by:
close in interface Engine
Overrides:
close in class EngineWrapper

rollback

public void rollback()
Description copied from interface: Engine
Undoes all changes made in the current transaction. If transactions are disabled it throws UnsupportedOperationException.

Specified by:
rollback in interface Engine
Overrides:
rollback in class EngineWrapper

clearCache

public void clearCache()
Description copied from interface: Engine
clears any underlying cache

Specified by:
clearCache in interface Engine
Overrides:
clearCache in class EngineWrapper


Copyright © 2014. All Rights Reserved.