#21 - first draft
authorVyacheslav Tyutyunkov <tyutyunkov@gmail.com>
Tue, 12 Mar 2013 17:16:46 +0000 (00:16 +0700)
committerVyacheslav Tyutyunkov <tyutyunkov@gmail.com>
Tue, 12 Mar 2013 17:16:46 +0000 (00:16 +0700)
17 files changed:
.idea/libraries/lib.xml [new file with mode: 0644]
.idea/misc.xml
.idea/modules.xml
jejdb/Makefile [new file with mode: 0644]
jejdb/jejdb.iml [new file with mode: 0644]
jejdb/lib/bson.jar [new file with mode: 0644]
jejdb/lib/mongo.jar [new file with mode: 0644]
jejdb/libjejdb.so [new file with mode: 0755]
jejdb/src/cpp/jejdb.c [new file with mode: 0644]
jejdb/src/cpp/jejdb.h [new file with mode: 0644]
jejdb/src/cpp/org_ejdb_driver_EJDB.h [new file with mode: 0644]
jejdb/src/cpp/org_ejdb_driver_EJDBCollection.h [new file with mode: 0644]
jejdb/src/cpp/org_ejdb_driver_EJDBDriver.h [new file with mode: 0644]
jejdb/src/java/org/ejdb/Test.java [new file with mode: 0644]
jejdb/src/java/org/ejdb/driver/EJDB.java [new file with mode: 0644]
jejdb/src/java/org/ejdb/driver/EJDBCollection.java [new file with mode: 0644]
jejdb/src/java/org/ejdb/driver/EJDBDriver.java [new file with mode: 0644]

diff --git a/.idea/libraries/lib.xml b/.idea/libraries/lib.xml
new file mode 100644 (file)
index 0000000..14d88d7
--- /dev/null
@@ -0,0 +1,11 @@
+<component name="libraryTable">
+  <library name="lib">
+    <CLASSES>
+      <root url="file://$PROJECT_DIR$/jejdb/lib" />
+      <root url="file://$PROJECT_DIR$/jejdb/lib" />
+    </CLASSES>
+    <JAVADOC />
+    <SOURCES />
+    <jarDirectory url="file://$PROJECT_DIR$/jejdb/lib" recursive="false" />
+  </library>
+</component>
\ No newline at end of file
index b1e5ea5..8f7b1e2 100644 (file)
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
+  <component name="CppTools.Loader" reportImplicitCastToBool="false" reportNameReferencedOnce="false" warnedAboutFileOutOfSourceRoot="true" version="3" currentProject="$PROJECT_DIR$/node/Makefile" additionalPreprocessorDefs="" compilerSelect="GCC" />
   <component name="ExportToHTMLSettings">
     <option name="printScope" value="1" />
     <option name="OPEN_IN_BROWSER" value="true" />
@@ -48,7 +49,7 @@
   <component name="ProjectResources">
     <default-html-doctype>http://www.w3.org/1999/xhtml</default-html-doctype>
   </component>
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_5" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_5" assert-keyword="true" jdk-15="true" project-jdk-name="1.6" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/out" />
   </component>
   <component name="Regex">
index 6648e1e..a24c007 100644 (file)
@@ -2,6 +2,7 @@
 <project version="4">
   <component name="ProjectModuleManager">
     <modules>
+      <module fileurl="file://$PROJECT_DIR$/jejdb/jejdb.iml" filepath="$PROJECT_DIR$/jejdb/jejdb.iml" />
       <module fileurl="file://$PROJECT_DIR$/pyejdb/pyejdb.iml" filepath="$PROJECT_DIR$/pyejdb/pyejdb.iml" />
       <module fileurl="file://$PROJECT_DIR$/tcejdb/tcejdb.iml" filepath="$PROJECT_DIR$/tcejdb/tcejdb.iml" />
     </modules>
