1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002,2008 Oracle. All rights reserved. 5 * 6 * $Id: MultipleRecnoNIODataEntry.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 record number/data pairs returned by a 18single {@link com.sleepycat.db.Database Database} or {@link com.sleepycat.db.Cursor Cursor} get call. 19*/ 20public class MultipleRecnoNIODataEntry 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 MultipleRecnoNIODataEntry() { 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 java.nio.ByteBuffer. 32 <p> 33 @param data 34 java.nio.ByteBuffer wrapped by the entry. 35 */ 36 public MultipleRecnoNIODataEntry(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 record number/data pair in the returned set. This method 52 may only 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 When used with the Queue and Recno access methods, 56 <code>data.getData()<code> will return <code>null</code> for deleted 57 records. 58 <p> 59 @param recno 60 an entry that is set to refer to the next record number in the returned 61 set. 62 <p> 63 @param data 64 an entry that is set to refer to the next data element in the returned 65 set. 66 <p> 67 @return 68 indicates whether a value was found. A return of <code>false</code> 69 indicates that the end of the set was reached. 70 */ 71 public boolean next(final DatabaseEntry recno, final DatabaseEntry data) { 72 byte[] intarr; 73 int saveoffset; 74 if (pos == 0) 75 pos = ulen - INT32SZ; 76 77 // pull the offsets out of the ByteBuffer. 78 if(this.data_nio.capacity() < 12) 79 return false; 80 intarr = new byte[12]; 81 saveoffset = this.data_nio.position(); 82 this.data_nio.position(pos - INT32SZ*2); 83 this.data_nio.get(intarr, 0, 12); 84 this.data_nio.position(saveoffset); 85 86 final int keyoff = DbUtil.array2int(intarr, 8); 87 88 // crack out the key offset and the data offset and length. 89 if (keyoff < 0) 90 return false; 91 92 final int dataoff = DbUtil.array2int(intarr, 4); 93 final int datasz = DbUtil.array2int(intarr, 0); 94 95 // move the position to one before the last offset read. 96 pos -= INT32SZ*3; 97 98 recno.setDataNIO(this.data_nio); 99 recno.setOffset(keyoff); 100 recno.setSize(INT32SZ); 101 102 data.setDataNIO(this.data_nio); 103 data.setOffset(dataoff); 104 data.setSize(datasz); 105 106 return true; 107 } 108} 109