Beispiel #1
0
int
b_inmem(int argc, char *argv[])
{
	COMPQUIET(argc, 0);
	COMPQUIET(argv, NULL);
	return (0);
}
Beispiel #2
0
JNIEXPORT jboolean JNICALL
Java_com_sleepycat_db_DbUtil_is_1big_1endian (JNIEnv *jnienv,
    jclass jthis_class)
{
	COMPQUIET(jnienv, NULL);
	COMPQUIET(jthis_class, NULL);

	return (__db_isbigendian() ? JNI_TRUE : JNI_FALSE);
}
Beispiel #3
0
JNIEXPORT void JNICALL Java_com_sleepycat_db_DbLsn_init_1lsn
  (JNIEnv *jnienv, /*DbLsn*/ jobject jthis)
{
	/* Note: the DB_LSN object stored in the private_dbobj_
	 * is allocated in get_DbLsn().
	 */

	COMPQUIET(jnienv, NULL);
	COMPQUIET(jthis, NULL);
}
Beispiel #4
0
void dbjie_call_feedback(DB_ENV_JAVAINFO *dbjie, DB_ENV *dbenv, jobject jenv,
                         int opcode, int percent)
{
    JNIEnv *jnienv;
    jclass feedback_class;
    jmethodID id;

    COMPQUIET(dbenv, NULL);
    jnienv = dbjie_get_jnienv(dbjie);
    if (jnienv == NULL) {
        fprintf(stderr, "Cannot attach to current thread!\n");
        return;
    }

    feedback_class = get_class(jnienv, name_DbEnvFeedback);
    id = (*jnienv)->GetMethodID(jnienv, feedback_class,
                                "feedback",
                                "(Lcom/sleepycat/db/DbEnv;II)V");
    if (!id) {
        fprintf(stderr, "Cannot find callback class\n");
        return;
    }

    (*jnienv)->CallVoidMethod(jnienv, dbjie->feedback_, id,
                              jenv, (jint)opcode, (jint)percent);
}
Beispiel #5
0
JNIEXPORT void JNICALL Java_com_sleepycat_db_xa_DbXAResource__1init
  (JNIEnv *jnienv, jobject jthis, jstring home, jint rmid, jint flags)
{
	int err;
	LOCKED_STRING ls_home;
	jclass cl;
	jmethodID mid;

	COMPQUIET(jthis, NULL);
	if (locked_string_get(&ls_home, jnienv, home) != 0)
		goto out;
	if ((err = __db_xa_open((char *)ls_home.string,
				rmid, flags)) != XA_OK) {
		verify_return(jnienv, err, EXCEPTION_XA);
	}

	/*
	 * Now create the DbEnv object, it will get attached
	 * to the DB_ENV just made in __db_xa_open.
	 */
	if ((cl = get_class(jnienv, name_DB_ENV)) == NULL)
		goto out;

	mid = (*jnienv)->GetStaticMethodID(jnienv, cl,
					   "_create_DbEnv_for_XA", "(II)V");
	(*jnienv)->CallStaticVoidMethod(jnienv, cl, mid, 0, rmid);

 out:
	locked_string_put(&ls_home, jnienv);
}
Beispiel #6
0
int dbji_call_dup_compare(DB_JAVAINFO *dbji, DB *db, jobject jdb,
                          const DBT *dbt1, const DBT *dbt2)
{
    JNIEnv *jnienv;
    jobject jdbt1, jdbt2;
    DBT_JAVAINFO *dbtji1, *dbtji2;

    COMPQUIET(db, NULL);
    jnienv = dbji_get_jnienv(dbji);
    if (jnienv == NULL) {
        fprintf(stderr, "Cannot attach to current thread!\n");
        return (0);
    }

    /* XXX
     * We should have a pool of Dbt objects used for this purpose
     * instead of creating new ones each time.  Because of
     * multithreading, we may need an arbitrary number (more than two).
     * We might also have a byte arrays that grow as needed,
     * so we don't need to allocate those either.
     */
    jdbt1 = create_default_object(jnienv, name_DBT);
    jdbt2 = create_default_object(jnienv, name_DBT);
    dbtji1 = get_DBT_JAVAINFO(jnienv, jdbt1);
    memcpy(&dbtji1->dbt, dbt1, sizeof(DBT));
    dbtji1->create_array_ = 1;
    dbtji2 = get_DBT_JAVAINFO(jnienv, jdbt2);
    memcpy(&dbtji2->dbt, dbt2, sizeof(DBT));
    dbtji2->create_array_ = 1;

    DB_ASSERT(dbji->dup_compare_method_id_ != NULL);
    return (*jnienv)->CallIntMethod(jnienv, dbji->dup_compare_,
                                    dbji->dup_compare_method_id_,
                                    jdb, jdbt1, jdbt2);
}
Beispiel #7
0
int dbjie_call_recovery_init(DB_ENV_JAVAINFO *dbjie, DB_ENV *dbenv,
                             jobject jenv)
{
    JNIEnv *jnienv;
    jclass recovery_init_class;
    jmethodID id;

    COMPQUIET(dbenv, NULL);
    jnienv = dbjie_get_jnienv(dbjie);
    if (jnienv == NULL) {
        fprintf(stderr, "Cannot attach to current thread!\n");
        return (EINVAL);
    }

    recovery_init_class = get_class(jnienv, name_DbRecoveryInit);
    id = (*jnienv)->GetMethodID(jnienv, recovery_init_class,
                                "recovery_init",
                                "(Lcom/sleepycat/db/DbEnv;)V");
    if (!id) {
        fprintf(stderr, "Cannot find callback class\n");
        return (EINVAL);
    }
    return (*jnienv)->CallIntMethod(jnienv, dbjie->recovery_init_,
                                    id, jenv);
}
Beispiel #8
0
/*
 * Realloc the java array to receive data if the DBT used
 * DB_DBT_REALLOC flag with a non-null data array, and the last
 * operation set the size field to an amount greater than ulen.
 * Return 1 if these conditions are met, otherwise 0.  This is used
 * internally to simulate the operations needed for DB_DBT_REALLOC.
 */
