resetting manifest requested domain to floor
[platform/upstream/db4.git] / libdb_java / db_java.i
1 %module db_java
2
3 %include "various.i"
4 %include "typemaps.i"
5
6 %include "java_util.i"
7 %include "java_except.i"
8 %include "java_typemaps.i"
9 %include "java_stat.i"
10 %include "java_callbacks.i"
11
12 /*
13  * No finalize methods in general - most classes have "destructor" methods
14  * that applications must call explicitly.
15  */
16 %typemap(javafinalize) SWIGTYPE ""
17
18 /*
19  * These are the exceptions - when there is no "close" method, we need to free
20  * the native part at finalization time.  These are exactly the cases where C
21  * applications manage the memory for the handles.
22  */
23 %typemap(javafinalize) struct DbLsn, struct DbLock %{
24   protected void finalize() {
25     try {
26       delete();
27     } catch(Exception e) {
28       System.err.println("Exception during finalization: " + e);
29       e.printStackTrace(System.err);
30     }
31   }
32 %}
33
34 %typemap(javaimports) SWIGTYPE %{
35 import com.sleepycat.db.*;
36 import java.util.Comparator;
37 %}
38
39 /* Class names */
40 %rename(LogSequenceNumber) DbLsn;
41
42 /* Destructors */
43 %rename(close0) close;
44 %rename(remove0) remove;
45 %rename(rename0) rename;
46 %rename(verify0) verify;
47 %rename(abort0) abort;
48 %rename(commit0) commit;
49 %rename(discard0) discard;
50
51 /* Special case methods */
52 %rename(set_tx_timestamp0) set_tx_timestamp;
53
54 /* Extra code in the Java classes */
55 %typemap(javacode) struct DbEnv %{
56         /*
57          * Internally, the JNI layer creates a global reference to each DbEnv,
58          * which can potentially be different to this.  We keep a copy here so
59          * we can clean up after destructors.
60          */
61         private long dbenv_ref;
62         public Environment wrapper;
63
64         private LogRecordHandler app_dispatch_handler;
65         private EventHandler event_notify_handler;
66         private FeedbackHandler env_feedback_handler;
67         private ErrorHandler error_handler;
68         private String errpfx;
69         private MessageHandler message_handler;
70         private PanicHandler panic_handler;
71         private ReplicationTransport rep_transport_handler;
72         private java.io.OutputStream error_stream;
73         private java.io.OutputStream message_stream;
74         private ThreadLocal errBuf;
75
76         public static class RepProcessMessage {
77                 public int envid;
78         }
79
80         /*
81          * Called by the public DbEnv constructor and for private environments
82          * by the Db constructor.
83          */
84         void initialize() {
85                 dbenv_ref = db_java.initDbEnvRef0(this, this);
86                 errBuf = new ThreadLocal();
87                 /* Start with System.err as the default error stream. */
88                 set_error_stream(System.err);
89                 set_message_stream(System.out);
90         }
91
92         void cleanup() {
93                 swigCPtr = 0;
94                 db_java.deleteRef0(dbenv_ref);
95                 dbenv_ref = 0L;
96         }
97
98         public synchronized void close(int flags) throws DatabaseException {
99                 try {
100                         close0(flags);
101                 } finally {
102                         cleanup();
103                 }
104         }
105
106         private final int handle_app_dispatch(DatabaseEntry dbt,
107                                               LogSequenceNumber lsn,
108                                               int recops) {
109                 return app_dispatch_handler.handleLogRecord(wrapper, dbt, lsn,
110                     RecoveryOperation.fromFlag(recops));
111         }
112
113         public LogRecordHandler get_app_dispatch() {
114                 return app_dispatch_handler;
115         }
116
117         private final void handle_panic_event_notify() {
118                 event_notify_handler.handlePanicEvent();
119         }
120
121         private final void handle_rep_client_event_notify() {
122                 event_notify_handler.handleRepClientEvent();
123         }
124
125         private final void handle_rep_elected_event_notify() {
126                 event_notify_handler.handleRepElectedEvent();
127         }
128
129         private final void handle_rep_master_event_notify() {
130                 event_notify_handler.handleRepMasterEvent();
131         }
132
133         private final void handle_rep_new_master_event_notify(int envid) {
134                 event_notify_handler.handleRepNewMasterEvent(envid);
135         }
136
137         private final void handle_rep_perm_failed_event_notify() {
138                 event_notify_handler.handleRepPermFailedEvent();
139         }
140
141         private final void handle_rep_startup_done_event_notify() {
142                 event_notify_handler.handleRepStartupDoneEvent();
143         }
144
145         private final void handle_write_failed_event_notify(int errno) {
146                 event_notify_handler.handleWriteFailedEvent(errno);
147         }
148
149         public EventHandler get_event_notify() {
150                 return event_notify_handler;
151         }
152
153         private final void handle_env_feedback(int opcode, int percent) {
154                 if (opcode == DbConstants.DB_RECOVER)
155                         env_feedback_handler.recoveryFeedback(wrapper, percent);
156                 /* No other environment feedback type supported. */
157         }
158
159         public FeedbackHandler get_feedback() {
160                 return env_feedback_handler;
161         }
162
163         public void set_errpfx(String errpfx) {
164                 this.errpfx = errpfx;
165         }
166
167         public String get_errpfx() {
168                 return errpfx;
169         }
170
171         private final void handle_error(String msg) {
172                 com.sleepycat.util.ErrorBuffer ebuf = (com.sleepycat.util.ErrorBuffer)errBuf.get();
173                 if (ebuf == null) {
174                         /*
175                          * Populate the errBuf ThreadLocal on demand, since the
176                          * callback can be made from different threads.
177                          */
178                         ebuf = new com.sleepycat.util.ErrorBuffer(3);
179                         errBuf.set(ebuf);
180                 }
181                 ebuf.append(msg);
182                 error_handler.error(wrapper, this.errpfx, msg);
183         }
184
185         private final String get_err_msg(String orig_msg) {
186                 com.sleepycat.util.ErrorBuffer ebuf = (com.sleepycat.util.ErrorBuffer)errBuf.get();
187                 String ret = null;
188                 if (ebuf != null) {
189                         ret = ebuf.get();
190                         ebuf.clear();
191                 }
192                 if (ret != null && ret.length() > 0)
193                         return orig_msg + ": " + ret;
194                 return orig_msg;
195         }
196
197         public ErrorHandler get_errcall() {
198                 return error_handler;
199         }
200
201         private final void handle_message(String msg) {
202                 message_handler.message(wrapper, msg);
203         }
204
205         public MessageHandler get_msgcall() {
206                 return message_handler;
207         }
208
209         private final void handle_panic(DatabaseException e) {
210                 panic_handler.panic(wrapper, e);
211         }
212
213         public PanicHandler get_paniccall() {
214                 return panic_handler;
215         }
216
217         private final int handle_rep_transport(DatabaseEntry control,
218                                                DatabaseEntry rec,
219                                                LogSequenceNumber lsn,
220                                                int envid, int flags)
221             throws DatabaseException {
222                 return rep_transport_handler.send(wrapper,
223                     control, rec, lsn, envid,
224                     (flags & DbConstants.DB_REP_NOBUFFER) != 0,
225                     (flags & DbConstants.DB_REP_PERMANENT) != 0,
226                     (flags & DbConstants.DB_REP_ANYWHERE) != 0,
227                     (flags & DbConstants.DB_REP_REREQUEST) != 0);
228         }
229
230         public void lock_vec(/*u_int32_t*/ int locker, int flags,
231                              LockRequest[] list, int offset, int count)
232             throws DatabaseException {
233                 db_javaJNI.DbEnv_lock_vec(swigCPtr, this, locker, flags, list,
234                     offset, count);
235         }
236
237         public synchronized void remove(String db_home, int flags)
238             throws DatabaseException, java.io.FileNotFoundException {
239                 try {
240                         remove0(db_home, flags);
241                 } finally {
242                         cleanup();
243                 }
244         }
245
246         public void set_error_stream(java.io.OutputStream stream) {
247                 error_stream = stream;
248                 final java.io.PrintWriter pw = new java.io.PrintWriter(stream);
249                 set_errcall(new ErrorHandler() {
250                         public void error(Environment env,
251                             String prefix, String buf) /* no exception */ {
252                                 if (prefix != null)
253                                         pw.print(prefix + ": ");
254                                 pw.println(buf);
255                                 pw.flush();
256                         }
257                 });
258         }
259
260         public java.io.OutputStream get_error_stream() {
261                 return error_stream;
262         }
263
264         public void set_message_stream(java.io.OutputStream stream) {
265                 message_stream = stream;
266                 final java.io.PrintWriter pw = new java.io.PrintWriter(stream);
267                 set_msgcall(new MessageHandler() {
268                         public void message(Environment env, String msg)
269                             /* no exception */ {
270                                 pw.println(msg);
271                                 pw.flush();
272                         }
273                 });
274         }
275
276         public java.io.OutputStream get_message_stream() {
277                 return message_stream;
278         }
279
280         public void set_tx_timestamp(java.util.Date timestamp) {
281                 set_tx_timestamp0(timestamp.getTime()/1000);
282         }
283 %}
284
285 %typemap(javacode) struct Db %{
286         /* package */ static final int GIGABYTE = 1 << 30;
287         /*
288          * Internally, the JNI layer creates a global reference to each Db,
289          * which can potentially be different to this.  We keep a copy here so
290          * we can clean up after destructors.
291          */
292         private long db_ref;
293         private DbEnv dbenv;
294         private boolean private_dbenv;
295
296         public Database wrapper;
297         private RecordNumberAppender append_recno_handler;
298         private Comparator bt_compare_handler;
299         private BtreeCompressor bt_compress_handler;
300         private BtreeCompressor bt_decompress_handler;
301         private BtreePrefixCalculator bt_prefix_handler;
302         private Comparator dup_compare_handler;
303         private FeedbackHandler db_feedback_handler;
304         private Comparator h_compare_handler;
305         private Hasher h_hash_handler;
306         private PartitionHandler partition_handler;
307         private SecondaryKeyCreator seckey_create_handler;
308         private SecondaryMultiKeyCreator secmultikey_create_handler;
309         private ForeignKeyNullifier foreignkey_nullify_handler;
310         private ForeignMultiKeyNullifier foreignmultikey_nullify_handler;
311
312         /* Called by the Db constructor */
313         private void initialize(DbEnv dbenv) {
314                 if (dbenv == null) {
315                         private_dbenv = true;
316                         dbenv = db_java.getDbEnv0(this);
317                         dbenv.initialize();
318                 }
319                 this.dbenv = dbenv;
320                 db_ref = db_java.initDbRef0(this, this);
321         }
322
323         private void cleanup() {
324                 swigCPtr = 0;
325                 db_java.deleteRef0(db_ref);
326                 db_ref = 0L;
327                 if (private_dbenv)
328                         dbenv.cleanup();
329                 dbenv = null;
330         }
331
332         public boolean getPrivateDbEnv() {
333                 return private_dbenv;
334         }
335
336         public synchronized void close(int flags) throws DatabaseException {
337                 try {
338                         close0(flags);
339                 } finally {
340                         cleanup();
341                 }
342         }
343
344         public DbEnv get_env() throws DatabaseException {
345                 return dbenv;
346         }
347
348         private final void handle_append_recno(DatabaseEntry data, int recno)
349             throws DatabaseException {
350                 append_recno_handler.appendRecordNumber(wrapper, data, recno);
351         }
352
353         public RecordNumberAppender get_append_recno() {
354                 return append_recno_handler;
355         }
356
357         private final int handle_bt_compare(byte[] arr1, byte[] arr2) {
358                 return bt_compare_handler.compare(arr1, arr2);
359         }
360
361         private final int handle_bt_compress(DatabaseEntry dbt1,
362             DatabaseEntry dbt2, DatabaseEntry dbt3, DatabaseEntry dbt4,
363             DatabaseEntry dbt5) {
364                 return bt_compress_handler.compress(wrapper, dbt1, dbt2,
365                     dbt3, dbt4, dbt5) ? 0 : DbConstants.DB_BUFFER_SMALL;
366         }
367
368         private final int handle_bt_decompress(DatabaseEntry dbt1,
369             DatabaseEntry dbt2, DatabaseEntry dbt3, DatabaseEntry dbt4,
370             DatabaseEntry dbt5) {
371                 return bt_compress_handler.decompress(wrapper, dbt1, dbt2,
372                     dbt3, dbt4, dbt5) ? 0 : DbConstants.DB_BUFFER_SMALL;
373         }
374
375         public Comparator get_bt_compare() {
376                 return bt_compare_handler;
377         }
378
379         public BtreeCompressor get_bt_compress() {
380                 return bt_compress_handler;
381         }
382
383         public BtreeCompressor get_bt_decompress() {
384                 return bt_decompress_handler;
385         }
386
387         private final int handle_bt_prefix(DatabaseEntry dbt1,
388                                            DatabaseEntry dbt2) {
389                 return bt_prefix_handler.prefix(wrapper, dbt1, dbt2);
390         }
391
392         public BtreePrefixCalculator get_bt_prefix() {
393                 return bt_prefix_handler;
394         }
395
396         private final void handle_db_feedback(int opcode, int percent) {
397                 if (opcode == DbConstants.DB_UPGRADE)
398                         db_feedback_handler.upgradeFeedback(wrapper, percent);
399                 else if (opcode == DbConstants.DB_VERIFY)
400                         db_feedback_handler.upgradeFeedback(wrapper, percent);
401                 /* No other database feedback types known. */
402         }
403
404         public FeedbackHandler get_feedback() {
405                 return db_feedback_handler;
406         }
407
408         private final int handle_h_compare(byte[] arr1, byte[] arr2) {
409                 return h_compare_handler.compare(arr1, arr2);
410         }
411
412         public Comparator get_h_compare() {
413                 return h_compare_handler;
414         }
415
416         private final int handle_dup_compare(byte[] arr1, byte[] arr2) {
417                 return dup_compare_handler.compare(arr1, arr2);
418         }
419
420         public Comparator get_dup_compare() {
421                 return dup_compare_handler;
422         }
423
424         private final int handle_h_hash(byte[] data, int len) {
425                 return h_hash_handler.hash(wrapper, data, len);
426         }
427
428         public Hasher get_h_hash() {
429                 return h_hash_handler;
430         }
431
432         private final boolean handle_foreignkey_nullify(
433                                                DatabaseEntry key,       
434                                                DatabaseEntry data,      
435                                                DatabaseEntry seckey)
436             throws DatabaseException {
437                 if (foreignmultikey_nullify_handler != null)
438                         return foreignmultikey_nullify_handler.nullifyForeignKey(
439                             (SecondaryDatabase)wrapper, key, data, seckey);
440                 else
441                         return foreignkey_nullify_handler.nullifyForeignKey(
442                             (SecondaryDatabase)wrapper, data);
443         }
444
445         private final DatabaseEntry[] handle_seckey_create(
446                                                DatabaseEntry key,
447                                                DatabaseEntry data)
448             throws DatabaseException {
449
450                 if (secmultikey_create_handler != null) {
451                         java.util.HashSet keySet = new java.util.HashSet();
452                         secmultikey_create_handler.createSecondaryKeys(
453                             (SecondaryDatabase)wrapper, key, data, keySet);
454                         if (!keySet.isEmpty())
455                                 return (DatabaseEntry[])keySet.toArray(
456                                     new DatabaseEntry[keySet.size()]);
457                 } else {
458                         DatabaseEntry result = new DatabaseEntry();
459                         if (seckey_create_handler.createSecondaryKey(
460                             (SecondaryDatabase)wrapper, key, data, result)) {
461                                 DatabaseEntry[] results = { result };
462                                 return results;
463                         }
464                 }
465
466                 return null;
467         }
468
469         public SecondaryKeyCreator get_seckey_create() {
470                 return seckey_create_handler;
471         }
472
473         public SecondaryMultiKeyCreator get_secmultikey_create() {
474                 return secmultikey_create_handler;
475         }
476
477         public void set_secmultikey_create(
478             SecondaryMultiKeyCreator secmultikey_create_handler) {
479                 this.secmultikey_create_handler = secmultikey_create_handler;
480         }
481
482         public void set_foreignmultikey_nullifier(ForeignMultiKeyNullifier nullify){
483                 this.foreignmultikey_nullify_handler = nullify;
484         }
485
486         private final int handle_partition(DatabaseEntry dbt1) {
487                 return partition_handler.partition(wrapper, dbt1);
488         }
489
490         public PartitionHandler get_partition_callback() {
491                 return partition_handler;
492         }
493
494         public synchronized void remove(String file, String database, int flags)
495             throws DatabaseException, java.io.FileNotFoundException {
496                 try {
497                         remove0(file, database, flags);
498                 } finally {
499                         cleanup();
500                 }
501         }
502
503         public synchronized void rename(String file, String database,
504             String newname, int flags)
505             throws DatabaseException, java.io.FileNotFoundException {
506                 try {
507                         rename0(file, database, newname, flags);
508                 } finally {
509                         cleanup();
510                 }
511         }
512
513         public synchronized boolean verify(String file, String database,
514             java.io.OutputStream outfile, int flags)
515             throws DatabaseException, java.io.FileNotFoundException {
516                 try {
517                         return verify0(file, database, outfile, flags);
518                 } finally {
519                         cleanup();
520                 }
521         }
522
523         public ErrorHandler get_errcall() {
524                 return dbenv.get_errcall();
525         }
526
527         public void set_errcall(ErrorHandler db_errcall_fcn) {
528                 dbenv.set_errcall(db_errcall_fcn);
529         }
530
531         public java.io.OutputStream get_error_stream() {
532                 return dbenv.get_error_stream();
533         }
534
535         public void set_error_stream(java.io.OutputStream stream) {
536                 dbenv.set_error_stream(stream);
537         }
538
539         public void set_errpfx(String errpfx) {
540                 dbenv.set_errpfx(errpfx);
541         }
542
543         public String get_errpfx() {
544                 return dbenv.get_errpfx();
545         }
546
547         public java.io.OutputStream get_message_stream() {
548                 return dbenv.get_message_stream();
549         }
550
551         public void set_message_stream(java.io.OutputStream stream) {
552                 dbenv.set_message_stream(stream);
553         }
554
555         public MessageHandler get_msgcall() {
556                 return dbenv.get_msgcall();
557         }
558
559         public void set_msgcall(MessageHandler db_msgcall_fcn) {
560                 dbenv.set_msgcall(db_msgcall_fcn);
561         }
562
563         public void set_paniccall(PanicHandler db_panic_fcn)
564             throws DatabaseException {
565                 dbenv.set_paniccall(db_panic_fcn);
566         }
567
568         public PanicHandler get_paniccall() {
569                 return dbenv.get_paniccall();
570         }
571 %}
572
573 %typemap(javacode) struct Dbc %{
574         public synchronized void close() throws DatabaseException {
575                 try {
576                         close0();
577                 } finally {
578                         swigCPtr = 0;
579                 }
580         }
581 %}
582
583 %typemap(javacode) struct DbLock %{
584         public Lock wrapper;
585 %}
586
587 %typemap(javacode) struct DbLogc %{
588         public synchronized void close(int flags) throws DatabaseException {
589                 try {
590                         close0(flags);
591                 } finally {
592                         swigCPtr = 0;
593                 }
594         }
595 %}
596
597 %typemap(javacode) struct DbSequence %{
598         public Sequence wrapper;
599
600         public synchronized void close(int flags) throws DatabaseException {
601                 try {
602                         close0(flags);
603                 } finally {
604                         swigCPtr = 0;
605                 }
606         }
607
608         public synchronized void remove(DbTxn txn, int flags)
609             throws DatabaseException {
610                 try {
611                         remove0(txn, flags);
612                 } finally {
613                         swigCPtr = 0;
614                 }
615         }
616 %}
617
618 %typemap(javacode) struct DbTxn %{
619         public void abort() throws DatabaseException {
620                 try {
621                         abort0();
622                 } finally {
623                         swigCPtr = 0;
624                 }
625         }
626
627         public void commit(int flags) throws DatabaseException {
628                 try {
629                         commit0(flags);
630                 } finally {
631                         swigCPtr = 0;
632                 }
633         }
634
635         public void discard(int flags) throws DatabaseException {
636                 try {
637                         discard0(flags);
638                 } finally {
639                         swigCPtr = 0;
640                 }
641         }
642
643         /*
644          * We override Object.equals because it is possible for the Java API to
645          * create multiple DbTxns that reference the same underlying object.
646          * This can happen for example during DbEnv.txn_recover().
647          */
648         public boolean equals(Object obj)
649         {
650                 if (this == obj)
651                         return true;
652
653                 if (obj != null && (obj instanceof DbTxn)) {
654                         DbTxn that = (DbTxn)obj;
655                         return (this.swigCPtr == that.swigCPtr);
656                 }
657                 return false;
658         }
659
660         /*
661          * We must override Object.hashCode whenever we override
662          * Object.equals() to enforce the maxim that equal objects have the
663          * same hashcode.
664          */
665         public int hashCode()
666         {
667                 return ((int)swigCPtr ^ (int)(swigCPtr >> 32));
668         }
669 %}
670
671 %native(initDbEnvRef0) jlong initDbEnvRef0(DB_ENV *self, void *handle);
672 %native(initDbRef0) jlong initDbRef0(DB *self, void *handle);
673 %native(deleteRef0) void deleteRef0(jlong ref);
674 %native(getDbEnv0) DB_ENV *getDbEnv0(DB *self);
675
676 %{
677 SWIGEXPORT jlong JNICALL
678 Java_com_sleepycat_db_internal_db_1javaJNI_initDbEnvRef0(
679     JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jobject jarg2) {
680         DB_ENV *self = *(DB_ENV **)(void *)&jarg1;
681         jlong ret;
682         COMPQUIET(jcls, NULL);
683         COMPQUIET(jarg1_, NULL);
684
685         DB_ENV_INTERNAL(self) = (void *)(*jenv)->NewGlobalRef(jenv, jarg2);
686         *(jobject *)(void *)&ret = (jobject)DB_ENV_INTERNAL(self);
687         return (ret);
688 }
689
690 SWIGEXPORT jlong JNICALL
691 Java_com_sleepycat_db_internal_db_1javaJNI_initDbRef0(
692     JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jobject jarg2) {
693         DB *self = *(DB **)(void *)&jarg1;
694         jlong ret;
695         COMPQUIET(jcls, NULL);
696         COMPQUIET(jarg1_, NULL);
697
698         DB_INTERNAL(self) = (void *)(*jenv)->NewGlobalRef(jenv, jarg2);
699         *(jobject *)(void *)&ret = (jobject)DB_INTERNAL(self);
700         return (ret);
701 }
702
703 SWIGEXPORT void JNICALL
704 Java_com_sleepycat_db_internal_db_1javaJNI_deleteRef0(
705     JNIEnv *jenv, jclass jcls, jlong jarg1) {
706         jobject jref = *(jobject *)(void *)&jarg1;
707         COMPQUIET(jcls, NULL);
708
709         if (jref != 0L)
710                 (*jenv)->DeleteGlobalRef(jenv, jref);
711 }
712
713 SWIGEXPORT jlong JNICALL
714 Java_com_sleepycat_db_internal_db_1javaJNI_getDbEnv0(
715     JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
716         DB *self = *(DB **)(void *)&jarg1;
717         jlong ret;
718
719         COMPQUIET(jenv, NULL);
720         COMPQUIET(jcls, NULL);
721         COMPQUIET(jarg1_, NULL);
722
723         *(DB_ENV **)(void *)&ret = self->dbenv;
724         return (ret);
725 }
726
727 SWIGEXPORT jboolean JNICALL
728 Java_com_sleepycat_db_internal_DbUtil_is_1big_1endian(
729     JNIEnv *jenv, jclass clazz)
730 {
731         COMPQUIET(jenv, NULL);
732         COMPQUIET(clazz, NULL);
733
734         return (__db_isbigendian() ? JNI_TRUE : JNI_FALSE);
735 }
736 %}