de.fhg.igd.atlas.core
Class MemoryDB

java.lang.Object
  extended byde.fhg.igd.atlas.core.MemoryDB
All Implemented Interfaces:
Runnable, StorageDB

public class MemoryDB
extends Object
implements StorageDB, Runnable

Provides a memory databases which is used to store entries of type MemoryDBEntry. The entries are held in a TreeMap what guarantees efficient management of this database structure. There exist two indexes to access the entries: one sorted over the implicit names, the other sorted over the timestamps. The first one is used for the operations lookup, init, update, delete, refresh. The second one is used for the implementation of a timeout mechanism, which is needed to fulfill the requirements of the Location Service Protocol (LSP), too. This database can be used as part of a Location Service Server (LSServer).

Version:
"$Id: MemoryDB.java 1913 2007-08-08 02:41:53Z jpeters $"
Author:
Jan Peters
See Also:
MemoryDBEntry, StorageDB

Field Summary
protected  SortedMap entries_
          References to all entries of the database sorted over implicit names (in a TreeMap).
protected  boolean eventFlag_
          If this flag is set, a StorageDBEvent ia generated whenever this database is manipulated.
protected  EventReflector eventReflector_
          The event reflector which is notified, after every successful request, which manipulates this database.
private  Object lock_
          The internal lock object.
protected  Signals signals_
          Used for for signaling between database and timeoutThread.
protected  long timeout_
          Timeout for entries in milli seconds
protected  SortedMap timeoutIndex_
          References to all entries of the database sorted over timestamps (in a TreeMap).
protected  Thread timeoutThread_
          Thread that provides the timeout functionality.
 
Constructor Summary
MemoryDB(long timeout)
          Creates a new instance of the database with the given timeout for its entries.
 
Method Summary
 boolean delete(byte[] implicitName)
          Deletes an existing entry with the given implicitName.
 boolean getEventFlag()
          Returns the current state of the eventFlag_.
 EventReflector getEventReflector()
          Returns the reference to the database's event reflector which is notified with StorageDBEvent objects incrementally describing the state of the database.
 int getSize()
          Returns the current number of entries in the database.
 long getTimeout()
          Returns the timeout of entries in the database.
protected  byte[] getTimeoutIndex(MemoryDBEntry entry)
          Creates an unique index key for the given entry that allowes to sort entries over their timestamps.
 boolean init(byte[] implicitName, URL contactAddress, byte[] cookie)
          Initializes a new entry with the given implicitName, contactAddress and cookie.
 Set list(long timestamp)
          Returns the content of the database as set of all entries with a timestamp newer then the given one.
 StorageDBImmutableEntry lookup(byte[] implicitName)
          Returns the result of a lookup with the given implicitName as immutable entry.
 boolean refresh(byte[] implicitName)
          Refreshes an existing entry with the given implicitName, by updating the entries timestamp to the current time.
 void run()
          This method represents the functionality of the timeout daemon.
 void setEventFlag(boolean flag)
          Sets the eventFlag_ to enable respectively disable the generation of events.
 void setTimeout(long timeout)
          Sets the timeout of entries in the database.
private  void stop()
          Stops the timeoutThread.
 String toString()
          Returns the string representation of this database as list of its entries sorted over their implicit names.
 String toStringTimeout()
          Returns the string representation of this database as list of its entries sorted over their timestamps.
 boolean update(byte[] implicitName, URL contactAddress, byte[] cookie)
          Updates an existing entry with the given implicitName.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

eventReflector_

protected EventReflector eventReflector_
The event reflector which is notified, after every successful request, which manipulates this database.


timeout_

protected long timeout_
Timeout for entries in milli seconds


entries_

protected SortedMap entries_
References to all entries of the database sorted over implicit names (in a TreeMap). The main index of the provided database.


timeoutIndex_

protected SortedMap timeoutIndex_
References to all entries of the database sorted over timestamps (in a TreeMap). This index is used to timeout the entries after a specified time if not refreshed.


timeoutThread_

protected Thread timeoutThread_
Thread that provides the timeout functionality.


signals_

protected Signals signals_
Used for for signaling between database and timeoutThread.


eventFlag_

protected boolean eventFlag_
If this flag is set, a StorageDBEvent ia generated whenever this database is manipulated.


lock_

private Object lock_
The internal lock object.

Constructor Detail

MemoryDB

public MemoryDB(long timeout)
Creates a new instance of the database with the given timeout for its entries.

