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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
/*************************************************************************************************
* Implementation of Curia for Java
* 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.
*************************************************************************************************/
#include "qdbm_Curia.h"
#include <depot.h>
#include <curia.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MAXOPEN 1024
CURIA *crtable[MAXOPEN];
static int getnewindex(void);
static int checkdup(const char *name);
static int getcromode(jint omode);
static int getcrdmode(jint dmode);
/*************************************************************************************************
* public objects
*************************************************************************************************/
JNIEXPORT void JNICALL
Java_qdbm_Curia_crinit(JNIEnv *env, jclass myclass)
{
int i;
for(i = 0; i < MAXOPEN; i++){
crtable[i] = NULL;
}
}
JNIEXPORT jstring JNICALL
Java_qdbm_Curia_crversion(JNIEnv *env, jclass myclass)
{
return (*env)->NewStringUTF(env, dpversion);
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crecode(JNIEnv *env, jclass myclass)
{
switch(dpecode){
case DP_ENOERR: return qdbm_Curia_ENOERR;
case DP_EFATAL: return qdbm_Curia_EFATAL;
case DP_EMODE: return qdbm_Curia_EMODE;
case DP_EBROKEN: return qdbm_Curia_EBROKEN;
case DP_EKEEP: return qdbm_Curia_EKEEP;
case DP_ENOITEM: return qdbm_Curia_ENOITEM;
case DP_EALLOC: return qdbm_Curia_EALLOC;
case DP_EMAP: return qdbm_Curia_EMAP;
case DP_EOPEN: return qdbm_Curia_EOPEN;
case DP_ECLOSE: return qdbm_Curia_ECLOSE;
case DP_ETRUNC: return qdbm_Curia_ETRUNC;
case DP_ESYNC: return qdbm_Curia_ESYNC;
case DP_ESTAT: return qdbm_Curia_ESTAT;
case DP_ESEEK: return qdbm_Curia_ESEEK;
case DP_EREAD: return qdbm_Curia_EREAD;
case DP_EWRITE: return qdbm_Curia_EWRITE;
case DP_ELOCK: return qdbm_Curia_ELOCK;
case DP_EUNLINK: return qdbm_Curia_EUNLINK;
case DP_EMKDIR: return qdbm_Curia_EMKDIR;
case DP_ERMDIR: return qdbm_Curia_ERMDIR;
case DP_EMISC: return qdbm_Curia_EMISC;
}
return -1;
}
JNIEXPORT jstring JNICALL
Java_qdbm_Curia_crerrmsg(JNIEnv *env, jclass myclass, jint ecode)
{
return (*env)->NewStringUTF(env, dperrmsg(ecode));
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_cropen(JNIEnv *env, jclass myclass, jstring name, jint omode, jint bnum, jint dnum)
{
CURIA *curia;
const char *tname;
jboolean ic;
int index;
if((index = getnewindex()) == -1) return -1;
tname = (*env)->GetStringUTFChars(env, name, &ic);
if(checkdup(tname) == -1){
if(ic == JNI_TRUE) (*env)->ReleaseStringUTFChars(env, name, tname);
dpecode = DP_EMISC;
return -1;
}
curia = cropen(tname, getcromode(omode), bnum, dnum);
if(ic == JNI_TRUE) (*env)->ReleaseStringUTFChars(env, name, tname);
if(!curia) return -1;
crtable[index] = curia;
return index;
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crclose(JNIEnv *env, jclass myclass, jint index)
{
CURIA *curia;
curia = crtable[index];
crtable[index] = NULL;
return crclose(curia);
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crput(JNIEnv *env, jclass myclass, jint index, jbyteArray key, jint ksiz,
jbyteArray val, jint vsiz, jint dmode){
jbyte *kbuf, *vbuf;
jboolean ick, icv;
int rv;
kbuf = (*env)->GetByteArrayElements(env, key, &ick);
vbuf = (*env)->GetByteArrayElements(env, val, &icv);
rv = crput(crtable[index], (char *)kbuf, ksiz, (char *)vbuf, vsiz, getcrdmode(dmode));
if(ick == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
if(icv == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, val, vbuf, JNI_ABORT);
return rv;
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crout(JNIEnv *env, jclass myclass, jint index, jbyteArray key, jint ksiz)
{
jbyte *kbuf;
jboolean ick;
int rv;
kbuf = (*env)->GetByteArrayElements(env, key, &ick);
rv = crout(crtable[index], (char *)kbuf, ksiz);
if(ick == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
return rv;
}
JNIEXPORT jbyteArray JNICALL
Java_qdbm_Curia_crget(JNIEnv *env, jclass myclass, jint index, jbyteArray key, jint ksiz,
jint start, jint max)
{
jbyte *kbuf;
jboolean ick;
char *val;
int vsiz;
jbyteArray vbuf;
kbuf = (*env)->GetByteArrayElements(env, key, &ick);
val = crget(crtable[index], (char *)kbuf, ksiz, start, max, &vsiz);
if(ick == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
if(val){
vbuf = (*env)->NewByteArray(env, vsiz);
(*env)->SetByteArrayRegion(env, vbuf, 0, vsiz, (jbyte *)val);
free(val);
} else {
vbuf = NULL;
}
return vbuf;
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crvsiz(JNIEnv *env, jclass myclass, jint index, jbyteArray key, jint ksiz)
{
jbyte *kbuf;
jboolean ick;
int rv;
kbuf = (*env)->GetByteArrayElements(env, key, &ick);
rv = crvsiz(crtable[index], (char *)kbuf, ksiz);
if(ick == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
return rv;
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_criterinit(JNIEnv *env, jclass myclass, jint index)
{
return criterinit(crtable[index]);
}
JNIEXPORT jbyteArray JNICALL
Java_qdbm_Curia_criternext(JNIEnv *env, jclass myclass, jint index)
{
char *val;
int vsiz;
jbyteArray vbuf;
val = criternext(crtable[index], &vsiz);
if(val){
vbuf = (*env)->NewByteArray(env, vsiz);
(*env)->SetByteArrayRegion(env, vbuf, 0, vsiz, (jbyte *)val);
free(val);
} else {
vbuf = NULL;
}
return vbuf;
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crsetalign(JNIEnv *env, jclass myclass, jint index, jint align)
{
return crsetalign(crtable[index], align);
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crsetfbpsiz(JNIEnv *env, jclass myclass, jint index, jint size)
{
return crsetfbpsiz(crtable[index], size);
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crsync(JNIEnv *env, jclass myclass, jint index)
{
return crsync(crtable[index]);
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_croptimize(JNIEnv *env, jclass myclass, jint index, jint bnum)
{
return croptimize(crtable[index], bnum);
}
JNIEXPORT jstring JNICALL
Java_qdbm_Curia_crname(JNIEnv *env, jclass myclass, jint index)
{
char *name;
jstring nbuf;
name = crname(crtable[index]);
if(name){
nbuf = (*env)->NewStringUTF(env, name);
free(name);
} else {
nbuf = NULL;
}
return nbuf;
}
JNIEXPORT jdouble JNICALL
Java_qdbm_Curia_crfsizd(JNIEnv *env, jclass myclass, jint index)
{
return crfsizd(crtable[index]);
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crbnum(JNIEnv *env, jclass myclass, jint index)
{
return crbnum(crtable[index]);
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crbusenum(JNIEnv *env, jclass myclass, jint index)
{
return crbusenum(crtable[index]);
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crrnum(JNIEnv *env, jclass myclass, jint index)
{
return crrnum(crtable[index]);
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crwritable(JNIEnv *env, jclass myclass, jint index)
{
return crwritable(crtable[index]);
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crfatalerror(JNIEnv *env, jclass myclass, jint index)
{
return crfatalerror(crtable[index]);
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crinode(JNIEnv *env, jclass myclass, jint index)
{
return crinode(crtable[index]);
}
JNIEXPORT jlong JNICALL
Java_qdbm_Curia_crmtime(JNIEnv *env, jclass myclass, jint index)
{
return crmtime(crtable[index]);
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crputlob(JNIEnv *env, jclass myclass, jint index, jbyteArray key, jint ksiz,
jbyteArray val, jint vsiz, jint dmode)
{
jbyte *kbuf, *vbuf;
jboolean ick, icv;
int rv;
kbuf = (*env)->GetByteArrayElements(env, key, &ick);
vbuf = (*env)->GetByteArrayElements(env, val, &icv);
rv = crputlob(crtable[index], (char *)kbuf, ksiz, (char *)vbuf, vsiz, getcrdmode(dmode));
if(ick == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
if(icv == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, val, vbuf, JNI_ABORT);
return rv;
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_croutlob(JNIEnv *env, jclass myclass, jint index, jbyteArray key, jint ksiz)
{
jbyte *kbuf;
jboolean ick;
int rv;
kbuf = (*env)->GetByteArrayElements(env, key, &ick);
rv = croutlob(crtable[index], (char *)kbuf, ksiz);
if(ick == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
return rv;
}
JNIEXPORT jbyteArray JNICALL
Java_qdbm_Curia_crgetlob(JNIEnv *env, jclass myclass, jint index, jbyteArray key, jint ksiz,
jint start, jint max)
{
jbyte *kbuf;
jboolean ick;
char *val;
int vsiz;
jbyteArray vbuf;
kbuf = (*env)->GetByteArrayElements(env, key, &ick);
val = crgetlob(crtable[index], (char *)kbuf, ksiz, start, max, &vsiz);
if(ick == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
if(val){
vbuf = (*env)->NewByteArray(env, vsiz);
(*env)->SetByteArrayRegion(env, vbuf, 0, vsiz, (jbyte *)val);
free(val);
} else {
vbuf = NULL;
}
return vbuf;
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crvsizlob(JNIEnv *env, jclass myclass, jint index, jbyteArray key, jint ksiz)
{
jbyte *kbuf;
jboolean ick;
int rv;
kbuf = (*env)->GetByteArrayElements(env, key, &ick);
rv = crvsizlob(crtable[index], (char *)kbuf, ksiz);
if(ick == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
return rv;
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crrnumlob(JNIEnv *env, jclass myclass, jint index)
{
return crrnumlob(crtable[index]);
}
JNIEXPORT jint JNICALL
Java_qdbm_Curia_crremove(JNIEnv *env, jclass myclass, jstring name)
{
const char *tname;
jboolean ic;
int rv;
tname = (*env)->GetStringUTFChars(env, name, &ic);
rv = crremove(tname);
if(ic == JNI_TRUE) (*env)->ReleaseStringUTFChars(env, name, tname);
return rv;
}
JNIEXPORT jbyteArray JNICALL
Java_qdbm_Curia_crsnaffle(JNIEnv *env, jclass myclass, jstring name, jbyteArray key, jint ksiz)
{
jbyte *kbuf;
jboolean icn, ick;
const char *tname;
char *val;
int vsiz;
jbyteArray vbuf;
tname = (*env)->GetStringUTFChars(env, name, &icn);
kbuf = (*env)->GetByteArrayElements(env, key, &ick);
val = crsnaffle(tname, (char *)kbuf, ksiz, &vsiz);
if(ick == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
if(icn == JNI_TRUE) (*env)->ReleaseStringUTFChars(env, name, tname);
if(val){
vbuf = (*env)->NewByteArray(env, vsiz);
(*env)->SetByteArrayRegion(env, vbuf, 0, vsiz, (jbyte *)val);
free(val);
} else {
vbuf = NULL;
}
return vbuf;
}
/*************************************************************************************************
* private objects
*************************************************************************************************/
static int getnewindex(void){
int i;
for(i = 0; i < MAXOPEN; i++){
if(crtable[i] == NULL) return i;
}
return -1;
}
static int checkdup(const char *name){
struct stat sbuf;
int i, inode;
if(stat(name, &sbuf) == -1) return 0;
inode = sbuf.st_ino;
for(i = 0; i < MAXOPEN; i++){
if(crtable[i] != NULL && crinode(crtable[i]) == inode) return -1;
}
return 0;
}
static int getcromode(jint omode){
int cromode;
cromode = CR_OREADER;
if(omode & qdbm_Curia_OWRITER){
cromode = CR_OWRITER;
if(omode & qdbm_Curia_OCREAT) cromode |= CR_OCREAT;
if(omode & qdbm_Curia_OTRUNC) cromode |= CR_OTRUNC;
}
if(omode & qdbm_Curia_ONOLCK) cromode |= CR_ONOLCK;
if(omode & qdbm_Curia_OLCKNB) cromode |= CR_OLCKNB;
if(omode & qdbm_Curia_OSPARSE) cromode |= CR_OSPARSE;
return cromode;
}
static int getcrdmode(jint dmode){
switch(dmode){
case qdbm_Curia_DOVER: return CR_DOVER;
case qdbm_Curia_DKEEP: return CR_DKEEP;
case qdbm_Curia_DCAT: return CR_DCAT;
}
return -1;
}
/* END OF FILE */
|