001: /*
002: $Header: /cvsroot/xorm/xorm/src/org/xorm/datastore/heap/HeapDatastore.java,v 1.1 2003/08/30 21:43:55 wbiggs Exp $
003:
004: This file is part of XORM.
005:
006: XORM is free software; you can redistribute it and/or modify
007: it under the terms of the GNU General Public License as published by
008: the Free Software Foundation; either version 2 of the License, or
009: (at your option) any later version.
010:
011: XORM is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU General Public License for more details.
015:
016: You should have received a copy of the GNU General Public License
017: along with XORM; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package org.xorm.datastore.heap;
021:
022: import java.util.HashMap;
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.Properties;
026:
027: import org.xorm.datastore.Column;
028: import org.xorm.datastore.Row;
029: import org.xorm.datastore.Table;
030: import org.xorm.cache.DataCache;
031:
032: /**
033: * Provides an in-memory database.
034: */
035: public class HeapDatastore implements DataCache {
036: private HashMap tableToCache = new HashMap();
037:
038: private static class CacheByTable {
039: private HashMap keyedObjects = new HashMap();
040: private Column pk;
041:
042: public CacheByTable(Column pk) {
043: this .pk = pk;
044: }
045:
046: /**
047: * Returns an iterator over every row in this cache.
048: * This operation should not be used concurrently with add()
049: * or remove().
050: *
051: * @exception ConcurrentModificationException if add() or remove()
052: * is called on the TableCache while iterating.
053: */
054: public Iterator iterator() {
055: return keyedObjects.values().iterator();
056: }
057:
058: /**
059: * Adds or replaces the Row in the cache. If the Table does not
060: * have a primary key defined, does nothing.
061: */
062: public void add(Row row) {
063: if (pk != null) {
064: keyedObjects.put(row.getPrimaryKeyValue(), row);
065: }
066: }
067:
068: /**
069: * Adds all Row instances of the collection to the cache.
070: *
071: * @exception ClassCastException if the Collection contains objects
072: * that are not instances of org.xorm.Row.
073: */
074: public void addAll(Collection rows) {
075: Iterator i = rows.iterator();
076: while (i.hasNext()) {
077: Row row = (Row) i.next();
078: add(row);
079: }
080: }
081:
082: /**
083: * Retrieves a cloned copy of the Row from the cache with the
084: * matching primary key.
085: *
086: * @return the Row retrieved, or null if no match is found
087: */
088: public Row get(Object primaryKey) {
089: return (Row) keyedObjects.get(primaryKey);
090: }
091:
092: /**
093: * Removes a Row from the cache by its primary key.
094: */
095: public void remove(Row row) {
096: if (pk != null) {
097: keyedObjects.remove(row.getValue(pk));
098: }
099:
100: // Note: need to consider semantics of removing a row which has
101: // had its primary key changed.
102: }
103: }
104:
105: public void setProperties(Properties props) {
106: // None
107: }
108:
109: public void setFactory(org.xorm.InterfaceManagerFactory factory) {
110: // N/A
111: }
112:
113: public void add(Row row) {
114: getCache(row.getTable()).add(row);
115: }
116:
117: public void remove(Row row) {
118: getCache(row.getTable()).remove(row);
119: }
120:
121: public Row get(Table table, Object primaryKey) {
122: return getCache(table).get(primaryKey);
123: }
124:
125: private synchronized CacheByTable getCache(Table table) {
126: CacheByTable c = (CacheByTable) tableToCache.get(table);
127: if (c == null) {
128: c = new CacheByTable(table.getPrimaryKey());
129: tableToCache.put(table, c);
130: }
131: return c;
132: }
133: }
|