/*
$Header: /cvsroot/xorm/xorm/src/org/xorm/datastore/heap/HeapDriver.java,v 1.2 2003/09/03 05:03:33 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.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import org.xorm.InterfaceManagerFactory;
import org.xorm.cache.DataCache;
import org.xorm.datastore.Column;
import org.xorm.datastore.DatastoreDriver;
import org.xorm.datastore.Row;
import org.xorm.datastore.Table;
import org.xorm.query.Selector;
import org.xorm.query.SimpleCondition;
/**
* An exceedingly useless implementation of the DatastoreDriver
* interface: a non-persisted, in-memory representation of a
* database. Not a full implementation -- in particular, query
* methods and transactions are not implemented. But it can be used
* to see when certain operations would occur, or extended for an
* application that wanted to use "fake JDO" and provide its own
* keys for looking up bootstrap objects.
*
* When using this driver, XORM's second-level cache is the actual
* datastore (see HeapDatastore).
*
* @author Wes Biggs
*/
public class HeapDriver implements DatastoreDriver {
private static Hashtable sequences = new Hashtable();
private static synchronized Integer nextSequenceValue(String sequence) {
Integer i = (Integer) sequences.get(sequence);
if (i == null) {
i = new Integer(1);
} else {
i = new Integer(i.intValue() + 1);
}
sequences.put(sequence, i);
return i;
}
private InterfaceManagerFactory factory;
public HeapDriver(InterfaceManagerFactory factory) {
this.factory = factory;
}
// Transaction methods
public void begin(boolean readOnly) {
System.out.println("HeapDriver: begin");
}
public void commit() {
System.out.println("HeapDriver: commit");
}
public void rollback() {
System.out.println("HeapDriver: rollback");
}
// CRUD methods
public void create(Row row) {
System.out.println("HeapDriver: create " + row.getTable().getName());
Column c = row.getTable().getPrimaryKey();
String s = c.getSequence();
if (c.isAutoIncremented() || (s != null)) {
row.setValue(c, nextSequenceValue(s));
}
factory.getCache().add(row);
}
public void update(Row row) {
System.out.println("HeapDriver: update " + row.getTable().getName() + " " + row.getPrimaryKeyValue());
factory.getCache().add(row);
}
public void delete(Row row) {
System.out.println("HeapDriver: delete " + row.getTable().getName() + " " + row.getPrimaryKeyValue());
factory.getCache().remove(row);
}
public Collection select(Selector selector, Set extraRows) {
System.out.println("HeapDriver: select " + selector);
// Assumes we're dealing with a PK selector, not fully implemented.
ArrayList list = new ArrayList();
if (selector.getCondition() instanceof SimpleCondition) {
SimpleCondition condition = (SimpleCondition) selector.getCondition();
Table table = selector.getTable();
Object primaryKey = condition.getValue();
Row found = factory.getCache().get(table, primaryKey);
if (found != null) {
list.add((Row) found.clone());
}
}
return list;
}
public int count(Selector selector) {
return select(selector, null).size();
}
}