Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

new fns and docs

  • Loading branch information...
commit 1d6590e8100d396c655623aab650ba8cab08bc23 1 parent 3802816
@wagjo wagjo authored
Showing with 185 additions and 110 deletions.
  1. +1 −0  .gitignore
  2. +41 −14 README.md
  3. +143 −96 src/borneo/core.clj
View
1  .gitignore
@@ -3,3 +3,4 @@ pom.xml
lib
classes
autodoc
+neo-db
View
55 README.md
@@ -2,9 +2,6 @@
Wrapper for Neo4j, a graph database.
-Because I was not happy with the current state (01/2011) of existing
-Neo4j wrappers for clojure, I've decided to create my own.
-
Purpose of this library is to provide intiutive access to commonly used
Neo4j operations. It uses official Neo4j Java bindings. It does not
use Blueprints interface.
@@ -17,13 +14,43 @@ Add the following dependency to your project.clj file:
## Documentation
-* API docs are at http://wagjo.github.com/borneo/
-
-* use with-db! to establish a connection to the database
-
-* all db operations must be inside with-db! body
-
-* TODO
+Detailed API docs are at [http://wagjo.github.com/borneo/](http://wagjo.github.com/borneo/)
+
+Quick overview of available functions (most important ones are emphasized):
+* __Database management__
+ * ___[with-db!](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/with-db!)_ - establish a connection to the database__
+ * ___[with-tx](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/with-tx)_ - establish a transaction__
+ * _[get-db](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/get-db)_ - get current database instance
+ * _[get-path](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/get-path)_ - get path to where database is stored
+ * _[read-only?](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/read-only?)_ - returns true if database is read only
+ * _[index](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/index)_ - returns Index Manager
+* __Property Containers _(both Nodes and Relationships)___
+ * _[prop?](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/prop?)_ - returns true if node or relationship contains given property
+ * ___[props](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/props)_ - returns map of properties for a given node or relationship__
+ * _[set-prop!](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/set-prop)_ - sets or removes property in a given node or relationship
+ * ___[set-props!](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/set-props!)_ - sets (or removes) properties for a given node or relationships__
+ * _[get-id](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/get-id)_ - returns id of a given node or relationship
+ * ___[delete!](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/delete!)_ - deletes relationship or free__
+* __Relationships__
+ * ___[rel-nodes](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/rel-nodes)_ - returns the two nodes attached to the given relationship__
+ * _[other-node](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/other-node)_ - returns other node for given relationsip
+ * ___[rel-type](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/rel-type)_ - returns type of given relationship__
+ * ___[create-rel!](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/create-rel!)_ - create relationship between two nodes__
+ * _[all-rel-types](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/all-rel-types)_ - returns lazy seq of all relationship types in database
+* __Nodes__
+ * _[rel?](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/rel?)_ - returns true if node has given relationship(s)
+ * ___[rels](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/rels)_ - returns relationships attached to given node__
+ * _[single-rel](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/single-rel)_ - returns single relationship for given node
+ * _[create-node!](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/create-node!)_ - creates new node, not linked with any other nodes
+ * ___[create-child!](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/create-child!)_ - creates a child node of a given parent__
+ * ___[delete-node!](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/delete-node!)_ - delete node and all its relationships__
+* __Graph traversal__
+ * _[all-nodes](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/all-nodes)_ - returns lazy-seq of all nodes in database
+ * _[node-by-id](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/node-by-id)_ - returns node with a given id
+ * _[rel-by-id](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/rel-by-id)_ - returns relationship with a given id
+ * ___[root](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/root)_ - returns root/reference node__
+ * ___[walk](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/walk)_ - walk though the graph by following through given relations__
+ * ___[traverse](http://wagjo.github.com/borneo/borneo.core-api.html#borneo.core/traverse)_ - traverse the graph__
## Examples
@@ -38,13 +65,13 @@ Add the following dependency to your project.clj file:
## Contact
-* http://github.com/wagjo
+* [http://github.com/wagjo](http://github.com/wagjo)
-* http://www.google.com/profiles/jozef.wagner
+* [http://www.google.com/profiles/jozef.wagner](http://www.google.com/profiles/jozef.wagner)
## License
-Disclaimer: Forked from hgavin/clojure-neo4j
+Disclaimer: Forked from [hgavin/clojure-neo4j](http://github.com/hgavin/clojure-neo4j)
Disclaimer: Small amount of comments and docs are based on official
Neo4j javadocs.
@@ -52,7 +79,7 @@ Neo4j javadocs.
Copyright (C) 2011, Jozef Wagner. All rights reserved.
The use and distribution terms for this software are covered by the
-Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+Eclipse Public License 1.0 ([http://opensource.org/licenses/eclipse-1.0.php](http://opensource.org/licenses/eclipse-1.0.php))
which can be found in the file epl-v10.html at the root of this
distribution.
View
239 src/borneo/core.clj
@@ -18,21 +18,18 @@
;; Forked from hgavin/clojure-neo4j
(ns borneo.core
- "Wrapper for Neo4j. Because I was not happy with the current state
- (01/2011) of existing Neo4j wrappers for clojure, I've decided to
- create my own.
-
- Purpose of this library is to provide intiutive access to commonly used
- Neo4j operations. It uses official Neo4j Java bindings. It does not
- use Blueprints interface.
+ "Clojure wrapper for Neo4j, a graph database.
See README for usage instructions, documentation and examples.
Code notes:
+ - Using official Neo4j bindings
+ - Not using Blueprints interface
- neo-db holds the current db instance, so that users do not have
to supply db instance at each call to db operations. This
approach has of course its drawbacks (e.g. only one connection at
- time), but I've found it suitable for my purposes."
+ time), but I've found it suitable for my purposes.
+ - TODO: note in default transactions"
(:import (org.neo4j.graphdb Direction
Node
PropertyContainer
@@ -172,6 +169,8 @@
check-prop (fn [[k v]] (= v (.getProperty node (name-or-str k) nil)))]
(every? check-prop props)))))
+;; TODO: Protocols
+
(defn- return-evaluator
"Translates value to the respective return evaluator.
Valid values are:
@@ -236,8 +235,81 @@
val#)
(finally (.finish tx#)))))
-(defn id
- "Returns id for a given node or relationship"
+(defn get-db
+ "Returns current database instance"
+ []
+ neo-db)
+
+(defn get-path
+ "Returns path to where the database is stored"
+ []
+ (.getStoreDir neo-db))
+
+(defn read-only?
+ "Returns true if database is read only"
+ []
+ (.isReadOnly neo-db))
+
+(defn index
+ "Returns the IndexManager paired with this graph database service"
+ []
+ (.index neo-db))
+
+;;; Property Containers
+
+(defn prop?
+ "Returns true if given node or relationship contains
+ property with a given key"
+ [^PropertyContainer c k]
+ (.hasProperty c (name-or-str k)))
+
+;; TODO: add fns which are less resource consuming :)
+
+(defn props
+ "Return map of properties for a given node or relationship.
+ Fetches all properties and can be very resource consuming if node
+ contains many large properties"
+ [^PropertyContainer c]
+ (let [keys (.getPropertyKeys c)
+ convert-fn (fn [k] [(keyword k)
+ (let [v (.getProperty c k)]
+ (if (array? v) ; handle multiple values
+ (seq v)
+ v))])]
+ (into {} (map convert-fn keys))))
+
+(defn set-prop!
+ "Sets or remove property for a given node or relationship.
+ The property value must be one of the valid property types (see Neo4j docs).
+ If a property value is nil, removes this property from the given
+ node or relationship."
+ ([^PropertyContainer c key]
+ (set-prop! c key nil))
+ ([^PropertyContainer c key value]
+ (io!)
+ (with-tx
+ (if value
+ ;; TODO: better support primivives and arrays of primitives
+ (.setProperty c (name-or-str key)
+ (if (coll? value) ; handle multiple values
+ (into-array String value)
+ value))
+ (.removeProperty c (name-or-str key))))))
+
+(defn set-props!
+ "Sets properties for a given node or relationship.
+ The property value must be one of the valid property types (see Neo4j docs).
+ If a property value is nil, removes this property from the given
+ node or relationship."
+ [^PropertyContainer c props]
+ (io!)
+ (with-tx
+ (doseq [[k v] props]
+ (set-prop! c k v))))
+
+(defn get-id
+ "Returns id for a given node or relationship.
+ Note that ids are not very good as unique identifiers."
[item]
(.getId item))
@@ -263,6 +335,36 @@
;; all" relationships that are either :out or :in. (from Neo4j
;; javadoc)
+(defn rel-nodes
+ "Returns the two nodes attached to the given relationship."
+ [^Relationship r]
+ (.getNodes r))
+
+(defn other-node
+ "Returns other node for given relationship."
+ [^Relationship r ^Node node]
+ (.getOtherNode r node))
+
+(defn rel-type
+ "Returns type of given relationship."
+ [^Relationship r]
+ (keyword (.name (.getType r))))
+
+(defn create-rel!
+ "Create relationship of a supplied type between from and to nodes."
+ [^Node from type ^Node to]
+ (io!)
+ (with-tx
+ (.createRelationshipTo from to (rel-type* type))))
+
+(defn all-rel-types
+ "Returns lazy seq of all relationship types currently in database"
+ []
+ (lazy-seq
+ (.getRelationshipTypes neo-db)))
+
+;;; Nodes
+
(defn rel?
"Returns true if there are relationships attached to this node. Syntax:
[node] - All relationships
@@ -293,7 +395,7 @@
:else (.hasRelationship node (rel-type* type) (rel-dir direction)))))
(defn rels
- "Returns all relationships attached to this node. Syntax:
+ "Returns relationships attached to this node. Syntax:
[node] - All relationships
[node type-or-types] - Relationships of any of specified types with
any direction
@@ -330,89 +432,8 @@
([^Node node type direction]
(.getSingleRelationship node (rel-type* type) (rel-dir direction))))
-(defn create-rel!
- "Create relationship of a supplied type between from and to nodes."
- [^Node from type ^Node to]
- (io!)
- (with-tx
- (.createRelationshipTo from to (rel-type* type))))
-
-(defn nodes
- "Returns the two nodes attached to the given relationship."
- [^Relationship r]
- (.getNodes r))
-
-(defn other
- "Returns other node for given relationship."
- [^Relationship r ^Node node]
- (.getOtherNode r node))
-
-(defn rel-type
- "Returns type of given relationship."
- [^Relationship r]
- (keyword (.name (.getType r))))
-
-;;; Properties
-
-(defn prop?
- "Returns true if given node or relationship contains
- property with a given key"
- [^PropertyContainer c k]
- (.hasProperty c (name-or-str k)))
-
-;; TODO: add fns which are less resource consuming :)
-
-(defn props
- "Return map of properties for a given node or relationship.
- Fetches all properties and can be very resource consuming if node
- contains many large properties"
- [^PropertyContainer c]
- (let [keys (.getPropertyKeys c)
- convert-fn (fn [k] [(keyword k)
- (let [v (.getProperty c k)]
- (if (array? v) ; handle multiple values
- (seq v)
- v))])]
- (into {} (map convert-fn keys))))
-
-(defn set-prop!
- "Sets or remove property for a given node or relationship.
- The property value must be one of the valid property types (see Neo4j docs).
- If a property value is nil, removes this property from the given
- node or relationship."
- ([^PropertyContainer c key]
- (set-prop! c key nil))
- ([^PropertyContainer c key value]
- (io!)
- (with-tx
- (if value
- ;; TODO: better support primivives and arrays of primitives
- (.setProperty c (name-or-str key)
- (if (coll? value) ; handle multiple values
- (into-array String value)
- value))
- (.removeProperty c (name-or-str key))))))
-
-(defn set-props!
- "Sets properties for a given node or relationship.
- The property value must be one of the valid property types (see Neo4j docs).
- If a property value is nil, removes this property from the given
- node or relationship."
- [^PropertyContainer c props]
- (io!)
- (with-tx
- (doseq [[k v] props]
- (set-prop! c k v))))
-
-;;; Nodes
-
-(defn root
- "Returns reference/root node."
- []
- (.getReferenceNode neo-db))
-
(defn create-node!
- "Creates a new node."
+ "Creates a new node, not linked with any other nodes."
([]
(io!)
(with-tx
@@ -422,6 +443,8 @@
(doto (create-node!)
(set-props! props)))))
+(declare root)
+
(defn create-child!
"Creates a node that is a child of the specified parent node
(or root node) along the specified relationship.
@@ -461,13 +484,35 @@
;;; Graph traversal
-(defn go
+(defn all-nodes
+ "Returns lazy-seq of all nodes in the db."
+ []
+ (lazy-seq (.getAllNodes neo-db)))
+
+(defn node-by-id
+ "Returns node with a given id.
+ Note that ids are not very good as unique identifiers."
+ [id]
+ (.getNodeById neo-db))
+
+(defn rel-by-id
+ "Returns relationship with a given id.
+ Note that ids are not very good as unique identifiers."
+ [id]
+ (.getRelationshipById neo-db id))
+
+(defn root
+ "Returns reference/root node."
+ []
+ (.getReferenceNode neo-db))
+
+(defn walk
"Walk through the graph by following specified relations. Returns last node.
Throws NullPointerException if path is wrong.
Throws NotFoundException if path is ambiguous."
[^Node node & types]
(let [next-node (fn [^Node node type]
- (second (nodes (single-rel node type))))]
+ (second (rel-nodes (single-rel node type))))]
(reduce next-node node types)))
(defn traverse
@@ -512,6 +557,8 @@
(start! "neo-db")
+ (stop!)
+
(traverse (root)
:depth
:end
@@ -522,7 +569,7 @@
{:property1 "value"}
{:foo :out :bar :out}))
- (count (traverse (go (root) :foo)
+ (count (traverse (walk (root) :foo)
:bar))
)
Please sign in to comment.
Something went wrong with that request. Please try again.