-
Notifications
You must be signed in to change notification settings - Fork 9
/
vertex.clj
72 lines (59 loc) · 1.91 KB
/
vertex.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(ns hermes.vertex
(:import (com.tinkerpop.blueprints Vertex))
(:use [hermes.core :only (*graph* transact! ensure-graph-is-transaction-safe)]
[hermes.util :only (immigrate)]))
(immigrate 'hermes.element)
;;
;;Information getters
;;
(defn prop-map [vertex]
"Returns a Persistent map representing the edge"
(into {:__id__ (get-id vertex)}
(map #(vector (keyword %) (get-property vertex %)) (get-keys vertex))))
(defn find-by-id [& ids]
"Retrieves nodes by id from the graph."
(ensure-graph-is-transaction-safe)
(if (= 1 (count ids))
(.getVertex *graph* (first ids))
(seq (for [id ids] (.getVertex *graph* id)))))
(defn find-by-kv [k v]
"Given a key and a value, returns the set of all vertices that
sastify the pair."
(ensure-graph-is-transaction-safe)
(set (.getVertices *graph* (name k) v)))
;;
;; Transaction management
;;
(defn refresh [vertex]
"Gets a vertex back from the database and refreshes it to be usable again."
(ensure-graph-is-transaction-safe)
(.getVertex *graph* vertex))
;;
;; Creation methods
;;
(defn create!
"Create a vertex, optionally with the given property map."
([] (create! {}))
([data]
(ensure-graph-is-transaction-safe)
(set-properties! (.addVertex *graph*) data)))
(defn upsert! [k m]
"Given a key and a property map, upsert! either creates a new node
with that property map or updates all nodes with the given key
value pair to have the new properties specified by the map. Always
returns the set of vertices that were just updated or created."
(ensure-graph-is-transaction-safe)
(let [vertices (find-by-kv (name k) (k m))]
(if (empty? vertices)
(set [(create! m)])
(do
(doseq [vertex vertices] (set-properties! vertex m))
vertices))))
;;
;; Deletion methods
;;
(defn delete!
"Delete a vertex."
([vertex]
(ensure-graph-is-transaction-safe)
(.removeVertex *graph* vertex)))