java_Db.c 24.4 KB
Newer Older
unknown's avatar
unknown committed
1 2 3
/*-
 * See the file LICENSE for redistribution information.
 *
unknown's avatar
unknown committed
4
 * Copyright (c) 1997-2002
unknown's avatar
unknown committed
5 6 7 8 9
 *	Sleepycat Software.  All rights reserved.
 */
#include "db_config.h"

#ifndef lint
unknown's avatar
unknown committed
10
static const char revid[] = "$Id: java_Db.c,v 11.80 2002/08/29 14:22:23 margo Exp $";
unknown's avatar
unknown committed
11 12 13 14 15 16 17
#endif /* not lint */

#include <jni.h>
#include <stdlib.h>
#include <string.h>

#include "db_int.h"
unknown's avatar
unknown committed
18 19 20
#include "dbinc/db_page.h"
#include "dbinc/btree.h"
#include "dbinc_auto/db_ext.h"
unknown's avatar
unknown committed
21
#include "java_util.h"
unknown's avatar
unknown committed
22
#include "java_stat_auto.h"
unknown's avatar
unknown committed
23 24 25 26 27 28 29 30 31 32 33
#include "com_sleepycat_db_Db.h"

/* This struct is used in Db.verify and its callback */
struct verify_callback_struct {
	JNIEnv *env;
	jobject streamobj;
	jbyteArray bytes;
	int nbytes;
	jmethodID writemid;
};

unknown's avatar
unknown committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
JAVADB_GET_FLD(Db, jint, flags_1raw, DB, flags)

JAVADB_SET_METH(Db, jint, flags, DB, flags)
JAVADB_SET_METH(Db, jint, h_1ffactor, DB, h_ffactor)
JAVADB_SET_METH(Db, jint, h_1nelem, DB, h_nelem)
JAVADB_SET_METH(Db, jint, lorder, DB, lorder)
JAVADB_SET_METH(Db, jint, re_1delim, DB, re_delim)
JAVADB_SET_METH(Db, jint, re_1len, DB, re_len)
JAVADB_SET_METH(Db, jint, re_1pad, DB, re_pad)
JAVADB_SET_METH(Db, jint, q_1extentsize, DB, q_extentsize)
JAVADB_SET_METH(Db, jint, bt_1maxkey, DB, bt_maxkey)
JAVADB_SET_METH(Db, jint, bt_1minkey, DB, bt_minkey)

/*
 * This only gets called once ever, at the beginning of execution
unknown's avatar
unknown committed
49 50 51 52 53 54
 * and can be used to initialize unchanging methodIds, fieldIds, etc.
 */
JNIEXPORT void JNICALL Java_com_sleepycat_db_Db_one_1time_1init
  (JNIEnv *jnienv,  /*Db.class*/ jclass jthisclass)
{
	COMPQUIET(jthisclass, NULL);
unknown's avatar
unknown committed
55 56

	one_time_init(jnienv);
unknown's avatar
unknown committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_Db__1init
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbEnv*/ jobject jdbenv, jint flags)
{
	int err;
	DB *db;
	DB_JAVAINFO *dbinfo;
	DB_ENV *dbenv;

	dbenv = get_DB_ENV(jnienv, jdbenv);
	dbinfo = get_DB_JAVAINFO(jnienv, jthis);
	DB_ASSERT(dbinfo == NULL);

	err = db_create(&db, dbenv, flags);
	if (verify_return(jnienv, err, 0)) {
		set_private_dbobj(jnienv, name_DB, jthis, db);
unknown's avatar
unknown committed
74
		dbinfo = dbji_construct(jnienv, jthis, flags);
unknown's avatar
unknown committed
75
		set_private_info(jnienv, name_DB, jthis, dbinfo);
unknown's avatar
unknown committed
76
		db->api_internal = dbinfo;
unknown's avatar
unknown committed
77 78 79
	}
}

unknown's avatar
unknown committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
JNIEXPORT void JNICALL Java_com_sleepycat_db_Db__1associate
    (JNIEnv *jnienv, /*Db*/ jobject jthis, /* DbTxn */ jobject jtxn,
     /*Db*/ jobject jsecondary, /*DbSecondaryKeyCreate*/ jobject jcallback,
     jint flags)
{
	DB *db, *secondary;
	DB_JAVAINFO *second_info;
	DB_TXN *txn;

	db = get_DB(jnienv, jthis);
	txn = get_DB_TXN(jnienv, jtxn);
	secondary = get_DB(jnienv, jsecondary);

	second_info = (DB_JAVAINFO*)secondary->api_internal;
	dbji_set_assoc_object(second_info, jnienv, db, txn, secondary,
			      jcallback, flags);

}

