Skip to content

Instantly share code, notes, and snippets.

Created December 29, 2009 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/016734cc1df33129811e to your computer and use it in GitHub Desktop.
Save anonymous/016734cc1df33129811e to your computer and use it in GitHub Desktop.
(ns com.bebop.appledelhi2
(:require [com.bebop.appledelhi2.neo4j :as neo4j]))
(defn unthunk
[t] (first (drop-while fn? (iterate #(if (fn? %1) (%1) %1) t))))
(def *main-content-field* "content")
(def *default-depth* 10)
(defn start
"Open a Neo4J database and indexers."
([path & indexers]
(neo4j/start-neo path)
(doseq [idxr indexers] (idxr)))extract-node #(if (fn? %1) %1 (%1 :node))
([path] (start path neo4j/start-lucene-ftsq)))
(defn shutdown
"Safely close the Neo4j database."
[] (neo4j/shutdown))
(defn fetch
"Retrieve nodes by their id."
([id] (neo4j/read-node id))
([id & ids] (conj (map fetch ids) (fetch id))))
(defn query
"Simple query for nodes by word terms. Powered by Lucene."
([qs] (neo4j/search-seq *main-content-field* query))
([qs field] (neo4j/search-seq field query))
([qs field order] (neo4j/search-seq (or field *main-content-field*) qs order)))
(defn relationship-map
"Represeting a link between nodes idiomatically in Clojure as a hash-map of attributes."
[r]
(neo4j/with-tx
(let [base (merge (neo4j/properties r)
{:id (neo4j/get-id r) :name (neo4j/relationship-name r)})]
(assoc base :endpoints (neo4j/endpoints r)))
))
(defn node-map
"Recursively constructs a lazy map representing the nodespace starting from a given node."
([n seen]
(neo4j/with-tx
(or (seen (neo4j/get-id n))
(let [id (neo4j/get-id n)
base (assoc (neo4j/properties n) :id id)
relmapper #(assoc (relationship-map %1)
:node (node-map (neo4j/follow %1 n) %2))]
(letfn [(nodemapping []
(merge base
{:incoming
(map #(relmapper %1 (assoc seen id nodemapping))
(neo4j/relationships n :incoming))
:outgoing
(map #(relmapper %1 (assoc seen id nodemapping))
(neo4j/relationships n :outgoing))}))]
(nodemapping))
))))
([n] (node-map n {})))
(defn node-space
"Creates a more natural representation of the node space using a seq of closures"
[nodemap]
(neo4j/with-tx
(if (fn? nodemap)
(comp node-space nodemap)
(let [incoming (nodemap :incoming)
outgoing (map :node (nodemap :outgoing))
basis (reduce dissoc nodemap [:incoming :outgoing])
self (reduce
(fn [n r] (assoc n (unthunk r) #(node-space (:node r))))
basis
outgoing)]
(conj (map node-space outgoing) self)))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment