/*
$Header: /cvsroot/xorm/xorm/src/org/xorm/datastore/heap/HeapDatastore.java,v 1.1 2003/08/30 21:43:55 wbiggs Exp $
This file is part of XORM.
XORM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
XORM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with XORM; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xorm.datastore.heap;
import java.util.HashMap;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
import org.xorm.datastore.Column;
import org.xorm.datastore.Row;
import org.xorm.datastore.Table;
import org.xorm.cache.DataCache;
/**
* Provides an in-memory database.
*/
public class HeapDatastore implements DataCache {
private HashMap tableToCache = new HashMap();
private static class CacheByTable {
private HashMap keyedObjects = new HashMap();
private Column pk;
public CacheByTable(Column pk) {
this.pk = pk;
}
/**
* Returns an iterator over every row in this cache.
* This operation should not be used concurrently with add()
* or remove().
*
* @exception ConcurrentModificationException if add() or remove()
* is called on the TableCache while iterating.
*/
public Iterator iterator() {
return keyedObjects.values().iterator();
}
/**
* Adds or replaces the Row in the cache. If the Table does not
* have a primary key defined, does nothing.
*/
public void add(Row row) {
if (pk != null) {
keyedObjects.put(row.getPrimaryKeyValue(), row);
}
}
/**
* Adds all Row instances of the collection to the cache.
*
* @exception ClassCastException if the Collection contains objects
* that are not instances of org.xorm.Row.
*/
public void addAll(Collection rows) {
Iterator i = rows.iterator();
while (i.hasNext()) {
Row row = (Row) i.next();
add(row);
}
}
/**
* Retrieves a cloned copy of the Row from the cache with the
* matching primary key.
*
* @return the Row retrieved, or null if no match is found
*/
public Row get(Object primaryKey) {
return (Row) keyedObjects.get(primaryKey);
}
/**
* Removes a Row from the cache by its primary key.
*/
public void remove(Row row) {
if (pk != null) {
keyedObjects.remove(row.getValue(pk));
}
// Note: need to consider semantics of removing a row which has
// had its primary key changed.
}
}
public void setProperties(Properties props) {
// None
}
public void setFactory(org.xorm.InterfaceManagerFactory factory) {
// N/A
}
public void add(Row row) {
getCache(row.getTable()).add(row);
}
public void remove(Row row) {
getCache(row.getTable()).remove(row);
}
public Row get(Table table, Object primaryKey) {
return getCache(table).get(primaryKey);
}
private synchronized CacheByTable getCache(Table table) {
CacheByTable c = (CacheByTable) tableToCache.get(table);
if (c == null) {
c = new CacheByTable(table.getPrimaryKey());
tableToCache.put(table, c);
}
return c;
}
}