krati.sos
Class SerializableObjectCache<T>

java.lang.Object
  extended by krati.sos.SerializableObjectCache<T>
Type Parameters:
T - Serializable object.
All Implemented Interfaces:
Persistable, ObjectCache<T>

public class SerializableObjectCache<T>
extends Object
implements ObjectCache<T>

A simple data cache for 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 SerializableObjectCache for a given data cache.
    2. There is one and only one thread is calling set 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  DataCache _cache
           
protected  ObjectSerializer<T> _serializer
           
 
Constructor Summary
SerializableObjectCache(DataCache cache, ObjectSerializer<T> serializer)
          Constructs an array-like object cache for serializable objects.
 
Method Summary
 void clear()
          Clears this object cache by removing all the persisted data permanently.
 boolean delete(int objectId, long scn)
          Deletes an object based on a user-specified object Id.
 T get(int objectId)
          Gets an object based on a user-specified object Id.
 byte[] getBytes(int objectId)
          Gets an object in raw bytes based on a user-specified object Id.
protected  DataCache getContentCache()
           
 long getHWMark()
          Gets the high water mark.
 long getLWMark()
          Gets the low water mark.
 int getObjectIdCount()
           
 int getObjectIdStart()
           
 ObjectSerializer<T> getSerializer()
           
 void persist()
          Persists this object cache.
 void saveHWMark(long endOfPeriod)
          Saves the high water mark.
 boolean set(int objectId, T object, long scn)
          Sets an object at a user-specified object Id.
 void sync()
          Sync this object cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_cache

protected final DataCache _cache

_serializer

protected final ObjectSerializer<T> _serializer
Constructor Detail

SerializableObjectCache

public SerializableObjectCache(DataCache cache,
                               ObjectSerializer<T> serializer)
Constructs an array-like object cache for serializable objects.

Parameters:
cache - the underlying data cache to store serializable objects.
serializer - the object serializer to serialize/de-serialize objects.
Method Detail

getContentCache

protected DataCache getContentCache()
Returns:
the underlying data cache.

getSerializer

public ObjectSerializer<T> getSerializer()
Returns:
the object serializer.

getObjectIdCount

public int getObjectIdCount()
Specified by:
getObjectIdCount in interface ObjectCache<T>
Returns:
the total number of objects in the cache.

getObjectIdStart

public int getObjectIdStart()
Specified by:
getObjectIdStart in interface ObjectCache<T>
Returns:
the start of ObjectId(s) allowed by the cache.

get

public T get(int objectId)
Gets an object based on a user-specified object Id.

Specified by:
get in interface ObjectCache<T>
Parameters:
objectId - the Id of an object to be retrieved from the cache.
Returns:
an object associated with the given objectId.

set

public boolean set(int objectId,
                   T object,
                   long scn)
            throws Exception
Sets an object at a user-specified object Id.

Specified by:
set in interface ObjectCache<T>
Parameters:
objectId - the object Id.
object - the object to put into the cache.
scn - the global scn (equivalent to a time stamp).
Throws:
Exception

delete

public boolean delete(int objectId,
                      long scn)
               throws Exception
Deletes an object based on a user-specified object Id.

Specified by:
delete in interface ObjectCache<T>
Parameters:
objectId - the object Id.
scn - the global scn (equivalent to a time stamp).
Throws:
Exception

sync

public void sync()
          throws IOException
Sync this object cache.

Specified by:
sync in interface Persistable
Throws:
IOException

persist

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

Specified by:
persist in interface Persistable
Specified by:
persist in interface ObjectCache<T>
Throws:
IOException

clear

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

Specified by:
clear in interface ObjectCache<T>
Throws:
IOException

getHWMark

public long getHWMark()
Description copied from interface: Persistable
Gets the high water mark.

Specified by:
getHWMark in interface Persistable
Returns:
the high water mark.

getLWMark

public long getLWMark()
Description copied from interface: Persistable
Gets the low water mark.

Specified by:
getLWMark in interface Persistable
Returns:
the low water mark.

saveHWMark

public void saveHWMark(long endOfPeriod)
                throws Exception
Saves the high water mark.

Specified by:
saveHWMark in interface Persistable
Throws:
Exception

getBytes

public byte[] getBytes(int objectId)
Gets an object in raw bytes based on a user-specified object Id.

Specified by:
getBytes in interface ObjectCache<T>
Parameters:
objectId - the Id of an object to be retrieved from the cache.
Returns:
an object in raw bytes according to the given object Id.


Copyright © 2011. All Rights Reserved.