2001-03-05 01:42:05 +01:00
|
|
|
/*-
|
|
|
|
* See the file LICENSE for redistribution information.
|
|
|
|
*
|
2002-10-30 12:57:05 +01:00
|
|
|
* Copyright (c) 1997-2002
|
2001-03-05 01:42:05 +01:00
|
|
|
* Sleepycat Software. All rights reserved.
|
|
|
|
*/
|
|
|
|
#include "db_config.h"
|
|
|
|
|
|
|
|
#ifndef lint
|
2002-10-30 12:57:05 +01:00
|
|
|
static const char revid[] = "$Id: java_locked.c,v 11.32 2002/08/06 05:19:07 bostic Exp $";
|
2001-03-05 01:42:05 +01:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
#include "db_int.h"
|
2001-03-05 01:42:05 +01:00
|
|
|
#include "java_util.h"
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
*
|
2002-10-30 12:57:05 +01:00
|
|
|
* Implementation of functions to manipulate LOCKED_DBT.
|
2001-03-05 01:42:05 +01:00
|
|
|
*/
|
|
|
|
int
|
2002-10-30 12:57:05 +01:00
|
|
|
locked_dbt_get(LOCKED_DBT *ldbt, JNIEnv *jnienv, DB_ENV *dbenv,
|
|
|
|
jobject jdbt, OpKind kind)
|
2001-03-05 01:42:05 +01:00
|
|
|
{
|
|
|
|
DBT *dbt;
|
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
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);
|
2001-03-05 01:42:05 +01:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
2002-10-30 12:57:05 +01:00
|
|
|
if (F_ISSET(ldbt->javainfo, DBT_JAVAINFO_LOCKED)) {
|
|
|
|
report_exception(jnienv, "Dbt is already in use", 0, 0);
|
|
|
|
F_SET(ldbt, LOCKED_ERROR);
|
2001-03-05 01:42:05 +01:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
2002-10-30 12:57:05 +01:00
|
|
|
dbt = &ldbt->javainfo->dbt;
|
2001-03-05 01:42:05 +01:00
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
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).
|
2001-03-05 01:42:05 +01:00
|
|
|
*/
|
2002-10-30 12:57:05 +01:00
|
|
|
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);
|
2001-03-05 01:42:05 +01:00
|
|
|
}
|
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
if ((F_ISSET(dbt, DB_DBT_USERMEM) || kind != outOp) &&
|
|
|
|
!F_ISSET(ldbt, LOCKED_CREATE_DATA)) {
|
2001-03-05 01:42:05 +01:00
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
/*
|
|
|
|
* If writing with DB_DBT_USERMEM
|
2001-03-05 01:42:05 +01:00
|
|
|
* 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).
|
|
|
|
*/
|
2002-10-30 12:57:05 +01:00
|
|
|
if (!ldbt->javainfo->array) {
|
2001-03-05 01:42:05 +01:00
|
|
|
report_exception(jnienv, "Dbt.data is null", 0, 0);
|
2002-10-30 12:57:05 +01:00
|
|
|
F_SET(ldbt, LOCKED_ERROR);
|
2001-03-05 01:42:05 +01:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify other parameters */
|
2002-10-30 12:57:05 +01:00
|
|
|
ldbt->java_array_len = (*jnienv)->GetArrayLength(jnienv,
|
|
|
|
ldbt->javainfo->array);
|
|
|
|
if (ldbt->javainfo->offset < 0 ) {
|
2001-03-05 01:42:05 +01:00
|
|
|
report_exception(jnienv, "Dbt.offset illegal", 0, 0);
|
2002-10-30 12:57:05 +01:00
|
|
|
F_SET(ldbt, LOCKED_ERROR);
|
2001-03-05 01:42:05 +01:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
2002-10-30 12:57:05 +01:00
|
|
|
if (dbt->size + ldbt->javainfo->offset > ldbt->java_array_len) {
|
2001-03-05 01:42:05 +01:00
|
|
|
report_exception(jnienv,
|
2002-10-30 12:57:05 +01:00
|
|
|
"Dbt.size + Dbt.offset greater than array length",
|
|
|
|
0, 0);
|
|
|
|
F_SET(ldbt, LOCKED_ERROR);
|
2001-03-05 01:42:05 +01:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
ldbt->java_data = (*jnienv)->GetByteArrayElements(jnienv,
|
|
|
|
ldbt->javainfo->array,
|
|
|
|
(jboolean *)0);
|
|
|
|
|
|
|
|
dbt->data = ldbt->before_data = ldbt->java_data +
|
|
|
|
ldbt->javainfo->offset;
|
2001-03-05 01:42:05 +01:00
|
|
|
}
|
2002-10-30 12:57:05 +01:00
|
|
|
else if (!F_ISSET(ldbt, LOCKED_CREATE_DATA)) {
|
2001-03-05 01:42:05 +01:00
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
/*
|
|
|
|
* If writing with DB_DBT_MALLOC or DB_DBT_REALLOC with
|
|
|
|
* a null array, then the data is allocated by DB.
|
2001-03-05 01:42:05 +01:00
|
|
|
*/
|
2002-10-30 12:57:05 +01:00
|
|
|
dbt->data = ldbt->before_data = 0;
|
2001-03-05 01:42:05 +01:00
|
|
|
}
|
2002-10-30 12:57:05 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2001-03-05 01:42:05 +01:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
/*
|
|
|
|
* locked_dbt_put must be called for any LOCKED_DBT struct before a
|
|
|
|
* java handler returns to the user. It can be thought of as the
|
|
|
|
* LOCKED_DBT destructor. It copies any information from temporary
|
|
|
|
* structures back to user accessible arrays, and of course must free
|
|
|
|
* memory and remove references. The LOCKED_DBT itself is not freed,
|
|
|
|
* as it is expected to be a stack variable.
|
|
|
|
*
|
|
|
|
* Note that after this call, the LOCKED_DBT can still be used in
|
|
|
|
* limited ways, e.g. to look at values in the C DBT.
|
2001-03-05 01:42:05 +01:00
|
|
|
*/
|
|
|
|
void
|
2002-10-30 12:57:05 +01:00
|
|
|
locked_dbt_put(LOCKED_DBT *ldbt, JNIEnv *jnienv, DB_ENV *dbenv)
|
2001-03-05 01:42:05 +01:00
|
|
|
{
|
|
|
|
DBT *dbt;
|
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
dbt = &ldbt->javainfo->dbt;
|
2001-03-05 01:42:05 +01:00
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
/*
|
|
|
|
* If the error flag was set, we never succeeded
|
|
|
|
* in allocating storage.
|
|
|
|
*/
|
|
|
|
if (F_ISSET(ldbt, LOCKED_ERROR))
|
|
|
|
return;
|
2001-03-05 01:42:05 +01:00
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
if (((F_ISSET(dbt, DB_DBT_USERMEM) ||
|
|
|
|
F_ISSET(ldbt, LOCKED_REALLOC_NONNULL)) ||
|
|
|
|
ldbt->kind == inOp) && !F_ISSET(ldbt, LOCKED_CREATE_DATA)) {
|
2001-03-05 01:42:05 +01:00
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
/*
|
|
|
|
* If writing with DB_DBT_USERMEM or it's a set
|
2001-03-05 01:42:05 +01:00
|
|
|
* (or get/set) operation, then the data may be already in
|
|
|
|
* the java array, in which case, we just need to release it.
|
|
|
|
* If DB didn't put it in the array (indicated by the
|
|
|
|
* dbt->data changing), we need to do that
|
|
|
|
*/
|
2002-10-30 12:57:05 +01:00
|
|
|
if (ldbt->before_data != ldbt->java_data) {
|
2001-03-05 01:42:05 +01:00
|
|
|
(*jnienv)->SetByteArrayRegion(jnienv,
|
2002-10-30 12:57:05 +01:00
|
|
|
ldbt->javainfo->array,
|
|
|
|
ldbt->javainfo->offset,
|
2001-03-05 01:42:05 +01:00
|
|
|
dbt->ulen,
|
2002-10-30 12:57:05 +01:00
|
|
|
ldbt->before_data);
|
2001-03-05 01:42:05 +01:00
|
|
|
}
|
2002-10-30 12:57:05 +01:00
|
|
|
(*jnienv)->ReleaseByteArrayElements(jnienv,
|
|
|
|
ldbt->javainfo->array,
|
|
|
|
ldbt->java_data, 0);
|
2001-03-05 01:42:05 +01:00
|
|
|
dbt->data = 0;
|
|
|
|
}
|
2002-10-30 12:57:05 +01:00
|
|
|
else if (F_ISSET(dbt, DB_DBT_MALLOC | DB_DBT_REALLOC) &&
|
|
|
|
ldbt->kind != inOp && !F_ISSET(ldbt, LOCKED_CREATE_DATA)) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If writing with DB_DBT_MALLOC, or DB_DBT_REALLOC
|
|
|
|
* with a zero buffer, then the data was allocated by
|
|
|
|
* DB. If dbt->data is zero, it means an error
|
|
|
|
* occurred (and should have been already reported).
|
2001-03-05 01:42:05 +01:00
|
|
|
*/
|
|
|
|
if (dbt->data) {
|
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
/*
|
|
|
|
* In the case of SET_RANGE, the key is inOutOp
|
2001-03-05 01:42:05 +01:00
|
|
|
* and when not found, its data will be left as
|
|
|
|
* its original value. Only copy and free it
|
|
|
|
* here if it has been allocated by DB
|
|
|
|
* (dbt->data has changed).
|
|
|
|
*/
|
2002-10-30 12:57:05 +01:00
|
|
|
if (dbt->data != ldbt->before_data) {
|
|
|
|
jbyteArray newarr;
|
|
|
|
|
|
|
|
if ((newarr = (*jnienv)->NewByteArray(jnienv,
|
|
|
|
dbt->size)) == NULL) {
|
|
|
|
/* The JVM has posted an exception. */
|
|
|
|
F_SET(ldbt, LOCKED_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
(*jnienv)->SetObjectField(jnienv, ldbt->jdbt,
|
|
|
|
fid_Dbt_data,
|
|
|
|
newarr);
|
|
|
|
ldbt->javainfo->offset = 0;
|
2001-03-05 01:42:05 +01:00
|
|
|
(*jnienv)->SetByteArrayRegion(jnienv,
|
2002-10-30 12:57:05 +01:00
|
|
|
newarr, 0, dbt->size,
|
2001-03-05 01:42:05 +01:00
|
|
|
(jbyte *)dbt->data);
|
2002-10-30 12:57:05 +01:00
|
|
|
(void)__os_ufree(dbenv, dbt->data);
|
2001-03-05 01:42:05 +01:00
|
|
|
dbt->data = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-30 12:57:05 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The size field may have changed after a DB API call,
|
|
|
|
* so we set that back too.
|
|
|
|
*/
|
|
|
|
(*jnienv)->SetIntField(jnienv, ldbt->jdbt, fid_Dbt_size, dbt->size);
|
|
|
|
ldbt->javainfo->array = NULL;
|
|
|
|
F_CLR(ldbt->javainfo, DBT_JAVAINFO_LOCKED);
|
2001-03-05 01:42:05 +01:00
|
|
|
}
|
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
2001-03-05 01:42:05 +01:00
|
|
|
*/
|
2002-10-30 12:57:05 +01:00
|
|
|
int locked_dbt_realloc(LOCKED_DBT *ldbt, JNIEnv *jnienv, DB_ENV *dbenv)
|
2001-03-05 01:42:05 +01:00
|
|
|
{
|
|
|
|
DBT *dbt;
|
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
COMPQUIET(dbenv, NULL);
|
|
|
|
dbt = &ldbt->javainfo->dbt;
|
2001-03-05 01:42:05 +01:00
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
if (!F_ISSET(ldbt, LOCKED_REALLOC_NONNULL) ||
|
|
|
|
F_ISSET(ldbt, LOCKED_ERROR) || dbt->size <= dbt->ulen)
|
2001-03-05 01:42:05 +01:00
|
|
|
return (0);
|
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
(*jnienv)->ReleaseByteArrayElements(jnienv, ldbt->javainfo->array,
|
|
|
|
ldbt->java_data, 0);
|
2001-03-05 01:42:05 +01:00
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
/*
|
|
|
|
* We allocate a new array of the needed size.
|
2001-03-05 01:42:05 +01:00
|
|
|
* We'll set the offset to 0, as the old offset
|
|
|
|
* really doesn't make any sense.
|
|
|
|
*/
|
2002-10-30 12:57:05 +01:00
|
|
|
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;
|
2001-03-05 01:42:05 +01:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
*
|
2002-10-30 12:57:05 +01:00
|
|
|
* Implementation of functions to manipulate LOCKED_STRING.
|
2001-03-05 01:42:05 +01:00
|
|
|
*/
|
|
|
|
int
|
2002-10-30 12:57:05 +01:00
|
|
|
locked_string_get(LOCKED_STRING *ls, JNIEnv *jnienv, jstring jstr)
|
2001-03-05 01:42:05 +01:00
|
|
|
{
|
2002-10-30 12:57:05 +01:00
|
|
|
ls->jstr = jstr;
|
2001-03-05 01:42:05 +01:00
|
|
|
|
|
|
|
if (jstr == 0)
|
2002-10-30 12:57:05 +01:00
|
|
|
ls->string = 0;
|
2001-03-05 01:42:05 +01:00
|
|
|
else
|
2002-10-30 12:57:05 +01:00
|
|
|
ls->string = (*jnienv)->GetStringUTFChars(jnienv, jstr,
|
2001-03-05 01:42:05 +01:00
|
|
|
(jboolean *)0);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-10-30 12:57:05 +01:00
|
|
|
void locked_string_put(LOCKED_STRING *ls, JNIEnv *jnienv)
|
2001-03-05 01:42:05 +01:00
|
|
|
{
|
2002-10-30 12:57:05 +01:00
|
|
|
if (ls->jstr)
|
|
|
|
(*jnienv)->ReleaseStringUTFChars(jnienv, ls->jstr, ls->string);
|
2001-03-05 01:42:05 +01:00
|
|
|
}
|