package com.dbxml.db.common.filers;
/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: MemFiler.java,v 1.5 2008/08/18 17:24:53 bradford Exp $
*/
import com.dbxml.db.core.Collection;
import com.dbxml.db.core.DBException;
import com.dbxml.db.core.FaultCodes;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.data.Record;
import com.dbxml.db.core.data.RecordMetaData;
import com.dbxml.db.core.data.RecordSet;
import com.dbxml.db.core.data.Value;
import com.dbxml.db.core.filer.Filer;
import com.dbxml.db.core.filer.FilerException;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.db.core.transaction.TransactionLog;
import com.dbxml.util.SimpleConfigurable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* MemFiler is an In-Memory Filer implementation for dbXML. MemFiler can be
* used for temporary collections and caching. It's basically a layering on
* top of HashMap.
*/
public final class MemFiler extends SimpleConfigurable implements Filer {
private Map records;
private boolean opened;
private boolean readOnly;
private Collection collection;
public MemFiler() {
records = Collections.synchronizedMap(new HashMap());
}
public MemFiler(Map records, boolean readOnly) {
this.records = records;
this.readOnly = readOnly;
}
public MemFiler(Map records) {
this(records, false);
}
public void setCollection(Collection collection) {
this.collection = collection;
}
public String getName() {
return "MemFiler";
}
private void checkOpened() throws DBException {
if ( !opened )
throw new FilerException(FaultCodes.COL_COLLECTION_CLOSED, "Filer is closed");
}
private void checkReadOnly() throws DBException {
if ( readOnly )
throw new FilerException(FaultCodes.COL_COLLECTION_READ_ONLY, "Filer is read-only");
}
public boolean create() {
records.clear();
return true;
}
public boolean open() {
opened = true;
return opened;
}
public boolean isOpened() {
return opened;
}
public boolean exists() {
return true;
}
public boolean drop() {
records.clear();
opened = false;
return !opened;
}
public boolean close() {
opened = false;
return !opened;
}
public void flush(Transaction tx) {
}
public RecordMetaData getRecordMetaData(Transaction tx, Key key) throws DBException {
checkOpened();
return null;
}
public Record readRecord(Transaction tx, Key key) throws DBException {
checkOpened();
return (Record)records.get(key);
}
public boolean writeRecord(Transaction tx, Key key, Value value) throws DBException {
checkOpened();
checkReadOnly();
records.put(key, new Record(key, value));
return true;
}
public boolean deleteRecord(Transaction tx, Key key) throws DBException {
checkOpened();
checkReadOnly();
return records.remove(key) != null;
}
public long getRecordCount(Transaction tx) throws DBException {
checkOpened();
return records.size();
}
public RecordSet getRecordSet(Transaction tx) throws DBException {
checkOpened();
return new MemRecordSet(tx);
}
public TransactionLog getTransactionLog() {
return null;
}
/**
* MemRecordSet
*/
private class MemRecordSet implements RecordSet {
private Iterator iter = records.keySet().iterator();
private Transaction tx;
public MemRecordSet(Transaction tx) {
this.tx = tx;
}
public synchronized boolean hasMoreRecords() throws DBException {
return iter.hasNext();
}
public synchronized Record getNextRecord() throws DBException {
checkOpened();
return readRecord(tx, (Key)iter.next());
}
public synchronized Value getNextValue() throws DBException {
return getNextRecord().getValue();
}
public synchronized Key getNextKey() {
return (Key)iter.next();
}
}
}