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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
|
%module db_java
%include "various.i"
%include "typemaps.i"
%include "java_util.i"
%include "java_except.i"
%include "java_typemaps.i"
%include "java_stat.i"
%include "java_callbacks.i"
/*
* No finalize methods in general - most classes have "destructor" methods
* that applications must call explicitly.
*/
%typemap(javafinalize) SWIGTYPE ""
/*
* These are the exceptions - when there is no "close" method, we need to free
* the native part at finalization time. These are exactly the cases where C
* applications manage the memory for the handles.
*/
%typemap(javafinalize) struct DbLsn, struct DbLock %{
protected void finalize() {
try {
delete();
} catch(Exception e) {
System.err.println("Exception during finalization: " + e);
e.printStackTrace(System.err);
}
}
%}
%typemap(javaimports) SWIGTYPE %{
import com.sleepycat.db.*;
import java.util.Comparator;
%}
/* Class names */
%rename(LogSequenceNumber) DbLsn;
/* Destructors */
%rename(close0) close;
%rename(remove0) remove;
%rename(rename0) rename;
%rename(verify0) verify;
%rename(abort0) abort;
%rename(commit0) commit;
%rename(discard0) discard;
/* Special case methods */
%rename(set_tx_timestamp0) set_tx_timestamp;
/* Extra code in the Java classes */
%typemap(javacode) struct DbEnv %{
/*
* Internally, the JNI layer creates a global reference to each DbEnv,
* which can potentially be different to this. We keep a copy here so
* we can clean up after destructors.
*/
private long dbenv_ref;
public Environment wrapper;
private LogRecordHandler app_dispatch_handler;
private EventHandler event_notify_handler;
private FeedbackHandler env_feedback_handler;
private ErrorHandler error_handler;
private String errpfx;
private MessageHandler message_handler;
private PanicHandler panic_handler;
private ReplicationTransport rep_transport_handler;
private java.io.OutputStream error_stream;
private java.io.OutputStream message_stream;
private ThreadLocal errMsg;
public static class RepProcessMessage {
public int envid;
}
/*
* Called by the public DbEnv constructor and for private environments
* by the Db constructor.
*/
void initialize() {
dbenv_ref = db_java.initDbEnvRef0(this, this);
errMsg = new ThreadLocal();
/* Start with System.err as the default error stream. */
set_error_stream(System.err);
set_message_stream(System.out);
}
void cleanup() {
swigCPtr = 0;
db_java.deleteRef0(dbenv_ref);
dbenv_ref = 0L;
}
public synchronized void close(int flags) throws DatabaseException {
try {
close0(flags);
} finally {
cleanup();
}
}
private final int handle_app_dispatch(DatabaseEntry dbt,
LogSequenceNumber lsn,
int recops) {
return app_dispatch_handler.handleLogRecord(wrapper, dbt, lsn,
RecoveryOperation.fromFlag(recops));
}
public LogRecordHandler get_app_dispatch() {
return app_dispatch_handler;
}
private final void handle_panic_event_notify() {
event_notify_handler.handlePanicEvent();
}
private final void handle_rep_client_event_notify() {
event_notify_handler.handleRepClientEvent();
}
private final void handle_rep_elected_event_notify() {
event_notify_handler.handleRepElectedEvent();
}
private final void handle_rep_master_event_notify() {
event_notify_handler.handleRepMasterEvent();
}
private final void handle_rep_new_master_event_notify(int envid) {
event_notify_handler.handleRepNewMasterEvent(envid);
}
private final void handle_rep_perm_failed_event_notify() {
event_notify_handler.handleRepPermFailedEvent();
}
private final void handle_rep_startup_done_event_notify() {
event_notify_handler.handleRepStartupDoneEvent();
}
private final void handle_write_failed_event_notify(int errno) {
event_notify_handler.handleWriteFailedEvent(errno);
}
public EventHandler get_event_notify() {
return event_notify_handler;
}
private final void handle_env_feedback(int opcode, int percent) {
if (opcode == DbConstants.DB_RECOVER)
env_feedback_handler.recoveryFeedback(wrapper, percent);
/* No other environment feedback type supported. */
}
public FeedbackHandler get_feedback() {
return env_feedback_handler;
}
public void set_errpfx(String errpfx) {
this.errpfx = errpfx;
}
public String get_errpfx() {
return errpfx;
}
private final void handle_error(String msg) {
StringBuffer tbuf = (StringBuffer) errMsg.get();
/*
* Populate the errMsg ThreadLocal on demand, since the
* callback can be made from different threads.
*/
if (tbuf == null) {
tbuf = new StringBuffer();
errMsg.set(tbuf);
}
tbuf.append(msg);
error_handler.error(wrapper, this.errpfx, msg);
}
private final String get_err_msg(String orig_msg) {
String ret = null;
StringBuffer tbuf = (StringBuffer) errMsg.get();
if (tbuf != null) {
ret = tbuf.toString();
tbuf.delete(0, tbuf.length());
}
return orig_msg + ": " + ret;
}
public ErrorHandler get_errcall() {
return error_handler;
}
private final void handle_message(String msg) {
message_handler.message(wrapper, msg);
}
public MessageHandler get_msgcall() {
return message_handler;
}
private final void handle_panic(DatabaseException e) {
panic_handler.panic(wrapper, e);
}
public PanicHandler get_paniccall() {
return panic_handler;
}
private final int handle_rep_transport(DatabaseEntry control,
DatabaseEntry rec,
LogSequenceNumber lsn,
int envid, int flags)
throws DatabaseException {
return rep_transport_handler.send(wrapper,
control, rec, lsn, envid,
(flags & DbConstants.DB_REP_NOBUFFER) != 0,
(flags & DbConstants.DB_REP_PERMANENT) != 0,
(flags & DbConstants.DB_REP_ANYWHERE) != 0,
(flags & DbConstants.DB_REP_REREQUEST) != 0);
}
public void lock_vec(/*u_int32_t*/ int locker, int flags,
LockRequest[] list, int offset, int count)
throws DatabaseException {
db_javaJNI.DbEnv_lock_vec(swigCPtr, locker, flags, list,
offset, count);
}
public synchronized void remove(String db_home, int flags)
throws DatabaseException, java.io.FileNotFoundException {
try {
remove0(db_home, flags);
} finally {
cleanup();
}
}
public void set_error_stream(java.io.OutputStream stream) {
error_stream = stream;
final java.io.PrintWriter pw = new java.io.PrintWriter(stream);
set_errcall(new ErrorHandler() {
public void error(Environment env,
String prefix, String buf) /* no exception */ {
if (prefix != null)
pw.print(prefix + ": ");
pw.println(buf);
pw.flush();
}
});
}
public java.io.OutputStream get_error_stream() {
return error_stream;
}
public void set_message_stream(java.io.OutputStream stream) {
message_stream = stream;
final java.io.PrintWriter pw = new java.io.PrintWriter(stream);
set_msgcall(new MessageHandler() {
public void message(Environment env, String msg)
/* no exception */ {
pw.println(msg);
pw.flush();
}
});
}
public java.io.OutputStream get_message_stream() {
return message_stream;
}
public void set_tx_timestamp(java.util.Date timestamp) {
set_tx_timestamp0(timestamp.getTime()/1000);
}
%}
%typemap(javacode) struct Db %{
/* package */ static final int GIGABYTE = 1 << 30;
/*
* Internally, the JNI layer creates a global reference to each Db,
* which can potentially be different to this. We keep a copy here so
* we can clean up after destructors.
*/
private long db_ref;
private DbEnv dbenv;
private boolean private_dbenv;
public Database wrapper;
private RecordNumberAppender append_recno_handler;
private Comparator bt_compare_handler;
private BtreePrefixCalculator bt_prefix_handler;
private Comparator dup_compare_handler;
private FeedbackHandler db_feedback_handler;
private Comparator h_compare_handler;
private Hasher h_hash_handler;
private SecondaryKeyCreator seckey_create_handler;
private SecondaryMultiKeyCreator secmultikey_create_handler;
/* Called by the Db constructor */
private void initialize(DbEnv dbenv) {
if (dbenv == null) {
private_dbenv = true;
dbenv = db_java.getDbEnv0(this);
dbenv.initialize();
}
this.dbenv = dbenv;
db_ref = db_java.initDbRef0(this, this);
}
private void cleanup() {
swigCPtr = 0;
db_java.deleteRef0(db_ref);
db_ref = 0L;
if (private_dbenv)
dbenv.cleanup();
dbenv = null;
}
public boolean getPrivateDbEnv() {
return private_dbenv;
}
public synchronized void close(int flags) throws DatabaseException {
try {
close0(flags);
} finally {
cleanup();
}
}
public DbEnv get_env() throws DatabaseException {
return dbenv;
}
private final void handle_append_recno(DatabaseEntry data, int recno)
throws DatabaseException {
append_recno_handler.appendRecordNumber(wrapper, data, recno);
}
public RecordNumberAppender get_append_recno() {
return append_recno_handler;
}
private final int handle_bt_compare(byte[] arr1, byte[] arr2) {
return bt_compare_handler.compare(arr1, arr2);
}
public Comparator get_bt_compare() {
return bt_compare_handler;
}
private final int handle_bt_prefix(DatabaseEntry dbt1,
DatabaseEntry dbt2) {
return bt_prefix_handler.prefix(wrapper, dbt1, dbt2);
}
public BtreePrefixCalculator get_bt_prefix() {
return bt_prefix_handler;
}
private final void handle_db_feedback(int opcode, int percent) {
if (opcode == DbConstants.DB_UPGRADE)
db_feedback_handler.upgradeFeedback(wrapper, percent);
else if (opcode == DbConstants.DB_VERIFY)
db_feedback_handler.upgradeFeedback(wrapper, percent);
/* No other database feedback types known. */
}
public FeedbackHandler get_feedback() {
return db_feedback_handler;
}
private final int handle_h_compare(byte[] arr1, byte[] arr2) {
return h_compare_handler.compare(arr1, arr2);
}
public Comparator get_h_compare() {
return h_compare_handler;
}
private final int handle_dup_compare(byte[] arr1, byte[] arr2) {
return dup_compare_handler.compare(arr1, arr2);
}
public Comparator get_dup_compare() {
return dup_compare_handler;
}
private final int handle_h_hash(byte[] data, int len) {
return h_hash_handler.hash(wrapper, data, len);
}
public Hasher get_h_hash() {
return h_hash_handler;
}
private final DatabaseEntry[] handle_seckey_create(
DatabaseEntry key,
DatabaseEntry data)
throws DatabaseException {
if (secmultikey_create_handler != null) {
java.util.HashSet keySet = new java.util.HashSet();
secmultikey_create_handler.createSecondaryKeys(
(SecondaryDatabase)wrapper, key, data, keySet);
if (!keySet.isEmpty())
return (DatabaseEntry[])keySet.toArray(
new DatabaseEntry[keySet.size()]);
} else {
DatabaseEntry result = new DatabaseEntry();
if (seckey_create_handler.createSecondaryKey(
(SecondaryDatabase)wrapper, key, data, result)) {
DatabaseEntry[] results = { result };
return results;
}
}
return null;
}
public SecondaryKeyCreator get_seckey_create() {
return seckey_create_handler;
}
public SecondaryMultiKeyCreator get_secmultikey_create() {
return secmultikey_create_handler;
}
public void get_secmultikey_create(
SecondaryMultiKeyCreator secmultikey_create_handler) {
this.secmultikey_create_handler = secmultikey_create_handler;
}
public synchronized void remove(String file, String database, int flags)
throws DatabaseException, java.io.FileNotFoundException {
try {
remove0(file, database, flags);
} finally {
cleanup();
}
}
public synchronized void rename(String file, String database,
String newname, int flags)
throws DatabaseException, java.io.FileNotFoundException {
try {
rename0(file, database, newname, flags);
} finally {
cleanup();
}
}
public synchronized boolean verify(String file, String database,
java.io.OutputStream outfile, int flags)
throws DatabaseException, java.io.FileNotFoundException {
try {
return verify0(file, database, outfile, flags);
} finally {
cleanup();
}
}
public ErrorHandler get_errcall() {
return dbenv.get_errcall();
}
public void set_errcall(ErrorHandler db_errcall_fcn) {
dbenv.set_errcall(db_errcall_fcn);
}
public java.io.OutputStream get_error_stream() {
return dbenv.get_error_stream();
}
public void set_error_stream(java.io.OutputStream stream) {
dbenv.set_error_stream(stream);
}
public void set_errpfx(String errpfx) {
dbenv.set_errpfx(errpfx);
}
public String get_errpfx() {
return dbenv.get_errpfx();
}
public java.io.OutputStream get_message_stream() {
return dbenv.get_message_stream();
}
public void set_message_stream(java.io.OutputStream stream) {
dbenv.set_message_stream(stream);
}
public MessageHandler get_msgcall() {
return dbenv.get_msgcall();
}
public void set_msgcall(MessageHandler db_msgcall_fcn) {
dbenv.set_msgcall(db_msgcall_fcn);
}
public void set_paniccall(PanicHandler db_panic_fcn)
throws DatabaseException {
dbenv.set_paniccall(db_panic_fcn);
}
public PanicHandler get_paniccall() {
return dbenv.get_paniccall();
}
%}
%typemap(javacode) struct Dbc %{
public synchronized void close() throws DatabaseException {
try {
close0();
} finally {
swigCPtr = 0;
}
}
%}
%typemap(javacode) struct DbLock %{
public Lock wrapper;
%}
%typemap(javacode) struct DbLogc %{
public synchronized void close(int flags) throws DatabaseException {
try {
close0(flags);
} finally {
swigCPtr = 0;
}
}
%}
%typemap(javacode) struct DbSequence %{
public Sequence wrapper;
public synchronized void close(int flags) throws DatabaseException {
try {
close0(flags);
} finally {
swigCPtr = 0;
}
}
public synchronized void remove(DbTxn txn, int flags)
throws DatabaseException {
try {
remove0(txn, flags);
} finally {
swigCPtr = 0;
}
}
%}
%typemap(javacode) struct DbTxn %{
public void abort() throws DatabaseException {
try {
abort0();
} finally {
swigCPtr = 0;
}
}
public void commit(int flags) throws DatabaseException {
try {
commit0(flags);
} finally {
swigCPtr = 0;
}
}
public void discard(int flags) throws DatabaseException {
try {
discard0(flags);
} finally {
swigCPtr = 0;
}
}
/*
* We override Object.equals because it is possible for the Java API to
* create multiple DbTxns that reference the same underlying object.
* This can happen for example during DbEnv.txn_recover().
*/
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj != null && (obj instanceof DbTxn)) {
DbTxn that = (DbTxn)obj;
return (this.swigCPtr == that.swigCPtr);
}
return false;
}
/*
* We must override Object.hashCode whenever we override
* Object.equals() to enforce the maxim that equal objects have the
* same hashcode.
*/
public int hashCode()
{
return ((int)swigCPtr ^ (int)(swigCPtr >> 32));
}
%}
%native(initDbEnvRef0) jlong initDbEnvRef0(DB_ENV *self, void *handle);
%native(initDbRef0) jlong initDbRef0(DB *self, void *handle);
%native(deleteRef0) void deleteRef0(jlong ref);
%native(getDbEnv0) DB_ENV *getDbEnv0(DB *self);
%{
JNIEXPORT jlong JNICALL
Java_com_sleepycat_db_internal_db_1javaJNI_initDbEnvRef0(
JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg2) {
DB_ENV *self = *(DB_ENV **)(void *)&jarg1;
jlong ret;
COMPQUIET(jcls, NULL);
DB_ENV_INTERNAL(self) = (void *)(*jenv)->NewGlobalRef(jenv, jarg2);
*(jobject *)(void *)&ret = (jobject)DB_ENV_INTERNAL(self);
return (ret);
}
JNIEXPORT jlong JNICALL
Java_com_sleepycat_db_internal_db_1javaJNI_initDbRef0(
JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg2) {
DB *self = *(DB **)(void *)&jarg1;
jlong ret;
COMPQUIET(jcls, NULL);
DB_INTERNAL(self) = (void *)(*jenv)->NewGlobalRef(jenv, jarg2);
*(jobject *)(void *)&ret = (jobject)DB_INTERNAL(self);
return (ret);
}
JNIEXPORT void JNICALL
Java_com_sleepycat_db_internal_db_1javaJNI_deleteRef0(
JNIEnv *jenv, jclass jcls, jlong jarg1) {
jobject jref = *(jobject *)(void *)&jarg1;
COMPQUIET(jcls, NULL);
if (jref != 0L)
(*jenv)->DeleteGlobalRef(jenv, jref);
}
JNIEXPORT jlong JNICALL
Java_com_sleepycat_db_internal_db_1javaJNI_getDbEnv0(
JNIEnv *jenv, jclass jcls, jlong jarg1) {
DB *self = *(DB **)(void *)&jarg1;
jlong ret;
COMPQUIET(jenv, NULL);
COMPQUIET(jcls, NULL);
*(DB_ENV **)(void *)&ret = self->dbenv;
return (ret);
}
JNIEXPORT jboolean JNICALL
Java_com_sleepycat_db_internal_DbUtil_is_1big_1endian(
JNIEnv *jenv, jclass clazz)
{
COMPQUIET(jenv, NULL);
COMPQUIET(clazz, NULL);
return (__db_isbigendian() ? JNI_TRUE : JNI_FALSE);
}
%}
|