package com.dbxml.db.core.filer;

/*
 * 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: Filer.java,v 1.2 2004/02/12 00:17:57 bradford Exp $
 */

import com.dbxml.db.core.Collection;
import com.dbxml.db.core.DBException;
import com.dbxml.db.core.data.*;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.db.core.transaction.TransactionLog;
import com.dbxml.util.Configurable;

/**
 * Filer is the low-level file management interface for dbXML.  A Filer object
 * is implemented in order to provide a data source to the dbXML Collection
 * class.  Filers are developed to perform transparent storage and retrieval to
 * and from heterogenous data sources (such as FTP, HTTP, RDBMS, etc...)
 */

public interface Filer extends Configurable {
   /**
    * getName returns the name of this Filer.
    *
    * @return the Filer name
    */
   String getName();

   /**
    * create creates a new Filer and any associated resources for the new
    * Filer, such as disk files, etc.
    *
    * @return Whether or not the Filer was created
    */
   boolean create() throws DBException;

   /**
    * open opens the Filer
    *
    * @return Whether or not the Filer was opened
    */
   boolean open() throws DBException;

   /**
    * isOpened returns whether or not the Filer is opened for business.
    *
    * @return The open status of the Filer
    */
   boolean isOpened() throws DBException;

   /**
    * exists returns whether or not a physical representation of this
    * Filer actually exists.  In the case of a BTreeFiler, this would
    * check for the file, and in the case of an FTPFiler, it might
    * perform a connection check.
    *
    * @return Whether or not the physical resource exists
    */
   boolean exists() throws DBException;

   /**
    * drop instructs the Filerimplementation to remove itself from
    * existence.  The Filer's parent is responsible for removing any
    * references to the Filer in its own context.
    *
    * @return Whether or not the Filer was dropped
    */
   boolean drop() throws DBException;

   /**
    * close closes the Filer
    *
    * @return Whether or not the Filer was closed
    */
   boolean close() throws DBException;

   /**
    * setCollection tells the Filer who its parent is.
    *
    * @param collection The owner Collection
    */
   void setCollection(Collection collection);

   /**
    * getRecordMetaData returns metadata about the Record identified
    * by the provided Key.  This method should make every attempt to
    * <b>not</b> read the actual record from its original location.
    *
    * @param tx The controlling Transaction
    * @param key The Record's Key
    * @return The Record's metadata
    */
   RecordMetaData getRecordMetaData(Transaction tx, Key key) throws DBException;

   /**
    * readRecord returns a Record from the Filer based on the specified
    * Key.
    *
    * @param tx The controlling Transaction
    * @param key The Record's Key
    * @return The returned Record
    */
   Record readRecord(Transaction tx, Key key) throws DBException;

   /**
    * writeRecord writes a Value to the Filer based on the specified Key.
    *
    * @param tx The controlling Transaction
    * @param key The Record's Key
    * @param value The Record's Value
    * @return Whether or not the Record could be written
    */
   boolean writeRecord(Transaction tx, Key key, Value value) throws DBException;

   /**
    * deleteRecord removes a Record from the Filer based on the
    * specified Key.
    *
    * @param tx The controlling Transaction
    * @param key The Record's Key
    * @return Whether or not the Record was deleted
    */
   boolean deleteRecord(Transaction tx, Key key) throws DBException;

   /**
    * getRecordCount returns the number of Records in the Filer.
    *
    * @param tx The controlling Transaction
    * @return The Record count
    */
   long getRecordCount(Transaction tx) throws DBException;

   /**
    * getRecordSet returns a RecordSet object for the current Filer.
    *
    * @param tx The controlling Transaction
    * @return The Filer Enumerator
    */
   RecordSet getRecordSet(Transaction tx) throws DBException;

   /**
    * flush forcefully flushes any unwritten buffers to disk.
    *
    * @param tx The controlling Transaction
    */
   void flush(Transaction tx) throws DBException;

   /**
    * getTransactionLog returns the Transaction Log for this Filer.
    * This result may be null if the Filer doesn't support
    * transactional operations.
    *
    * @return The Filer's TransactionLog
    */
   TransactionLog getTransactionLog() throws DBException;
}