int locked_dbt_realloc(LOCKED_DBT *ldbt, JNIEnv *jnienv, DB_ENV *dbenv)
{
	DBT *dbt;

	COMPQUIET(dbenv, NULL);
	dbt = &ldbt->javainfo->dbt;

	if (!F_ISSET(ldbt, LOCKED_REALLOC_NONNULL) ||
	    F_ISSET(ldbt, LOCKED_ERROR) || dbt->size <= dbt->ulen)
		return (0);

	(*jnienv)->ReleaseByteArrayElements(jnienv, ldbt->javainfo->array,
					    ldbt->java_data, 0);

	/*
	 * We allocate a new array of the needed size.
	 * We'll set the offset to 0, as the old offset
	 * really doesn't make any sense.
	 */
	if ((ldbt->javainfo->array = (*jnienv)->NewByteArray(jnienv,
	    dbt->size)) == NULL) {
		F_SET(ldbt, LOCKED_ERROR);
		return (0);
	}

	ldbt->java_array_len = dbt->ulen = dbt->size;
	ldbt->javainfo->offset = 0;
	(*jnienv)->SetObjectField(jnienv, ldbt->jdbt, fid_Dbt_data,
	    ldbt->javainfo->array);
	ldbt->java_data = (*jnienv)->GetByteArrayElements(jnienv,
	    ldbt->javainfo->array, (jboolean *)0);
	memcpy(ldbt->java_data, ldbt->before_data, dbt->ulen);
	dbt->data = ldbt->before_data = ldbt->java_data;
	return (1);
}
Beispiel #9
0
JNIEXPORT void JNICALL Java_com_sleepycat_db_xa_DbXAResource__1end
  (JNIEnv *jnienv, jobject jthis, jobject jxid, jint rmid, jint flags)
{
	XID xid;
	int err;

	COMPQUIET(jthis, NULL);
	if (!get_XID(jnienv, jxid, &xid))
		return;
	if ((err = __db_xa_end(&xid, rmid, flags)) != XA_OK)
		verify_return(jnienv, err, EXCEPTION_XA);
}
Beispiel #10
0
DbEnv::DbEnv(DB_ENV *env, u_int32_t flags)
:	imp_(0)
,	construct_error_(0)
,	construct_flags_(flags)
,	tx_recover_callback_(0)
,	paniccall_callback_(0)
{
	int err;

	COMPQUIET(err, 0);
	if ((err = initialize(env)) != 0)
		DB_ERROR("DbEnv::DbEnv", err, error_policy());
}
Beispiel #11
0
JNIEXPORT jobject JNICALL Java_com_sleepycat_db_xa_DbXAResource_xa_1attach
  (JNIEnv *jnienv, jclass jthisclass, jobject jxid, jobject jrmid)
{
	XID xid;
	XID *xidp;
	int ret;
	DB_ENV *env;
	DB_TXN *txn;
	int rmid;
	int *rmidp;
	jobject jtxn;
	jobject jenv;
	jclass cl;
	jmethodID mid;

	COMPQUIET(jthisclass, NULL);
	if (jxid == NULL) {
		xidp = NULL;
	}
	else {
		xidp = &xid;
		if (!get_XID(jnienv, jxid, &xid))
			return (NULL);
	}
	if (jrmid == NULL) {
		rmidp = NULL;
	}
	else {
		rmidp = &rmid;
		rmid = (int)(*jnienv)->CallIntMethod(jnienv, jrmid,
						     mid_Integer_intValue);
	}

	if ((ret = db_env_xa_attach(rmidp, xidp, &env, &txn)) != 0) {
		/*
		 * DB_NOTFOUND is a normal return, it means we
		 * have no current transaction,
		 */
		if (ret != DB_NOTFOUND)
			verify_return(jnienv, ret, 0);
		return (NULL);
	}

	jenv = ((DB_ENV_JAVAINFO *)env->api2_internal)->jenvref;
	jtxn = get_DbTxn(jnienv, txn);
	if ((cl = get_class(jnienv, name_DB_XAATTACH)) == NULL)
		return (NULL);
	mid = (*jnienv)->GetMethodID(jnienv, cl, "<init>",
		     "(Lcom/sleepycat/db/DbEnv;Lcom/sleepycat/db/DbTxn;)V");
	return (*jnienv)->NewObject(jnienv, cl, mid, jenv, jtxn);
}
Beispiel #12
0
JNIEXPORT void JNICALL Java_com_sleepycat_db_xa_DbXAResource__1close
  (JNIEnv *jnienv, jobject jthis, jstring home, jint rmid, jint flags)
{
	int err;
	LOCKED_STRING ls_home;

	COMPQUIET(jthis, NULL);
	if (locked_string_get(&ls_home, jnienv, home) != 0)
		goto out;
	if ((err = __db_xa_close((char *)ls_home.string,
				 rmid, flags)) != XA_OK)
		verify_return(jnienv, err, EXCEPTION_XA);
 out:
	locked_string_put(&ls_home, jnienv);
}
Beispiel #13
0
JNIEXPORT jint JNICALL Java_com_sleepycat_db_xa_DbXAResource__1prepare
  (JNIEnv *jnienv, jobject jthis, jobject jxid, jint rmid)
{
	XID xid;
	int err;

	COMPQUIET(jthis, NULL);
	if (!get_XID(jnienv, jxid, &xid))
		return (0);
	err = __db_xa_prepare(&xid, rmid, 0);
	if (err != XA_OK && err != XA_RDONLY)
		verify_return(jnienv, err, EXCEPTION_XA);

	return (err);
}
Beispiel #14
0
void dbji_call_feedback(DB_JAVAINFO *dbji, DB *db, jobject jdb,
                        int opcode, int percent)
{
    JNIEnv *jnienv;

    COMPQUIET(db, NULL);
    jnienv = dbji_get_jnienv(dbji);
    if (jnienv == NULL) {
        fprintf(stderr, "Cannot attach to current thread!\n");
        return;
    }

    DB_ASSERT(dbji->feedback_method_id_ != NULL);
    (*jnienv)->CallVoidMethod(jnienv, dbji->feedback_,
                              dbji->feedback_method_id_,
                              jdb, (jint)opcode, (jint)percent);
}
Beispiel #15
0
JNIEXPORT void JNICALL Java_com_sleepycat_db_xa_DbXAResource__1commit
  (JNIEnv *jnienv, jobject jthis, jobject jxid, jint rmid,
   jboolean onePhase)
{
	XID xid;
	long flags;
	int err;

	COMPQUIET(jthis, NULL);
	if (!get_XID(jnienv, jxid, &xid))
		return;
	flags = 0;
	if (onePhase == JNI_TRUE)
		flags |= TMONEPHASE;
	if ((err = __db_xa_commit(&xid, rmid, flags)) != XA_OK)
		verify_return(jnienv, err, EXCEPTION_XA);
}
Beispiel #16
0
int dbjie_call_tx_recover(DB_ENV_JAVAINFO *dbjie, DB_ENV *dbenv, jobject jenv,
                          DBT *dbt, DB_LSN *lsn, int recops)
{
    JNIEnv *jnienv;
    jclass tx_recover_class;
    jmethodID id;
    jobject jdbt;
    jobject jlsn;

    COMPQUIET(dbenv, NULL);
    jnienv = dbjie_get_jnienv(dbjie);
    if (jnienv == NULL) {
        fprintf(stderr, "Cannot attach to current thread!\n");
        return (0);
    }

    tx_recover_class = get_class(jnienv, name_DbTxnRecover);
    id = (*jnienv)->GetMethodID(jnienv, tx_recover_class,
                                "tx_recover",
                                "(Lcom/sleepycat/db/DbEnv;"
                                "Lcom/sleepycat/db/Dbt;"
                                "Lcom/sleepycat/db/DbLsn;"
                                "I)I");
    if (!id) {
        fprintf(stderr, "Cannot find callback class\n");
        return (0);
    }

    if (dbt == NULL)
        jdbt = NULL;
    else
        jdbt = get_Dbt(jnienv, dbt);

    if (lsn == NULL)
        jlsn = NULL;
    else
        jlsn = get_DbLsn(jnienv, *lsn);

    return (*jnienv)->CallIntMethod(jnienv, dbjie->tx_recover_, id, jenv,
                                    jdbt, jlsn, recops);
}
Beispiel #17
0
int dbji_call_h_hash(DB_JAVAINFO *dbji, DB *db, jobject jdb,
                     const void *data, int len)
{
    JNIEnv *jnienv;
    jbyteArray jarray;

    COMPQUIET(db, NULL);
    jnienv = dbji_get_jnienv(dbji);
    if (jnienv == NULL) {
        fprintf(stderr, "Cannot attach to current thread!\n");
        return (0);
    }

    DB_ASSERT(dbji->h_hash_method_id_ != NULL);

    jarray = (*jnienv)->NewByteArray(jnienv, len);
    (*jnienv)->SetByteArrayRegion(jnienv, jarray, 0, len, (void *)data);
    return (*jnienv)->CallIntMethod(jnienv, dbji->h_hash_,
                                    dbji->h_hash_method_id_,
                                    jdb, jarray, len);
}
Beispiel #18
0
int DbEnv::close(u_int32_t flags)
{
	DB_ENV *env = unwrap(this);
	int err, init_err;

	COMPQUIET(init_err, 0);

	// after a close (no matter if success or failure),
	// the underlying DB_ENV object must not be accessed,
	// so we clean up in advance.
	//
	cleanup();

	// It's safe to throw an error after the close,
	// since our error mechanism does not peer into
	// the DB* structures.
	//
	if ((err = env->close(env, flags)) != 0) {
		DB_ERROR("DbEnv::close", err, error_policy());
	}
	return (err);
}
Beispiel #19
0
JNIEXPORT jobjectArray JNICALL Java_com_sleepycat_db_xa_DbXAResource__1recover
  (JNIEnv *jnienv, jobject jthis, jint rmid, jint flags)
{
	XID *xids;
	int err;
	int total;
	int cnt;
	int i;
	int curflags;
	size_t nbytes;
	jclass xid_class;
	jmethodID mid;
	jobject obj;
	jobjectArray retval;

	COMPQUIET(jthis, NULL);
	total = 0;
	cnt = 0;
	xids = NULL;
	flags &= ~(DB_FIRST | DB_LAST | DB_NEXT);

	/* Repeatedly call __db_xa_recover to fill up an array of XIDs */
	curflags = flags | DB_FIRST;
	do {
		total += cnt;
		nbytes = sizeof(XID) * (total + 10);
		if ((err = __os_realloc(NULL, nbytes, &xids)) != 0) {
			if (xids != NULL)
				__os_free(NULL, xids);
			verify_return(jnienv, XAER_NOTA, EXCEPTION_XA);
			return (NULL);
		}
		cnt = __db_xa_recover(&xids[total], 10, rmid, curflags);
		curflags = flags | DB_NEXT;
	} while (cnt > 0);

	if (xids != NULL)
		__os_free(NULL, xids);

	if (cnt < 0) {
		verify_return(jnienv, cnt, EXCEPTION_XA);
		return (NULL);
	}

	/* Create the java DbXid array and fill it up */
	if ((xid_class = get_class(jnienv, name_DB_XID)) == NULL)
		return (NULL);
	mid = (*jnienv)->GetMethodID(jnienv, xid_class, "<init>",
				     "(I[B[B)V");
	if ((retval = (*jnienv)->NewObjectArray(jnienv, total, xid_class, 0))
	    == NULL)
		goto out;

	for (i = 0; i < total; i++) {
		jobject gtrid;
		jobject bqual;
		jsize gtrid_len;
		jsize bqual_len;

		gtrid_len = (jsize)xids[i].gtrid_length;
		bqual_len = (jsize)xids[i].bqual_length;
		gtrid = (*jnienv)->NewByteArray(jnienv, gtrid_len);
		bqual = (*jnienv)->NewByteArray(jnienv, bqual_len);
		if (gtrid == NULL || bqual == NULL)
			goto out;
		(*jnienv)->SetByteArrayRegion(jnienv, gtrid, 0, gtrid_len,
		    (jbyte *)&xids[i].data[0]);
		(*jnienv)->SetByteArrayRegion(jnienv, bqual, 0, bqual_len,
		    (jbyte *)&xids[i].data[gtrid_len]);
		if ((obj = (*jnienv)->NewObject(jnienv, xid_class, mid,
		    (jint)xids[i].formatID, gtrid, bqual)) == NULL)
			goto out;
		(*jnienv)->SetObjectArrayElement(jnienv, retval, i, obj);
	}
out:	return (retval);
}
Beispiel #20
0
static void
b_inmem_op_ds_bulk(u_int ops, u_int *totalp)
{
	DB_ENV *dbenv;
	DB *dbp;
	DBC *dbc;
	DBT key, data;
	u_int32_t len, klen;
	u_int i, total;
	char *keybuf, *databuf;
	void *pointer, *dp, *kp;
	DB_MPOOL_STAT  *gsp;

	DB_BENCH_ASSERT((keybuf = malloc(keysize)) != NULL);
	DB_BENCH_ASSERT((databuf = malloc(bulkbufsize)) != NULL);

	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	key.data = keybuf;
	key.size = keysize;

	data.data = databuf;
	data.size = datasize;
	memset(databuf, 'b', datasize);

	DB_BENCH_ASSERT(db_create(&dbp, NULL, 0) == 0);
	dbenv = dbp->dbenv;
	dbp->set_errfile(dbp, stderr);

	DB_BENCH_ASSERT(dbp->set_pagesize(dbp, pagesize) == 0);
	DB_BENCH_ASSERT(dbp->set_cachesize(dbp, 0, cachesize, 1) == 0);
	DB_BENCH_ASSERT(
	    dbp->open(dbp, NULL, NULL, NULL, DB_BTREE, DB_CREATE, 0666) == 0);

	for (i = 1; i <= numitems; ++i) {
		(void)snprintf(keybuf, keysize, "%7d", i);
		DB_BENCH_ASSERT(dbp->put(dbp, NULL, &key, &data, 0) == 0);
	}

#if 0
	fp = fopen("before", "w");
	dbp->set_msgfile(dbp, fp);
	DB_BENCH_ASSERT (dbp->stat_print(dbp, DB_STAT_ALL) == 0);
#endif

	DB_BENCH_ASSERT(dbp->cursor(dbp, NULL, &dbc, 0) == 0);

	data.ulen = bulkbufsize;
	data.flags = DB_DBT_USERMEM;

	(void)dbenv->memp_stat(dbenv, &gsp, NULL, DB_STAT_CLEAR);

	TIMER_START;
	for (total = 0; ops > 0; --ops) {
		DB_BENCH_ASSERT(dbc->c_get(
		    dbc, &key, &data, DB_FIRST | DB_MULTIPLE_KEY) == 0);
		DB_MULTIPLE_INIT(pointer, &data);
		while (pointer != NULL) {
			DB_MULTIPLE_KEY_NEXT(pointer, &data, kp, klen, dp, len);
			if (kp != NULL)
				++total;
		}
	}
	TIMER_STOP;
	*totalp = total;

	if (dbenv->memp_stat(dbenv, &gsp, NULL, 0) == 0)
	    DB_BENCH_ASSERT(gsp->st_cache_miss == 0);

#if 0
	fp = fopen("before", "w");
	dbp->set_msgfile(dbp, fp);
	DB_BENCH_ASSERT (dbp->stat_print(dbp, DB_STAT_ALL) == 0);
#endif

	DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);

	COMPQUIET(dp, NULL);
	COMPQUIET(klen, 0);
	COMPQUIET(len, 0);
}
Beispiel #21
0
void *sqlite3DbMallocZero(sqlite3 *db, unsigned n) {
	COMPQUIET(db, NULL);
	return calloc(n, 1);
}
Beispiel #22
0
extern int dbji_call_append_recno(DB_JAVAINFO *dbji, DB *db, jobject jdb,
                                  DBT *dbt, jint recno)
{
    JNIEnv *jnienv;
    jobject jdbt;
    DBT_JAVAINFO *dbtji;
    jbyteArray arr;
    unsigned int arraylen;
    unsigned char *data;

    COMPQUIET(db, NULL);
    jnienv = dbji_get_jnienv(dbji);
    if (jnienv == NULL) {
        fprintf(stderr, "Cannot attach to current thread!\n");
        return (0);
    }

    /* XXX
     * We should have a pool of Dbt objects used for this purpose
     * instead of creating new ones each time.  Because of
     * multithreading, we may need an arbitrary number (more than two).
     * We might also have a byte arrays that grow as needed,
     * so we don't need to allocate those either.
     *
     * Note, we do not set the 'create_array_' flag as on other
     * callbacks as we are creating the array here.
     */
    jdbt = create_default_object(jnienv, name_DBT);
    dbtji = get_DBT_JAVAINFO(jnienv, jdbt);
    memcpy(&dbtji->dbt, dbt, sizeof(DBT));
    dbtji->dbt.data = NULL;
    arr = (*jnienv)->NewByteArray(jnienv, dbt->size);
    (*jnienv)->SetByteArrayRegion(jnienv, arr, 0, dbt->size,
                                  (jbyte *)dbt->data);
    dbtji->array_ = (jbyteArray)NEW_GLOBAL_REF(jnienv, arr);

    DB_ASSERT(dbji->append_recno_method_id_ != NULL);
    (*jnienv)->CallVoidMethod(jnienv, dbji->append_recno_,
                              dbji->append_recno_method_id_,
                              jdb, jdbt, recno);

    /* The underlying C API requires that an errno be returned
     * on error.  Java users know nothing of errnos, so we
     * allow them to throw exceptions instead.  We leave the
     * exception in place and return DB_JAVA_CALLBACK to the C API
     * that called us.  Eventually the DB->get will fail and
     * when java prepares to throw an exception in
     * report_exception(), this will be spotted as a special case,
     * and the original exception will be preserved.
     *
     * Note: we have sometimes noticed strange behavior with
     * exceptions under Linux 1.1.7 JVM.  (i.e. multiple calls
     * to ExceptionOccurred() may report different results).
     * Currently we don't know of any problems related to this
     * in our code, but if it pops up in the future, users are
     * encouranged to get a more recent JVM.
     */
    if ((*jnienv)->ExceptionOccurred(jnienv) != NULL)
        return (DB_JAVA_CALLBACK);

    if (dbtji->array_ == NULL) {
        report_exception(jnienv, "Dbt.data is null", 0, 0);
        return (EFAULT);
    }

    arraylen = (*jnienv)->GetArrayLength(jnienv, dbtji->array_);
    if (dbtji->offset_ < 0 ) {
        report_exception(jnienv, "Dbt.offset illegal", 0, 0);
        return (EFAULT);
    }
    if (dbt->ulen + dbtji->offset_ > arraylen) {
        report_exception(jnienv,
                         "Dbt.ulen + Dbt.offset greater than array length", 0, 0);
        return (EFAULT);
    }

    data = (*jnienv)->GetByteArrayElements(jnienv, dbtji->array_,
                                           (jboolean *)0);
    dbt->data = data + dbtji->offset_;
    return (0);
}
Beispiel #23
0
/****************************************************************
 *
 * Implementation of functions to manipulate LOCKED_DBT.
 */
