#21
authorVyacheslav Tyutyunkov <tyutyunkov@gmail.com>
Tue, 19 Mar 2013 03:59:59 +0000 (10:59 +0700)
committerVyacheslav Tyutyunkov <tyutyunkov@gmail.com>
Tue, 19 Mar 2013 03:59:59 +0000 (10:59 +0700)
jejdb/src/cpp/jejdb.c
jejdb/src/cpp/org_ejdb_driver_EJDBCollection.h
jejdb/src/java/org/ejdb/driver/EJDBCollection.java

index a760a3c..c0d32a3 100755 (executable)
@@ -620,3 +620,57 @@ JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBResultSet_close (JNIEnv *env, jo
 
        set_rs_to_object(env, obj, NULL);
 };
+
+/*
+ * Class:     org_ejdb_driver_EJDBCollection
+ * Method:    txControl
+ * Signature: (I)Z
+ */
+JNIEXPORT jboolean JNICALL Java_org_ejdb_driver_EJDBCollection_txControl (JNIEnv *env, jobject obj, jint mode) {
+       EJDB* db = get_ejdb_from_object(env, obj);
+       if (!ejdbisopen(db)) {
+               set_error(env, 0, "EJDB not opened");
+               return JNI_FALSE;
+       }
+
+       jstring colname = get_coll_name(env, obj);
+       const char *cname = (*env)->GetStringUTFChars(env, colname, NULL);
+       EJCOLL * coll = ejdbcreatecoll(db, cname, NULL);
+       (*env)->ReleaseStringUTFChars(env, colname, cname);
+
+       if (!coll) {
+               set_ejdb_error(env, db);
+               return JNI_FALSE;
+       }
+
+       bool status = false, txActive = false;
+
+       switch (mode) {
+       case org_ejdb_driver_EJDBCollection_JBTXBEGIN : 
+               status = ejdbtranbegin(coll);
+               break;
+
+       case org_ejdb_driver_EJDBCollection_JBTXCOMMIT : 
+               status = ejdbtrancommit(coll);
+               break;
+
+       case org_ejdb_driver_EJDBCollection_JBTXROLLBACK : 
+               status = ejdbtranabort(coll);
+               break;
+
+       case org_ejdb_driver_EJDBCollection_JBTXSTATUS :
+               status = ejdbtranstatus(coll, &txActive);
+               break;
+
+       default:
+               set_error(env, 0, "Unexpected txControl option");
+               return JNI_FALSE;
+       }
+
+       if (!status) {
+               set_ejdb_error(env, db);
+               return JNI_FALSE;
+       }
+
+       return status ? JNI_TRUE : JNI_FALSE;
+};
index 0486665..343d6ec 100755 (executable)
@@ -23,12 +23,21 @@ extern "C" {
 #define org_ejdb_driver_EJDBCollection_JBIDXARR 64L
 #undef org_ejdb_driver_EJDBCollection_JBIDXISTR
 #define org_ejdb_driver_EJDBCollection_JBIDXISTR 128L
+#undef org_ejdb_driver_EJDBCollection_JBTXBEGIN
+#define org_ejdb_driver_EJDBCollection_JBTXBEGIN 1L
+#undef org_ejdb_driver_EJDBCollection_JBTXCOMMIT
+#define org_ejdb_driver_EJDBCollection_JBTXCOMMIT 2L
+#undef org_ejdb_driver_EJDBCollection_JBTXROLLBACK
+#define org_ejdb_driver_EJDBCollection_JBTXROLLBACK 4L
+#undef org_ejdb_driver_EJDBCollection_JBTXSTATUS
+#define org_ejdb_driver_EJDBCollection_JBTXSTATUS 8L
+
 /*
  * Class:     org_ejdb_driver_EJDBCollection
- * Method:    txControlDB
- * Signature: (I)V
+ * Method:    txControl
+ * Signature: (I)Z
  */
-JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBCollection_txControlDB
+JNIEXPORT jboolean JNICALL Java_org_ejdb_driver_EJDBCollection_txControl
   (JNIEnv *, jobject, jint);
 
 /*
index 586797f..0123f79 100644 (file)
@@ -1,10 +1,8 @@
 package org.ejdb.driver;
 
-import org.bson.BSON;
 import org.bson.BSONObject;
 import org.bson.types.ObjectId;
 
-import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -24,6 +22,11 @@ public class EJDBCollection {
     public final static int JBIDXARR = 1 << 6; /**< Array token index. */
     public final static int JBIDXISTR = 1 << 7; /**< Case insensitive string index */
 
+    protected final static int JBTXBEGIN = 1 << 0;
+    protected final static int JBTXCOMMIT = 1 << 1;
+    protected final static int JBTXROLLBACK = 1 << 2;
+    protected final static int JBTXSTATUS = 1 << 3;
+
     private EJDB db;
     private String cname;
 
@@ -33,9 +36,7 @@ public class EJDBCollection {
     }
 
     // TODO:
-    protected native void txControlDB(int mode);
-
-
+    protected native boolean txControl(int mode);
 
     public void ensureExists() {
         this.ensureExists(null);
@@ -73,6 +74,22 @@ public class EJDBCollection {
         return new EJDBQuery(this, query, qors, hints, flags);
     }
 
+    public void beginTransaction() {
+        this.txControl(JBTXBEGIN);
+    }
+
+    public void commitTransaction() {
+        this.txControl(JBTXCOMMIT);
+    }
+
+    public void rollbakcTransaction() {
+        this.txControl(JBTXROLLBACK);
+    }
+
+    public boolean isTransactionActive() {
+        return this.txControl(JBTXSTATUS);
+    }
+
     public static class Options {
         private boolean compressed;
         private boolean large;