|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.helidb.impl.AbstractDatabase<K,V,P>
K
- The type of the keys in the database.V
- The type of the values in the database.P
- The type of positions used by the
DatabaseBackend
to locate records.public abstract class AbstractDatabase<K,V,P>
Abstract implementation of the Database
interface that can be used as
a starting point for database implementation. This class implements all
methods in the Database
interface. Subclasses only have to implement
the abstract methods defined in this class.
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface java.util.Map |
---|
Map.Entry<K,V> |
Constructor Summary | |
---|---|
protected |
AbstractDatabase(LogAdapterHolder lah)
|
Method Summary | |
---|---|
protected void |
assertNotClosed()
Throw an exception if this database instance has been closed. |
void |
clear()
|
void |
close()
If overriding this method, make sure super.close() is called. |
protected abstract void |
closeBackend()
Subclasses implement this to call close on the backend. |
boolean |
compact()
Defragment the database backend. |
boolean |
containsKey(Object key)
|
boolean |
containsValue(Object val)
|
boolean |
delete(K key)
Delete the record with the supplied key from the database. |
Set<Map.Entry<K,V>> |
entrySet()
|
boolean |
equals(Object o)
Implemented according as specified by the contract in Map . |
void |
fasterInsert(K key,
V value)
This is a faster version of Database.insert(Object, Object) that does not
check if the key already exists in the database. |
Cursor<K,V> |
find(K key)
Search for the supplied key in the database and return a Cursor
at the found record. |
Cursor<K,V> |
find(K key,
SearchMode mode)
Search for the key in the database using the supplied SearchMode . |
Cursor<K,V> |
find(K key,
SearchMode mode,
Comparator<? super K> cmp)
Search for the key in the database using the supplied SearchMode . |
Cursor<K,V> |
firstRecord()
Get a Cursor at the first record in the database. |
V |
get(Object key)
|
protected abstract DatabaseBackend<K,V,P> |
getBackendForReading()
Subclasses implement this to return a backend object that can be used for reading data. |
protected abstract DatabaseBackend<K,V,P> |
getBackendForWriting()
Subclasses implement this to return a backend object that can be used for writing data to. |
protected LogAdapterHolder |
getLogAdapterHolder()
Subclasses call this to get something to log to. |
int |
hashCode()
Implemented according as specified by the contract in Map . |
void |
insert(K key,
V value)
Insert a new record in the database. |
boolean |
insertOrUpdate(K key,
V value)
If the record's key already exists in the database, update the existing record. |
boolean |
isClosed()
Is this database closed? |
boolean |
isEmpty()
|
Iterator<Record<K,V>> |
iterator()
|
Iterator<K> |
keyIterator()
This method returns an iterator over the keys in the database. |
Set<K> |
keySet()
|
Cursor<K,V> |
lastRecord()
Get a Cursor at the last record in the database. |
V |
put(K key,
V value)
|
void |
putAll(Map<? extends K,? extends V> m)
|
V |
remove(Object key)
|
int |
size()
|
void |
update(K key,
V value)
Update an existing record in the database with a new value. |
Iterator<V> |
valueIterator()
Get an iterator over the values in the database. |
Collection<V> |
values()
|
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected AbstractDatabase(LogAdapterHolder lah)
Method Detail |
---|
protected abstract DatabaseBackend<K,V,P> getBackendForReading()
protected abstract DatabaseBackend<K,V,P> getBackendForWriting()
protected abstract void closeBackend()
close
on the backend. This is
called by close()
.
protected final LogAdapterHolder getLogAdapterHolder()
protected void assertNotClosed() throws IllegalStateException
IllegalStateException
- If the database instance has been closed.public Set<K> keySet()
keySet
in interface Map<K,V>
public Collection<V> values()
values
in interface Map<K,V>
public boolean isEmpty()
isEmpty
in interface Map<K,V>
public Set<Map.Entry<K,V>> entrySet()
entrySet
in interface Map<K,V>
public boolean containsValue(Object val)
containsValue
in interface Map<K,V>
public void insert(K key, V value)
Database
This method makes sure that the supplied key is unique before the record
is inserted. The Database.fasterInsert(Object, Object)
can be used
instead when the client is absolute sure that the key is unique. It
should be significantly faster for large databases.
insert
in interface Database<K,V>
key
- The record's keyvalue
- The record's valueDatabase.insertOrUpdate(Object, Object)
,
Database.update(Object, Object)
,
Map.put(Object, Object)
,
Database.fasterInsert(Object, Object)
public void fasterInsert(K key, V value)
Database
Database.insert(Object, Object)
that does not
check if the key already exists in the database. It is the client's
responsibility to avoid key collisions.
The database's behavior if duplicate keys are inserted is unspecified,
and it probably varies between different
DatabaseBackend
implementations.
fasterInsert
in interface Database<K,V>
key
- The record's key.value
- The record's value.Database.insert(Object, Object)
,
Database.insertOrUpdate(Object, Object)
,
Map.put(Object, Object)
public boolean insertOrUpdate(K key, V value)
Database
This method is quite similar to Map.put(Object, Object)
, but its
implementation should be slightly more efficient since it does not return
the record's old value.
insertOrUpdate
in interface Database<K,V>
key
- The record's key.value
- The record's value.
true
if an existing record was updated, false
if
a new record was inserted.Database.insert(Object, Object)
,
Database.update(Object, Object)
,
Map.put(Object, Object)
,
Database.fasterInsert(Object, Object)
public void update(K key, V value)
Database
update
in interface Database<K,V>
key
- The record's key.value
- The record's new value.Database.insert(Object, Object)
,
Database.fasterInsert(Object, Object)
,
Database.insertOrUpdate(Object, Object)
,
Map.put(Object, Object)
public V put(K key, V value)
put
in interface Map<K,V>
public void putAll(Map<? extends K,? extends V> m)
putAll
in interface Map<K,V>
public void clear()
clear
in interface Map<K,V>
public boolean compact()
Database
Since defragmenting a database potentially involves modifying the entire database, this method may take a long time to complete depending on the size of the database.
compact
in interface Database<K,V>
true
if the database was compacted, or false
if
there was nothing to compact.DatabaseBackend.compact()
public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
public int size()
size
in interface Map<K,V>
public V get(Object key) throws ClassCastException
get
in interface Map<K,V>
ClassCastException
public V remove(Object key) throws ClassCastException, NullPointerException
remove
in interface Map<K,V>
ClassCastException
NullPointerException
public boolean delete(K key)
Database
false
.
This method should be slightly more efficient than
Map.remove(Object)
since it does not return the record's previous
value.
delete
in interface Database<K,V>
key
- The key of the record to delete.
true
if a record was deleted, false
if there was
no record with the supplied key to delete.Map.remove(Object)
public Iterator<Record<K,V>> iterator()
iterator
in interface Iterable<Record<K,V>>
public Iterator<K> keyIterator()
Database
DatabaseBackend
implementation used by the
database and whether the database uses an index or not. (See the class
documentation above.)
If the database is modified after calling this method, the methods of the
Iterator
returned will throw
ConcurrentModificationException
on subsequent
invocations.
keyIterator
in interface Database<K,V>
Iterator
over the database's keys.public Iterator<V> valueIterator()
Database
DatabaseBackend
implementation used by the
database and whether the database uses an index or not. (See the class
documentation above.)
If the database is modified after calling this method, the methods of the
Iterator
returned will throw
ConcurrentModificationException
on subsequent
invocations.
valueIterator
in interface Database<K,V>
Iterator
over the database's values.public Cursor<K,V> firstRecord()
Database
Cursor
at the first record in the database. Which record
that is and how the Cursor
will navigate through the database is
determined by the database backend's search order.
The returned Cursor
is valid until the database is updated.
firstRecord
in interface Database<K,V>
Cursor
at the database's first record, or null
if the database is empty.Database.lastRecord()
public Cursor<K,V> lastRecord()
Database
Cursor
at the last record in the database. Which record
that is and how the Cursor
will navigate through the database is
determined by the database backend's search order.
The returned Cursor
is valid until the database is updated.
lastRecord
in interface Database<K,V>
Cursor
at the database's last record, or null
if the database is empty.Database.lastRecord()
public Cursor<K,V> find(K key)
Database
Cursor
at the found record.
The returned Cursor
is valid until the database is updated.
find
in interface Database<K,V>
key
- The key to search for.
Cursor
at the found record, or null
if the key
was not in the database.Database.find(Object, SearchMode)
,
Database.find(Object, SearchMode, Comparator)
public Cursor<K,V> find(K key, SearchMode mode)
Database
SearchMode
.
The natural ordering of the database keys, which must be
Comparable
, is used to find closest matches.
The returned Cursor
is valid until the database is updated.
find
in interface Database<K,V>
key
- The key to search for.mode
- The search mode. This should be a mode that is supported by
the database and its backend.
Cursor
at the found record, or null
if no
matching key was found.Database.find(Object)
,
Database.find(Object, SearchMode, Comparator)
public Cursor<K,V> find(K key, SearchMode mode, Comparator<? super K> cmp)
Database
SearchMode
.
The supplied Comparator
is used to find closest matches.
The returned Cursor
is valid until the database is updated.
find
in interface Database<K,V>
key
- The key to search for.mode
- The search mode. This should be a mode that is supported by
the database and its backend.cmp
- A Comparator
used for finding closest matches between
the search key and the keys in the database.
Cursor
at the found record, or null
if no
matching key was found.Database.find(Object)
,
Database.find(Object, SearchMode)
public void close()
super.close()
is called.
close
in interface Database<K,V>
public boolean isClosed()
Database
isClosed
in interface Database<K,V>
true
if this database is closed.public boolean equals(Object o)
Map
.
equals
in interface Map<K,V>
equals
in class Object
public int hashCode()
Map
.
hashCode
in interface Map<K,V>
hashCode
in class Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |