summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVyacheslav Tyutyunkov <tyutyunkov@gmail.com>2013-04-05 15:10:05 +0700
committerVyacheslav Tyutyunkov <tyutyunkov@gmail.com>2013-04-05 15:10:05 +0700
commitd6ee76fc96172c77ba971e56c7dad2a85746bd81 (patch)
tree8f9f8d7939aea5659a755e3401ed80beff12e941
parenta05cf846c4cc8e4c466f00525dc3528b48a1548c (diff)
downloadejdb-d6ee76fc96172c77ba971e56c7dad2a85746bd81.tar.gz
ejdb-d6ee76fc96172c77ba971e56c7dad2a85746bd81.tar.bz2
ejdb-d6ee76fc96172c77ba971e56c7dad2a85746bd81.zip
Java binding - results caching
-rw-r--r--jejdb/build.xml6
-rw-r--r--jejdb/src/cpp/jejdb.c10
-rw-r--r--jejdb/src/cpp/org_ejdb_driver_EJDBCollection.h6
-rw-r--r--jejdb/src/cpp/org_ejdb_driver_EJDBQuery.h2
-rw-r--r--jejdb/src/cpp/org_ejdb_driver_EJDBResultSet.h6
-rw-r--r--jejdb/src/java/org/ejdb/driver/EJDBResultSet.java50
6 files changed, 61 insertions, 19 deletions
diff --git a/jejdb/build.xml b/jejdb/build.xml
index 8c1d9fa..92e8c70 100644
--- a/jejdb/build.xml
+++ b/jejdb/build.xml
@@ -73,8 +73,8 @@
</junit>
</target>
- <target name="compile.headers" depends="compile.production" description="Build JNI headers for JEJDB" if="build.native.headers">
- <javah destdir="${source.native.dir}">
+ <target name="compile.headers" depends="compile.production" if="build.native.headers">
+ <javah destdir="${source.native.dir}" verbose="true">
<class name="org.ejdb.driver.EJDB"/>
<class name="org.ejdb.driver.EJDBCollection"/>
<class name="org.ejdb.driver.EJDBQuery"/>
@@ -102,7 +102,7 @@
<delete dir="${distr.home}"/>
</target>
- <target name="build.native.headers" depends="clean">
+ <target name="build.native.headers" depends="clean" description="Build JNI headers for JEJDB">
<property name="build.native.headers" value="true"/>
<ant target="compile" inheritall="true" inheritrefs="true"/>
</target>
diff --git a/jejdb/src/cpp/jejdb.c b/jejdb/src/cpp/jejdb.c
index 18b53b9..7a7f769 100644
--- a/jejdb/src/cpp/jejdb.c
+++ b/jejdb/src/cpp/jejdb.c
@@ -766,11 +766,11 @@ finish:
};
/*
-* Class: org_ejdb_driver_EJDBResultSet
-* Method: get
-* Signature: (I)Lorg/ejdb/bson/BSONObject;
-*/
-JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBResultSet_get (JNIEnv *env, jobject obj, jint indx) {
+ * Class: org_ejdb_driver_EJDBResultSet
+ * Method: _get
+ * Signature: (I)Lorg/ejdb/bson/BSONObject;
+ */
+JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBResultSet__1get (JNIEnv *env, jobject obj, jint indx) {
TCLIST *rs = get_rs_from_object(env, obj);
if (!rs) {
diff --git a/jejdb/src/cpp/org_ejdb_driver_EJDBCollection.h b/jejdb/src/cpp/org_ejdb_driver_EJDBCollection.h
index 19eb70c..2bfd0f3 100644
--- a/jejdb/src/cpp/org_ejdb_driver_EJDBCollection.h
+++ b/jejdb/src/cpp/org_ejdb_driver_EJDBCollection.h
@@ -66,7 +66,7 @@ JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBCollection_updateMeta
/*
* Class: org_ejdb_driver_EJDBCollection
* Method: load
- * Signature: (Lorg/bson/types/ObjectId;)Lorg/bson/BSONObject;
+ * Signature: (Lorg/ejdb/bson/types/ObjectId;)Lorg/ejdb/bson/BSONObject;
*/
JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_load
(JNIEnv *, jobject, jobject);
@@ -74,7 +74,7 @@ JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_load
/*
* Class: org_ejdb_driver_EJDBCollection
* Method: save
- * Signature: (Lorg/bson/BSONObject;)Lorg/bson/types/ObjectId;
+ * Signature: (Lorg/ejdb/bson/BSONObject;)Lorg/ejdb/bson/types/ObjectId;
*/
JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_save
(JNIEnv *, jobject, jobject);
@@ -82,7 +82,7 @@ JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_save
/*
* Class: org_ejdb_driver_EJDBCollection
* Method: remove
- * Signature: (Lorg/bson/types/ObjectId;)V
+ * Signature: (Lorg/ejdb/bson/types/ObjectId;)V
*/
JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBCollection_remove
(JNIEnv *, jobject, jobject);
diff --git a/jejdb/src/cpp/org_ejdb_driver_EJDBQuery.h b/jejdb/src/cpp/org_ejdb_driver_EJDBQuery.h
index 49ec7eb..d90ea9d 100644
--- a/jejdb/src/cpp/org_ejdb_driver_EJDBQuery.h
+++ b/jejdb/src/cpp/org_ejdb_driver_EJDBQuery.h
@@ -12,7 +12,7 @@ extern "C" {
/*
* Class: org_ejdb_driver_EJDBQuery
* Method: execute
- * Signature: (Lorg/bson/BSONObject;[Lorg/bson/BSONObject;Lorg/bson/BSONObject;ILjava/io/OutputStream;)Lorg/ejdb/driver/EJDBQuery$QResult;
+ * Signature: (Lorg/ejdb/bson/BSONObject;[Lorg/ejdb/bson/BSONObject;Lorg/ejdb/bson/BSONObject;ILjava/io/OutputStream;)Lorg/ejdb/driver/EJDBQuery$QResult;
*/
JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBQuery_execute
(JNIEnv *, jobject, jobject, jobjectArray, jobject, jint, jobject);
diff --git a/jejdb/src/cpp/org_ejdb_driver_EJDBResultSet.h b/jejdb/src/cpp/org_ejdb_driver_EJDBResultSet.h
index feb5867..a6ce063 100644
--- a/jejdb/src/cpp/org_ejdb_driver_EJDBResultSet.h
+++ b/jejdb/src/cpp/org_ejdb_driver_EJDBResultSet.h
@@ -9,10 +9,10 @@ extern "C" {
#endif
/*
* Class: org_ejdb_driver_EJDBResultSet
- * Method: get
- * Signature: (I)Lorg/bson/BSONObject;
+ * Method: _get
+ * Signature: (I)Lorg/ejdb/bson/BSONObject;
*/
-JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBResultSet_get
+JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBResultSet__1get
(JNIEnv *, jobject, jint);
/*
diff --git a/jejdb/src/java/org/ejdb/driver/EJDBResultSet.java b/jejdb/src/java/org/ejdb/driver/EJDBResultSet.java
index 2bf9cdf..5c136a6 100644
--- a/jejdb/src/java/org/ejdb/driver/EJDBResultSet.java
+++ b/jejdb/src/java/org/ejdb/driver/EJDBResultSet.java
@@ -2,18 +2,27 @@ package org.ejdb.driver;
import org.ejdb.bson.BSONObject;
+import java.io.Closeable;
+import java.lang.ref.WeakReference;
+import java.util.HashMap;
import java.util.Iterator;
+import java.util.Map;
import java.util.NoSuchElementException;
/**
* @author Tyutyunkov Vyacheslav (tve@softmotions.com)
* @version $Id$
*/
-public class EJDBResultSet implements Iterable<BSONObject>, Iterator<BSONObject> {
+public class EJDBResultSet implements Iterable<BSONObject>, Iterator<BSONObject>, Closeable {
private transient long rsPointer;
+ private transient Map<Integer, WeakReference<BSONObject>> cache;
private int position;
+ {
+ cache = new HashMap<Integer, WeakReference<BSONObject>>();
+ }
+
EJDBResultSet(long rsPointer) {
this.rsPointer = rsPointer;
@@ -23,7 +32,23 @@ public class EJDBResultSet implements Iterable<BSONObject>, Iterator<BSONObject>
/**
* Returns object by position
*/
- public native BSONObject get(int position) throws EJDBException;
+ protected native BSONObject _get(int position) throws EJDBException;
+
+ /**
+ * Returns object by position
+ */
+ public BSONObject get(int position) throws EJDBException {
+ BSONObject obj;
+ WeakReference<BSONObject> wr;
+ if (cache.containsKey(position) && (wr = cache.get(position)).get() != null) {
+ obj = wr.get();
+ } else {
+ obj = _get(position);
+ cache.put(position, new WeakReference<BSONObject>(obj));
+ }
+
+ return obj;
+ }
/**
* Returns objects count in result set
@@ -34,7 +59,24 @@ public class EJDBResultSet implements Iterable<BSONObject>, Iterator<BSONObject>
* {@inheritDoc}
*/
public Iterator<BSONObject> iterator() {
- return this;
+ return new Iterator<BSONObject>() {
+ private int pos = 0;
+
+ public boolean hasNext() {
+ return pos < EJDBResultSet.this.length();
+ }
+
+ public BSONObject next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException();
+ }
+ return get(pos++);
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
}
/**
@@ -63,7 +105,7 @@ public class EJDBResultSet implements Iterable<BSONObject>, Iterator<BSONObject>
}
/**
- * Close result set
+ * {@inheritDoc}
*/
public native void close() throws EJDBException;