001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: DatabaseEntry.java,v 1.41.2.3 2008/01/07 15:14:07 cwl Exp $
007: */
008:
009: package com.sleepycat.je;
010:
011: import com.sleepycat.je.tree.TreeUtils;
012: import com.sleepycat.util.keyrange.KeyRange;
013:
014: /**
015: * Javadoc for this public class is generated
016: * via the doc templates in the doc_src directory.
017: */
018: public class DatabaseEntry {
019:
020: /* Currently, JE stores all data records as byte array */
021: private byte[] data;
022: private int dlen = 0;
023: private int doff = 0;
024: private int offset = 0;
025: private int size = 0;
026: private boolean partial = false;
027:
028: /* The maximum number of bytes to show when toString() is called. */
029: /* FindBugs - ignore not "final" since a user can set this. */
030: public static int MAX_DUMP_BYTES = 100;
031:
032: public String toString() {
033: StringBuffer sb = new StringBuffer("<DatabaseEntry");
034: sb.append(" dlen=\"").append(dlen);
035: sb.append("\" doff=\"").append(doff);
036: sb.append("\" doff=\"").append(doff);
037: sb.append("\" offset=\"").append(offset);
038: sb.append("\" size=\"").append(size);
039: sb.append("\" data=\"").append(dumpData());
040: if ((size - 1) > MAX_DUMP_BYTES) {
041: sb.append(" ... ").append(
042: (size - MAX_DUMP_BYTES) + " bytes not shown ");
043: }
044: sb.append("\"/>");
045: return sb.toString();
046: }
047:
048: /*
049: * Constructors
050: */
051:
052: /**
053: * Javadoc for this public method is generated via
054: * the doc templates in the doc_src directory.
055: */
056: public DatabaseEntry() {
057: }
058:
059: /**
060: * Javadoc for this public method is generated via
061: * the doc templates in the doc_src directory.
062: */
063: public DatabaseEntry(byte[] data) {
064: this .data = data;
065: if (data != null) {
066: this .size = data.length;
067: }
068: }
069:
070: /**
071: * Javadoc for this public method is generated via
072: * the doc templates in the doc_src directory.
073: */
074: public DatabaseEntry(byte[] data, int offset, int size) {
075: this .data = data;
076: this .offset = offset;
077: this .size = size;
078: }
079:
080: /*
081: * Accessors
082: */
083:
084: /**
085: * Javadoc for this public method is generated via
086: * the doc templates in the doc_src directory.
087: */
088: public byte[] getData() {
089: return data;
090: }
091:
092: /**
093: * Javadoc for this public method is generated via
094: * the doc templates in the doc_src directory.
095: */
096: public void setData(byte[] data) {
097: this .data = data;
098: offset = 0;
099: 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 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 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 || dlen != e.dlen
232: || doff != e.doff) {
233: return false;
234: }
235: }
236: if (data == null && e.data == null) {
237: return true;
238: }
239: if (data == null || e.data == null) {
240: return false;
241: }
242: if (size != e.size) {
243: return false;
244: }
245: for (int i = 0; i < size; i += 1) {
246: if (data[offset + i] != e.data[e.offset + i]) {
247: return false;
248: }
249: }
250: return true;
251: }
252:
253: /**
254: * Returns a hash code based on the data value.
255: */
256: public int hashCode() {
257: int hash = 0;
258: if (data != null) {
259: for (int i = 0; i < size; i += 1) {
260: hash += data[offset + i];
261: }
262: }
263: return hash;
264: }
265: }
|