1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002,2008 Oracle. All rights reserved. 5 * 6 * $Id: MultipleKeyDataEntry.java,v 12.8 2008/01/17 05:04:53 mjc Exp $ 7 */ 8 9package com.sleepycat.db; 10 11import com.sleepycat.db.internal.DbConstants; 12import com.sleepycat.db.internal.DbUtil; 13 14/** 15A DatabaseEntry that holds multiple key/data pairs returned by a single 16{@link com.sleepycat.db.Database Database} or {@link com.sleepycat.db.Cursor Cursor} get call. 17*/ 18public class MultipleKeyDataEntry extends MultipleEntry { 19 /** 20 Construct an entry with no data. The object must be configured 21 before use with the {@link com.sleepycat.db.MultipleEntry#setUserBuffer MultipleEntry.setUserBuffer} method. 22 */ 23 public MultipleKeyDataEntry() { 24 super(null, 0, 0); 25 } 26 27 /** 28 Construct an entry with a given byte array. The offset is 29 set to zero; the size is set to the length of the array. If null 30 is passed, the object must be configured before use with the 31 {@link com.sleepycat.db.MultipleEntry#setUserBuffer MultipleEntry.setUserBuffer} method. 32 <p> 33 @param data 34 Byte array wrapped by the entry. 35 */ 36 public MultipleKeyDataEntry(final byte[] data) { 37 super(data, 0, (data == null) ? 0 : data.length); 38 } 39 40 /** 41 Constructs a DatabaseEntry with a given byte array, offset and size. 42 <p> 43 @param data 44 Byte array wrapped by the DatabaseEntry. 45 @param offset 46 Offset in the first byte in the byte array to be included. 47 @param size 48 Number of bytes in the byte array to be included. 49 */ 50 public MultipleKeyDataEntry(final byte[] data, 51 final int offset, 52 final int size) { 53 super(data, offset, size); 54 } 55 56 /** 57 * Return the bulk retrieval flag and reset the entry position so that the 58 * next set of key/data can be returned. 59 */ 60 /* package */ 61 int getMultiFlag() { 62 pos = 0; 63 return DbConstants.DB_MULTIPLE_KEY; 64 } 65 66 /** 67 Get the next key/data pair in the returned set. This method may only 68 be called after a successful call to a {@link com.sleepycat.db.Database Database} or 69 {@link com.sleepycat.db.Cursor Cursor} get method with this object as the data parameter. 70 <p> 71 @param key 72 an entry that is set to refer to the next key element in the returned 73 set. 74 <p> 75 @param data 76 an entry that is set to refer to the next data element in the returned 77 set. 78 <p> 79 @return 80 indicates whether a value was found. A return of <code>false</code> 81 indicates that the end of the set was reached. 82 */ 83 public boolean next(final DatabaseEntry key, final DatabaseEntry data) { 84 if (pos == 0) 85 pos = ulen - INT32SZ; 86 87 final int keyoff = DbUtil.array2int(this.data, pos); 88 89 // crack out the key and data offsets and lengths. 90 if (keyoff < 0) 91 return false; 92 93 pos -= INT32SZ; 94 final int keysz = DbUtil.array2int(this.data, pos); 95 pos -= INT32SZ; 96 final int dataoff = DbUtil.array2int(this.data, pos); 97 pos -= INT32SZ; 98 final int datasz = DbUtil.array2int(this.data, pos); 99 pos -= INT32SZ; 100 101 key.setData(this.data); 102 key.setOffset(keyoff); 103 key.setSize(keysz); 104 105 data.setData(this.data); 106 data.setOffset(dataoff); 107 data.setSize(datasz); 108 109 return true; 110 } 111} 112