2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2002
5
* Sleepycat Software. All rights reserved.
10
static const char revid[] = "$Id$";
19
#include "java_util.h"
21
/****************************************************************
23
* Implementation of functions to manipulate LOCKED_DBT.
26
locked_dbt_get(LOCKED_DBT *ldbt, JNIEnv *jnienv, DB_ENV *dbenv,
27
jobject jdbt, OpKind kind)
31
COMPQUIET(dbenv, NULL);
33
ldbt->java_array_len = 0;
37
ldbt->before_data = 0;
39
(DBT_JAVAINFO *)get_private_dbobj(jnienv, name_DBT, jdbt);
41
if (!verify_non_null(jnienv, ldbt->javainfo)) {
42
report_exception(jnienv, "Dbt is gc'ed?", 0, 0);
43
F_SET(ldbt, LOCKED_ERROR);
46
if (F_ISSET(ldbt->javainfo, DBT_JAVAINFO_LOCKED)) {
47
report_exception(jnienv, "Dbt is already in use", 0, 0);
48
F_SET(ldbt, LOCKED_ERROR);
51
dbt = &ldbt->javainfo->dbt;
53
if ((*jnienv)->GetBooleanField(jnienv,
54
jdbt, fid_Dbt_must_create_data) != 0)
55
F_SET(ldbt, LOCKED_CREATE_DATA);
57
ldbt->javainfo->array =
58
(*jnienv)->GetObjectField(jnienv, jdbt, fid_Dbt_data);
60
dbt->size = (*jnienv)->GetIntField(jnienv, jdbt, fid_Dbt_size);
61
dbt->ulen = (*jnienv)->GetIntField(jnienv, jdbt, fid_Dbt_ulen);
62
dbt->dlen = (*jnienv)->GetIntField(jnienv, jdbt, fid_Dbt_dlen);
63
dbt->doff = (*jnienv)->GetIntField(jnienv, jdbt, fid_Dbt_doff);
64
dbt->flags = (*jnienv)->GetIntField(jnienv, jdbt, fid_Dbt_flags);
65
ldbt->javainfo->offset = (*jnienv)->GetIntField(jnienv, jdbt,
69
* If no flags are set, use default behavior of DB_DBT_MALLOC.
70
* We can safely set dbt->flags because flags will never be copied
71
* back to the Java Dbt.
74
!F_ISSET(dbt, DB_DBT_USERMEM | DB_DBT_MALLOC | DB_DBT_REALLOC))
75
F_SET(dbt, DB_DBT_MALLOC);
78
* If this is requested to be realloc with an existing array,
79
* we cannot use the underlying realloc, because the array we
80
* will pass in is allocated by the Java VM, not us, so it
81
* cannot be realloced. We simulate the reallocation by using
82
* USERMEM and reallocating the java array when a ENOMEM error
83
* occurs. We change the flags during the operation, and they
84
* are reset when the operation completes (in locked_dbt_put).
86
if (F_ISSET(dbt, DB_DBT_REALLOC) && ldbt->javainfo->array != NULL) {
87
F_CLR(dbt, DB_DBT_REALLOC);
88
F_SET(dbt, DB_DBT_USERMEM);
89
F_SET(ldbt, LOCKED_REALLOC_NONNULL);
92
if ((F_ISSET(dbt, DB_DBT_USERMEM) || kind != outOp) &&
93
!F_ISSET(ldbt, LOCKED_CREATE_DATA)) {
96
* If writing with DB_DBT_USERMEM
97
* or it's a set (or get/set) operation,
98
* then the data should point to a java array.
99
* Note that outOp means data is coming out of the database
100
* (it's a get). inOp means data is going into the database
101
* (either a put, or a key input).
103
if (!ldbt->javainfo->array) {
104
report_exception(jnienv, "Dbt.data is null", 0, 0);
105
F_SET(ldbt, LOCKED_ERROR);
109
/* Verify other parameters */
110
ldbt->java_array_len = (*jnienv)->GetArrayLength(jnienv,
111
ldbt->javainfo->array);
112
if (ldbt->javainfo->offset < 0 ) {
113
report_exception(jnienv, "Dbt.offset illegal", 0, 0);
114
F_SET(ldbt, LOCKED_ERROR);
117
if (dbt->size + ldbt->javainfo->offset > ldbt->java_array_len) {
118
report_exception(jnienv,
119
"Dbt.size + Dbt.offset greater than array length",
121
F_SET(ldbt, LOCKED_ERROR);
125
ldbt->java_data = (*jnienv)->GetByteArrayElements(jnienv,
126
ldbt->javainfo->array,
129
dbt->data = ldbt->before_data = ldbt->java_data +
130
ldbt->javainfo->offset;
132
else if (!F_ISSET(ldbt, LOCKED_CREATE_DATA)) {
135
* If writing with DB_DBT_MALLOC or DB_DBT_REALLOC with
136
* a null array, then the data is allocated by DB.
138
dbt->data = ldbt->before_data = 0;
142
* RPC makes the assumption that if dbt->size is non-zero, there
143
* is data to copy from dbt->data. We may have set dbt->size
144
* to a non-zero integer above but decided not to point
145
* dbt->data at anything. (One example is if we're doing an outOp
146
* with an already-used Dbt whose values we expect to just
149
* Clean up the dbt fields so we don't run into trouble.
150
* (Note that doff, dlen, and flags all may contain meaningful
153
if (dbt->data == NULL)
154
dbt->size = dbt->ulen = 0;
156
F_SET(ldbt->javainfo, DBT_JAVAINFO_LOCKED);
161
* locked_dbt_put must be called for any LOCKED_DBT struct before a
162
* java handler returns to the user. It can be thought of as the
163
* LOCKED_DBT destructor. It copies any information from temporary
164
* structures back to user accessible arrays, and of course must free
165
* memory and remove references. The LOCKED_DBT itself is not freed,
166
* as it is expected to be a stack variable.
168
* Note that after this call, the LOCKED_DBT can still be used in
169
* limited ways, e.g. to look at values in the C DBT.
172
locked_dbt_put(LOCKED_DBT *ldbt, JNIEnv *jnienv, DB_ENV *dbenv)
176
dbt = &ldbt->javainfo->dbt;
179
* If the error flag was set, we never succeeded
180
* in allocating storage.
182
if (F_ISSET(ldbt, LOCKED_ERROR))
185
if (((F_ISSET(dbt, DB_DBT_USERMEM) ||
186
F_ISSET(ldbt, LOCKED_REALLOC_NONNULL)) ||
187
ldbt->kind == inOp) && !F_ISSET(ldbt, LOCKED_CREATE_DATA)) {
190
* If writing with DB_DBT_USERMEM or it's a set
191
* (or get/set) operation, then the data may be already in
192
* the java array, in which case, we just need to release it.
193
* If DB didn't put it in the array (indicated by the
194
* dbt->data changing), we need to do that
196
if (ldbt->before_data != ldbt->java_data) {
197
(*jnienv)->SetByteArrayRegion(jnienv,
198
ldbt->javainfo->array,
199
ldbt->javainfo->offset,
203
(*jnienv)->ReleaseByteArrayElements(jnienv,
204
ldbt->javainfo->array,
208
else if (F_ISSET(dbt, DB_DBT_MALLOC | DB_DBT_REALLOC) &&
209
ldbt->kind != inOp && !F_ISSET(ldbt, LOCKED_CREATE_DATA)) {
212
* If writing with DB_DBT_MALLOC, or DB_DBT_REALLOC
213
* with a zero buffer, then the data was allocated by
214
* DB. If dbt->data is zero, it means an error
215
* occurred (and should have been already reported).
220
* In the case of SET_RANGE, the key is inOutOp
221
* and when not found, its data will be left as
222
* its original value. Only copy and free it
223
* here if it has been allocated by DB
224
* (dbt->data has changed).
226
if (dbt->data != ldbt->before_data) {
229
if ((newarr = (*jnienv)->NewByteArray(jnienv,
230
dbt->size)) == NULL) {
231
/* The JVM has posted an exception. */
232
F_SET(ldbt, LOCKED_ERROR);
235
(*jnienv)->SetObjectField(jnienv, ldbt->jdbt,
238
ldbt->javainfo->offset = 0;
239
(*jnienv)->SetByteArrayRegion(jnienv,
240
newarr, 0, dbt->size,
242
(void)__os_ufree(dbenv, dbt->data);
249
* The size field may have changed after a DB API call,
250
* so we set that back too.
252
(*jnienv)->SetIntField(jnienv, ldbt->jdbt, fid_Dbt_size, dbt->size);
253
ldbt->javainfo->array = NULL;
254
F_CLR(ldbt->javainfo, DBT_JAVAINFO_LOCKED);
258
* Realloc the java array to receive data if the DBT used
259
* DB_DBT_REALLOC flag with a non-null data array, and the last
260
* operation set the size field to an amount greater than ulen.
261
* Return 1 if these conditions are met, otherwise 0. This is used
262
* internally to simulate the operations needed for DB_DBT_REALLOC.
264
int locked_dbt_realloc(LOCKED_DBT *ldbt, JNIEnv *jnienv, DB_ENV *dbenv)
268
COMPQUIET(dbenv, NULL);
269
dbt = &ldbt->javainfo->dbt;
271
if (!F_ISSET(ldbt, LOCKED_REALLOC_NONNULL) ||
272
F_ISSET(ldbt, LOCKED_ERROR) || dbt->size <= dbt->ulen)
275
(*jnienv)->ReleaseByteArrayElements(jnienv, ldbt->javainfo->array,
279
* We allocate a new array of the needed size.
280
* We'll set the offset to 0, as the old offset
281
* really doesn't make any sense.
283
if ((ldbt->javainfo->array = (*jnienv)->NewByteArray(jnienv,
284
dbt->size)) == NULL) {
285
F_SET(ldbt, LOCKED_ERROR);
289
ldbt->java_array_len = dbt->ulen = dbt->size;
290
ldbt->javainfo->offset = 0;
291
(*jnienv)->SetObjectField(jnienv, ldbt->jdbt, fid_Dbt_data,
292
ldbt->javainfo->array);
293
ldbt->java_data = (*jnienv)->GetByteArrayElements(jnienv,
294
ldbt->javainfo->array, (jboolean *)0);
295
memcpy(ldbt->java_data, ldbt->before_data, dbt->ulen);
296
dbt->data = ldbt->before_data = ldbt->java_data;
300
/****************************************************************
302
* Implementation of functions to manipulate LOCKED_STRING.
305
locked_string_get(LOCKED_STRING *ls, JNIEnv *jnienv, jstring jstr)
312
ls->string = (*jnienv)->GetStringUTFChars(jnienv, jstr,
317
void locked_string_put(LOCKED_STRING *ls, JNIEnv *jnienv)
320
(*jnienv)->ReleaseStringUTFChars(jnienv, ls->jstr, ls->string);