Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / lang / java / src / com / sleepycat / db / MultipleKeyDataEntry.java
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002, 2012 Oracle and/or its affiliates.  All rights reserved.
5  *
6  * $Id$
7  */
8
9 package com.sleepycat.db;
10
11 import com.sleepycat.db.internal.DbConstants;
12 import com.sleepycat.db.internal.DbUtil;
13
14 /**
15 A 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 */
18 public 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     /**
113     Append an entry to the bulk buffer.
114     <p>
115     @param key
116     an array containing the key to be added.
117     @param koff
118     the position in the <b>key</b> array where the record starts.
119     @param klen
120     the length of the record, in bytes, to be copied from the <b>key</b> array.
121     @param data
122     an array containing the value to be added.
123     @param doff
124     the position in the <b>data</b> array where the record starts.
125     @param dlen
126     the length of the record, in bytes, to be copied from the <b>data</b> array.
127     <p>
128     @return
129     indicates whether there was space.  A return of <code>false</code>
130     indicates that the specified entry could not fit in the buffer.
131     */
132     public boolean append(final byte[] key, int koff, int klen,
133                           final byte[] data, int doff, int dlen)
134         throws DatabaseException {
135
136         return append_internal(key, koff, klen) &&
137             this.append_internal(data, doff, dlen);
138     }
139
140     /**
141     Append an entry to the bulk buffer.
142     <p>
143     @param key
144     the key to be appended, using the offset and size specified in the
145     {@link com.sleepycat.db.DatabaseEntry DatabaseEntry}.
146     @param data
147     the value to be appended, using the offset and size specified in the
148     {@link com.sleepycat.db.DatabaseEntry DatabaseEntry}.
149     <p>
150     @return
151     indicates whether there was space.  A return of <code>false</code>
152     indicates that the specified entry could not fit in the buffer.
153     */
154     public boolean append(final DatabaseEntry key, final DatabaseEntry data)
155         throws DatabaseException {
156         
157         return append(key.data, key.offset, key.size,
158             data.data, data.offset, data.size);
159     }
160
161     /**
162     Append an entry to the bulk buffer.
163     <p>
164     @param key
165     an array containing the key to be added.
166     @param data
167     an array containing the value to be added.
168     <p>
169     @return
170     indicates whether there was space.  A return of <code>false</code>
171     indicates that the specified entry could not fit in the buffer.
172     */
173     public boolean append(final byte[] key, final byte[] data)
174         throws DatabaseException {
175         return append(key, 0, key.length, data, 0, data.length);
176     }
177 }
178