1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
/*************************************************************************************************
* The Java API of cursor functions of Villa
* Copyright (C) 2000-2006 Mikio Hirabayashi
* This file is part of QDBM, Quick Database Manager.
* QDBM is free software; you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation; either version
* 2.1 of the License or any later version. QDBM is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
* You should have received a copy of the GNU Lesser General Public License along with QDBM; if
* not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA.
*************************************************************************************************/
package qdbm;
/**
* The Java API of cursor functions of Villa
* This class depends on the native library `jqdbm'.
*/
public class VillaCursor {
//----------------------------------------------------------------
// instance fields
//----------------------------------------------------------------
/** pointer to the native object */
private long coreptr;
/** database handle */
private Villa villa;
//----------------------------------------------------------------
// constructors and finalizers
//----------------------------------------------------------------
/**
* Get a multiple cursor handle.
* @param villa a database handle connected as a reader.
* @throws VillaException if an error occurs.
*/
VillaCursor(Villa villa) throws VillaException {
this.villa = villa;
synchronized(ADBM.class){
int index = villa.getindex();
if(index < 0) throw new VillaException();
if(vlmulcurnew(index) == 0) throw new VillaException(vlmulcurecode());
}
}
/**
* Release resources.
*/
protected void finalize(){
vlmulcurdelete();
}
//----------------------------------------------------------------
// public or protected methods
//----------------------------------------------------------------
/**
* Move the multiple cursor to the first record.
* @return always true. However, if the silent flag is true and no record corresponds, false
* is returned instead of exception.
* @throws VillaException if an error occurs or there is no record in the database.
*/
public boolean first() throws VillaException {
synchronized(ADBM.class){
int index = villa.getindex();
if(index < 0) throw new VillaException();
if(vlmulcurfirst(index) == 0){
if(villa.silent && vlmulcurecode() == Villa.ENOITEM) return false;
throw new VillaException(vlmulcurecode());
}
return true;
}
}
/**
* Move the multiple cursor to the last record.
* @return always true. However, if the silent flag is true and no record corresponds, false
* is returned instead of exception.
* @throws VillaException if an error occurs or there is no record in the database.
*/
public boolean last() throws VillaException {
synchronized(ADBM.class){
int index = villa.getindex();
if(index < 0) throw new VillaException();
if(vlmulcurlast(index) == 0){
if(villa.silent && vlmulcurecode() == Villa.ENOITEM) return false;
throw new VillaException(vlmulcurecode());
}
return true;
}
}
/**
* Move the multiple cursor to the next record.
* @return always true. However, if the silent flag is true and no record corresponds, false
* is returned instead of exception.
* @throws VillaException if an error occurs or there is no previous record.
*/
public boolean prev() throws VillaException {
synchronized(ADBM.class){
int index = villa.getindex();
if(index < 0) throw new VillaException();
if(vlmulcurprev(index) == 0){
if(villa.silent && vlmulcurecode() == Villa.ENOITEM) return false;
throw new VillaException(vlmulcurecode());
}
return true;
}
}
/**
* Move the multiple cursor to the next record.
* @return always true. However, if the silent flag is true and no record corresponds, false
* is returned instead of exception.
* @throws VillaException if an error occurs or there is no next record.
*/
public boolean next() throws VillaException {
synchronized(ADBM.class){
int index = villa.getindex();
if(index < 0) throw new VillaException();
if(vlmulcurnext(index) == 0){
if(villa.silent && vlmulcurecode() == Villa.ENOITEM) return false;
throw new VillaException(vlmulcurecode());
}
return true;
}
}
/**
* Move the multiple cursor to a position around a record.
* @param key a byte array of a key.
* @param jmode detail adjustment: `Villa.JFORWARD', which means that the cursor is set to
* the first record of the same key and that the cursor is set to the next substitute if
* completely matching record does not exist, `Villa.JBACKWARD', which means that the cursor
* is set to the last record of the same key and that the cursor is set to the previous
* substitute if completely matching record does not exist.
* @return always true. However, if the silent flag is true and no record corresponds, false
* is returned instead of exception.
* @throws VillaException if an error occurs or there is no record corresponding the condition.
*/
public boolean jump(byte[] key, int jmode) throws VillaException {
synchronized(ADBM.class){
int index = villa.getindex();
if(index < 0) throw new VillaException();
if(vlmulcurjump(index, key, key.length, jmode) == 0){
if(villa.silent && vlmulcurecode() == Villa.ENOITEM) return false;
throw new VillaException(vlmulcurecode());
}
return true;
}
}
/**
* Move the multiple cursor to a position around a record for stepping forward.
* The same as `jump(key, Villa.JFORFARD)'.
* @see #jump(byte[], int)
*/
public boolean jump(byte[] key) throws VillaException {
return jump(key, Villa.JFORWARD);
}
/**
* Move the multiple cursor to a position around a record composed of serializable objects.
* The same as `jump(qdbm.Util.serialize(key), jmode)'.
* @see #jump(byte[], int)
* @note If serialization is failed, an instance of `VillaException' is thrown.
*/
public boolean jumpobj(Object key, int jmode) throws VillaException {
byte[] kbuf = Util.serialize(key);
if(kbuf == null) throw new VillaException();
return jump(kbuf, jmode);
}
/**
* Get the key of the record where the multiple cursor is.
* @return a byte array of the key of the corresponding record. If the silent flag is true and
* no record corresponds, `null' is returned instead of exception.
* @throws VillaException if an error occurs or no record corresponds to the cursor.
*/
public byte[] key() throws VillaException {
synchronized(ADBM.class){
int index = villa.getindex();
if(index < 0) throw new VillaException();
byte[] val = vlmulcurkey(index);
if(val == null){
if(villa.silent && vlmulcurecode() == Villa.ENOITEM) return null;
throw new VillaException(vlmulcurecode());
}
return val;
}
}
/**
* Get the key of the record composed of serializable objects, where the multiple cursor is.
* The same as `qdbm.Util.deserialize(key())'.
* @see #key()
* @note If serialization is failed, an instance of `VillaException' is thrown.
*/
public Object keyobj() throws VillaException {
byte[] kbuf = key();
if(kbuf == null) return null;
Object key = Util.deserialize(kbuf);
if(key == null) throw new VillaException();
return key;
}
/**
* Get the value of the record where the multiple cursor is.
* @return a byte array of the value of the corresponding record. If the silent flag is true
* and no record corresponds, `null' is returned instead of exception.
* @throws VillaException if an error occurs or no record corresponds to the cursor.
*/
public byte[] val() throws VillaException {
synchronized(ADBM.class){
int index = villa.getindex();
if(index < 0) throw new VillaException();
byte[] val = vlmulcurval(index);
if(val == null){
if(villa.silent && vlmulcurecode() == Villa.ENOITEM) return null;
throw new VillaException(vlmulcurecode());
}
return val;
}
}
/**
* Get the value of the record where the multiple cursor is.
* The same as `qdbm.Util.deserialize(val())'.
* @see #val()
* @note If serialization is failed, an instance of `VillaException' is thrown.
*/
public Object valobj() throws VillaException {
byte[] vbuf = val();
if(vbuf == null) return vbuf;
Object val = Util.deserialize(vbuf);
if(val == null) throw new VillaException();
return val;
}
//----------------------------------------------------------------
// native methods
//----------------------------------------------------------------
private static synchronized final native int vlmulcurecode();
private synchronized final native int vlmulcurnew(int index);
private synchronized final native void vlmulcurdelete();
private synchronized final native int vlmulcurfirst(int index);
private synchronized final native int vlmulcurlast(int index);
private synchronized final native int vlmulcurprev(int index);
private synchronized final native int vlmulcurnext(int index);
private synchronized final native int vlmulcurjump(int index, byte[] key, int ksiz, int jmode);
private synchronized final native byte[] vlmulcurkey(int index);
private synchronized final native byte[] vlmulcurval(int index);
}
/* END OF FILE */
|