KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > tree > Key


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: Key.java,v 1.61 2006/11/09 22:26:30 cwl Exp $
7  */

8
9 package com.sleepycat.je.tree;
10
11 import java.util.Comparator JavaDoc;
12
13 import com.sleepycat.bind.tuple.IntegerBinding;
14 import com.sleepycat.je.DatabaseEntry;
15
16 /**
17  * Key represents a JE B-Tree Key. Keys are immutable. Within JE, keys are
18  * usually represented as byte arrays rather than as Key instances in order to
19  * reduce the in-memory footprint. The static methods of this class are used to
20  * operate on the byte arrays.
21  *
22  * One exception is when keys are held within a collection. In that case, Key
23  * objects are instantiated so that keys are hashed and compared by value.
24  */

25
26 public final class Key implements Comparable JavaDoc {
27     public static boolean DUMP_BINARY = true;
28     /* Not declared final since unit tests use it. */
29     public static boolean DUMP_INT_BINDING = false;
30     public static final byte[] EMPTY_KEY = new byte[0];
31     private byte[] key;
32
33     /**
34      * Construct a new key from a byte array.
35      */

36     public Key(byte[] key) {
37     if (key == null) {
38         this.key = null;
39     } else {
40             this.key = new byte[key.length];
41             System.arraycopy(key, 0, this.key, 0, key.length);
42     }
43     }
44
45     public static byte[] makeKey(DatabaseEntry dbt) {
46         byte[] entryKey = dbt.getData();
47     if (entryKey == null) {
48             return EMPTY_KEY;
49     } else {
50             byte[] newKey = new byte[dbt.getSize()];
51             System.arraycopy(entryKey, dbt.getOffset(), newKey,
52                              0, dbt.getSize());
53             return newKey;
54     }
55     }
56
57     /**
58      * Get the byte array for the key.
59      */

60     public byte[] getKey() {
61     return key;
62     }
63
64     /**
65      * Compare two keys. Standard compareTo function and returns.
66      *
67      * Note that any configured user comparison function is not used, and
68      * therefore this method should not be used for comparison of keys during
69      * Btree operations.
70      */

71     public int compareTo(Object JavaDoc o) {
72     if (o == null) {
73         throw new NullPointerException JavaDoc();
74     }
75
76         Key argKey = (Key) o;
77         return compareUnsignedBytes(this.key, argKey.key);
78     }
79
80     /**
81      * Support Set of Key in BINReference.
82      */

83     public boolean equals(Object JavaDoc o) {
84         return (o instanceof Key) && (compareTo(o) == 0);
85     }
86
87     /**
88      * Support HashSet of Key in BINReference.
89      */

90     public int hashCode() {
91         int code = 0;
92         for (int i = 0; i < key.length; i += 1) {
93             code += key[i];
94         }
95         return code;
96     }
97
98     /**
99      * Compare keys with an optional comparator.
100      */

101     public static int compareKeys(byte[] key1, byte[] key2,
102                                   Comparator JavaDoc comparator) {
103         if (comparator != null) {
104             return comparator.compare(key1, key2);
105         } else {
106             return compareUnsignedBytes(key1, key2);
107         }
108     }
109
110     /**
111      * Compare using a default unsigned byte comparison.
112      */

113     private static int compareUnsignedBytes(byte[] key1, byte[] key2) {
114     int a1Len = key1.length;
115     int a2Len = key2.length;
116
117     int limit = Math.min(a1Len, a2Len);
118
119     for (int i = 0; i < limit; i++) {
120         byte b1 = key1[i];
121         byte b2 = key2[i];
122         if (b1 == b2) {
123         continue;
124         } else {
125         /* Remember, bytes are signed, so convert to shorts so that
126            we effectively do an unsigned byte comparison. */

127         return (b1 & 0xff) - (b2 & 0xff);
128         }
129     }
130
131     return (a1Len - a2Len);
132     }
133
134     public static String JavaDoc dumpString(byte[] key, int nspaces) {
135     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
136         sb.append(TreeUtils.indent(nspaces));
137     sb.append("<key v=\"");
138
139         /** uncomment for hex formatting
140         for (int i = 0 ; i < key.length; i++) {
141         sb.append(Integer.toHexString(key[i] & 0xFF)).append(" ");
142         }
143         **/

144
145     if (DUMP_BINARY) {
146         if (key == null) {
147         sb.append("<null>");
148         } else {
149         sb.append(TreeUtils.dumpByteArray(key));
150         }
151         } else if (DUMP_INT_BINDING) {
152             if (key == null) {
153         sb.append("<null>");
154             } else {
155                 DatabaseEntry e = new DatabaseEntry(key);
156                 sb.append(IntegerBinding.entryToInt(e));
157             }
158         } else {
159         sb.append(key == null ? "" : new String JavaDoc(key));
160     }
161     sb.append("\"/>");
162
163     return sb.toString();
164     }
165
166     /**
167      * Print the string w/out XML format.
168      */

169     public static String JavaDoc getNoFormatString(byte[] key) {
170         return "key=" + dumpString(key, 0);
171     }
172 }
173
Popular Tags