return NULL;
};
+static void fill_ejdb_collopts(JNIEnv *env, jobject obj, EJCOLLOPTS *ejcopts) {
+ memset(ejcopts, 0, sizeof (*ejcopts));
+
+ if (NULL == obj) {
+ return;
+ }
+
+ jclass clazz = (*env)->GetObjectClass(env, obj);
+
+ jfieldID recordsFID = (*env)->GetFieldID(env, clazz, "records", "J");
+ jfieldID cachedrecordsFID = (*env)->GetFieldID(env, clazz, "cachedrecords", "I");
+ jfieldID largeFID = (*env)->GetFieldID(env, clazz, "large", "Z");
+ jfieldID compressedFID = (*env)->GetFieldID(env, clazz, "compressed", "Z");
+
+ jlong records = (*env)->GetLongField(env, obj, recordsFID);
+ jint cachedrecords = (*env)->GetIntField(env, obj, cachedrecordsFID);
+ jboolean large = (*env)->GetBooleanField(env, obj, largeFID);
+ jboolean compressed = (*env)->GetBooleanField(env, obj, compressedFID);
+
+ ejcopts->records = records > 0 ? records : 0;
+ ejcopts->cachedrecords = cachedrecords > 0 ? cachedrecords : 0;
+ ejcopts->large = (JNI_TRUE == large);
+ ejcopts->compressed = (JNI_TRUE == compressed);
+};
+
static bson *encode_bson(JNIEnv *env, jobject jbson, bson *out) {
jclass jBSONClazz = (*env)->FindClass(env, "org/bson/BSON");
jmethodID encodeMethodID = (*env)->GetStaticMethodID(env, jBSONClazz, "encode", "(Lorg/bson/BSONObject;)[B");
/*
* Class: org_ejdb_driver_EJDBCollection
* Method: ensureDB
-* Signature: (Ljava/lang/Object;)V
+* Signature: (Lorg/ejdb/driver/EJDBCollection$Options;)V
*/
JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBCollection_ensureDB (JNIEnv *env, jobject obj, jobject opts) {
EJDB* db = get_ejdb_from_object(env, obj);
jstring colname = get_coll_name(env, obj);
- EJCOLLOPTS jcopts;
- memset(&jcopts, 0, sizeof (jcopts));
- // todo: open options
+ EJCOLLOPTS ejcopts;
+ fill_ejdb_collopts(env, opts, &ejcopts);
const char *cname = (*env)->GetStringUTFChars(env, colname, NULL);
- EJCOLL *coll = ejdbcreatecoll(db, cname, &jcopts);
+ EJCOLL *coll = ejdbcreatecoll(db, cname, &ejcopts);
(*env)->ReleaseStringUTFChars(env, colname, cname);
if (!coll) {
};
/*
-* Class: org_ejdb_driver_EJDBCollection
-* Method: loadDB
-* Signature: ([B)Ljava/lang/Object;
-*/
-JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_loadDB (JNIEnv *env, jobject obj, jbyteArray oidArray) {
+ * Class: org_ejdb_driver_EJDBCollection
+ * Method: loadDB
+ * Signature: (Lorg/bson/types/ObjectId;)Lorg/bson/BSONObject;
+ */
+JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_loadDB (JNIEnv *env, jobject obj, jobject joid) {
EJDB* db = get_ejdb_from_object(env, obj);
if (!ejdbisopen(db)) {
set_error(env, 0, "EJDB not opened");
return NULL;
}
- bson_oid_t *oid = (void*)(*env)->GetByteArrayElements(env, oidArray, NULL);
+ jclass jObjectIdClazz = (*env)->FindClass(env, "org/bson/types/ObjectId");
+ jmethodID encodeMethodID = (*env)->GetMethodID(env, jObjectIdClazz, "toByteArray", "()[B");
+ jbyteArray joiddata = (*env)->CallObjectMethod(env, joid, encodeMethodID);
+ bson_oid_t *oid = (bson_oid_t*)(*env)->GetByteArrayElements(env, joiddata, NULL);
bson* bson = ejdbloadbson(coll, oid);
- (*env)->ReleaseByteArrayElements(env, oidArray, (jbyte*)oid, 0);
+ (*env)->ReleaseByteArrayElements(env, joiddata, (jbyte*)oid, 0);
+ (*env)->DeleteLocalRef(env, joiddata);
if (!bson) {
return NULL;
}
/*
-* Class: org_ejdb_driver_EJDBCollection
-* Method: saveDB
-* Signature: ([B)Ljava/lang/Object;
-*/
-JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_saveDB (JNIEnv *env, jobject obj, jbyteArray objdata) {
+ * Class: org_ejdb_driver_EJDBCollection
+ * Method: saveDB
+ * Signature: (Lorg/bson/BSONObject;)Lorg/bson/types/ObjectId;
+ */
+JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_saveDB (JNIEnv *env, jobject obj, jobject jdata) {
EJDB* db = get_ejdb_from_object(env, obj);
if (!ejdbisopen(db)) {
set_error(env, 0, "EJDB not opened");
bson_oid_t oid;
- jbyte *bdata = (*env)->GetByteArrayElements(env, objdata, NULL);
- jsize blength = (*env)->GetArrayLength(env, objdata);
- bson *bson = bson_create_from_buffer(bdata, blength);
+ bson *bson = encode_bson(env, jdata, NULL);
bool status = ejdbsavebson(coll, bson, &oid);
bson_del(bson);
- (*env)->ReleaseByteArrayElements(env, objdata, bdata, 0);
if (!status) {
set_ejdb_error(env, db);
}
/*
-* Class: org_ejdb_driver_EJDBCollection
-* Method: removeDB
-* Signature: ([B)V
-*/
-JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBCollection_removeDB (JNIEnv *env, jobject obj, jbyteArray oidArray) {
+ * Class: org_ejdb_driver_EJDBCollection
+ * Method: removeDB
+ * Signature: (Lorg/bson/types/ObjectId;)V
+ */
+JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBCollection_removeDB (JNIEnv *env, jobject obj, jobject joid) {
EJDB* db = get_ejdb_from_object(env, obj);
if (!ejdbisopen(db)) {
set_error(env, 0, "EJDB not opened");
return;
}
- // TODO:
- bson_oid_t *oid = (void*)(*env)->GetByteArrayElements(env, oidArray, NULL);
+ jclass jObjectIdClazz = (*env)->FindClass(env, "org/bson/types/ObjectId");
+ jmethodID encodeMethodID = (*env)->GetMethodID(env, jObjectIdClazz, "toByteArray", "()[B");
+ jbyteArray joiddata = (*env)->CallObjectMethod(env, joid, encodeMethodID);
+ bson_oid_t *oid = (bson_oid_t*)(*env)->GetByteArrayElements(env, joiddata, NULL);
bool status = ejdbrmbson(coll, oid);
- (*env)->ReleaseByteArrayElements(env, oidArray, (jbyte*)oid, 0);
+ (*env)->ReleaseByteArrayElements(env, joiddata, (jbyte*)oid, 0);
+ (*env)->DeleteLocalRef(env, joiddata);
if (!status) {
set_ejdb_error(env, db);
/*
* Class: org_ejdb_driver_EJDBCollection
* Method: ensureDB
- * Signature: (Ljava/lang/Object;)V
+ * Signature: (Lorg/ejdb/driver/EJDBCollection/Options;)V
*/
JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBCollection_ensureDB
(JNIEnv *, jobject, jobject);
/*
* Class: org_ejdb_driver_EJDBCollection
* Method: loadDB
- * Signature: ([B)Ljava/lang/Object;
+ * Signature: (Lorg/bson/types/ObjectId;)Lorg/bson/BSONObject;
*/
JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_loadDB
- (JNIEnv *, jobject, jbyteArray);
+ (JNIEnv *, jobject, jobject);
/*
* Class: org_ejdb_driver_EJDBCollection
* Method: saveDB
- * Signature: ([B)Ljava/lang/Object;
+ * Signature: (Lorg/bson/BSONObject;)Lorg/bson/types/ObjectId;
*/
JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_saveDB
- (JNIEnv *, jobject, jbyteArray);
+ (JNIEnv *, jobject, jobject);
/*
* Class: org_ejdb_driver_EJDBCollection
* Method: removeDB
- * Signature: ([B)V
+ * Signature: (Lorg/bson/types/ObjectId;)V
*/
JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBCollection_removeDB
- (JNIEnv *, jobject, jbyteArray);
+ (JNIEnv *, jobject, jobject);
/*
* Class: org_ejdb_driver_EJDBCollection
JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBCollection_setIndexDB
(JNIEnv *, jobject, jstring, jint);
+/*
+ * Class: org_ejdb_driver_EJDBCollection
+ * Method: txControlDB
+ * Signature: (I)V
+ */
+JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBCollection_txControlDB
+ (JNIEnv *, jobject, jint);
+
#ifdef __cplusplus
}
#endif
public final static int JBIDXARR = 1 << 6; /**< Array token index. */
public final static int JBIDXISTR = 1 << 7; /**< Case insensitive string index */
- // todo: get dbPointer from db object
private EJDB db;
private String cname;
this.cname = cname;
}
- // todo: bson object for options
- protected native void ensureDB(Object opts);
+ protected native void ensureDB(Options opts);
protected native void dropDB(boolean prune);
protected native void syncDB();
- protected native Object loadDB(byte[] oid);
- protected native Object saveDB(byte[] objdata);
- protected native void removeDB(byte[] objdata);
+ protected native BSONObject loadDB(ObjectId oid);
+ protected native ObjectId saveDB(BSONObject obj);
+ protected native void removeDB(ObjectId oid);
protected native void setIndexDB(String path, int flags);
protected native void txControlDB(int mode);
this.ensureExists(null);
}
- public void ensureExists(Object opts) {
+ public void ensureExists(Options opts) {
this.ensureDB(opts);
}
}
public BSONObject load(ObjectId oid) {
- return (BSONObject) this.loadDB(oid.toByteArray());
+ return this.loadDB(oid);
}
public ObjectId save(BSONObject object) {
- return (ObjectId) this.saveDB(BSON.encode(object));
+ return this.saveDB(object);
}
public List<ObjectId> save(List<BSONObject> objects) {
}
public void remove(ObjectId oid) {
- this.removeDB(oid.toByteArray());
+ this.removeDB(oid);
}
public void setIndex(String path, int flags) {
public EJDBQuery createQuery(BSONObject query, BSONObject[] qors, BSONObject hints, int flags) {
return new EJDBQuery(this, query, qors, hints, flags);
}
+
+ public static class Options {
+ private boolean compressed;
+ private boolean large;
+ private long records;
+ private int cachedrecords;
+
+ public Options() {
+ }
+
+ public Options(boolean compressed, boolean large, long records, int cachedrecords) {
+ this.compressed = compressed;
+ this.large = large;
+ this.records = records;
+ this.cachedrecords = cachedrecords;
+ }
+
+ public boolean isCompressed() {
+ return compressed;
+ }
+
+ public void setCompressed(boolean compressed) {
+ this.compressed = compressed;
+ }
+
+ public boolean isLarge() {
+ return large;
+ }
+
+ public void setLarge(boolean large) {
+ this.large = large;
+ }
+
+ public long getRecords() {
+ return records;
+ }
+
+ public void setRecords(long records) {
+ this.records = records;
+ }
+
+ public int getCachedrecords() {
+ return cachedrecords;
+ }
+
+ public void setCachedrecords(int cachedrecords) {
+ this.cachedrecords = cachedrecords;
+ }
+ }
}