krati.sos
Class SerializableObjectStore<K,V>

java.lang.Object
  extended by krati.sos.SerializableObjectStore<K,V>
Type Parameters:
K - Key (serializable object)
V - Value (serializable object)
All Implemented Interfaces:
Iterable<Map.Entry<K,V>>, ObjectStore<K,V>, DataStore<K,V>

public class SerializableObjectStore<K,V>
extends Object
implements ObjectStore<K,V>

A key-value store for serializable objects. The store requires that both key and value be serializable objects. This class is not thread-safe by design. It is expected that the conditions below hold within one JVM.

    1. There is one and only one instance of SerializableObjectStore for a given data store.
    2. There is one and only one thread is calling put and delete methods at any given time. 
 
It is expected that this class is used in the case of multiple readers and single writer.

Author:
jwu

Field Summary
protected  ObjectSerializer<K> _keySerializer
           
protected  DataStore<byte[],byte[]> _store
           
protected  ObjectSerializer<V> _valSerializer
           
 
Constructor Summary
SerializableObjectStore(DataStore<byte[],byte[]> store, ObjectSerializer<K> keySerializer, ObjectSerializer<V> valSerializer)
          Constructs a key-value store for serializable objects.
 
Method Summary
 void clear()
          Clears this object store by removing all the persisted data permanently.
 boolean delete(K key)
          Deletes an object from the store based on its key.
 V get(K key)
          Gets an object based on its key from the store.
 byte[] getBytes(byte[] keyBytes)
          Gets an object in the form of byte array from the store.
 byte[] getBytes(K key)
          Gets an object in the form of byte array from the store.
protected  DataStore<byte[],byte[]> getContentStore()
           
 ObjectSerializer<K> getKeySerializer()
           
 ObjectSerializer<V> getValueSerializer()
           
 Iterator<Map.Entry<K,V>> iterator()
           
 Iterator<K> keyIterator()
           
 void persist()
          Persists this object store.
 boolean put(K key, V value)
          Puts an serializable object into the store.
 void sync()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_store

protected final DataStore<byte[],byte[]> _store

_keySerializer

protected final ObjectSerializer<K> _keySerializer

_valSerializer

protected final ObjectSerializer<V> _valSerializer
Constructor Detail

SerializableObjectStore

public SerializableObjectStore(DataStore<byte[],byte[]> store,
                               ObjectSerializer<K> keySerializer,
                               ObjectSerializer<V> valSerializer)
Constructs a key-value store for serializable objects.

Parameters:
store - the underlying data store for serializable objects.
keySerializer - the object serializer to serialize/de-serialize keys.
valSerializer - the object serializer to serialize/de-serialize values.
Method Detail

getContentStore

protected DataStore<byte[],byte[]> getContentStore()
Returns:
the underlying content data store.

getKeySerializer

public ObjectSerializer<K> getKeySerializer()
Returns:
the key serializer.

getValueSerializer

public ObjectSerializer<V> getValueSerializer()
Returns:
the value serializer.

get

public V get(K key)
Gets an object based on its key from the store.

Specified by:
get in interface DataStore<K,V>
Parameters:
key - the retrieving key for a serializable object in the store.
Returns:
the retrieved object.

getBytes

public byte[] getBytes(K key)
Gets an object in the form of byte array from the store.

Specified by:
getBytes in interface ObjectStore<K,V>
Parameters:
key - the retrieving key.
Returns:
the retrieved object in raw bytes.

getBytes

public byte[] getBytes(byte[] keyBytes)
Gets an object in the form of byte array from the store.

Specified by:
getBytes in interface ObjectStore<K,V>
Parameters:
key - the retrieving key in raw bytes.
Returns:
the retrieved object in raw bytes.

put

public boolean put(K key,
                   V value)
            throws Exception
Puts an serializable object into the store.

Specified by:
put in interface DataStore<K,V>
Parameters:
key - the object key.
value - the serializable object.
Returns:
true if the put operation is succeeds.
Throws:
Exception

delete

public boolean delete(K key)
               throws Exception
Deletes an object from the store based on its key.

Specified by:
delete in interface DataStore<K,V>
Parameters:
key - the object key.
Returns:
true if the delete operation succeeds.
Throws:
Exception

sync

public void sync()
          throws IOException
Specified by:
sync in interface DataStore<K,V>
Throws:
IOException

persist

public void persist()
             throws IOException
Persists this object store.

Specified by:
persist in interface DataStore<K,V>
Throws:
IOException

clear

public void clear()
           throws IOException
Clears this object store by removing all the persisted data permanently.

Specified by:
clear in interface DataStore<K,V>
Throws:
IOException

keyIterator

public Iterator<K> keyIterator()
Specified by:
keyIterator in interface DataStore<K,V>

iterator

public Iterator<Map.Entry<K,V>> iterator()
Specified by:
iterator in interface Iterable<Map.Entry<K,V>>


Copyright © 2011. All Rights Reserved.