File: java_DbEnv.cpp

package info (click to toggle)
htdig 3.1.6-3woody1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,324 kB
  • ctags: 10,056
  • sloc: ansic: 39,949; cpp: 21,701; tcl: 8,401; sh: 3,227; perl: 2,280; makefile: 1,650; java: 1,632; awk: 111; xml: 80; asm: 41
file content (262 lines) | stat: -rw-r--r-- 8,401 bytes parent folder | download | duplicates (5)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1997, 1998
 *	Sleepycat Software.  All rights reserved.
 */
#include "config.h"

#ifndef lint
static const char sccsid[] = "@(#)java_DbEnv.cpp	10.7 (Sleepycat) 10/27/98";
#endif /* not lint */

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

#include "db.h"
#include "java_util.h"
#include "com_sleepycat_db_DbEnv.h"

// XXX TODO:
// A prefix_info is allocated and stuffed into the db_errpfx for
// every DB_ENV created.  It holds a little extra info that is
// needed to resurrect the environment to make a callback.
// Strictly speaking, we only need this stuffed in if db_errfcn
// is set, but it's easier to always allocate it in the constructor
// and know it's always there.
//
// Note: We assume the following:
// We are the only one fiddling with the contents of db_errpfx and
// db_errcall.  In particular, db_errpfx and db_errcall are not
// initialized by appinit to some default value.
//
struct prefix_info
{
    JNIEnv *jnienv;
    char *orig_prefix;
    jobject dberrcall;
};

static void java_errcall_callback(const char *prefix, char *message)
{
    prefix_info *pi = (prefix_info *)prefix;

    // Note: these error cases are "impossible", and would
    // normally warrant an exception.  However, without
    // a jnienv, we cannot throw an exception...
    // We don't want to trap or exit, since the point of
    // this facility is for the user to completely control
    // error situations.
    //
    // TODO: find another way to indicate this error.
    //
    if (!pi)
        return;                 // These are really fatal asserts

    if (!pi->dberrcall)
        return;                 // These are really fatal asserts

    if (!pi->jnienv)
        return;                 // These are really fatal asserts

    jstring pre = get_java_string(pi->jnienv, pi->orig_prefix);
    jstring msg = get_java_string(pi->jnienv, message);
    jclass errcall_class = get_class(pi->jnienv, name_DbErrcall);
    jmethodID id = pi->jnienv->GetMethodID(errcall_class,
                           "errcall",
                           "(Ljava/lang/String;Ljava/lang/String;)V");
    if (!id)
        return;

    pi->jnienv->CallVoidMethod(pi->dberrcall, id, pre, msg);
}

JAVADB_WO_ACCESS_BEFORE_APPINIT(DbEnv, jint, lorder, DB_ENV, db_lorder)
JAVADB_WO_ACCESS_BEFORE_APPINIT(DbEnv, jint, verbose, DB_ENV, db_verbose)
JAVADB_WO_ACCESS_BEFORE_APPINIT(DbEnv, jint, lk_1modes, DB_ENV, lk_modes)
JAVADB_WO_ACCESS_BEFORE_APPINIT(DbEnv, jint, lk_1max, DB_ENV, lk_max)
JAVADB_WO_ACCESS_BEFORE_APPINIT(DbEnv, jint, lk_1detect, DB_ENV, lk_detect)
JAVADB_WO_ACCESS_BEFORE_APPINIT(DbEnv, jint, lg_1max, DB_ENV, lg_max)
JAVADB_WO_ACCESS_BEFORE_APPINIT(DbEnv, jlong, mp_1mmapsize, DB_ENV, mp_mmapsize)
JAVADB_WO_ACCESS_BEFORE_APPINIT(DbEnv, jlong, mp_1size, DB_ENV, mp_size)
JAVADB_WO_ACCESS_BEFORE_APPINIT(DbEnv, jint, tx_1max, DB_ENV, tx_max)

