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