Permalink
Browse files

Remove ClojureDBObject java class, and update readme accordingly

  • Loading branch information...
1 parent 8f462f5 commit 182518d43d10ffa30069a622915072cbd7158518 @purcell committed Jul 15, 2010
Showing with 5 additions and 158 deletions.
  1. +0 −139 java/somnium/congomongo/ClojureDBObject.java
  2. +2 −13 readme.markdown
  3. +3 −5 src/somnium/congomongo.clj
  4. +0 −1 test/congomongo_test.clj
@@ -1,139 +0,0 @@
-/*
- * Subclass of com.mongodb.BasicDBObject for
- * rapid creation of clojure persistent data structures.
- */
-
-package somnium.congomongo;
-import com.mongodb.BasicDBObject;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-import java.util.Map;
-import java.util.Arrays;
-import clojure.lang.Keyword;
-import clojure.lang.MapEntry;
-import clojure.lang.IPersistentMap;
-
-
-/**
- *
- * @author somnium
- */
-
-public class ClojureDBObject extends BasicDBObject {
-
- public ClojureDBObject() {
- }
-
- public ClojureDBObject(IPersistentMap m) {
- this.putClojure(m);
- }
-
- public void putClojure(IPersistentMap m) {
- Iterator<MapEntry> iter = m.iterator();
- while (iter.hasNext()) {
- MapEntry pair = iter.next();
- Object cKey = pair.getKey();
- String mKey;
-
- // check key for keyword type
- // and assign result to var mKey
-
- if (cKey instanceof Keyword) {
- mKey = ((Keyword)cKey).getName();}
- else {mKey = cKey.toString();
- }
-
- Object v = pair.getValue();
-
- // delegate to readClojureObject
-
- this.put(mKey, readClojureObject(v));
- }
- }
-
- private Object readClojureObject(Object o) {
-
- if (o instanceof Keyword) {
- return ((Keyword)o).getName();
- }
-
- else if (o instanceof IPersistentMap) {
- ClojureDBObject clj = new ClojureDBObject((IPersistentMap) o);
- return clj;
- }
-
- else if (o instanceof List) {
- int msize = ((List)o).size();
- Object[] ary = new Object[msize];
- Iterator<Object> iter = ((List)o).iterator();
- for (int idx = 0; idx < msize; idx++) {
- Object obj = iter.next();
- ary[idx] = readClojureObject(obj);
- }
- return Arrays.asList(ary);
- }
-
- else return o;
-
- }
-
- // options:
- // use struct-map?
- // keywordize keys?
- // call .toString on ObjectIds?
-
- public IPersistentMap toClojure() {
- return toClojureMap(this, true);
- }
-
- public IPersistentMap toClojure(boolean keywordize) {
- return toClojureMap(this, keywordize);
- }
-
- public static IPersistentMap toClojureMap(Map m, boolean keywordize){
- int msize = m.size() * 2;
- Object[] ary = new Object[msize];
- Set keys = m.keySet();
- Iterator<String> iter = keys.iterator();
- for (int idx = 0; idx < msize; idx++) {
- String s = iter.next();
- Object v = m.get(s);
-
- if (keywordize) {
- Keyword k = Keyword.intern(s);
- ary[idx] = k;
- } else {
- ary[idx] = s;
- }
- idx++;
- ary[idx] = toClojureVal(v, keywordize);
- }
-
- return clojure.lang.RT.map(ary);
-
- }
-
- private static Object toClojureVal(Object o, boolean keywordize) {
-
- if (o instanceof Map) {
- return toClojureMap((Map)o, keywordize);
- }
-
- else if (o instanceof List) {
- int msize = ((List)o).size();
- Object[] ary = new Object[msize];
-
- Iterator<Object> i = ((java.util.List) o).iterator();
- for (int idx = 0; idx < msize; idx ++) {
- Object obj = i.next();
- ary[idx] = toClojureVal(obj, keywordize);
- }
-
- return clojure.lang.RT.vector(ary);
-
- }
-
- else return o;
- }
- }
View
@@ -26,19 +26,8 @@ Clojars group is now congomongo.
Summary
---------
-note: The .java bits are likely to disappear once Clojure 1.2 is stable.
-
-CongoMongo is essentially two parts.
-
-One is the ClojureDBObject class written in java.
-It extends the BasicDBObject class with two methods (putClojure,
-toClojure) and a convenience constructor.
-
-It is basically a close-to-the-metal wrapper around the mongo-java-driver's
-BasicDBObject class to handle coercions (keyword->string and nested structures)
-while offering similar performance to the BasicDBObject class itself.
-
-The rest is a Clojure api for the mongo-java-driver.
+CongoMongo is essentially a Clojure api for the mongo-java-driver, transparently handling
+coercions between Clojure and Java data types.
Basics
--------
@@ -28,8 +28,7 @@
[clojure.contrib.json read write])
(:import [com.mongodb Mongo DB DBCollection DBObject ObjectId]
[com.mongodb.gridfs GridFS]
- [com.mongodb.util JSON]
- [somnium.congomongo ClojureDBObject]))
+ [com.mongodb.util JSON]))
(defunk mongo!
"Creates a Mongo object and sets the default database.
@@ -65,9 +64,8 @@
(definline get-coll
"Returns a DBCollection object"
[collection]
- `(doto (.getCollection #^DB (:db @*mongo-config*)
- #^String (named ~collection))
- (.setObjectClass ClojureDBObject)))
+ `(.getCollection #^DB (:db @*mongo-config*)
+ #^String (named ~collection)))
(defunk fetch
"Fetches objects from a collection.
@@ -5,7 +5,6 @@
somnium.congomongo.util
somnium.congomongo.coerce
clojure.contrib.pprint)
- (:import somnium.congomongo.ClojureDBObject)
(:use [clojure.contrib.json :only (read-json json-str)]))
(deftest coercions

0 comments on commit 182518d

Please sign in to comment.