package org.dbxml.core.indexer;

/*
 * dbXML License, Version 1.0
 *
 *
 * Copyright (c) 1999-2001 The dbXML Group, L.L.C.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by
 *        The dbXML Group (http://www.dbxml.com/)."
 *    Alternately, this acknowledgment may appear in the software
 *    itself, if and wherever such third-party acknowledgments normally
 *    appear.
 *
 * 4. The names "dbXML" and "The dbXML Group" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    info@dbxml.com.
 *
 * 5. Products derived from this software may not be called "dbXML",
 *    nor may "dbXML" appear in their name, without prior written
 *    permission of The dbXML Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE DBXML GROUP OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: NameIndexer.java,v 1.8 2001/08/06 03:10:02 tom Exp $
 */

import org.dbxml.core.*;
import org.dbxml.core.Collection;
import org.dbxml.core.data.*;
import org.dbxml.core.filer.*;
import org.dbxml.core.query.*;
import org.dbxml.server.*;
import org.dbxml.xml.*;
import org.dbxml.xml.dom.*;

import java.io.*;
import java.util.*;

import org.w3c.dom.*;

/**
 * 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 Value EmptyValue = new Value(new byte[0]);
   
   private static final byte MATCHES = 20;

   private static final String NAME = "name";
   private static final String PATTERN = "pattern";
   private static final String PAGESIZE = "pagesize";
   private static final String MAXKEYSIZE = "maxkeysize";
      
   private Configuration config;
   private Collection collection;
   private SymbolTable symbols;

   private String name;
   private String pattern;
   private boolean wildcard = false;

   private FileHeader fileHeader;

   public NameIndexer() {
      super();
      fileHeader = getFileHeader();
   }

   public void setConfig(Configuration config) {
      this.config = config;
      try {
         name = config.getAttribute(NAME);

         pattern = config.getAttribute(PATTERN);
         wildcard = pattern.indexOf('*') != -1;
         
         fileHeader.setPageSize(config.getIntAttribute(PAGESIZE, fileHeader.getPageSize()));
         fileHeader.setMaxKeySize(config.getShortAttribute(MAXKEYSIZE, fileHeader.getMaxKeySize()));

         setLocation(name);
      }
      catch ( Exception e ) {
         org.dbxml.Debug.printStackTrace(e);
      }
   }

   public Configuration getConfig() {
      return config;
   }

   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 ) {
         org.dbxml.Debug.printStackTrace(e);
      }
   }

   public String getIndexStyle() {
      return STYLE_NODENAME;
   }

   public String getPattern() {
      return pattern;
   }

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

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

   public IndexMatch[] queryMatches(final IndexQuery query) throws DBException {
      final List results = new ArrayList();
      final IndexPattern pattern = query.getPattern();
      
      try {
         query(query, new BTreeCallback() {
            public boolean indexInfo(Value value, long pos) {
               results.add(new IndexMatch(new Key(value), pattern));
               return true;
            }
         });
      }
      catch ( DBException d ) {
         throw d;
      }
      catch ( Exception e ) {
         org.dbxml.Debug.printStackTrace(e);
      }
      
      return (IndexMatch[])results.toArray(EmptyMatches);
   }
}