Parameters:
timeout - The timeout in milli seconds.
Method Detail

getTimeoutIndex

protected byte[] getTimeoutIndex(MemoryDBEntry entry)
Creates an unique index key for the given entry that allowes to sort entries over their timestamps. The key is build out of the entry's timestamp and its unique implicit name. (It's not possible to use only the timestamp as key, because several different entries may have the same timestamp, and different entries in a SortedMap must have different keys too.)

Parameters:
entry - The entry.
Returns:
The unique index key.

getEventReflector

public EventReflector getEventReflector()
Returns the reference to the database's event reflector which is notified with StorageDBEvent objects incrementally describing the state of the database. These envents are triggered every time the content of the database has changed, iff the eventFlag_ is set.

Specified by:
getEventReflector in interface StorageDB
Returns:
The server's event reflector.

setEventFlag

public void setEventFlag(boolean flag)
Sets the eventFlag_ to enable respectively disable the generation of events.

Specified by:
setEventFlag in interface StorageDB
Parameters:
flag - The event flag to be set.

getEventFlag

public boolean getEventFlag()
Returns the current state of the eventFlag_.

Specified by:
getEventFlag in interface StorageDB
Returns:
The event flag.

getSize

public int getSize()
Returns the current number of entries in the database.

Specified by:
getSize in interface StorageDB
Returns:
The current database size as number of entries; or Integer.MAX_VALUE, iff the number of entries is larger than Integer.MAX_VALUE.

lookup

public StorageDBImmutableEntry lookup(byte[] implicitName)
Returns the result of a lookup with the given implicitName as immutable entry.

Specified by:
lookup in interface StorageDB
Parameters:
implicitName - The implicit name.
Returns:
The immutable entry corresponding to the implicitName or null if the entry was not found.

init

public boolean init(byte[] implicitName,
                    URL contactAddress,
                    byte[] cookie)
Initializes a new entry with the given implicitName, contactAddress and cookie. If an entry with the implicitName already exists, false is returned.

Specified by:
init in interface StorageDB
Parameters:
implicitName - The implicit name.
contactAddress - The contact address.
cookie - The cookie.
Returns:
true if has been created successfully; false otherwise.

update

public boolean update(byte[] implicitName,
                      URL contactAddress,
                      byte[] cookie)
Updates an existing entry with the given implicitName. If no entry with the implicitName exists, false is returned.

Specified by:
update in interface StorageDB
Parameters:
implicitName - The implicit name.
contactAddress - The contact address.
cookie - The cookie.
Returns:
true if has been updated successfully; false otherwise.

delete

public boolean delete(byte[] implicitName)
Deletes an existing entry with the given implicitName. If no entry with the implicitName exists, false is returned.

Specified by:
delete in interface StorageDB
Parameters:
implicitName - The implicit name.
Returns:
true if has been deleted successfully; false otherwise.

refresh

public boolean refresh(byte[] implicitName)
Refreshes an existing entry with the given implicitName, by updating the entries timestamp to the current time. If no entry with the implicitName exists, false is returned.

Specified by:
refresh in interface StorageDB
Parameters:
implicitName - The implicit name.
Returns:
true if has been deleted refreshed; false otherwise.

list

public Set list(long timestamp)
Returns the content of the database as set of all entries with a timestamp newer then the given one.

Specified by:
list in interface StorageDB
Parameters:
timestamp - The timestamp in milliseconds since January 1, 1970, 00:00:00 GMT
Returns:
The set of database entries of type StorageDBEntry.

run

public void run()
This method represents the functionality of the timeout daemon.

Specified by:
run in interface Runnable

stop

private void stop()
Stops the timeoutThread.


getTimeout

public long getTimeout()
Returns the timeout of entries in the database.

Specified by:
getTimeout in interface StorageDB
Returns:
The timeout in milli seconds.

setTimeout

public void setTimeout(long timeout)
Sets the timeout of entries in the database. If the timeout is 0 and the timeoutThread_ is inactive, it will be started. If the timeout is not 0 and the timeoutThread_ is active, it will be stopped.

Specified by:
setTimeout in interface StorageDB
Parameters:
timeout - The timeout in milli seconds.

toString

public String toString()
Returns the string representation of this database as list of its entries sorted over their implicit names.

Returns:
The string representation.

toStringTimeout

public String toStringTimeout()
Returns the string representation of this database as list of its entries sorted over their timestamps.

Returns:
The string representation.


Copyright © Fraunhofer Gesellschaft. All Rights Reserved.