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

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.query.QueryEngine;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.util.ByteArray;
import com.dbxml.util.Configuration;
import com.dbxml.util.dbXMLException;
import com.dbxml.xml.SymbolTable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

public final class ValueIndexer 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 long MATCH_INFO = -1000;

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

   private static final int STRING = 0;
   private static final int TRIMMED = 1;
   private static final int INTEGER = 2;
   private static final int FLOAT = 3;
   private static final int BYTE = 4;
   private static final int CHAR = 5;
   private static final int BOOLEAN = 6;

   private static final int[] sizes = {-1, -1, 8, 8, 1, 2, 1};

   private static final String STRING_VAL = "string";
   private static final String TRIMMED_VAL = "trimmed";
   private static final String SHORT_VAL = "short";
   private static final String INT_VAL = "int";
   private static final String LONG_VAL = "long";
   private static final String FLOAT_VAL = "float";
   private static final String DOUBLE_VAL = "double";
   private static final String BYTE_VAL = "byte";
   private static final String CHAR_VAL = "char";
   private static final String BOOLEAN_VAL = "boolean";

   private static final String DEFAULT_VAL = STRING_VAL;

   private Collection collection;
   private SymbolTable symbols;

   private String name;
   private String pattern;
   private int type;
   private int typeSize = 32;
   private boolean wildcard;

   private FileHeader fileHeader;

   public ValueIndexer() {
      super(true);
      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;

         // Determine the Index Type
         String tv = config.getAttribute(TYPE, DEFAULT_VAL).toLowerCase();
         if ( tv.equals(STRING_VAL) )
            type = STRING;
         else if ( tv.equals(TRIMMED_VAL) )
            type = TRIMMED;
         else if ( tv.equals(SHORT_VAL) )
            type = INTEGER;
         else if ( tv.equals(INT_VAL) )
            type = INTEGER;
         else if ( tv.equals(LONG_VAL) )
            type = INTEGER;
         else if ( tv.equals(FLOAT_VAL) )
            type = FLOAT;
         else if ( tv.equals(DOUBLE_VAL) )
            type = FLOAT;
         else if ( tv.equals(BYTE_VAL) )
            type = BYTE;
         else if ( tv.equals(CHAR_VAL) )
            type = CHAR;
         else if ( tv.equals(BOOLEAN_VAL) )
            type = BOOLEAN;
         else
            throw new dbXMLException("Illegal type for ValueIndexer: '"+tv+"'");

         typeSize = sizes[type];

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

         setLocation(name);
      }
      catch ( dbXMLException e ) {
         throw e;
      }
      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_NODEVALUE;
   }

   public String getPattern() {
      return pattern;
   }

   private byte recenterByte(byte value) {
      return (byte)(value - Byte.MIN_VALUE);
   }

   private int recenterInt(int value) {
      return value - Integer.MIN_VALUE;
   }

   private long recenterLong(long value) {
      return value - Long.MIN_VALUE;
   }

   /**
    * @todo Convert String types to UTF8 Byte Arrays
    */
   private Value getTypedValue(String value) {
      if ( type != STRING && type != TRIMMED ) {
         value = value.trim();

         if ( value.length() == 0 )
            return EmptyValue;

         byte[] b = new byte[typeSize];
         try {
            switch ( type ) {
               case INTEGER:
                  long l = Long.parseLong(value);
                  ByteArray.writeLong(b, 0, recenterLong(l));
                  break;
               case FLOAT:
                  double d = Double.parseDouble(value);
                  int i1 = (int)Math.round(d);
                  int i2 = (int)Math.round((d - i1) * 1000000000);
                  ByteArray.writeInt(b, 0, recenterInt(i1));
                  ByteArray.writeInt(b, 4, i2);
                  break;
               case BYTE:
                  b[0] = recenterByte(Byte.parseByte(value));
                  break;
               case CHAR:
                  char c = value.charAt(0);
                  ByteArray.writeChar(b, 0, c);
                  break;
               case BOOLEAN:
                  if ( "[true][yes][1][y][on]".indexOf("[" + value.toString().toLowerCase() + "]") != -1 )
                     b[0] = 1;
                  else if ( "[false][no][0][n][off]".indexOf("[" + value.toString().toLowerCase() + "]") != -1 )
                     b[0] = 0;
                  else
                     return EmptyValue;
                  break;
            }
            return new Value(b);
         }
         catch ( Exception e ) {
            return EmptyValue;
         }
      }

      if ( type == TRIMMED )
         value = QueryEngine.normalizeString(value);

      return new Value(value);
   }

   private Value getCombinedValue(Value value, Key key, int pos, int elemID, int attrID) {
      Value result;
      try {
         int valSize = value.getLength();
         int keySize = key.getLength();
         int totalSize = valSize+keySize+16;

         byte[] b = new byte[totalSize];

         System.arraycopy(value.getRawData(), value.getOffset(), b, 0, valSize);
         System.arraycopy(key.getRawData(), key.getOffset(), b, valSize+1, keySize);

         int l = valSize+keySize+2;

         ByteArray.writeInt(b, l, pos);    // Write the pos
         ByteArray.writeInt(b, l+4, elemID); // Write the elemID
         ByteArray.writeInt(b, l+8, attrID); // Write the attrID
         ByteArray.writeShort(b, l+12, (short)valSize);

         result = new Value(b);
      }
      catch ( Exception e ) {
         result = null; // This will never happen
      }
      return result;
   }

   private IndexMatch getIndexMatch(Value v) {
      byte[] b = v.getData();
      int l = b.length - 13;
      Key key = new Key(b, 0, b.length - 13);

      int pos = ByteArray.readInt(b, l+1);
      int elemID = ByteArray.readInt(b, l+5);
      int attrID = ByteArray.readInt(b, l+9);

      return new IndexMatch(key, pos, elemID, attrID);
   }

   public void remove(Transaction tx, String value, Key key, int pos, int elemID, int attrID) throws DBException {
      Value v = getTypedValue(value);
      if ( type != STRING && type != TRIMMED && v.getLength() == 0 )
         return;

      try {
         Value cv = getCombinedValue(v, key, pos, elemID, attrID);
         removeValue(tx, cv);
      }
      catch ( DBException d ) {
         throw d;
      }
      catch ( Exception e ) {
         e.printStackTrace(System.err);
      }
   }

   public void add(Transaction tx, String value, Key key, int pos, int elemID, int attrID) throws DBException {
      Value v = getTypedValue(value);
      if ( type != STRING && type != TRIMMED && v.getLength() == 0 )
         return;

      try {
         Value cv = getCombinedValue(v, key, pos, elemID, attrID);
         addValue(tx, cv, MATCH_INFO);
      }
      catch ( DBException d ) {
         throw d;
      }
      catch ( IOException e ) {
         throw new BTreeCorruptException("Corruption detected on add", e);
      }
      catch ( Exception e ) {
         e.printStackTrace(System.err);
      }
   }

   public IndexMatch[] queryMatches(Transaction tx, final IndexQuery query) throws DBException {
      // Pre-process the value-set for typing and trimming
      if ( type != STRING ) {
         Value[] vals = query.getValues();
         for ( int i = 0; i < vals.length; i++ )
            vals[i] = getTypedValue(vals[i].toString());
      }

      // Now issue the query
      final List results = new ArrayList(128);

      try {
         query(tx, query, new BTreeCallback() {
            public void indexInfo(Value value, Value extra) {
               try {
                  IndexMatch match = getIndexMatch(extra);
                  if ( !wildcard )
                     results.add(match);
                  else {
                     IndexPattern pt = new IndexPattern(symbols, match.getElement(), match.getAttribute());
                     if ( pt.getMatchLevel(query.getPattern()) > 0 )
                        results.add(match);
                  }
               }
               catch ( Exception e ) {
                  e.printStackTrace(System.err);
               }
            }
         });
      }
      catch ( IOException e ) {
         throw new BTreeCorruptException("Corruption detected on query", e);
      }
      catch ( Exception e ) {
         e.printStackTrace(System.err);
      }

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