diff --git a/jejdb/Makefile b/jejdb/Makefile
new file mode 100644 (file)
index 0000000..be03803
--- /dev/null
@@ -0,0 +1,68 @@
+#================================================================
+# Setting Variables
+#================================================================
+
+
+# Generic settings
+SHELL = /bin/bash
+
+# Package information
+PACKAGE = tcejdb
+VERSION = 1.0.65
+PACKAGEDIR = $(PACKAGE)-$(VERSION)
+PACKAGETGZ = $(PACKAGE)-$(VERSION).tar.gz
+LIBVER = 9
+LIBREV = 11
+FORMATVER = 1.0
+
+# Targets
+#HEADERFILES = tcutil.h tchdb.h tcbdb.h tcfdb.h tctdb.h tcadb.h ejdb.h ejdb_private.h bson.h myconf.h
+#LIBRARYFILES = libtcejdb.a libtcejdb.so.9.11.0 libtcejdb.so.9 libtcejdb.so
+#LIBOBJFILES = tcutil.o tchdb.o tcbdb.o tcfdb.o tctdb.o tcadb.o myconf.o md5.o ejdb.o bson.o numbers.o encoding.o utf8proc.o ejdbutl.o
+#COMMANDFILES = tcutest tcumttest tcucodec tchtest tchmttest tchmgr tcbtest tcbmttest tcbmgr tcftest tcfmttest tcfmgr tcttest tctmttest tctmgr tcatest tcamttest tcamgr
+#CGIFILES = tcawmgr.cgi
+#MAN1FILES =
+#MAN3FILES = libtcejdb.3
+#DOCUMENTFILES = COPYING Changelog
+#PCFILES = tcejdb.pc
+
+# Install destinations
+prefix = /usr/local
+exec_prefix = ${prefix}
+datarootdir = ${prefix}/share
+INCLUDEDIR = ${prefix}/include
+LIBDIR = ${exec_prefix}/lib
+BINDIR = ${exec_prefix}/bin
+LIBEXECDIR = ${exec_prefix}/libexec
+DATADIR = ${datarootdir}/$(PACKAGE)
+MAN1DIR = ${datarootdir}/man/man1
+MAN3DIR = ${datarootdir}/man/man3
+PCDIR = ${exec_prefix}/lib/pkgconfig
+
+SRCDIR = ./src/cpp/
+DESTDIR =
+
+# Building configuration
+CC = gcc
+CPPFLAGS = -I. -I../tcejdb -I/usr/lib/jvm/java-6-oracle/include -I/usr/lib/jvm/java-6-oracle/include/linux
+CFLAGS =  -std=c99 -Wall -fPIC -fsigned-char -O2
+LDFLAGS = -L. -L../tcejdb
+CMDLDFLAGS =
+LIBS = -lz -lrt -lpthread -lm -lc -shared -ltcejdb
+# LDENV = LD_RUN_PATH=/lib:/usr/lib:$(LIBDIR):$(HOME)/lib:/usr/local/lib:$(LIBDIR):.
+RUNENV = LD_LIBRARY_PATH=.
+POSTCMD = true
+
+
+all: jejdb
+
+tcejdb:
+       $(MAKE) -C ../ all
+
+jejdb:
+       $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $(DESTDIR)libjejdb.so $(SRCDIR)jejdb.c  $(LIBS)
+
+clean:
+       - $(MAKE) -C . clean
+
+.PHONY: all tcejdb jni-jejdb clean
diff --git a/jejdb/jejdb.iml b/jejdb/jejdb.iml
new file mode 100644 (file)
index 0000000..75741fb
--- /dev/null
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src/java" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="library" name="lib" level="project" />
+  </component>
+</module>
+
diff --git a/jejdb/lib/bson.jar b/jejdb/lib/bson.jar
new file mode 100644 (file)
index 0000000..e735a4b
Binary files /dev/null and b/jejdb/lib/bson.jar differ
diff --git a/jejdb/lib/mongo.jar b/jejdb/lib/mongo.jar
new file mode 100644 (file)
index 0000000..717d0a5
Binary files /dev/null and b/jejdb/lib/mongo.jar differ
diff --git a/jejdb/libjejdb.so b/jejdb/libjejdb.so
new file mode 100755 (executable)
index 0000000..63619fb
Binary files /dev/null and b/jejdb/libjejdb.so differ
diff --git a/jejdb/src/cpp/jejdb.c b/jejdb/src/cpp/jejdb.c
new file mode 100644 (file)
index 0000000..403c5f7
--- /dev/null
@@ -0,0 +1,185 @@
+#include <stdio.h>
+#include "jejdb.h"
+
+/*
+ * Class:     org_ejdb_driver_EJDB
+ * Method:    openDB
+ * Signature: (Ljava/lang/String;I)J
+ */
+JNIEXPORT jlong JNICALL Java_org_ejdb_driver_EJDB_openDB
+  (JNIEnv *env, jobject ojb, jstring path, jint mode) {
+    EJDB* db = ejdbnew();
+
+    const char *dbpath;
+    dbpath = (*env)->GetStringUTFChars(env, path, NULL);
+    ejdbopen(db, dbpath, mode);
+    (*env)->ReleaseStringUTFChars(env, path, dbpath);
+
+    return (jlong)db;
+};
+
+/*
+ * Class:     org_ejdb_driver_EJDB
+ * Method:    isOpenDB
+ * Signature: (J)Z
+ */
+JNIEXPORT jboolean JNICALL Java_org_ejdb_driver_EJDB_isOpenDB
+  (JNIEnv *env, jobject obj, jlong dbp) {
+    EJDB* db = (EJDB*)dbp;
+
+    return ejdbisopen(db);
+};
+
+/*
+ * Class:     org_ejdb_driver_EJDB
+ * Method:    closeDB
+ * Signature: (J)V
+ */
+JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDB_closeDB
+  (JNIEnv *env, jobject obj, jlong dbp) {
+    EJDB* db = (EJDB*)dbp;
+
+    ejdbclose(db);
+    ejdbdel(db);
+};
+
+/*
+ * Class:     org_ejdb_driver_EJDBCollection
+ * Method:    ensureCollectionDB
+ * Signature: (JLjava/lang/String;Ljava/lang/Object;)Z
+ */
+JNIEXPORT jboolean JNICALL Java_org_ejdb_driver_EJDBCollection_ensureCollectionDB
+  (JNIEnv *env, jobject obj, jlong dbp, jstring colname, jobject opts) {
+    EJDB* db = (EJDB*)dbp;
+
+    EJCOLLOPTS jcopts;
+    memset(&jcopts, 0, sizeof (jcopts));
+
+    const char *cname;
+    cname = (*env)->GetStringUTFChars(env, colname, NULL);
+
+    EJCOLL *coll = ejdbcreatecoll(db, cname, &jcopts);
+
+    (*env)->ReleaseStringUTFChars(env, colname, cname);
+
+    return !coll ? JNI_FALSE : JNI_TRUE;
+};
+
+
+/*
+ * Class:     org_ejdb_driver_EJDBCollection
+ * Method:    dropCollectionDB
+ * Signature: (JLjava/lang/String;Z)Z
+ */
+JNIEXPORT jboolean JNICALL Java_org_ejdb_driver_EJDBCollection_dropCollectionDB
+  (JNIEnv *env, jobject obj, jlong dbp, jstring colname, jboolean prune) {
+    EJDB* db = (EJDB*)dbp;
+
+    const char *cname;
+    cname = (*env)->GetStringUTFChars(env, colname, NULL);
+
+    bool status = ejdbrmcoll(db, cname, (prune == JNI_TRUE));
+
+    (*env)->ReleaseStringUTFChars(env, colname, cname);
+
+    return status ? JNI_TRUE : JNI_FALSE;
+};
+
+/*
+ * Class:     org_ejdb_driver_EJDBCollection
+ * Method:    loadDB
+ * Signature: (JLjava/lang/String;[B)Ljava/lang/Object;
+ */
+JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_loadDB
+  (JNIEnv *env, jobject obj, jlong dbp, jstring colname, jbyteArray oidArray) {
+    EJDB* db = (EJDB*)dbp;
+
+    const char *cname = (*env)->GetStringUTFChars(env, colname, NULL);
+    bson_oid_t *oid = (jbyte*)(*env)->GetByteArrayElements(env, oidArray, NULL);
+
+    // todo: check
+    EJCOLL * coll = ejdbgetcoll(db, cname);
+
+    bson* bson = ejdbloadbson(coll, oid);
+
+    (*env)->ReleaseStringUTFChars(env, colname, cname);
+    (*env)->ReleaseByteArrayElements(env, oidArray, (jbyte*)oid, 0);
+
+    if (!bson) {
+        return NULL;
+//        result = (*env)->NewByteArray(env, 0);
+    }
+        jclass clazz = (*env)->GetObjectClass(env, obj);
+        jmethodID method = (*env)->GetStaticMethodID(env, clazz, "handleBSONData", "(Ljava/nio/ByteBuffer;)Ljava/lang/Object;");
+        // todo: checks
+
+//        printf("%d", bson_size(bson));
+
+//        jbyteArray buff = (*env)->NewByteArray(env, bson_size(bson));
+//        memcpy(buff, bson_data(bson), bson_size(bson));
+//        jbyte* tbuff;
+//        (*env)>GetByteArrayRegion(env, buff, 0, bson_size(bson), tbuff);
+//        (*env)>SetByteArrayRegion(env, buff, (jsize)0, (jsize)bson_size(bson), (jbyte*)bson_data(bson));
+//
+//        printf("0x%x", bson_data(bson));
+
+//        void *bb = malloc(bson_size(bson));
+//        memcpy(bb, bson_data(bson), bson_size(bson));
+//
+        jobject buff = (*env)->NewDirectByteBuffer(env, (void*)bson_data(bson), bson_size(bson));
+        jobject result = (*env)->CallStaticObjectMethod(env, clazz, method, buff);
+
+//        free (bb);
+        (*env)->DeleteLocalRef(env, buff);
+//        (*env)->DeleteLocalRef(env, method);
+//        (*env)->DeleteLocalRef(env, clazz);
+        bson_del(bson);
+
+    return result;
+}
+
+/*
+ * Class:     org_ejdb_driver_EJDBCollection
+ * Method:    saveDB
+ * Signature: (JLjava/lang/String;[B)Ljava/lang/Object;
+ */
+JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_saveDB
+  (JNIEnv *env, jobject obj, jlong dbp, jstring colname, jbyteArray objdata) {
+    EJDB* db = (EJDB*)dbp;
+
+    const char *cname = (*env)->GetStringUTFChars(env, colname, NULL);
+    jbyte *bdata = (*env)->GetByteArrayElements(env, objdata, NULL);
+    jsize blength = (*env)->GetArrayLength(env, objdata);
+
+    bson_oid_t oid;
+
+    // todo: check
+    EJCOLL * coll = ejdbgetcoll(db, cname);
+
+    bson *bson = bson_create_from_buffer(bdata, blength);
+    bool ss = ejdbsavebson(coll, bson, &oid);
+    bson_del(bson);
+
+    (*env)->ReleaseStringUTFChars(env, colname, cname);
+    (*env)->ReleaseByteArrayElements(env, objdata, bdata, 0);
+
+
+    if (!ss) {
+        // todo: error
+        return NULL;
+    }
+
+    jclass clazz = (*env)->GetObjectClass(env, obj);
+    jmethodID method = (*env)->GetStaticMethodID(env, clazz, "handleObjectIdData", "(Ljava/nio/ByteBuffer;)Ljava/lang/Object;");
+
+        jobject buff = (*env)->NewDirectByteBuffer(env, (void*)&oid, sizeof(oid));
+        jobject result = (*env)->CallStaticObjectMethod(env, clazz, method, buff);
+
+//        free (bb);
+        (*env)->DeleteLocalRef(env, buff);
+//        (*env)->DeleteLocalRef(env, method);
+//        (*env)->DeleteLocalRef(env, clazz);
+
+    return result;
+
+}
diff --git a/jejdb/src/cpp/jejdb.h b/jejdb/src/cpp/jejdb.h
new file mode 100644 (file)
index 0000000..a833b91
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef JEJDB_H
+#define JEJDB_H
+
+#include <ejdb_private.h>
+
+#include "org_ejdb_driver_EJDBDriver.h"
+#include "org_ejdb_driver_EJDB.h"
+#include "org_ejdb_driver_EJDBCollection.h"
+
+#endif
\ No newline at end of file
diff --git a/jejdb/src/cpp/org_ejdb_driver_EJDB.h b/jejdb/src/cpp/org_ejdb_driver_EJDB.h
new file mode 100644 (file)
index 0000000..1ec2302
--- /dev/null
@@ -0,0 +1,53 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+#include <jni.h>
+/* Header for class org_ejdb_driver_EJDB */
+
+#ifndef _Included_org_ejdb_driver_EJDB
+#define _Included_org_ejdb_driver_EJDB
+#ifdef __cplusplus
+extern "C" {
+#endif
+#undef org_ejdb_driver_EJDB_JBOREADER
+#define org_ejdb_driver_EJDB_JBOREADER 1L
+#undef org_ejdb_driver_EJDB_JBOWRITER
+#define org_ejdb_driver_EJDB_JBOWRITER 2L
+#undef org_ejdb_driver_EJDB_JBOCREAT
+#define org_ejdb_driver_EJDB_JBOCREAT 4L
+#undef org_ejdb_driver_EJDB_JBOTRUNC
+#define org_ejdb_driver_EJDB_JBOTRUNC 8L
+#undef org_ejdb_driver_EJDB_JBONOLCK
+#define org_ejdb_driver_EJDB_JBONOLCK 16L
+#undef org_ejdb_driver_EJDB_JBOLCKNB
+#define org_ejdb_driver_EJDB_JBOLCKNB 32L
+#undef org_ejdb_driver_EJDB_JBOTSYNC
+#define org_ejdb_driver_EJDB_JBOTSYNC 64L
+#undef org_ejdb_driver_EJDB_JBO_DEFAULT
+#define org_ejdb_driver_EJDB_JBO_DEFAULT 70L
+/*
+ * Class:     org_ejdb_driver_EJDB
+ * Method:    openDB
+ * Signature: (Ljava/lang/String;I)J
+ */
+JNIEXPORT jlong JNICALL Java_org_ejdb_driver_EJDB_openDB
+  (JNIEnv *, jobject, jstring, jint);
+
+/*
+ * Class:     org_ejdb_driver_EJDB
+ * Method:    isOpenDB
+ * Signature: (J)Z
+ */
+JNIEXPORT jboolean JNICALL Java_org_ejdb_driver_EJDB_isOpenDB
+  (JNIEnv *, jobject, jlong);
+
+/*
+ * Class:     org_ejdb_driver_EJDB
+ * Method:    closeDB
+ * Signature: (J)V
+ */
+JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDB_closeDB
+  (JNIEnv *, jobject, jlong);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/jejdb/src/cpp/org_ejdb_driver_EJDBCollection.h b/jejdb/src/cpp/org_ejdb_driver_EJDBCollection.h
new file mode 100644 (file)
index 0000000..65b1561
--- /dev/null
@@ -0,0 +1,45 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+#include <jni.h>
+/* Header for class org_ejdb_driver_EJDBCollection */
+
+#ifndef _Included_org_ejdb_driver_EJDBCollection
+#define _Included_org_ejdb_driver_EJDBCollection
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class:     org_ejdb_driver_EJDBCollection
+ * Method:    ensureCollectionDB
+ * Signature: (JLjava/lang/String;Ljava/lang/Object;)Z
+ */
+JNIEXPORT jboolean JNICALL Java_org_ejdb_driver_EJDBCollection_ensureCollectionDB
+  (JNIEnv *, jobject, jlong, jstring, jobject);
+
+/*
+ * Class:     org_ejdb_driver_EJDBCollection
+ * Method:    dropCollectionDB
+ * Signature: (JLjava/lang/String;Z)Z
+ */
+JNIEXPORT jboolean JNICALL Java_org_ejdb_driver_EJDBCollection_dropCollectionDB
+  (JNIEnv *, jobject, jlong, jstring, jboolean);
+
+/*
+ * Class:     org_ejdb_driver_EJDBCollection
+ * Method:    loadDB
+ * Signature: (JLjava/lang/String;[B)Ljava/lang/Object;
+ */
+JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_loadDB
+  (JNIEnv *, jobject, jlong, jstring, jbyteArray);
+
+/*
+ * Class:     org_ejdb_driver_EJDBCollection
+ * Method:    saveDB
+ * Signature: (JLjava/lang/String;[B)Ljava/lang/Object;
+ */
+JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_saveDB
+  (JNIEnv *, jobject, jlong, jstring, jbyteArray);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/jejdb/src/cpp/org_ejdb_driver_EJDBDriver.h b/jejdb/src/cpp/org_ejdb_driver_EJDBDriver.h
new file mode 100644 (file)
index 0000000..2d83634
--- /dev/null
@@ -0,0 +1,13 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+#include <jni.h>
+/* Header for class org_ejdb_driver_EJDBDriver */
+
+#ifndef _Included_org_ejdb_driver_EJDBDriver
+#define _Included_org_ejdb_driver_EJDBDriver
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/jejdb/src/java/org/ejdb/Test.java b/jejdb/src/java/org/ejdb/Test.java
new file mode 100644 (file)
index 0000000..277cad0
--- /dev/null
@@ -0,0 +1,82 @@
+package org.ejdb;
+
+import org.bson.BSONObject;
+import org.bson.BasicBSONObject;
+import org.bson.types.ObjectId;
+import org.ejdb.driver.EJDB;
+import org.ejdb.driver.EJDBCollection;
+import org.ejdb.driver.EJDBDriver;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Random;
+
+/**
+ * @author Tyutyunkov Vyacheslav (tve@softmotions.com)
+ * @version $Id$
+ */
+public class Test {
+    public static void main(String[] args) throws InterruptedException, IOException {
+        EJDB db = new EJDB();
+
+        try {
+            db.open("test");
+
+            System.out.println("DB opened");
+//            System.in.read();
+
+            db.ensureCollection("test");
+
+            System.out.println("collection 'test' created");
+//            System.in.read();
+
+//            db.ensureCollection("test2");
+//
+//            System.out.println("collection 'test2' created");
+////            System.in.read();
+//
+//            db.dropCollection("test");
+//
+//            System.out.println("collection 'test' dropped");
+////            System.in.read();
+//
+//            db.dropCollection("test2", true);
+//
+//            System.out.println("collection 'test2' dropped");
+//            System.in.read();
+
+
+            EJDBCollection coll = db.getCollection("test");
+            BSONObject bson = coll.load(new ObjectId("513f04d563f08b6400000000"));
+            if (bson != null) {
+                System.out.println(bson.toString());
+            }
+
+            Random rand = new Random();
+
+            int ri;
+
+            for (int i = 0; i < 5; ++ i) {
+                ri = rand.nextInt();
+                System.out.println("Random: " + ri);
+
+                ObjectId oid = coll.save(new BasicBSONObject("test", ri));
+                if (oid == null) {
+                    System.out.println("Error saving");
+                } else {
+                    bson = coll.load(oid);
+                    if (bson == null) {
+                        System.out.println("Error loading");
+                    } else {
+                        System.out.println(bson.toString());
+                    }
+                }
+            }
+
+        } finally {
+            db.close();
+            System.out.println("DB closed");
+        }
+    }
+}
+
diff --git a/jejdb/src/java/org/ejdb/driver/EJDB.java b/jejdb/src/java/org/ejdb/driver/EJDB.java
new file mode 100644 (file)
index 0000000..c540f79
--- /dev/null
@@ -0,0 +1,109 @@
+package org.ejdb.driver;
+
+/**
+ * @author Tyutyunkov Vyacheslav (tve@softmotions.com)
+ * @version $Id$
+ */
+public class EJDB {
+    // Open modes
+    public static final int JBOREADER = 1 << 0; /**< Open as a reader. */
+    public static final int JBOWRITER = 1 << 1; /**< Open as a writer. */
+    public static final int JBOCREAT = 1 << 2; /**< Create if db file not exists. */
+    public static final int JBOTRUNC = 1 << 3; /**< Truncate db on open. */
+    public static final int JBONOLCK = 1 << 4; /**< Open without locking. */
+    public static final int JBOLCKNB = 1 << 5; /**< Lock without blocking. */
+    public static final int JBOTSYNC = 1 << 6; /**< Synchronize every transaction. */
+
+    public static final int JBO_DEFAULT = (JBOWRITER | JBOCREAT | JBOTSYNC);
+
+    static {
+        System.loadLibrary("jejdb");
+    }
+
+    // todo: rewrite impls - use `this.db` from c code
+    protected native long openDB(String path, int mode);
+    protected native boolean isOpenDB(long dbp);
+    protected native void closeDB(long dbp);
+
+    private Long dbp;
+
+
+    // TODO: move to driver class
+    public void open(String path) {
+        this.open(path, JBO_DEFAULT);
+    }
+
+    public void open(String path, int mode) {
+        dbp = this.openDB(path, mode);
+    }
+
+    public boolean isOpen() {
+        return dbp != null && this.isOpenDB(dbp);
+    }
+
+    public void close() {
+        if (dbp != null) {
+            this.closeDB(dbp);
+        }
+        dbp = null;
+    }
+
+
+    public void ensureCollection(String cname) {
+        this.ensureCollection(cname, null);
+    }
+
+    public EJDBCollection ensureCollection(String cname, Object opts) {
+        if (dbp == null) {
+            // todo
+            throw new RuntimeException("Connection does not exists");
+        }
+
+        EJDBCollection collection = getCollection(cname);
+        collection.ensureExists(opts);
+
+        return collection;
+    }
+
+    public boolean dropCollection(String cname) {
+        return this.dropCollection(cname, false);
+    }
+
+    public boolean dropCollection(String cname, boolean prune) {
+        if (dbp == null) {
+            // todo
+            throw new RuntimeException("Connection does not exists");
+        }
+
+        EJDBCollection collection = getCollection(cname);
+
+        return collection.drop(prune);
+    }
+
+    public EJDBCollection getCollection(String cname) {
+        return this.getCollection(cname, false);
+    }
+
+    public EJDBCollection getCollection(String cname, boolean ecreate) {
+        return this.getCollection(cname, ecreate, null);
+    }
+
+    public EJDBCollection getCollection(String cname, boolean ecreate, Object opts) {
+        EJDBCollection collection = new EJDBCollection(dbp, cname);
+
+        if (ecreate) {
+            collection.ensureExists(opts);
+        }
+
+        return collection;
+    }
+
+
+    //////////////////////////////////////////
+    @Override
+    protected void finalize() throws Throwable {
+        this.close();
+        super.finalize();
+    }
+
+}
diff --git a/jejdb/src/java/org/ejdb/driver/EJDBCollection.java b/jejdb/src/java/org/ejdb/driver/EJDBCollection.java
new file mode 100644 (file)
index 0000000..4b5e64c
--- /dev/null
@@ -0,0 +1,83 @@
+package org.ejdb.driver;
+
+import org.bson.BSON;
+import org.bson.BSONObject;
+import org.bson.types.ObjectId;
+
+import java.awt.image.DataBufferByte;
+import java.nio.ByteBuffer;
+import java.nio.MappedByteBuffer;
+import java.util.Arrays;
+
+/**
+ * @author Tyutyunkov Vyacheslav (tve@softmotions.com)
+ * @version $Id$
+ */
+public class EJDBCollection {
+
+    private long dbp;
+    private String name;
+
+    EJDBCollection(long dbp, String name) {
+        this.dbp = dbp;
+        this.name = name;
+    }
+
+    // todo: bson object for options
+    protected native boolean ensureCollectionDB(long dbp, String cname, Object opts);
+
+    protected native boolean dropCollectionDB(long dbp, String cname, boolean prune);
+
+    protected native Object loadDB(long dbp, String cname, byte[] oid);
+    protected native Object saveDB(long dbp, String cname, byte[] objdata);
+
+    public boolean ensureExists() {
+        return this.ensureExists(null);
+    }
+
+    public boolean ensureExists(Object opts) {
+        return this.ensureCollectionDB(dbp, name, opts);
+    }
+
+    public boolean drop() {
+        return this.drop(false);
+    }
+
+    public boolean drop(boolean prune) {
+        return this.dropCollectionDB(dbp, name, prune);
+    }
+
+
+    public BSONObject load(ObjectId oid) {
+        return (BSONObject) this.loadDB(dbp, name, oid.toByteArray());
+//        byte[] data = this.loadDB(dbp, name, oid.toByteArray());
+//        if (data.length > 0) {
+//            return BSON.decode(data);
+//        } else {
+//            return null;
+//        }
+    }
+
+    public ObjectId save(BSONObject object) {
+        return (ObjectId) this.saveDB(dbp, name, BSON.encode(object));
+    }
+
+    private static Object handleBSONData(ByteBuffer data) {
+        byte[] tmp = new byte[data.limit()];
+        data.get(tmp);
+
+//        System.out.println(data.getClass().getName());
+//        System.out.println(((MappedByteBuffer)data).load().array());
+        //        System.out.println(Arrays.toString(data.array()));
+        return BSON.decode(tmp);
+
+//        return null;
+    }
+
+    private static Object handleObjectIdData(ByteBuffer data) {
+        byte[] tmp = new byte[data.limit()];
+        data.get(tmp);
+
+        return new ObjectId(tmp);
+    }
+}
diff --git a/jejdb/src/java/org/ejdb/driver/EJDBDriver.java b/jejdb/src/java/org/ejdb/driver/EJDBDriver.java
new file mode 100644 (file)
index 0000000..7c8bcac
--- /dev/null
@@ -0,0 +1,50 @@
+package org.ejdb.driver;
+
+/**
+ * @author Tyutyunkov Vyacheslav (tve@softmotions.com)
+ * @version $Id$
+ */
+public class EJDBDriver {
+//
+//    // Open modes
+//    public static final int JBOREADER = 1 << 0; /**< Open as a reader. */
+//    public static final int JBOWRITER = 1 << 1; /**< Open as a writer. */
+//    public static final int JBOCREAT = 1 << 2; /**< Create if db file not exists. */
+//    public static final int JBOTRUNC = 1 << 3; /**< Truncate db on open. */
+//    public static final int JBONOLCK = 1 << 4; /**< Open without locking. */
+//    public static final int JBOLCKNB = 1 << 5; /**< Lock without blocking. */
+//    public static final int JBOTSYNC = 1 << 6; /**< Synchronize every transaction. */
+//
+//    public static final int JBO_DEFAULT = (JBOWRITER | JBOCREAT | JBOTSYNC);
+//
+//
+//    static {
+//        System.loadLibrary("jejdb");
+//    }
+//
+//    private native long openDB(String path, int mode);
+//    private native void closeDB(long dbp);
+//
+//    private Long dbp;
+//
+//    public void open(String path) {
+//        this.open(path, JBO_DEFAULT);
+//    }
+//
+//    public void open(String path, int mode) {
+//        dbp = this.openDB(path, mode);
+//    }
+//
+//    public void close() {
+//        if (dbp != null) {
+//            this.closeDB(dbp);
+//        }
+//        dbp = null;
+//    }
+//
+//    @Override
+//    protected void finalize() throws Throwable {
+//        this.close();
+//        super.finalize();
+//    }
+}