KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > DatabaseEntry


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DatabaseEntry.java,v 1.41 2006/12/04 18:47:40 cwl Exp $
7  */

8
9 package com.sleepycat.je;
10
11 import com.sleepycat.je.tree.TreeUtils;
12 import com.sleepycat.util.keyrange.KeyRange;
13
14 /**
15  * Javadoc for this public class is generated
16  * via the doc templates in the doc_src directory.
17  */

18 public class DatabaseEntry {
19
20     /* Currently, JE stores all data records as byte array */
21     private byte[] data;
22     private int dlen = 0;
23     private int doff = 0;
24     private int offset = 0;
25     private int size = 0;
26     private boolean partial = false;
27
28     /* The maximum number of bytes to show when toString() is called. */
29     /* FindBugs - ignore not "final" since a user can set this. */
30     public static int MAX_DUMP_BYTES = 100;
31
32     public String JavaDoc toString() {
33     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<DatabaseEntry");
34     sb.append(" dlen=\"").append(dlen);
35     sb.append("\" doff=\"").append(doff);
36     sb.append("\" doff=\"").append(doff);
37     sb.append("\" offset=\"").append(offset);
38     sb.append("\" size=\"").append(size);
39     sb.append("\" data=\"").append(dumpData());
40     if ((size - 1) > MAX_DUMP_BYTES) {
41         sb.append(" ... ").append((size - MAX_DUMP_BYTES) +
42                       " bytes not shown ");
43     }
44     sb.append("\"/>");
45     return sb.toString();
46     }
47
48     /*
49      * Constructors
50      */

51
52     /**
53      * Javadoc for this public method is generated via
54      * the doc templates in the doc_src directory.
55      */

56     public DatabaseEntry() {
57     }
58
59     /**
60      * Javadoc for this public method is generated via
61      * the doc templates in the doc_src directory.
62      */

63     public DatabaseEntry(byte[] data) {
64         this.data = data;
65         if (data != null) {
66             this.size = data.length;
67         }
68     }
69
70     /**
71      * Javadoc for this public method is generated via
72      * the doc templates in the doc_src directory.
73      */

74     public DatabaseEntry(byte[] data, int offset, int size) {
75         this.data = data;
76         this.offset = offset;
77         this.size = size;
78     }
79
80     /*
81      * Accessors
82      */

83
84     /**
85      * Javadoc for this public method is generated via
86      * the doc templates in the doc_src directory.
87      */

88     public byte[] getData() {
89         return data;
90     }
91
92     /**
93      * Javadoc for this public method is generated via
94      * the doc templates in the doc_src directory.
95      */

96     public void setData(byte[] data) {
97     this.data = data;
98         offset = 0;
99     size = (data == null) ? 0 : data.length;
100     }
101
102     /**
103      * Javadoc for this public method is generated via
104      * the doc templates in the doc_src directory.
105      */

106     public void setData(byte[] data, int offset, int size) {
107     this.data = data;
108         this.offset = offset;
109         this.size = size;
110     }
111
112     /**
113      * Javadoc for this public method is generated via
114      * the doc templates in the doc_src directory.
115      */

116     public void setPartial(int doff, int dlen, boolean partial) {
117         setPartialOffset(doff);
118         setPartialLength(dlen);
119         setPartial(partial);
120     }
121
122     /**
123      * Javadoc for this public method is generated via
124      * the doc templates in the doc_src directory.
125      */

126     public int getPartialLength() {
127         return dlen;
128     }
129
130     /**
131      * Javadoc for this public method is generated via
132      * the doc templates in the doc_src directory.
133      */

134     public void setPartialLength(int dlen) {
135         this.dlen = dlen;
136     }
137
138     /**
139      * Javadoc for this public method is generated via
140      * the doc templates in the doc_src directory.
141      */

142     public int getPartialOffset() {
143         return doff;
144     }
145
146     /**
147      * Javadoc for this public method is generated via
148      * the doc templates in the doc_src directory.
149      */

150     public void setPartialOffset(int doff) {
151         this.doff = doff;
152     }
153
154     /**
155      * Javadoc for this public method is generated via
156      * the doc templates in the doc_src directory.
157      */

158     public boolean getPartial() {
159         return partial;
160     }
161
162     /**
163      * Javadoc for this public method is generated via
164      * the doc templates in the doc_src directory.
165      */

166     public void setPartial(boolean partial) {
167         this.partial = partial;
168     }
169
170     /**
171      * Javadoc for this public method is generated via
172      * the doc templates in the doc_src directory.
173      */

174     public int getOffset() {
175         return offset;
176     }
177
178     /**
179      * Javadoc for this public method is generated via
180      * the doc templates in the doc_src directory.
181      */

182     public void setOffset(int offset) {
183         this.offset = offset;
184     }
185
186     /**
187      * Javadoc for this public method is generated via
188      * the doc templates in the doc_src directory.
189      */

190     public int getSize() {
191         return size;
192     }
193
194     /**
195      * Javadoc for this public method is generated via
196      * the doc templates in the doc_src directory.
197      */

198     public void setSize(int size) {
199         this.size = size;
200     }
201     
202     /**
203      * Dumps the data as a byte array, for tracing purposes
204      */

205     String JavaDoc dumpData() {
206         return TreeUtils.dumpByteArray(KeyRange.getByteArray(this,
207                                  MAX_DUMP_BYTES));
208     }
209
210     /**
211      * Compares the data of two entries for byte-by-byte equality.
212      *
213      * <p>In either entry, if the offset is non-zero or the size is not equal
214      * to the data array length, then only the data bounded by these values is
215      * compared. The data array length and offset need not be the same in both
216      * entries for them to be considered equal.</p>
217      *
218      * <p>If the data array is null in one entry, then to be considered equal
219      * both entries must have a null data array.</p>
220      *
221      * <p>If the partial property is set in either entry, then to be considered
222      * equal both entries must have the same partial properties: partial,
223      * partialOffset and partialLength.
224      */

225     public boolean equals(Object JavaDoc o) {
226         if (!(o instanceof DatabaseEntry)) {
227             return false;
228         }
229         DatabaseEntry e = (DatabaseEntry) o;
230         if (partial || e.partial) {
231             if (partial != e.partial ||
232                 dlen != e.dlen ||
233                 doff != e.doff) {
234                 return false;
235             }
236         }
237         if (data == null && e.data == null) {
238             return true;
239         }
240         if (data == null || e.data == null) {
241             return false;
242         }
243         if (size != e.size) {
244             return false;
245         }
246         for (int i = 0; i < size; i += 1) {
247             if (data[offset + i] != e.data[e.offset + i]) {
248                 return false;
249             }
250         }
251         return true;
252     }
253
254     /**
255      * Returns a hash code based on the data value.
256      */

257     public int hashCode() {
258         int hash = 0;
259         if (data != null) {
260             for (int i = 0; i < size; i += 1) {
261                 hash += data[offset + i];
262             }
263         }
264         return hash;
265     }
266 }
267
Popular Tags