Fetching contributors…
Cannot retrieve contributors at this time
97 lines (84 sloc) 4.05 KB
;; Copyright (c) 2011-2015 Michael S. Klishin, Alex Petrov, and The ClojureWerkz
;; Team
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
"Operations on labels (Neo4J 2.0+)"
(:require [clj-http.client :as http]
[cheshire.core :as json]
[clojure.string :as string]
[ :as rest]
[ :as conv]
[ :as records]
[ :refer [missing?]])
(:import [ Connection])
(:refer-clojure :exclude [node replace remove rest]))
(defn- get-location-url
(str (:location-uri node) "/labels"))
(defn add
"Adds a string label or a list of labels (string or keyword) to a node.
[^Connection connection node labels]
(get-location-url node)
:body (json/encode (conv/kw-to-string labels))))
(defn replace
"This removes any existing labels for the node and adds the labels passes to the function.
[^Connection connection node labels]
(get-location-url node)
:body (json/encode labels))))
(defn remove
"This removes the specified label from the node.
[^Connection connection node label]
(str (get-location-url node) "/" (conv/encode-kw-to-string label))))
(defn- get-labels
[^Connection connection ^String uri]
(let [{:keys [status headers body]} (rest/GET connection uri)]
(when-not (missing? status)
(json/decode body true)))))
(defn get-all-labels
"This function gets all labels in the database if no argument is passed.
If a node is passed, then it returns all the labels associated with the node.
([^Connection connection]
(get-labels connection (str (get-in connection [:endpoint :uri]) "labels")))
([^Connection connection node]
(get-labels connection (get-location-url node))))
(defn- encode-params
[^Connection connection ^String label ^String x y]
(str (get-in connection [:endpoint :uri])
(conv/encode-kw-to-string label)
(when (and x y)
(str "?"
[[(conv/kw-to-string x) (json/encode y)]])))))
(defn get-all-nodes
"This returns all the nodes which have a particular label.
You can also pass a property name and value you want to filter the nodes on.
([^Connection connection label]
(get-all-nodes connection label nil nil))
([^Connection connection label prop-name prop-value]
(let [base-uri (encode-params connection label prop-name prop-value)
{:keys [status headers body]} (rest/GET connection base-uri)]
(when-not (missing? status)
(map records/instantiate-node-from (json/decode body true))))))