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

import java.util.Arrays;

import com.dbxml.db.core.data.Value;

/**
 * IndexQuery represents the most primitive form of index querying.
 * Instances of this object should be created by QueryResolvers and
 * cached in Query instances.
 */

public class IndexQuery {
   // No Operator

   /** Any And All Matches */
   public static final int ANY = 0;


   // Singleton Operators

   /** Equal To */
   public static final int EQ = 1;
   /** Not Equal To */
   public static final int NEQ = -1;
   /** Greater Than */
   public static final int GT = 2;
   /** Less Than Or Equal To */
   public static final int LTE = -2;
   /** Less Than */
   public static final int LT = 3;
   /** Greater Than Or Equal To */
   public static final int GTE = -3;


   // Range Operators

   /** Between (Inclusive) */
   public static final int BW = 4;
   /** Not Between (Inclusive) */
   public static final int NBW = -4;
   /** Between (Exclusive) */
   public static final int BWX = 5;
   /** Not Between (Exclusive) */
   public static final int NBWX = -5;


   // Set Operators

   /** In The Set */
   public static final int IN = 6;
   /** Not In The Set */
   public static final int NIN = -6;


   // Other operators

   /** Starts-with */
   public static final int SW = 7;
   /** Not Starts-with */
   public static final int NSW = -7;


   protected IndexPattern pattern;
   protected int op;
   protected Value[] vals;

   protected IndexQuery() {
   }

   public IndexQuery(IndexPattern pattern) {
      this.pattern = pattern;
      op = ANY;
   }

   public IndexQuery(IndexPattern pattern, int op, Value[] vals) {
      this.pattern = pattern;
      this.op = op;
      this.vals = vals;
   }

   public IndexQuery(IndexPattern pattern, Value[] vals) {
      this(pattern, IN, vals);
   }

   public IndexQuery(IndexPattern pattern, int op, Value val1) {
      this.pattern = pattern;
      this.op = op;
      if ( op == SW || op == NSW ) {
         byte[] b = new byte[val1.getLength() + 1];
         System.arraycopy(val1.getData(), 0, b, 0, b.length - 1);
         b[b.length - 1] = (byte)0xFF; // Was 127
         Value val2 = new Value(b);
         vals = new Value[]{val1, val2};
      }
      else
         vals = new Value[]{val1};
   }

   public IndexQuery(IndexPattern pattern, Value val1) {
      this(pattern, EQ, val1);
   }

   public IndexQuery(IndexPattern pattern, int op, Value val1, Value val2) {
      this.pattern = pattern;
      this.op = op;
      vals = new Value[]{val1, val2};
   }

   public IndexQuery(IndexPattern pattern, Value val1, Value val2) {
      this(pattern, IN, val1, val2);
   }

   /**
    * getPattern returns the IndexPattern associated with this query.
    *
    * @return the IndexPattern
    */
   public IndexPattern getPattern() {
      return pattern;
   }

   /**
    * getOperator returns the operator associated with this query.
    *
    * @return The operator
    */
   public int getOperator() {
      return op;
   }

   /**
    * getValue returns one of the Values associated with this query.
    *
    * @param index The Value index
    * @return The request Value
    */
   public final Value getValue(int index) {
      return vals[index];
   }

   /**
    * getValues returns the Values associated with this query.
    *
    * @return The Value set
    */
   public Value[] getValues() {
      return vals;
   }

   /**
    * getLength returns the length of the Value set associated with
    * this query.
    *
    * @return The Value set length
    */
   public final int getLength() {
      return vals.length;
   }

   /**
    * testValue tests the specified value for validity against this
    * IndexQuery.  The helper classes in com.dbxml.db.core.indexer.helpers
    * should be used for optimized performance.
    *
    * @param value The Value to compare
    * @return Whether or not the value matches
    */
   public boolean testValue(Value v) {
      switch ( op ) {
         // No Comparison (Any)
         case ANY:
            return true;

         // Singleton Comparisons
         case EQ:
            return v.equals(vals[0]);
         case NEQ:
            return !v.equals(vals[0]);
         case GT:
            return v.compareTo(vals[0]) > 0;
         case LTE:
            return v.compareTo(vals[0]) <= 0;
         case LT:
            return v.compareTo(vals[0]) < 0;
         case GTE:
            return v.compareTo(vals[0]) >= 0;

         // Range Comparisons
         case BW:
            return v.compareTo(vals[0]) >= 0 && v.compareTo(vals[1]) <= 0;
         case NBW:
            return v.compareTo(vals[0]) <= 0 || v.compareTo(vals[1]) >= 0;
         case BWX:
            return v.compareTo(vals[0]) > 0 && v.compareTo(vals[1]) < 0;
         case NBWX:
            return v.compareTo(vals[0]) < 0 || v.compareTo(vals[1]) > 0;

         // Set Comparisons
         case IN:
         case NIN:
				if ( Arrays.binarySearch(vals, v) >= 0 )
					return op == IN;
				else
					return op == NIN;

         // Other comparisons
         case SW:
         case NSW:
				if ( v.startsWith(vals[0]) )
					return op == SW;
				else
					return op == NSW;

      }
      return false;
   }
}