JNIEXPORT void JNICALL Java_com_sleepycat_db_DbEnv_init
  (JNIEnv *jnienv, jobject jthis)
{
    DB_ENV *dbenv = NEW(DB_ENV);
    memset(dbenv, 0, sizeof(DB_ENV));
    prefix_info *pi = NEW(prefix_info);
    pi->jnienv = 0;
    pi->orig_prefix = 0;
    pi->dberrcall = 0;
    dbenv->db_errpfx = (const char*)pi;
    set_private_info(jnienv, name_DB_ENV, jthis, dbenv);
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_DbEnv_appinit
  (JNIEnv *jnienv, /*DbEnv*/ jobject jthis, jstring homeDir,
   jobjectArray /*String[]*/ db_config, jint flags)
{
    int err;
    DB_ENV *dbenv = get_DB_ENV(jnienv, jthis);
    LockedString dbhomeDir(jnienv, homeDir);
    LockedStringArray db_db_config(jnienv, db_config);

    // Always turn on the thread flag for use with java.
    flags |= DB_THREAD;

    err = db_appinit(dbhomeDir.string,
                     (char *const *)db_db_config.string_array,
                     dbenv, flags);
    if (verify_return(jnienv, err)) {
        jclass dbenvClass = get_class(jnienv, name_DB_ENV);
        jobject obj;
        if (dbenv->lk_info) {
            obj = get_DbLockTab(jnienv, dbenv->lk_info);
            set_object_field(jnienv, dbenvClass, jthis, name_DB_LOCKTAB,
                             "lk_info_", obj);
        }
        if (dbenv->lg_info) {
            obj = get_DbLog(jnienv, dbenv->lg_info);
            set_object_field(jnienv, dbenvClass, jthis, name_DB_LOG,
                             "lg_info_", obj);
        }
        if (dbenv->mp_info) {
            obj = get_DbMpool(jnienv, dbenv->mp_info);
            set_object_field(jnienv, dbenvClass, jthis, name_DB_MPOOL,
                             "mp_info_", obj);
        }
        if (dbenv->tx_info) {
            obj = get_DbTxnMgr(jnienv, dbenv->tx_info);
            set_object_field(jnienv, dbenvClass, jthis, name_DB_TXNMGR,
                             "tx_info_", obj);
        }
    }
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_DbEnv_appexit
  (JNIEnv *jnienv, /*DbEnv*/ jobject jthis)
{
    int err;
    DB_ENV *dbenv = get_DB_ENV(jnienv, jthis);

    if (dbenv) {
        err = db_appexit(dbenv);
        set_private_info(jnienv, name_DB_ENV, jthis, 0);
        verify_return(jnienv, err);
    }
}


JNIEXPORT void JNICALL Java_com_sleepycat_db_DbEnv_set_1lk_1conflicts
  (JNIEnv *jnienv, jobject jthis, jobjectArray array)
{
    DB_ENV *dbenv = get_DB_ENV(jnienv, jthis);
    static const char * const array_length_msg =
        "array length does not match lk_modes";

    if (!verify_non_null(jnienv, dbenv))
        return;

    int len = dbenv->lk_modes;
    jsize jlen = jnienv->GetArrayLength(array);
    if (jlen != len) {
        report_exception(jnienv, array_length_msg, 0);
        return;
    }

    // TODO: possibly delete old contents, and delete this array
    // in destructor.  How to distinguish from "default" value?
    //
    dbenv->lk_conflicts = NEW_ARRAY(unsigned char, len * len);

    for (int i=0; i<len; i++) {
        jobject subArray = jnienv->GetObjectArrayElement(array, i);
        jnienv->GetByteArrayRegion((jbyteArray)subArray, 0, len,
                                   (jbyte *)&dbenv->lk_conflicts[i*len]);
    }
}

JNIEXPORT jint JNICALL Java_com_sleepycat_db_DbEnv_get_1version_1major
  (JNIEnv *jnienv, jclass /*this_class*/)
{
    return DB_VERSION_MAJOR;
}

JNIEXPORT jint JNICALL Java_com_sleepycat_db_DbEnv_get_1version_1minor
  (JNIEnv *jnienv, jclass /*this_class*/)
{
    return DB_VERSION_MINOR;
}

JNIEXPORT jint JNICALL Java_com_sleepycat_db_DbEnv_get_1version_1patch
  (JNIEnv *jnienv, jclass /*this_class*/)
{
    return DB_VERSION_PATCH;
}

JNIEXPORT jstring JNICALL Java_com_sleepycat_db_DbEnv_get_1version_1string
  (JNIEnv *jnienv, jclass /*this_class*/)
{
    return jnienv->NewStringUTF(DB_VERSION_STRING);
}

// See discussion on errpfx above
JNIEXPORT void JNICALL Java_com_sleepycat_db_DbEnv_set_1errcall
  (JNIEnv *jnienv, jobject jthis, jobject errcall)
{
    DB_ENV *dbenv = get_DB_ENV(jnienv, jthis);

    if (verify_non_null(jnienv, dbenv)) {
        prefix_info *pi = (prefix_info*)(dbenv->db_errpfx);
        if (pi->dberrcall) {
            jnienv->DeleteGlobalRef(pi->dberrcall);
        }
        if (errcall) {
            pi->dberrcall = jnienv->NewGlobalRef(errcall);
            dbenv->db_errcall = java_errcall_callback;
            pi->jnienv = jnienv;
        }
        else {
            pi->dberrcall = 0;
            dbenv->db_errcall = 0;
            pi->jnienv = 0;
        }
    }
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_DbEnv_set_1errpfx
  (JNIEnv *jnienv, jobject jthis, jstring str)
{
    DB_ENV *dbenv = get_DB_ENV(jnienv, jthis);

    if (verify_non_null(jnienv, dbenv)) {
        prefix_info *pi = (prefix_info*)(dbenv->db_errpfx);
        if (pi->orig_prefix)
            DELETE(pi->orig_prefix);
        if (str)
            pi->orig_prefix =
                dup_string(jnienv->GetStringUTFChars(str, NULL));
        else
            pi->orig_prefix = 0;
    }
}

JNIEXPORT void JNICALL Java_com_sleepycat_db_DbEnv_finalize
  (JNIEnv *jnienv, jobject jthis)
{
    DB_ENV *dbenv = get_DB_ENV(jnienv, jthis);
    if (dbenv) {
        // Free any data related to DB_ENV here
        prefix_info *pi = (prefix_info*)dbenv->db_errpfx;
        if (pi)
            DELETE(pi);
        DELETE(dbenv);
    }

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