package com.dbxml.db.common.filers;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: MemFiler.java,v 1.3 2004/02/12 00:17:56 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 enum = records.keySet().iterator();
      private Transaction tx;

      public MemRecordSet(Transaction tx) {
         this.tx = tx;
      }

      public synchronized boolean hasMoreRecords() throws DBException {
         return enum.hasNext();
      }

      public synchronized Record getNextRecord() throws DBException {
         checkOpened();
         return readRecord(tx, (Key)enum.next());
      }

      public synchronized Value getNextValue() throws DBException {
         return getNextRecord().getValue();
      }

      public synchronized Key getNextKey() {
         return (Key)enum.next();
      }
   }
}


