001: /*
002: $Header: /cvsroot/xorm/xorm/src/org/xorm/datastore/heap/HeapDriver.java,v 1.2 2003/09/03 05:03:33 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.ArrayList;
023: import java.util.Collection;
024: import java.util.Hashtable;
025: import java.util.Iterator;
026: import java.util.Set;
027:
028: import org.xorm.InterfaceManagerFactory;
029: import org.xorm.cache.DataCache;
030: import org.xorm.datastore.Column;
031: import org.xorm.datastore.DatastoreDriver;
032: import org.xorm.datastore.Row;
033: import org.xorm.datastore.Table;
034: import org.xorm.query.Selector;
035: import org.xorm.query.SimpleCondition;
036:
037: /**
038: * An exceedingly useless implementation of the DatastoreDriver
039: * interface: a non-persisted, in-memory representation of a
040: * database. Not a full implementation -- in particular, query
041: * methods and transactions are not implemented. But it can be used
042: * to see when certain operations would occur, or extended for an
043: * application that wanted to use "fake JDO" and provide its own
044: * keys for looking up bootstrap objects.
045: *
046: * When using this driver, XORM's second-level cache is the actual
047: * datastore (see HeapDatastore).
048: *
049: * @author Wes Biggs
050: */
051: public class HeapDriver implements DatastoreDriver {
052: private static Hashtable sequences = new Hashtable();
053:
054: private static synchronized Integer nextSequenceValue(
055: String sequence) {
056: Integer i = (Integer) sequences.get(sequence);
057: if (i == null) {
058: i = new Integer(1);
059: } else {
060: i = new Integer(i.intValue() + 1);
061: }
062: sequences.put(sequence, i);
063: return i;
064: }
065:
066: private InterfaceManagerFactory factory;
067:
068: public HeapDriver(InterfaceManagerFactory factory) {
069: this .factory = factory;
070: }
071:
072: // Transaction methods
073: public void begin(boolean readOnly) {
074: System.out.println("HeapDriver: begin");
075: }
076:
077: public void commit() {
078: System.out.println("HeapDriver: commit");
079: }
080:
081: public void rollback() {
082: System.out.println("HeapDriver: rollback");
083: }
084:
085: // CRUD methods
086: public void create(Row row) {
087: System.out.println("HeapDriver: create "
088: + row.getTable().getName());
089: Column c = row.getTable().getPrimaryKey();
090: String s = c.getSequence();
091: if (c.isAutoIncremented() || (s != null)) {
092: row.setValue(c, nextSequenceValue(s));
093: }
094: factory.getCache().add(row);
095: }
096:
097: public void update(Row row) {
098: System.out.println("HeapDriver: update "
099: + row.getTable().getName() + " "
100: + row.getPrimaryKeyValue());
101:
102: factory.getCache().add(row);
103: }
104:
105: public void delete(Row row) {
106: System.out.println("HeapDriver: delete "
107: + row.getTable().getName() + " "
108: + row.getPrimaryKeyValue());
109:
110: factory.getCache().remove(row);
111: }
112:
113: public Collection select(Selector selector, Set extraRows) {
114: System.out.println("HeapDriver: select " + selector);
115: // Assumes we're dealing with a PK selector, not fully implemented.
116: ArrayList list = new ArrayList();
117: if (selector.getCondition() instanceof SimpleCondition) {
118: SimpleCondition condition = (SimpleCondition) selector
119: .getCondition();
120: Table table = selector.getTable();
121: Object primaryKey = condition.getValue();
122: Row found = factory.getCache().get(table, primaryKey);
123: if (found != null) {
124: list.add((Row) found.clone());
125: }
126: }
127: return list;
128: }
129:
130: public int count(Selector selector) {
131: return select(selector, null).size();
132: }
133: }
|