001: package com.quadcap.sql.types;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.ByteArrayInputStream;
042: import java.io.ByteArrayOutputStream;
043:
044: import com.quadcap.sql.index.Comparator;
045:
046: import com.quadcap.sql.io.ObjectInputStream;
047: import com.quadcap.sql.io.ObjectOutputStream;
048:
049: import com.quadcap.util.Debug;
050: import com.quadcap.util.Util;
051:
052: /**
053: * Generic multi-field comparator implementation
054: *
055: * @author Stan Bailes
056: */
057: public class CompareValues extends Comparator {
058: int directions;
059: int cnt;
060:
061: public CompareValues() {
062: }
063:
064: public CompareValues(int count) {
065: this .cnt = count;
066: this .directions = ~((~0) << count);
067: }
068:
069: public void addField(boolean asc) {
070: directions <<= 1;
071: if (asc)
072: directions |= 1;
073: cnt++;
074: }
075:
076: //#ifdef NOTDEF
077: //- public int compare2(byte[] a, int aoff, int alen,
078: //- byte[] b, int boff, int blen) {
079: //- try {
080: //- int alim = aoff + alen;
081: //- int blim = boff + blen;
082: //- for (int i = 0; i < cnt; i++) {
083: //- int as = a[aoff++] & 0xff;
084: //- if ((as & 0x80) == 0) {
085: //- as <<= 8;
086: //- as |= a[aoff++] & 0xff;
087: //- } else {
088: //- as &= 0x7f;
089: //- }
090: //- int bs = b[boff++] & 0xff;
091: //- if ((bs & 0x80) == 0) {
092: //- bs <<= 8;
093: //- bs |= b[boff++] & 0xff;
094: //- } else {
095: //- bs &= 0x7f;
096: //- }
097: //- for (int j = 0; j < as && j < bs; j++) {
098: //- int av = a[aoff++] & 0xff;
099: //- int bv = b[boff++] & 0xff;
100: //- if (av == bv) continue;
101: //- boolean asc = ((directions >> (cnt - i - 1)) & 1) != 0;
102: //- return ((av < bv) ^ asc) ? -1 : 1;
103: //- }
104: //- }
105: //- return 0;
106: //- } catch (Exception e) {
107: //- throw new RuntimeException(e.toString());
108: //- }
109: //- }
110: //#endif
111:
112: public int compare(byte[] a, int aoff, int alen, byte[] b,
113: int boff, int blen) {
114: try {
115: ByteArrayInputStream ai = new ByteArrayInputStream(a, aoff,
116: alen);
117: ObjectInputStream aoi = new ObjectInputStream(ai);
118: ByteArrayInputStream bi = new ByteArrayInputStream(b, boff,
119: blen);
120: ObjectInputStream boi = new ObjectInputStream(bi);
121: for (int i = 0; i < cnt; i++) {
122: Value va = (Value) aoi.readObject();
123: Value vb = (Value) boi.readObject();
124: Value cmp = Value.binop(Op.COMPARE, va, vb);
125: //Debug.println("[" + i + "]: " + cmp + " <= (" + va + ") vs (" + vb + ")");
126: ValueInteger vi = (ValueInteger) cmp;
127: int ret = vi.intValue();
128: if (ret != 0) {
129: boolean asc = ((directions >> (cnt - i - 1)) & 1) != 0;
130: ret = asc ? ret : 0 - ret;
131: return ret;
132: }
133: }
134: return 0;
135: } catch (Exception e) {
136: //#ifdef DEBUG
137: Debug.print(e);
138: Debug.println("a = " + Util.hexBytes(a, aoff, alen));
139: Debug.println("b = " + Util.hexBytes(b, boff, blen));
140: //#endif
141: throw new RuntimeException(e.toString());
142: }
143: }
144: }
|