Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add tests and fixes for non-protobuf layers.

  • Loading branch information...
commit 9a12b3f13a5d3f929415eb3a63110fff7531f2c8 1 parent 8da61d5
@ninjudd authored
View
3  .gitignore
@@ -2,4 +2,5 @@ lib
classes
*.jar
*.class
-*.java
+*.java
+*~
View
13 build.xml
@@ -188,4 +188,17 @@
<target name="package" depends="compile">
<jar destfile="jiraph.jar" basedir="classes"/>
</target>
+
+ <target name="compile_test_proto" depends="compile">
+ <exec dir="src" executable="../lib/clojure-protobuf/lib/install/bin/protoclj">
+ <arg value="test/test.proto"/>
+ </exec>
+ </target>
+
+ <target name="test" depends="compile_test_proto">
+ <java classname="clojure.main" classpathref="classpath" failonerror="true" fork="true">
+ <sysproperty key="java.library.path" value="lib/install/lib"/>
+ <arg value="src/test.clj"/>
+ </java>
+ </target>
</project>
View
2  src/jiraph.proto → examples/jiraph.proto
@@ -11,7 +11,7 @@ message Edge {
message Node {
optional uint32 id = 1;
- repeated Edge edges = 2 [(map_by = "to_id")];
+ repeated Edge edges = 2 [(map_by) = "to_id"];
optional string type = 3;
optional string data = 4;
repeated uint32 _len = 5;
View
15 src/jiraph.clj
@@ -109,13 +109,13 @@
(when-not (= old new)
(db-set (*graph* layer) id new))))))
-(defn append-node! [layer id & args]
+(defn conj-node! [layer id & args]
{:pre [(not (opt layer :disable-append))]}
(let [node (make-node layer args)]
(with-callbacks :append [layer id node]
(if (opt layer :store-length-on-append)
(transaction layer
- (let [node (assoc node :_len (db-len (*graph* layer) id))]
+ (let [node (assoc node :_len [(db-len (*graph* layer) id)])]
(db-append (*graph* layer) id node)))
(db-append (*graph* layer) id node)))))
@@ -123,8 +123,6 @@
(with-callbacks :delete [layer id]
(db-delete (*graph* layer) id)))
-(def conj-node! append-node!)
-
(defn assoc-node! [layer id & args]
(update-node! layer id
(fn [node]
@@ -134,7 +132,7 @@
(defn conj-edge! [layer from-id to-id & args]
(let [edge (args-map args :to-id to-id)]
- (conj-node! layer from-id :edges [edge])
+ (conj-node! layer from-id :edges {to-id edge})
edge))
(defn get-edges
@@ -146,16 +144,15 @@
(fn [node]
(if node
(assoc node
- :edges (vec (vals
- (apply update-fn (node :edges) args))))
+ :edges (apply update-fn (node :edges) args))
(let [edges (apply update-fn {} args)]
(when-not (empty? edges)
- (make-node layer :id id :edges (vec (vals edges)))))))))
+ (make-node layer :id id :edges edges)))))))
(defn update-edge! [layer from-id to-id update-fn & args]
(update-edges! layer from-id
(fn [edges]
- (let [edge (apply update-fn (edges to-id) args)]
+ (let [edge (apply update-fn ((or edges {}) to-id) args)]
(when edge
(assoc edges
to-id (assoc edge :to-id to-id)))))))
View
6 src/jiraph/tc.clj
@@ -7,9 +7,9 @@
(def in-transaction? false)
(def default-opts
- {:key #(.getBytes (str %))
- :dump #(.getBytes (pr-str %))
- :load #(read-append (String. #^bytes %))})
+ {:key #(.getBytes (str %))
+ :dump #(.getBytes (pr-str %))
+ :load read-append-bytes})
(defn- tflags [opts]
(bit-or
View
4 src/jiraph/utils.clj
@@ -110,6 +110,10 @@
(defn read-append [str]
(apply merge-with append (read-mult str)))
+(defn read-append-bytes
+ ([#^bytes b] (read-append (String. b)))
+ ([#^bytes b len] (read-append (String. b 0 (int len)))))
+
(defn sort-with
"Sort using decorate-sort-undecorate (Schwartzian transform)."
([keyfn coll]
View
19 src/test/jiraph.clj
@@ -6,14 +6,20 @@
(defn append-actions [layer id & args]
(conj-node! :actions id layer [(into [jiraph/*callback* id] args)]))
-(defgraph graph
- :path "/tmp/jiraph-test" :proto jiraph.Proto$Node :create true :bnum 1000000
+(defgraph proto-graph
+ :path "/tmp/jiraph-proto-test" :proto test.jiraph.Proto$Node :create true :bnum 1000000
(layer :friends :store-length-on-append true)
(layer :enemies :post-write append-actions)
(layer :actions :append-only true :proto nil))
+(defgraph graph
+ :path "/tmp/jiraph-test" :create true :bnum 1000000
+ (layer :friends :store-length-on-append true)
+ (layer :enemies :post-write append-actions)
+ (layer :actions :append-only true))
+
(defn clear-graph [f]
- (doseq [layer (vals graph)]
+ (doseq [layer (concat (vals graph) (vals proto-graph))]
(jiraph.tc/db-truncate layer))
(f))
@@ -24,7 +30,8 @@
(assoc m :data (.toUpperCase data))))
(deftest graph-access
- (with-graph graph
+ (doseq [g [graph proto-graph]]
+ (with-graph g
(testing "nodes"
(add-node! :friends 1 :type "person" :data "foo")
(let [node (get-node :friends 1)]
@@ -61,6 +68,7 @@
(is (= nil (get-node :friends 1)))
)
(testing "edges"
+ (add-node! :enemies 1 :data "bar")
(assoc-edge! :enemies 1 5 :data "arch-nemesis")
(let [edges (get-edges :enemies 1)]
(is (= 1 (count edges)))
@@ -91,7 +99,7 @@
(conj-edge! :friends 1 4 :data "ZAP!")
(conj-edge! :friends 1 8 :data "spiderman")
(let [node (get-node :friends 1)
- edges (get-edges node)
+ edges (:edges node)
len (node :_len)]
(is (= 3 (count edges)))
(is (= (edges 2) {:to-id 2, :data "ZAP!"}))
@@ -135,3 +143,4 @@
{:id 15, :type "arch-rival", :data "bad"}] ((actions :enemies) 1)))
(is (= [:post-delete 15] ((actions :enemies) 2))))
)))
+)
View
2  src/test/jiraph/tc.clj
@@ -3,7 +3,7 @@
(:use jiraph.utils)
(:use clojure.test))
-(def db (db-open {:path "/tmp/jiraph-test-foo"}))
+(def db (db-open {:path "/tmp/jiraph-test-foo" :create true}))
(defn clear-db [f]
(map db-truncate db)
View
17 src/test/test.proto
@@ -0,0 +1,17 @@
+package test.jiraph;
+import "clojure/protobuf/collections.proto";
+
+option java_outer_classname = "Proto";
+
+message Edge {
+ required uint32 to_id = 1;
+ optional string data = 2;
+}
+
+message Node {
+ optional uint32 id = 1;
+ repeated Edge edges = 2 [(map_by) = "to_id"];
+ optional string type = 3;
+ optional string data = 4;
+ repeated uint32 _len = 5;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.