package com.dbxml.db.core.indexer;

/*
 * 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: Indexer.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.Key;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.db.core.transaction.TransactionLog;
import com.dbxml.util.Configurable;

/**
 * Indexer is the abstract indexing interface for dbXML.  An Indexer
 * object is implemented in order to retrieve and manage indexes.
 * <br><br>
 * Any number of Indexer instances may be associated with a single
 * Collection.  The type of Indexer utilized by a query depends on
 * the 'Style' of Indexer and the type of QueryResolver that is being
 * used to performt he query.  Currently, dbXML only internally
 * supports one kind of Indexer: 'XPath'.
 */

public interface Indexer extends Configurable {
   static final String STYLE_NODENAME = "Node:Name";
   static final String STYLE_NODEVALUE = "Node:Value";
   static final String STYLE_FULLTEXT = "Node:FullText";

   /**
    * getName returns the name of this Indexer.
    *
    * @return the Indexer name
    */
   String getName();

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

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

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

   /**
    * exists returns whether or not a physical representation of this
    * Indexer actually exists.
    *
    * @return Whether or not the physical resource exists
    */
   boolean exists() throws DBException;

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

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

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

   /**
    * getIndexStyle returns the Index style.  Different query languages
    * will need to draw from different indexing styles.  For example, A
    * query that is written in quilt will require XPath indexing.
    *
    * @return The index style
    */
   String getIndexStyle();

   /**
    * getPattern returns the pattern recognized by this Indexer.  Patterns
    * must be in the form of (elem|*)[@(attr|*)] to tell the IndexManager
    * which element types to send to it, so for example:
    * <pre>
    *    contact@name  Indexes all contacts by name attribute
    *    memo          Indexes the text of all memo elements
    *    contact@*     Indexes all contact attributes
    *    *@name        Indexes the name attribute for all elements
    *    *             Indexes the text of all elements
    *    *@*           Indexes all attributes of all elements
    * </pre>
    * These patterns are used by the IndexManager when handling SAX events.
    * All events that match the specified pattern will result in an add or
    * remove call to the Indexer.
    *
    * @return The Pattern used
    */
   String getPattern();

   /**
    * remove removes all references to the specified Key from the Indexer.
    *
    * @param tx The controlling Transaction
    * @param value The value to remove
    * @param key The Object ID
    * @param pos The offset into the stream the Element occurs at
    * @param len The length of the substream for the Element
    * @param elemID The Element ID of the value
    * @param attrID The Attribute ID of the value (if any, else -1)
    */
   void remove(Transaction tx, String value, Key key, int pos, int elemID, int attrID) throws DBException;

   /**
    * add adds a Document to the Indexer.
    *
    * @param tx The controlling Transaction
    * @param value The value to remove
    * @param key The Object ID
    * @param pos The offset into the stream the Element occurs at
    * @param len The length of the substream for the Element
    * @param elemID The Element ID of the value
    * @param attrID The Attribute ID of the value (if any, else -1)
    */
   void add(Transaction tx, String value, Key key, int pos, int elemID, int attrID) throws DBException;

   /**
    * queryMatches retrieves a set of MatchEntry instances that match
    * the supplied query.  The matches are then used by the  QueryEngine
    * in co-sequential processing.  If this indexer doesn't support the
    * passed value, it should return 'null'.  If no matches are found,
    * it should return an empty set.  queryMatches will typically be
    * used in XPath processing.
    *
    * @param tx The controlling Transaction
    * @param query The IndexQuery to use
    * @return The resulting matches
    */
   IndexMatch[] queryMatches(Transaction tx, IndexQuery query) 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 Indexer.
    * This result may be null if the Indexer doesn't support
    * transactional operations.
    *
    * @return The Indexer's TransactionLog
    */
   TransactionLog getTransactionLog() throws DBException;

}