unknown's avatar
unknown committed
99 100 101 102 103 104 105 106 107 108 109 110
JNIEXPORT jint JNICALL Java_com_sleepycat_db_Db__1close
  (JNIEnv *jnienv, /*Db*/ jobject jthis, jint flags)
{
	int err;
	DB *db;
	DB_JAVAINFO *dbinfo;

	db = get_DB(jnienv, jthis);
	dbinfo = get_DB_JAVAINFO(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		return (0);

unknown's avatar
unknown committed
111 112
	/*
	 * Null out the private data to indicate the DB is invalid.
unknown's avatar
unknown committed
113 114 115 116 117 118
	 * We do this in advance to help guard against multithreading
	 * issues.
	 */
	set_private_dbobj(jnienv, name_DB, jthis, 0);

	err = db->close(db, flags);
unknown's avatar
unknown committed
119
	verify_return(jnienv, err, 0);
unknown's avatar
unknown committed
120 121 122 123 124
	dbji_dealloc(dbinfo, jnienv);

	return (err);
}

unknown's avatar
unknown committed
125 126
/*
 * We are being notified that the parent DbEnv has closed.
unknown's avatar
unknown committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
 * Zero out the pointer to the DB, since it is no longer
 * valid, to prevent mistakes.  The user will get a null
 * pointer exception if they try to use this Db again.
 */
JNIEXPORT void JNICALL Java_com_sleepycat_db_Db__1notify_1internal
  (JNIEnv *jnienv, /*Db*/ jobject jthis)
{
	set_private_dbobj(jnienv, name_DB, jthis, 0);
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_Db_append_1recno_1changed
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbAppendRecno*/ jobject jcallback)
{
	DB *db;
	DB_JAVAINFO *dbinfo;

	db = get_DB(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		return;

unknown's avatar
unknown committed
147
	dbinfo = (DB_JAVAINFO*)db->api_internal;
unknown's avatar
unknown committed
148 149 150 151 152 153 154 155 156 157 158 159 160
	dbji_set_append_recno_object(dbinfo, jnienv, db, jcallback);
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_Db_bt_1compare_1changed
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbBtreeCompare*/ jobject jbtcompare)
{
	DB *db;
	DB_JAVAINFO *dbinfo;

	db = get_DB(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		return;

unknown's avatar
unknown committed
161
	dbinfo = (DB_JAVAINFO*)db->api_internal;
unknown's avatar
unknown committed
162 163 164 165 166 167 168 169 170 171 172 173 174
	dbji_set_bt_compare_object(dbinfo, jnienv, db, jbtcompare);
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_Db_bt_1prefix_1changed
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbBtreePrefix*/ jobject jbtprefix)
{
	DB *db;
	DB_JAVAINFO *dbinfo;

	db = get_DB(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		return;

unknown's avatar
unknown committed
175
	dbinfo = (DB_JAVAINFO*)db->api_internal;
unknown's avatar
unknown committed
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
	dbji_set_bt_prefix_object(dbinfo, jnienv, db, jbtprefix);
}

JNIEXPORT jobject JNICALL Java_com_sleepycat_db_Db_cursor
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbTxn*/ jobject txnid, jint flags)
{
	int err;
	DBC *dbc;
	DB *db = get_DB(jnienv, jthis);
	DB_TXN *dbtxnid = get_DB_TXN(jnienv, txnid);

	if (!verify_non_null(jnienv, db))
		return (NULL);
	err = db->cursor(db, dbtxnid, &dbc, flags);
	verify_return(jnienv, err, 0);
	return (get_Dbc(jnienv, dbc));
}

JNIEXPORT jint JNICALL Java_com_sleepycat_db_Db_del
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbTxn*/ jobject txnid,
   /*Dbt*/ jobject key, jint dbflags)
{
	int err;
	DB_TXN *dbtxnid;
	DB *db;
unknown's avatar
unknown committed
201
	LOCKED_DBT lkey;
unknown's avatar
unknown committed
202 203 204 205 206 207 208

	err = 0;
	db = get_DB(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		return (0);

	dbtxnid = get_DB_TXN(jnienv, txnid);
unknown's avatar
unknown committed
209
	if (locked_dbt_get(&lkey, jnienv, db->dbenv, key, inOp) != 0)
unknown's avatar
unknown committed
210 211
		goto out;

unknown's avatar
unknown committed
212 213
	err = db->del(db, dbtxnid, &lkey.javainfo->dbt, dbflags);
	if (!DB_RETOK_DBDEL(err))
unknown's avatar
unknown committed
214 215 216
		verify_return(jnienv, err, 0);

 out:
unknown's avatar
unknown committed
217
	locked_dbt_put(&lkey, jnienv, db->dbenv);
unknown's avatar
unknown committed
218 219 220 221 222 223 224 225 226 227 228 229 230
	return (err);
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_Db_dup_1compare_1changed
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbDupCompare*/ jobject jdupcompare)
{
	DB *db;
	DB_JAVAINFO *dbinfo;

	db = get_DB(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		return;

unknown's avatar
unknown committed
231
	dbinfo = (DB_JAVAINFO*)db->api_internal;
unknown's avatar
unknown committed
232 233 234 235 236 237 238
	dbji_set_dup_compare_object(dbinfo, jnienv, db, jdupcompare);
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_Db_err
  (JNIEnv *jnienv, /*Db*/ jobject jthis, jint ecode, jstring msg)
{
	DB *db;
unknown's avatar
unknown committed
239
	LOCKED_STRING ls_msg;
unknown's avatar
unknown committed
240

unknown's avatar
unknown committed
241
	if (locked_string_get(&ls_msg, jnienv, msg) != 0)
unknown's avatar
unknown committed
242 243 244 245 246
		goto out;
	db = get_DB(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		goto out;

unknown's avatar
unknown committed
247
	db->err(db, ecode, "%s", ls_msg.string);
unknown's avatar
unknown committed
248 249

 out:
unknown's avatar
unknown committed
250
	locked_string_put(&ls_msg, jnienv);
unknown's avatar
unknown committed
251 252 253 254 255
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_Db_errx
  (JNIEnv *jnienv, /*Db*/ jobject jthis, jstring msg)
{
unknown's avatar
unknown committed
256
	LOCKED_STRING ls_msg;
unknown's avatar
unknown committed
257 258
	DB *db = get_DB(jnienv, jthis);

unknown's avatar
unknown committed
259
	if (locked_string_get(&ls_msg, jnienv, msg) != 0)
unknown's avatar
unknown committed
260 261 262 263
		goto out;
	if (!verify_non_null(jnienv, db))
		goto out;

unknown's avatar
unknown committed
264
	db->errx(db, "%s", ls_msg.string);
unknown's avatar
unknown committed
265 266

 out:
unknown's avatar
unknown committed
267
	locked_string_put(&ls_msg, jnienv);
unknown's avatar
unknown committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
}

JNIEXPORT jint JNICALL Java_com_sleepycat_db_Db_fd
  (JNIEnv *jnienv, /*Db*/ jobject jthis)
{
	int err;
	int return_value = 0;
	DB *db = get_DB(jnienv, jthis);

	if (!verify_non_null(jnienv, db))
		return (0);

	err = db->fd(db, &return_value);
	verify_return(jnienv, err, 0);

	return (return_value);
}

unknown's avatar
unknown committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
JNIEXPORT void JNICALL Java_com_sleepycat_db_Db_set_1encrypt
  (JNIEnv *jnienv, /*Db*/ jobject jthis, jstring jpasswd, jint flags)
{
	int err;
	DB *db;
	LOCKED_STRING ls_passwd;

	db = get_DB(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		return;
	if (locked_string_get(&ls_passwd, jnienv, jpasswd) != 0)
		goto out;

	err = db->set_encrypt(db, ls_passwd.string, flags);
	verify_return(jnienv, err, 0);

out:	locked_string_put(&ls_passwd, jnienv);
}

unknown's avatar
unknown committed
305 306 307 308 309 310 311 312 313 314
JNIEXPORT void JNICALL Java_com_sleepycat_db_Db_feedback_1changed
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbFeedback*/ jobject jfeedback)
{
	DB *db;
	DB_JAVAINFO *dbinfo;

	db = get_DB(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		return;

unknown's avatar
unknown committed
315
	dbinfo = (DB_JAVAINFO*)db->api_internal;
unknown's avatar
unknown committed
316 317 318 319 320 321 322 323 324
	dbji_set_feedback_object(dbinfo, jnienv, db, jfeedback);
}

JNIEXPORT jint JNICALL Java_com_sleepycat_db_Db_get
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbTxn*/ jobject txnid,
   /*Dbt*/ jobject key, /*Dbt*/ jobject data, jint flags)
{
	int err, op_flags, retry;
	DB *db;
unknown's avatar
unknown committed
325
	DB_ENV *dbenv;
unknown's avatar
unknown committed
326 327
	OpKind keyop, dataop;
	DB_TXN *dbtxnid;
unknown's avatar
unknown committed
328
	LOCKED_DBT lkey, ldata;
unknown's avatar
unknown committed
329 330 331 332 333

	err = 0;
	db = get_DB(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		goto out3;
unknown's avatar
unknown committed
334
	dbenv = db->dbenv;
unknown's avatar
unknown committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

	/* Depending on flags, the key may be input/output. */
	keyop = inOp;
	dataop = outOp;
	op_flags = flags & DB_OPFLAGS_MASK;
	if (op_flags == DB_SET_RECNO) {
		keyop = inOutOp;
	}
	else if (op_flags == DB_GET_BOTH) {
		keyop = inOutOp;
		dataop = inOutOp;
	}

	dbtxnid = get_DB_TXN(jnienv, txnid);

unknown's avatar
unknown committed
350
	if (locked_dbt_get(&lkey, jnienv, dbenv, key, keyop) != 0)
unknown's avatar
unknown committed
351
		goto out2;
unknown's avatar
unknown committed
352
	if (locked_dbt_get(&ldata, jnienv, dbenv, data, dataop) != 0)
unknown's avatar
unknown committed
353 354
		goto out1;
	for (retry = 0; retry < 3; retry++) {
unknown's avatar
unknown committed
355 356
		err = db->get(db,
		    dbtxnid, &lkey.javainfo->dbt, &ldata.javainfo->dbt, flags);
unknown's avatar
unknown committed
357

unknown's avatar
unknown committed
358 359
		/*
		 * If we failed due to lack of memory in our DBT arrays,
unknown's avatar
unknown committed
360 361 362 363
		 * retry.
		 */
		if (err != ENOMEM)
			break;
unknown's avatar
unknown committed
364 365
		if (!locked_dbt_realloc(&lkey, jnienv, dbenv) &&
		    !locked_dbt_realloc(&ldata, jnienv, dbenv))
unknown's avatar
unknown committed
366 367 368
			break;
	}
 out1:
unknown's avatar
unknown committed
369
	locked_dbt_put(&ldata, jnienv, dbenv);
unknown's avatar
unknown committed
370
 out2:
unknown's avatar
unknown committed
371
	locked_dbt_put(&lkey, jnienv, dbenv);
unknown's avatar
unknown committed
372
 out3:
unknown's avatar
unknown committed
373 374 375 376 377
	if (!DB_RETOK_DBGET(err)) {
		if (verify_dbt(jnienv, err, &lkey) &&
		    verify_dbt(jnienv, err, &ldata))
			verify_return(jnienv, err, 0);
	}
unknown's avatar
unknown committed
378 379 380 381 382 383 384 385 386 387 388 389 390
	return (err);
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_Db_hash_1changed
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbHash*/ jobject jhash)
{
	DB *db;
	DB_JAVAINFO *dbinfo;

	db = get_DB(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		return;

unknown's avatar
unknown committed
391
	dbinfo = (DB_JAVAINFO*)db->api_internal;
unknown's avatar
unknown committed
392 393 394 395 396 397 398 399
	dbji_set_h_hash_object(dbinfo, jnienv, db, jhash);
}

JNIEXPORT jobject JNICALL Java_com_sleepycat_db_Db_join
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*Dbc[]*/ jobjectArray curslist,
   jint flags)
{
	int err;
unknown's avatar
unknown committed
400 401 402
	DB *db;
	int count;
	DBC **newlist;
unknown's avatar
unknown committed
403 404
	DBC *dbc;
	int i;
unknown's avatar
unknown committed
405 406 407 408 409 410 411 412 413
	int size;

	db = get_DB(jnienv, jthis);
	count = (*jnienv)->GetArrayLength(jnienv, curslist);
	size = sizeof(DBC *) * (count+1);
	if ((err = __os_malloc(db->dbenv, size, &newlist)) != 0) {
		if (!verify_return(jnienv, err, 0))
			return (NULL);
	}
unknown's avatar
unknown committed
414 415

	/* Convert the java array of Dbc's to a C array of DBC's. */
unknown's avatar
unknown committed
416 417 418
	for (i = 0; i < count; i++) {
		jobject jobj =
		    (*jnienv)->GetObjectArrayElement(jnienv, curslist, i);
unknown's avatar
unknown committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
		if (jobj == 0) {
			/*
			 * An embedded null in the array is treated
			 * as an endpoint.
			 */
			newlist[i] = 0;
			break;
		}
		else {
			newlist[i] = get_DBC(jnienv, jobj);
		}
	}
	newlist[count] = 0;

	if (!verify_non_null(jnienv, db))
		return (NULL);

	err = db->join(db, newlist, &dbc, flags);
	verify_return(jnienv, err, 0);
unknown's avatar
unknown committed
438
	__os_free(db->dbenv, newlist);
unknown's avatar
unknown committed
439 440 441 442 443

	return (get_Dbc(jnienv, dbc));
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_Db_key_1range
unknown's avatar
unknown committed
444
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbTxn*/ jobject txnid,
unknown's avatar
unknown committed
445 446 447
   /*Dbt*/ jobject jkey, jobject /*DbKeyRange*/ range, jint flags)
{
	int err;
unknown's avatar
unknown committed
448 449 450
	DB *db;
	DB_TXN *dbtxnid;
	LOCKED_DBT lkey;
unknown's avatar
unknown committed
451 452 453 454
	DB_KEY_RANGE result;
	jfieldID fid;
	jclass krclass;

unknown's avatar
unknown committed
455 456
	db = get_DB(jnienv, jthis);
	dbtxnid = get_DB_TXN(jnienv, txnid);
unknown's avatar
unknown committed
457 458 459 460
	if (!verify_non_null(jnienv, db))
		return;
	if (!verify_non_null(jnienv, range))
		return;
unknown's avatar
unknown committed
461
	if (locked_dbt_get(&lkey, jnienv, db->dbenv, jkey, inOp) != 0)
unknown's avatar
unknown committed
462
		goto out;
unknown's avatar
unknown committed
463
	err = db->key_range(db, dbtxnid, &lkey.javainfo->dbt, &result, flags);
unknown's avatar
unknown committed
464 465
	if (verify_return(jnienv, err, 0)) {
		/* fill in the values of the DbKeyRange structure */
unknown's avatar
unknown committed
466 467
		if ((krclass = get_class(jnienv, "DbKeyRange")) == NULL)
			return;	/* An exception has been posted. */
unknown's avatar
unknown committed
468 469 470 471 472 473 474 475
		fid = (*jnienv)->GetFieldID(jnienv, krclass, "less", "D");
		(*jnienv)->SetDoubleField(jnienv, range, fid, result.less);
		fid = (*jnienv)->GetFieldID(jnienv, krclass, "equal", "D");
		(*jnienv)->SetDoubleField(jnienv, range, fid, result.equal);
		fid = (*jnienv)->GetFieldID(jnienv, krclass, "greater", "D");
		(*jnienv)->SetDoubleField(jnienv, range, fid, result.greater);
	}
 out:
unknown's avatar
unknown committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
	locked_dbt_put(&lkey, jnienv, db->dbenv);
}

JNIEXPORT jint JNICALL Java_com_sleepycat_db_Db_pget
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbTxn*/ jobject txnid,
   /*Dbt*/ jobject key, /*Dbt*/ jobject rkey, /*Dbt*/ jobject data, jint flags)
{
	int err, op_flags, retry;
	DB *db;
	DB_ENV *dbenv;
	OpKind keyop, rkeyop, dataop;
	DB_TXN *dbtxnid;
	LOCKED_DBT lkey, lrkey, ldata;

	err = 0;
	db = get_DB(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		goto out4;
	dbenv = db->dbenv;

	/* Depending on flags, the key may be input/output. */
	keyop = inOp;
	rkeyop = outOp;
	dataop = outOp;
	op_flags = flags & DB_OPFLAGS_MASK;
	if (op_flags == DB_SET_RECNO) {
		keyop = inOutOp;
	}
	else if (op_flags == DB_GET_BOTH) {
		keyop = inOutOp;
		rkeyop = inOutOp;
		dataop = inOutOp;
	}

	dbtxnid = get_DB_TXN(jnienv, txnid);

	if (locked_dbt_get(&lkey, jnienv, dbenv, key, keyop) != 0)
		goto out3;
	if (locked_dbt_get(&lrkey, jnienv, dbenv, rkey, rkeyop) != 0)
		goto out2;
	if (locked_dbt_get(&ldata, jnienv, dbenv, data, dataop) != 0)
		goto out1;
	for (retry = 0; retry < 3; retry++) {
		err = db->pget(db, dbtxnid, &lkey.javainfo->dbt,
		    &lrkey.javainfo->dbt, &ldata.javainfo->dbt, flags);

		/*
		 * If we failed due to lack of memory in our DBT arrays,
		 * retry.
		 */
		if (err != ENOMEM)
			break;
		if (!locked_dbt_realloc(&lkey, jnienv, dbenv) &&
		    !locked_dbt_realloc(&lrkey, jnienv, dbenv) &&
		    !locked_dbt_realloc(&ldata, jnienv, dbenv))
			break;
	}
 out1:
	locked_dbt_put(&ldata, jnienv, dbenv);
 out2:
	locked_dbt_put(&lrkey, jnienv, dbenv);
 out3:
	locked_dbt_put(&lkey, jnienv, dbenv);
 out4:
	if (!DB_RETOK_DBGET(err)) {
		if (verify_dbt(jnienv, err, &lkey) &&
		    verify_dbt(jnienv, err, &lrkey) &&
		    verify_dbt(jnienv, err, &ldata))
			verify_return(jnienv, err, 0);
	}
	return (err);
unknown's avatar
unknown committed
547 548 549 550 551 552 553 554
}

JNIEXPORT jint JNICALL Java_com_sleepycat_db_Db_put
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbTxn*/ jobject txnid,
   /*Dbt*/ jobject key, /*Dbt*/ jobject data, jint flags)
{
	int err;
	DB *db;
unknown's avatar
unknown committed
555
	DB_ENV *dbenv;
unknown's avatar
unknown committed
556
	DB_TXN *dbtxnid;
unknown's avatar
unknown committed
557 558
	LOCKED_DBT lkey, ldata;
	OpKind keyop;
unknown's avatar
unknown committed
559 560 561 562 563

	err = 0;
	db = get_DB(jnienv, jthis);
	dbtxnid = get_DB_TXN(jnienv, txnid);
	if (!verify_non_null(jnienv, db))
unknown's avatar
unknown committed
564 565
		return (0);   /* error will be thrown, retval doesn't matter */
	dbenv = db->dbenv;
unknown's avatar
unknown committed
566

unknown's avatar
unknown committed
567 568 569 570 571 572 573 574 575 576
	/*
	 * For DB_APPEND, the key may be output-only;  for all other flags,
	 * it's input-only.
	 */
	if ((flags & DB_OPFLAGS_MASK) == DB_APPEND)
		keyop = outOp;
	else
		keyop = inOp;

	if (locked_dbt_get(&lkey, jnienv, dbenv, key, keyop) != 0)
unknown's avatar
unknown committed
577
		goto out2;
unknown's avatar
unknown committed
578
	if (locked_dbt_get(&ldata, jnienv, dbenv, data, inOp) != 0)
unknown's avatar
unknown committed
579 580 581 582
		goto out1;

	if (!verify_non_null(jnienv, db))
		goto out1;
unknown's avatar
unknown committed
583 584 585 586

	err = db->put(db,
	    dbtxnid, &lkey.javainfo->dbt, &ldata.javainfo->dbt, flags);
	if (!DB_RETOK_DBPUT(err))
unknown's avatar
unknown committed
587
		verify_return(jnienv, err, 0);
unknown's avatar
unknown committed
588

unknown's avatar
unknown committed
589
 out1:
unknown's avatar
unknown committed
590
	locked_dbt_put(&ldata, jnienv, dbenv);
unknown's avatar
unknown committed
591
 out2:
unknown's avatar
unknown committed
592
	locked_dbt_put(&lkey, jnienv, dbenv);
unknown's avatar
unknown committed
593 594 595
	return (err);
}

unknown's avatar
unknown committed
596 597 598
JNIEXPORT void JNICALL Java_com_sleepycat_db_Db__1remove
  (JNIEnv *jnienv, /*Db*/ jobject jthis,
   jstring file, jstring database, jint flags)
unknown's avatar
unknown committed
599 600 601 602
{
	int err;
	DB *db;
	DB_JAVAINFO *dbinfo;
unknown's avatar
unknown committed
603 604
	LOCKED_STRING ls_file;
	LOCKED_STRING ls_database;
unknown's avatar
unknown committed
605 606 607

	db = get_DB(jnienv, jthis);
	dbinfo = get_DB_JAVAINFO(jnienv, jthis);
unknown's avatar
unknown committed
608

unknown's avatar
unknown committed
609 610
	if (!verify_non_null(jnienv, db))
		return;
unknown's avatar
unknown committed
611
	if (locked_string_get(&ls_file, jnienv, file) != 0)
unknown's avatar
unknown committed
612
		goto out2;
unknown's avatar
unknown committed
613
	if (locked_string_get(&ls_database, jnienv, database) != 0)
unknown's avatar
unknown committed
614
		goto out1;
unknown's avatar
unknown committed
615
	err = db->remove(db, ls_file.string, ls_database.string, flags);
unknown's avatar
unknown committed
616 617

	set_private_dbobj(jnienv, name_DB, jthis, 0);
unknown's avatar
unknown committed
618
	verify_return(jnienv, err, EXCEPTION_FILE_NOT_FOUND);
unknown's avatar
unknown committed
619 620

 out1:
unknown's avatar
unknown committed
621
	locked_string_put(&ls_database, jnienv);
unknown's avatar
unknown committed
622
 out2:
unknown's avatar
unknown committed
623 624 625
	locked_string_put(&ls_file, jnienv);

	dbji_dealloc(dbinfo, jnienv);
unknown's avatar
unknown committed
626 627
}

unknown's avatar
unknown committed
628 629 630
JNIEXPORT void JNICALL Java_com_sleepycat_db_Db__1rename
  (JNIEnv *jnienv, /*Db*/ jobject jthis,
   jstring file, jstring database, jstring newname, jint flags)
unknown's avatar
unknown committed
631 632
{
	int err;
unknown's avatar
unknown committed
633 634 635 636 637
	DB *db;
	DB_JAVAINFO *dbinfo;
	LOCKED_STRING ls_file;
	LOCKED_STRING ls_database;
	LOCKED_STRING ls_newname;
unknown's avatar
unknown committed
638

unknown's avatar
unknown committed
639
	db = get_DB(jnienv, jthis);
unknown's avatar
unknown committed
640 641 642
	dbinfo = get_DB_JAVAINFO(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		return;
unknown's avatar
unknown committed
643 644 645
	if (locked_string_get(&ls_file, jnienv, file) != 0)
		goto out3;
	if (locked_string_get(&ls_database, jnienv, database) != 0)
unknown's avatar
unknown committed
646
		goto out2;
unknown's avatar
unknown committed
647
	if (locked_string_get(&ls_newname, jnienv, newname) != 0)
unknown's avatar
unknown committed
648 649
		goto out1;

unknown's avatar
unknown committed
650 651 652
	err = db->rename(db, ls_file.string, ls_database.string,
			 ls_newname.string, flags);

unknown's avatar
unknown committed
653
	verify_return(jnienv, err, EXCEPTION_FILE_NOT_FOUND);
unknown's avatar
unknown committed
654
	set_private_dbobj(jnienv, name_DB, jthis, 0);
unknown's avatar
unknown committed
655 656

 out1:
unknown's avatar
unknown committed
657
	locked_string_put(&ls_newname, jnienv);
unknown's avatar
unknown committed
658
 out2:
unknown's avatar
unknown committed
659 660 661
	locked_string_put(&ls_database, jnienv);
 out3:
	locked_string_put(&ls_file, jnienv);
unknown's avatar
unknown committed
662

unknown's avatar
unknown committed
663
	dbji_dealloc(dbinfo, jnienv);
unknown's avatar
unknown committed
664 665
}

unknown's avatar
unknown committed
666 667 668 669 670 671 672
JAVADB_METHOD(Db_set_1pagesize, (JAVADB_ARGS, jlong pagesize), DB,
    set_pagesize, (c_this, (u_int32_t)pagesize))
JAVADB_METHOD(Db_set_1cachesize,
    (JAVADB_ARGS, jint gbytes, jint bytes, jint ncaches), DB,
    set_cachesize, (c_this, gbytes, bytes, ncaches))
JAVADB_METHOD(Db_set_1cache_1priority, (JAVADB_ARGS, jint priority), DB,
    set_cache_priority, (c_this, (DB_CACHE_PRIORITY)priority))
unknown's avatar
unknown committed
673 674 675 676 677 678 679 680 681 682 683 684 685

JNIEXPORT void JNICALL
  Java_com_sleepycat_db_Db_set_1re_1source
  (JNIEnv *jnienv, /*Db*/ jobject jthis, jstring re_source)
{
	int err;
	DB *db;

	db = get_DB(jnienv, jthis);
	if (verify_non_null(jnienv, db)) {

		/* XXX does the string from get_c_string ever get freed? */
		if (re_source != NULL)
unknown's avatar
unknown committed
686 687
			err = db->set_re_source(db,
			    get_c_string(jnienv, re_source));
unknown's avatar
unknown committed
688 689 690 691 692 693 694 695 696 697
		else
			err = db->set_re_source(db, 0);

		verify_return(jnienv, err, 0);
	}
}

JNIEXPORT jobject JNICALL Java_com_sleepycat_db_Db_stat
  (JNIEnv *jnienv, jobject jthis, jint flags)
{
unknown's avatar
unknown committed
698
	DB *db;
unknown's avatar
unknown committed
699 700 701
	DB_BTREE_STAT *bstp;
	DB_HASH_STAT *hstp;
	DB_QUEUE_STAT *qstp;
unknown's avatar
unknown committed
702 703 704 705 706
	DBTYPE dbtype;
	jobject retval;
	jclass dbclass;
	size_t bytesize;
	void *statp;
unknown's avatar
unknown committed
707

unknown's avatar
unknown committed
708 709 710 711 712
	bytesize = 0;
	retval = NULL;
	statp = NULL;

	db = get_DB(jnienv, jthis);
unknown's avatar
unknown committed
713 714 715
	if (!verify_non_null(jnienv, db))
		return (NULL);

unknown's avatar
unknown committed
716 717
	if (verify_return(jnienv, db->stat(db, &statp, flags), 0) &&
	    verify_return(jnienv, db->get_type(db, &dbtype), 0)) {
unknown's avatar
unknown committed
718 719 720 721 722
		switch (dbtype) {
			/* Btree and recno share the same stat structure */
		case DB_BTREE:
		case DB_RECNO:
			bstp = (DB_BTREE_STAT *)statp;
unknown's avatar
unknown committed
723
			bytesize = sizeof(DB_BTREE_STAT);
unknown's avatar
unknown committed
724 725
			retval = create_default_object(jnienv,
						       name_DB_BTREE_STAT);
unknown's avatar
unknown committed
726 727 728
			if ((dbclass =
			    get_class(jnienv, name_DB_BTREE_STAT)) == NULL)
				break;	/* An exception has been posted. */
unknown's avatar
unknown committed
729

unknown's avatar
unknown committed
730
			__jv_fill_bt_stat(jnienv, dbclass, retval, bstp);
unknown's avatar
unknown committed
731 732 733 734 735
			break;

			/* Hash stat structure */
		case DB_HASH:
			hstp = (DB_HASH_STAT *)statp;
unknown's avatar
unknown committed
736
			bytesize = sizeof(DB_HASH_STAT);
unknown's avatar
unknown committed
737 738
			retval = create_default_object(jnienv,
						       name_DB_HASH_STAT);
unknown's avatar
unknown committed
739 740 741
			if ((dbclass =
			    get_class(jnienv, name_DB_HASH_STAT)) == NULL)
				break;	/* An exception has been posted. */
unknown's avatar
unknown committed
742

unknown's avatar
unknown committed
743
			__jv_fill_h_stat(jnienv, dbclass, retval, hstp);
unknown's avatar
unknown committed
744 745 746 747
			break;

		case DB_QUEUE:
			qstp = (DB_QUEUE_STAT *)statp;
unknown's avatar
unknown committed
748
			bytesize = sizeof(DB_QUEUE_STAT);
unknown's avatar
unknown committed
749 750
			retval = create_default_object(jnienv,
						       name_DB_QUEUE_STAT);
unknown's avatar
unknown committed
751 752 753 754 755
			if ((dbclass =
			    get_class(jnienv, name_DB_QUEUE_STAT)) == NULL)
				break;	/* An exception has been posted. */

			__jv_fill_qam_stat(jnienv, dbclass, retval, qstp);
unknown's avatar
unknown committed
756 757 758 759 760 761
			break;

			/* That's all the database types we're aware of! */
		default:
			report_exception(jnienv,
					 "Db.stat not implemented for types"
unknown's avatar
unknown committed
762 763
					 " other than BTREE, HASH, QUEUE,"
					 " and RECNO",
unknown's avatar
unknown committed
764 765 766
					 EINVAL, 0);
			break;
		}
unknown's avatar
unknown committed
767 768
		if (bytesize != 0)
			__os_ufree(db->dbenv, statp);
unknown's avatar
unknown committed
769 770 771 772
	}
	return (retval);
}

unknown's avatar
unknown committed
773 774
JAVADB_METHOD(Db_sync, (JAVADB_ARGS, jint flags), DB,
    sync, (c_this, flags))
unknown's avatar
unknown committed
775 776 777 778 779

JNIEXPORT jboolean JNICALL Java_com_sleepycat_db_Db_get_1byteswapped
  (JNIEnv *jnienv, /*Db*/ jobject jthis)
{
	DB *db;
unknown's avatar
unknown committed
780 781 782 783
	int err, isbyteswapped;

	/* This value should never be seen, because of the exception. */
	isbyteswapped = 0;
unknown's avatar
unknown committed
784 785 786 787 788

	db = get_DB(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		return (0);

unknown's avatar
unknown committed
789 790 791 792
	err = db->get_byteswapped(db, &isbyteswapped);
	(void)verify_return(jnienv, err, 0);

	return ((jboolean)isbyteswapped);
unknown's avatar
unknown committed
793 794 795 796 797 798
}

JNIEXPORT jint JNICALL Java_com_sleepycat_db_Db_get_1type
  (JNIEnv *jnienv, /*Db*/ jobject jthis)
{
	DB *db;
unknown's avatar
unknown committed
799 800 801 802 803
	int err;
	DBTYPE dbtype;

	/* This value should never be seen, because of the exception. */
	dbtype = DB_UNKNOWN;
unknown's avatar
unknown committed
804 805 806 807 808

	db = get_DB(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		return (0);

unknown's avatar
unknown committed
809 810 811 812
	err = db->get_type(db, &dbtype);
	(void)verify_return(jnienv, err, 0);

	return ((jint)dbtype);
unknown's avatar
unknown committed
813 814 815
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_Db__1open
unknown's avatar
unknown committed
816 817
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbTxn*/ jobject txnid,
   jstring file, jstring database, jint type, jint flags, jint mode)
unknown's avatar
unknown committed
818 819 820
{
	int err;
	DB *db;
unknown's avatar
unknown committed
821 822 823
	DB_TXN *dbtxnid;
	LOCKED_STRING ls_file;
	LOCKED_STRING ls_database;
unknown's avatar
unknown committed
824

unknown's avatar
unknown committed
825
	/* Java is assumed to be threaded */
unknown's avatar
unknown committed
826 827 828
	flags |= DB_THREAD;

	db = get_DB(jnienv, jthis);
unknown's avatar
unknown committed
829 830 831

	dbtxnid = get_DB_TXN(jnienv, txnid);
	if (locked_string_get(&ls_file, jnienv, file) != 0)
unknown's avatar
unknown committed
832
		goto out2;
unknown's avatar
unknown committed
833
	if (locked_string_get(&ls_database, jnienv, database) != 0)
unknown's avatar
unknown committed
834 835
		goto out1;
	if (verify_non_null(jnienv, db)) {
unknown's avatar
unknown committed
836
		err = db->open(db, dbtxnid, ls_file.string, ls_database.string,
unknown's avatar
unknown committed
837 838 839 840
			       (DBTYPE)type, flags, mode);
		verify_return(jnienv, err, EXCEPTION_FILE_NOT_FOUND);
	}
 out1:
unknown's avatar
unknown committed
841
	locked_string_put(&ls_database, jnienv);
unknown's avatar
unknown committed
842
 out2:
unknown's avatar
unknown committed
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
	locked_string_put(&ls_file, jnienv);
}

JNIEXPORT jint JNICALL Java_com_sleepycat_db_Db_truncate
  (JNIEnv *jnienv, /*Db*/ jobject jthis, /*DbTxn*/ jobject jtxnid, jint flags)
{
	int err;
	DB *db;
	u_int32_t count;
	DB_TXN *dbtxnid;

	db = get_DB(jnienv, jthis);
	dbtxnid = get_DB_TXN(jnienv, jtxnid);
	count = 0;
	if (verify_non_null(jnienv, db)) {
		err = db->truncate(db, dbtxnid, &count, flags);
		verify_return(jnienv, err, 0);
	}
	return (jint)count;
unknown's avatar
unknown committed
862 863 864 865 866 867 868 869
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_Db_upgrade
  (JNIEnv *jnienv, /*Db*/ jobject jthis, jstring name,
   jint flags)
{
	int err;
	DB *db = get_DB(jnienv, jthis);
unknown's avatar
unknown committed
870
	LOCKED_STRING ls_name;
unknown's avatar
unknown committed
871 872

	if (verify_non_null(jnienv, db)) {
unknown's avatar
unknown committed
873
		if (locked_string_get(&ls_name, jnienv, name) != 0)
unknown's avatar
unknown committed
874
			goto out;
unknown's avatar
unknown committed
875
		err = db->upgrade(db, ls_name.string, flags);
unknown's avatar
unknown committed
876 877 878
		verify_return(jnienv, err, 0);
	}
 out:
unknown's avatar
unknown committed
879
	locked_string_put(&ls_name, jnienv);
unknown's avatar
unknown committed
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
}

static int java_verify_callback(void *handle, const void *str_arg)
{
	char *str;
	struct verify_callback_struct *vc;
	int len;
	JNIEnv *jnienv;

	str = (char *)str_arg;
	vc = (struct verify_callback_struct *)handle;
	jnienv = vc->env;
	len = strlen(str)+1;
	if (len > vc->nbytes) {
		vc->nbytes = len;
		vc->bytes = (*jnienv)->NewByteArray(jnienv, len);
	}

unknown's avatar
unknown committed
898 899 900 901 902 903 904 905
	if (vc->bytes != NULL) {
		(*jnienv)->SetByteArrayRegion(jnienv, vc->bytes, 0, len,
		    (jbyte*)str);
		(*jnienv)->CallVoidMethod(jnienv, vc->streamobj,
		    vc->writemid, vc->bytes, 0, len-1);
	}

	if ((*jnienv)->ExceptionOccurred(jnienv) != NULL)
unknown's avatar
unknown committed
906 907 908 909 910 911 912 913 914 915 916
		return (EIO);

	return (0);
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_Db_verify
  (JNIEnv *jnienv, /*Db*/ jobject jthis, jstring name,
   jstring subdb, jobject stream, jint flags)
{
	int err;
	DB *db;
unknown's avatar
unknown committed
917 918
	LOCKED_STRING ls_name;
	LOCKED_STRING ls_subdb;
unknown's avatar
unknown committed
919 920 921 922 923 924
	struct verify_callback_struct vcs;
	jclass streamclass;

	db = get_DB(jnienv, jthis);
	if (!verify_non_null(jnienv, db))
		return;
unknown's avatar
unknown committed
925
	if (locked_string_get(&ls_name, jnienv, name) != 0)
unknown's avatar
unknown committed
926
		goto out2;
unknown's avatar
unknown committed
927
	if (locked_string_get(&ls_subdb, jnienv, subdb) != 0)
unknown's avatar
unknown committed
928 929 930 931 932 933
		goto out1;

	/* set up everything we need for the callbacks */
	vcs.env = jnienv;
	vcs.streamobj = stream;
	vcs.nbytes = 100;
unknown's avatar
unknown committed
934 935
	if ((vcs.bytes = (*jnienv)->NewByteArray(jnienv, vcs.nbytes)) == NULL)
		goto out1;
unknown's avatar
unknown committed
936 937 938 939 940 941 942

	/* get the method ID for OutputStream.write(byte[], int, int); */
	streamclass = (*jnienv)->FindClass(jnienv, "java/io/OutputStream");
	vcs.writemid = (*jnienv)->GetMethodID(jnienv, streamclass,
					      "write", "([BII)V");

	/* invoke verify - this will invoke the callback repeatedly. */
unknown's avatar
unknown committed
943
	err = __db_verify_internal(db, ls_name.string, ls_subdb.string,
unknown's avatar
unknown committed
944 945 946 947
				   &vcs, java_verify_callback, flags);
	verify_return(jnienv, err, 0);

out1:
unknown's avatar
unknown committed
948
	locked_string_put(&ls_subdb, jnienv);
unknown's avatar
unknown committed
949
out2:
unknown's avatar
unknown committed
950
	locked_string_put(&ls_name, jnienv);
unknown's avatar
unknown committed
951 952 953 954 955 956 957 958 959 960 961 962 963
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_Db__1finalize
    (JNIEnv *jnienv, jobject jthis,
     jobject /*DbErrcall*/ errcall, jstring errpfx)
{
	DB_JAVAINFO *dbinfo;
	DB *db;

	dbinfo = get_DB_JAVAINFO(jnienv, jthis);
	db = get_DB(jnienv, jthis);
	DB_ASSERT(dbinfo != NULL);

unknown's avatar
unknown committed
964 965
	/*
	 * Note: We can never be sure if the underlying DB is attached to
unknown's avatar
unknown committed
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
	 * a DB_ENV that was already closed.  Sure, that's a user error,
	 * but it shouldn't crash the VM.  Therefore, we cannot just
	 * automatically close if the handle indicates we are not yet
	 * closed.  The best we can do is detect this and report it.
	 */
	if (db != NULL) {
		/* If this error occurs, this object was never closed. */
		report_errcall(jnienv, errcall, errpfx,
			       "Db.finalize: open Db object destroyed");
	}

	/* Shouldn't see this object again, but just in case */
	set_private_dbobj(jnienv, name_DB, jthis, 0);
	set_private_info(jnienv, name_DB, jthis, 0);

	dbji_destroy(dbinfo, jnienv);
}