package com.dbxml.db.common.indexers;

/*
 * 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: NameIndexer.java,v 1.4 2004/06/04 19:53:30 bradford Exp $
 */

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.dbxml.db.common.btree.BTree;
import com.dbxml.db.common.btree.BTreeCallback;
import com.dbxml.db.common.btree.BTreeCorruptException;
import com.dbxml.db.core.Collection;
import com.dbxml.db.core.DBException;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.data.Value;
import com.dbxml.db.core.indexer.IndexMatch;
import com.dbxml.db.core.indexer.IndexPattern;
import com.dbxml.db.core.indexer.IndexQuery;
import com.dbxml.db.core.indexer.Indexer;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.util.Configuration;
import com.dbxml.xml.SymbolTable;
import com.dbxml.util.dbXMLException;

/**
 * NameIndexer is a basic implementation of the Indexer interface.
 * It is used for maintaining element and element@attribute unique
 * indexes.
 */

public final class NameIndexer extends BTree implements Indexer {
   private static final IndexMatch[] EmptyMatches = new IndexMatch[0];

   private static final String NAME = "name";
   private static final String PATTERN = "pattern";
   private static final String PAGESIZE = "pagesize";

   private Collection collection;
   private SymbolTable symbols;

   private String name;
   private String pattern;
   private boolean wildcard;

   private FileHeader fileHeader;

   public NameIndexer() {
      super(false);
      setTransactionSupported(true);
      fileHeader = getFileHeader();
   }

   public void setConfig(Configuration config) throws dbXMLException {
      super.setConfig(config);
      try {
         name = config.getAttribute(NAME);

         pattern = config.getAttribute(PATTERN);
         wildcard = pattern.indexOf('*') != -1;

         fileHeader.setPageSize(config.getIntAttribute(PAGESIZE, fileHeader.getPageSize()));

         setLocation(name);
      }
      catch ( Exception e ) {
         e.printStackTrace(System.err);
      }
   }

   public String getName() {
      return name;
   }

   public void setLocation(String location) {
      setFile(new File(collection.getCollectionRoot(), location + ".idx"));
   }

   public void setCollection(Collection collection) {
      try {
         this.collection = collection;
         symbols = collection.getSymbols();
      }
      catch ( Exception e ) {
         e.printStackTrace(System.err);
      }
   }

   public String getIndexStyle() {
      return STYLE_NODENAME;
   }

   public String getPattern() {
      return pattern;
   }

   public void remove(Transaction tx, String value, Key key, int pos, int elemID, int attrID) throws DBException {
      try {
         removeValue(tx, key);
      }
      catch ( IOException e ) {
         throw new BTreeCorruptException("Corruption detected on remove", e);
      }
   }

   public void add(Transaction tx, String value, Key key, int pos, int elemID, int attrID) throws DBException {
      try {
         addValue(tx, key, 0);
      }
      catch ( IOException e ) {
         throw new BTreeCorruptException("Corruption detected on add", e);
      }
   }

   public IndexMatch[] queryMatches(Transaction tx, final IndexQuery query) throws DBException {
      final List results = new ArrayList(128);
      final IndexPattern pattern = query.getPattern();

      try {
         query(tx, query, new BTreeCallback() {
            public void indexInfo(Value value, Value extra) {
               results.add(new IndexMatch(new Key(value), pattern));
            }
         });
      }
      catch ( DBException d ) {
         throw d;
      }
      catch ( Exception e ) {
         e.printStackTrace(System.err);
      }

      return (IndexMatch[])results.toArray(EmptyMatches);
   }
}