int
locked_dbt_get(LOCKED_DBT *ldbt, JNIEnv *jnienv, DB_ENV *dbenv,
	       jobject jdbt, OpKind kind)
{
	DBT *dbt;

	COMPQUIET(dbenv, NULL);
	ldbt->jdbt = jdbt;
	ldbt->java_array_len = 0;
	ldbt->flags = 0;
	ldbt->kind = kind;
	ldbt->java_data = 0;
	ldbt->before_data = 0;
	ldbt->javainfo =
		(DBT_JAVAINFO *)get_private_dbobj(jnienv, name_DBT, jdbt);

	if (!verify_non_null(jnienv, ldbt->javainfo)) {
		report_exception(jnienv, "Dbt is gc'ed?", 0, 0);
		F_SET(ldbt, LOCKED_ERROR);
		return (EINVAL);
	}
	if (F_ISSET(ldbt->javainfo, DBT_JAVAINFO_LOCKED)) {
		report_exception(jnienv, "Dbt is already in use", 0, 0);
		F_SET(ldbt, LOCKED_ERROR);
		return (EINVAL);
	}
	dbt = &ldbt->javainfo->dbt;

	if ((*jnienv)->GetBooleanField(jnienv,
	    jdbt, fid_Dbt_must_create_data) != 0)
		F_SET(ldbt, LOCKED_CREATE_DATA);
	else
		ldbt->javainfo->array =
			(*jnienv)->GetObjectField(jnienv, jdbt, fid_Dbt_data);

	dbt->size = (*jnienv)->GetIntField(jnienv, jdbt, fid_Dbt_size);
	dbt->ulen = (*jnienv)->GetIntField(jnienv, jdbt, fid_Dbt_ulen);
	dbt->dlen = (*jnienv)->GetIntField(jnienv, jdbt, fid_Dbt_dlen);
	dbt->doff = (*jnienv)->GetIntField(jnienv, jdbt, fid_Dbt_doff);
	dbt->flags = (*jnienv)->GetIntField(jnienv, jdbt, fid_Dbt_flags);
	ldbt->javainfo->offset = (*jnienv)->GetIntField(jnienv, jdbt,
						    fid_Dbt_offset);

	/*
	 * If no flags are set, use default behavior of DB_DBT_MALLOC.
	 * We can safely set dbt->flags because flags will never be copied
	 * back to the Java Dbt.
	 */
	if (kind != inOp &&
	    !F_ISSET(dbt, DB_DBT_USERMEM | DB_DBT_MALLOC | DB_DBT_REALLOC))
		F_SET(dbt, DB_DBT_MALLOC);

	/*
	 * If this is requested to be realloc with an existing array,
	 * we cannot use the underlying realloc, because the array we
	 * will pass in is allocated by the Java VM, not us, so it
	 * cannot be realloced.  We simulate the reallocation by using
	 * USERMEM and reallocating the java array when a ENOMEM error
	 * occurs.  We change the flags during the operation, and they
	 * are reset when the operation completes (in locked_dbt_put).
	 */
	if (F_ISSET(dbt, DB_DBT_REALLOC) && ldbt->javainfo->array != NULL) {
		F_CLR(dbt, DB_DBT_REALLOC);
		F_SET(dbt, DB_DBT_USERMEM);
		F_SET(ldbt, LOCKED_REALLOC_NONNULL);
	}

	if ((F_ISSET(dbt, DB_DBT_USERMEM) || kind != outOp) &&
	    !F_ISSET(ldbt, LOCKED_CREATE_DATA)) {

		/*
		 * If writing with DB_DBT_USERMEM
		 * or it's a set (or get/set) operation,
		 * then the data should point to a java array.
		 * Note that outOp means data is coming out of the database
		 * (it's a get).  inOp means data is going into the database
		 * (either a put, or a key input).
		 */
		if (!ldbt->javainfo->array) {
			report_exception(jnienv, "Dbt.data is null", 0, 0);
			F_SET(ldbt, LOCKED_ERROR);
			return (EINVAL);
		}

		/* Verify other parameters */
		ldbt->java_array_len = (*jnienv)->GetArrayLength(jnienv,
							ldbt->javainfo->array);
		if (ldbt->javainfo->offset < 0 ) {
			report_exception(jnienv, "Dbt.offset illegal", 0, 0);
			F_SET(ldbt, LOCKED_ERROR);
			return (EINVAL);
		}
		if (dbt->size + ldbt->javainfo->offset > ldbt->java_array_len) {
			report_exception(jnienv,
			 "Dbt.size + Dbt.offset greater than array length",
					 0, 0);
			F_SET(ldbt, LOCKED_ERROR);
			return (EINVAL);
		}

		ldbt->java_data = (*jnienv)->GetByteArrayElements(jnienv,
						ldbt->javainfo->array,
						(jboolean *)0);

		dbt->data = ldbt->before_data = ldbt->java_data +
			ldbt->javainfo->offset;
	}
	else if (!F_ISSET(ldbt, LOCKED_CREATE_DATA)) {

		/*
		 * If writing with DB_DBT_MALLOC or DB_DBT_REALLOC with
		 * a null array, then the data is allocated by DB.
		 */
		dbt->data = ldbt->before_data = 0;
	}

	/*
	 * RPC makes the assumption that if dbt->size is non-zero, there
	 * is data to copy from dbt->data.  We may have set dbt->size
	 * to a non-zero integer above but decided not to point
	 * dbt->data at anything.  (One example is if we're doing an outOp
	 * with an already-used Dbt whose values we expect to just
	 * overwrite.)
	 *
	 * Clean up the dbt fields so we don't run into trouble.
	 * (Note that doff, dlen, and flags all may contain meaningful
	 * values.)
	 */
	if (dbt->data == NULL)
		dbt->size = dbt->ulen = 0;

	F_SET(ldbt->javainfo, DBT_JAVAINFO_LOCKED);
	return (0);
}