├── .circleci └── config.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── dev └── user.clj ├── doc └── intro.md ├── project.clj ├── src └── clojure │ └── clj_arangodb │ ├── arangodb │ ├── adapter.clj │ ├── admin.clj │ ├── aql.clj │ ├── collections.clj │ ├── core.clj │ ├── cursor.clj │ ├── databases.clj │ ├── graph.clj │ └── options.clj │ └── velocypack │ ├── core.clj │ └── utils.clj └── test └── clj_arangodb ├── arangodb ├── aql_test.clj ├── collections_test.clj ├── cursor_test.clj ├── databases_test.clj ├── helper.clj └── test_data.clj └── velocypack └── core_test.clj /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # Clojure CircleCI 2.0 configuration file 2 | # 3 | # Check https://circleci.com/docs/2.0/language-clojure/ for more details 4 | # 5 | version: 2 6 | jobs: 7 | build: 8 | docker: 9 | # specify the version you desire here 10 | - image: circleci/clojure:lein-2.7.1 11 | 12 | # Specify service dependencies here if necessary 13 | # CircleCI maintains a library of pre-built images 14 | # documented at https://circleci.com/docs/2.0/circleci-images/ 15 | - image: arangodb:3.7.2 16 | environment: 17 | ARANGO_NO_AUTH: 1 18 | 19 | working_directory: ~/repo 20 | 21 | environment: 22 | LEIN_ROOT: "true" 23 | # Customize the JVM maximum heap limit 24 | JVM_OPTS: -Xmx3200m 25 | 26 | steps: 27 | - checkout 28 | 29 | # Download and cache dependencies 30 | - restore_cache: 31 | keys: 32 | - v1-dependencies-{{ checksum "project.clj" }} 33 | # fallback to using the latest cache if no exact match is found 34 | - v1-dependencies- 35 | 36 | - run: lein deps 37 | 38 | - save_cache: 39 | paths: 40 | - ~/.m2 41 | key: v1-dependencies-{{ checksum "project.clj" }} 42 | 43 | # run tests! 44 | - run: lein test -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | .DS_Store 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). 3 | 4 | ## [Unreleased] 5 | ### Changed 6 | - Add a new arity to `make-widget-async` to provide a different widget shape. 7 | 8 | ## [0.1.1] - 2017-09-25 9 | ### Changed 10 | - Documentation on how to make the widgets. 11 | 12 | ### Removed 13 | - `make-widget-sync` - we're all async, all the time. 14 | 15 | ### Fixed 16 | - Fixed widget maker to keep working when daylight savings switches over. 17 | 18 | ## 0.1.0 - 2017-09-25 19 | ### Added 20 | - Files from the new template. 21 | - Widget maker public API - `make-widget-sync`. 22 | 23 | [Unreleased]: https://github.com/your-name/clj-arangodb/compare/0.1.1...HEAD 24 | [0.1.1]: https://github.com/your-name/clj-arangodb/compare/0.1.0...0.1.1 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor to control, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # clj-arangodb 2 | 3 | [![Clojars Project](https://img.shields.io/clojars/v/beoliver/clj-arangodb.svg)](https://clojars.org/beoliver/clj-arangodb) 4 | [![CircleCI](https://circleci.com/gh/beoliver/clj-arangodb.svg?style=shield)](https://circleci.com/gh/beoliver/clj-arangodb) 5 | 6 | ### A Clojure interface for the `arangodb-java-driver` 7 | 8 | The maintainers of arangodb provide a [java driver](https://www.arangodb.com/docs/stable/drivers/java-getting-started.html) for communicating with an arangodb server. `clj-arangodb` provides a _thin_ (and incomplete) abstraction. 9 | 10 | ## Getting started 11 | 12 | For the most up to date information it is best to consult the official java [documentation](https://www.arangodb.com/docs/stable/drivers/java-reference.html). In general functions are lispy versions of their java counterparts (methods). 13 | 14 | Options are passed as maps. Keys can be _keywords_ or _strings_, but should be written using `cammelCase`. If an option takes multiple arguments then a vector should be used. 15 | 16 | For more information about what constitutes a valid option for a method you must consult the java api documentation. 17 | 18 | ### Creating a connection 19 | 20 | Assuming that you have created a `user` in the arango database... 21 | 22 | ```clojure 23 | (ns user 24 | (:require [clj-arangodb.arangodb.core :as a] 25 | [clj-arangodb.arangodb.databases :as d] 26 | [clj-arangodb.arangodb.collections :as c] 27 | [clj-arangodb.arangodb.adapter :as adapter]) 28 | (:import [com.arangodb Protocol] 29 | [com.arangodb.entity BaseDocument] 30 | [com.arangodb.velocypack VPackSlice])) 31 | 32 | (defonce conn ;; connections are thread safe 33 | (let [config {:useProtocol Protocol/VST 34 | :user "test" 35 | :host ["127.0.0.1" 8529]}] 36 | (a/connect config))) 37 | ``` 38 | 39 | ### Databases 40 | 41 | ```clojure 42 | user> (a/database? conn "testDB") 43 | false 44 | user> (def db (a/create-and-get-database conn "testDB")) 45 | #'user/db 46 | user> db 47 | #object[com.arangodb.internal.ArangoDatabaseImpl 0x38f9457a "com.arangodb.internal.ArangoDatabaseImpl@38f9457a"] 48 | user> (a/database? conn "testDB") 49 | true 50 | user> (a/get-databases conn) 51 | ["_system" "testDB"] 52 | ``` 53 | 54 | ### Collections 55 | 56 | ```clojure 57 | user> (d/collection? db "testColl") 58 | false 59 | user> (def coll (d/create-and-get-collection db "testColl")) 60 | #'user/coll 61 | user> (d/collection? db "testColl") 62 | true 63 | user> (def coll-again (d/get-collection db "testColl")) 64 | #'user/coll-again 65 | user> (= coll coll-again) 66 | false 67 | user> coll 68 | #object[com.arangodb.internal.ArangoCollectionImpl 0x23266a4f "com.arangodb.internal.ArangoCollectionImpl@23266a4f"] 69 | user> coll-again 70 | #object[com.arangodb.internal.ArangoCollectionImpl 0x33859f41 "com.arangodb.internal.ArangoCollectionImpl@33859f41"] 71 | user> (= (adapter/from-entity (:info (bean coll))) 72 | (adapter/from-entity (:info (bean coll-again)))) 73 | true 74 | ``` 75 | 76 | By default functions that return a `Entity` of some kind are wrapped with `adapter/from-entity`. 77 | `Entity` results are only data - ie they are not handles. 78 | The default for this is to call `bean` and then examines the values under the keys. 79 | In general if the entity contains results, these results are _not_ 80 | desearialzed. all sub classes are - (some are converted to string to give sensible data) 81 | 82 | ### Documents 83 | 84 | inserting a document 85 | 86 | ```clojure 87 | user> (c/insert-document coll {:hello "world"}) 88 | {:class com.arangodb.entity.DocumentCreateEntity, 89 | :id "testColl/34730", 90 | :key "34730", 91 | :new nil, 92 | :old nil, 93 | :rev "_bH1Uxmq---"} 94 | ``` 95 | 96 | #### Retrieving a document. 97 | 98 | By default when retreiving a document the `VPackSlice` class is used. This is then parsed using the [cheshire(https://github.com/dakrone/cheshire) json library. 99 | 100 | ```clojure 101 | user> (c/get-document coll "34730") 102 | {:_key "34730", 103 | :_id "testColl/34730", 104 | :_rev "_bH1Uxmq---", 105 | :hello "world"} 106 | 107 | user> (c/get-document coll "34730" VPackSlice) 108 | {:_key "34730", 109 | :_id "testColl/34730", 110 | :_rev "_bH1Uxmq---", 111 | :hello "world"} 112 | ``` 113 | 114 | By passing a class as well we can get a different type back. 115 | The `BaseDocument` (belonging to ArangoDB) class is converted by calling `bean`. 116 | 117 | ```clojure 118 | user> (c/get-document coll "34730" BaseDocument) 119 | {:class com.arangodb.entity.BaseDocument, 120 | :id "testColl/34730", 121 | :key "34730", 122 | :properties {"hello" "world"}, 123 | :revision "_bH1Uxmq---"} 124 | ``` 125 | 126 | ```clojure 127 | user> (c/get-document coll "34730" String) 128 | "{\"_key\":\"34730\",\"_id\":\"testColl\\/34730\",\"_rev\":\"_bH1Uxmq---\",\"hello\":\"world\"}" 129 | ``` 130 | 131 | ```clojure 132 | user> (c/get-document coll "34730" java.util.Map) 133 | {"_key" "34730", 134 | "_id" "testColl/34730", 135 | "_rev" "_bH1Uxmq---", 136 | "hello" "world"} 137 | ``` 138 | 139 | If you want to use a custom json serializer/deserializer then you can extend the multimethods `serialize-doc` and `deserialize-doc` for the `String` class. 140 | 141 | ## AQL 142 | 143 | The AQL query syntax can be represented as clojure data structures, there is no EBFN document at the moment so you will have to read the source file, an example taken from one of the tests: 144 | In this example the FOR statement is used to execute a graph query 145 | 146 | ```clojure 147 | (ns user 148 | (:require [clj-arangodb.arangodb.databases :as d] 149 | [clj-arangodb.arangodb.test-data :as td] 150 | [clj-arangodb.arangodb.helper :as h])) 151 | ``` 152 | 153 | ```clojure 154 | user> (td/init-game-of-thrones-db) 155 | nil 156 | user> (h/with-db [db td/game-of-thrones-db-label] 157 | (let [query [:FOR ["c" "Characters"] 158 | [:FILTER [:EQ "c.name" "\"Bran\""]] 159 | [:FOR ["v" {:start "c" 160 | :type :outbound 161 | :depth [1 1] 162 | :collections ["ChildOf"]}] 163 | [:RETURN "v.name"]]]] 164 | (println (vec (d/query db query String))))) 165 | [Ned Catelyn] 166 | ``` 167 | 168 | Have a play - and remeber the multimethods! - if you don't like the data you are getting, change it... 169 | -------------------------------------------------------------------------------- /dev/user.clj: -------------------------------------------------------------------------------- 1 | (ns user 2 | (:require [clojure.reflect :as r] 3 | [clj-arangodb.arangodb.core :as a] 4 | [clj-arangodb.arangodb.databases :as d] 5 | [clj-arangodb.arangodb.collections :as c] 6 | [clj-arangodb.arangodb.graph :as g] 7 | [clj-arangodb.velocypack.core :as v] 8 | [clj-arangodb.arangodb.adapter :as adapter] 9 | [clj-arangodb.arangodb.test-data :as td] 10 | [clj-arangodb.arangodb.helper :as h] 11 | ) 12 | (:import [com.arangodb Protocol] 13 | [com.arangodb.entity BaseDocument] 14 | [com.arangodb.velocypack VPackSlice])) 15 | -------------------------------------------------------------------------------- /doc/intro.md: -------------------------------------------------------------------------------- 1 | # Introduction to clj-arangodb 2 | 3 | TODO: write [great documentation](http://jacobian.org/writing/what-to-write/) 4 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject beoliver/clj-arangodb "0.0.9" 2 | :description "A Clojure wrapper for ArangoDB" 3 | :url "https://github.com/beoliver/clj-arangodb" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :source-paths ["src/clojure"] 7 | :java-source-paths ["src/java"] 8 | :dependencies [[org.clojure/clojure "1.10.1"] 9 | [com.arangodb/arangodb-java-driver "6.7.4"] 10 | [org.slf4j/slf4j-simple "1.7.30"] 11 | [cheshire "5.10.0"]]) 12 | -------------------------------------------------------------------------------- /src/clojure/clj_arangodb/arangodb/adapter.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.adapter 2 | (:require [clj-arangodb.velocypack.core :as vpack]) 3 | (:import [java.util 4 | ArrayList] 5 | [clojure.lang 6 | PersistentArrayMap 7 | PersistentHashMap] 8 | [com.arangodb.velocypack 9 | VPackSlice] 10 | [com.arangodb.entity 11 | Entity 12 | MultiDocumentEntity 13 | BaseDocument])) 14 | 15 | (defmulti double-quote-strings class) 16 | (defmethod double-quote-strings String 17 | [x] (str "\"" x "\"")) 18 | (defmethod double-quote-strings clojure.lang.APersistentVector 19 | [xs] (mapv double-quote-strings xs)) 20 | (defmethod double-quote-strings clojure.lang.APersistentMap 21 | [xs] (into {} (for [[k v] xs] [(double-quote-strings k) (double-quote-strings v)]))) 22 | (defmethod double-quote-strings clojure.lang.ASeq 23 | [xs] (seq (map double-quote-strings xs))) 24 | (defmethod double-quote-strings clojure.lang.APersistentSet 25 | [xs] (set (map double-quote-strings xs))) 26 | (defmethod double-quote-strings :default 27 | [x] x) 28 | 29 | (def ^:const entity-package-name "com.arangodb.entity") 30 | 31 | ;; *default-doc-class* is used to indicate 32 | ;; the class of the objects that we want RETURNED 33 | (def ^:dynamic *default-doc-class* VPackSlice) 34 | 35 | (defmulti serialize-doc class) 36 | (defmulti deserialize-doc class) 37 | 38 | (defmethod serialize-doc :default [o] (vpack/pack o)) 39 | 40 | (defmethod deserialize-doc VPackSlice [o] (vpack/unpack o)) 41 | (defmethod deserialize-doc BaseDocument [o] (bean o)) 42 | (defmethod deserialize-doc :default [o] o) 43 | 44 | (defmulti from-entity class) 45 | 46 | (defn is-entity? [o] 47 | (when o 48 | (or (instance? Entity o) 49 | (= entity-package-name (-> o class .getPackage .getName))))) 50 | 51 | (defn as-entity-vec-or-deserialized [^ArrayList o] 52 | (if (= 0 (.size o)) 53 | [] 54 | (let [f (if (is-entity? (.get o 0)) from-entity deserialize-doc) 55 | n (.size o)] 56 | (loop [i 0 57 | xs (transient [])] 58 | (if (= i n) 59 | (persistent! xs) 60 | (recur 61 | (inc i) 62 | (conj! xs (f (.get o i))))))))) 63 | 64 | (defmethod from-entity Enum [o] (str o)) 65 | 66 | (defmethod from-entity MultiDocumentEntity 67 | [^MultiDocumentEntity o] 68 | (-> o 69 | bean 70 | (dissoc :documentsAndErrors) 71 | (update :documents as-entity-vec-or-deserialized) 72 | (update :errors as-entity-vec-or-deserialized))) 73 | 74 | (defmethod from-entity ArrayList 75 | [^ArrayList o] 76 | (if (= 0 (.size o)) 77 | [] 78 | (let [x (.get o 0)] 79 | (if (is-entity? x) 80 | (let [n (.size o)] 81 | (loop [i 0 82 | xs (transient [])] 83 | (if (= i n) 84 | (persistent! xs) 85 | (recur 86 | (inc i) 87 | (conj! xs (from-entity (.get o i))))))) 88 | (vec o))))) 89 | 90 | (defmethod from-entity :default [o] 91 | (if-not (is-entity? o) 92 | o 93 | (persistent! 94 | (reduce (fn [m [k v]] 95 | (assoc! m k (from-entity v))) 96 | (transient {}) (bean o))))) 97 | -------------------------------------------------------------------------------- /src/clojure/clj_arangodb/arangodb/admin.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.admin) 2 | 3 | (defn version 4 | "can be called on a `conn` or `database`, retuns a map with the keys 5 | `:liscense`, `:server`, `:version`" 6 | [conn-or-db] 7 | (-> (.getVersion conn-or-db) 8 | bean 9 | (dissoc :class) 10 | (update :license str))) 11 | 12 | (defn users 13 | "returns a map with user names as keys. Vals are maps with additional info" 14 | [conn] 15 | (reduce (fn [user-map obj] 16 | (let [user (-> obj bean (dissoc :class))] 17 | (assoc user-map (:user user) user))) {} (.getUsers conn))) 18 | -------------------------------------------------------------------------------- /src/clojure/clj_arangodb/arangodb/aql.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.aql 2 | (:require [clojure.string :as str])) 3 | 4 | (defmulti serialize class) 5 | (defmulti aql-lang (fn [x & _] x)) 6 | 7 | ;;; janky handling for option maps for graphs 8 | 9 | (defn- parse-traversal-options 10 | [{bfs :bfs {v :vertices e :edges} :unique :as options}] 11 | (if (empty? options) 12 | "" 13 | (format "OPTIONS {%s%s%s}" 14 | (if-not bfs 15 | "" 16 | "bfs : true, ") 17 | (if-not v 18 | "" 19 | (str "uniqueVertices : \"" (name v) "\", ")) 20 | (if-not e 21 | "" 22 | (str "uniqueEdges : \"" (name e) "\""))))) 23 | 24 | (defn- parse-graph-or-collections [graph collections] 25 | (if graph 26 | (str "GRAPH " (serialize graph)) 27 | (str/join "," (for [c collections] 28 | (if-not (sequential? c) 29 | (serialize c) 30 | (str (str/upper-case (name (first c))) " " 31 | (serialize (second c)))))))) 32 | 33 | (defn- parse-shortest-path 34 | [{:keys [type start shortest-path graph collections options] 35 | :as traversal}] 36 | (let [type (str/upper-case (name type))] 37 | (format "%s SHORTEST_PATH %s TO %s %s %s" 38 | type 39 | (serialize start) 40 | (serialize shortest-path) 41 | (parse-graph-or-collections graph collections) 42 | (parse-traversal-options options)))) 43 | 44 | (defn- format-aql-traversal 45 | [o] (if-not (map? o) 46 | (serialize o) 47 | (let [{:keys [depth type start shortest-path graph collections options]} o] 48 | (if shortest-path 49 | (parse-shortest-path o) 50 | (let [mn (or (first depth) 1) 51 | mx (or (second depth) mn)] 52 | (format "%s %s %s %s %s" 53 | (str mn ".." mx) 54 | (str/upper-case (name type)) 55 | (serialize start) 56 | (parse-graph-or-collections graph collections) 57 | (parse-traversal-options options))))))) 58 | 59 | (defmethod serialize nil [o] "null") 60 | (defmethod serialize :default [o] o) 61 | 62 | (defmethod serialize clojure.lang.PersistentVector 63 | ;; if a vetcor has a keyword in position 0 then it is 64 | ;; treated as a language construct 65 | [o] 66 | (if (keyword? (first o)) 67 | (apply aql-lang o) 68 | (format "[%s]" (str/join "," (map serialize o))))) 69 | 70 | (defmethod serialize clojure.lang.APersistentMap 71 | [o] 72 | (let [serialize-entry (fn [[k v]] 73 | (format "%s:%s" (name k) (serialize v)))] 74 | (format "{%s}" (str/join "," (map serialize-entry o))))) 75 | 76 | (defmethod serialize clojure.lang.APersistentSet 77 | [o] 78 | (format "[%s]" (str/join "," (map serialize o)))) 79 | 80 | (defmethod aql-lang :WITH 81 | ([op collections & body] 82 | (let [lables (str/join "," collections) 83 | parsed-body (str/join "\n" (map serialize body))] 84 | (format "(WITH %s\n%s)" lables parsed-body)))) 85 | 86 | (defmethod aql-lang :FOR 87 | [_ bindings & body] 88 | (let [params (butlast bindings) 89 | expr (last bindings)] 90 | (format "FOR %s IN %s\n%s" 91 | (str/join "," (map serialize params)) 92 | (format-aql-traversal expr) 93 | (str/join "\n" (map serialize body))))) 94 | 95 | (defmethod aql-lang :LET 96 | [op bindings & body] 97 | (let [var-expr-pairs (partition 2 bindings) 98 | parsed-bindings (str/join "\n" 99 | (for [[v e] var-expr-pairs] 100 | (format "LET %s = %s" v (serialize e)))) 101 | parsed-body (str/join "\n" (map serialize body))] 102 | (str parsed-bindings "\n" parsed-body))) 103 | 104 | (defmethod aql-lang :COLLECT 105 | ([op bindings & [{into-expr :into keep-vars :keep options :options :as extra}]] 106 | (let [parsed-bindings (str/join "," 107 | (for [[v e] (partition 2 bindings)] 108 | (format "%s = %s" v (serialize e)))) 109 | parsed-into (when into-expr 110 | (if-not (vector? into-expr) 111 | (name into-expr) 112 | (format "%s = %s" 113 | (name (first into-expr)) 114 | (serialize (second into-expr))))) 115 | parsed-keep-vars (when keep-vars 116 | (if-not (vector? keep-vars) 117 | (name keep-vars) 118 | (str/join "," (map name keep-vars)))) 119 | parsed-options (when options 120 | (serialize options))] 121 | (cond-> (format "COLLECT %s" parsed-bindings) 122 | parsed-into (#(format "%s INTO %s" % parsed-into)) 123 | parsed-keep-vars (#(format "%s KEEP %s" % parsed-keep-vars)) 124 | parsed-options (#(format "%s OPTIONS %s" % parsed-options)))))) 125 | 126 | (defmethod aql-lang :RETURN 127 | [op expression] 128 | (format "RETURN %s" (serialize expression))) 129 | 130 | (defmethod aql-lang :RETURN-DISTINCT 131 | [op expression] 132 | (format "RETURN DISTINCT %s" (serialize expression))) 133 | 134 | (defmethod aql-lang :UPDATE 135 | [_ document-key object collection] 136 | (format "UPDATE %s WITH %s IN %s" 137 | (serialize document-key) 138 | (serialize object) 139 | (serialize collection))) 140 | 141 | (defmethod aql-lang :REPLACE 142 | [_ document-key object collection] 143 | (format "REPLACE %s WITH %s IN %s" 144 | (serialize document-key) 145 | (serialize object) 146 | (serialize collection))) 147 | 148 | (defmethod aql-lang :REMOVE 149 | [_ document-key collection] 150 | (format "REMOVE %s IN %s" 151 | (serialize document-key) 152 | (serialize collection))) 153 | 154 | (defmethod aql-lang :INSERT 155 | [op document coll] 156 | (format "INSERT %s INTO %s" 157 | (serialize document) 158 | (serialize coll))) 159 | 160 | (defmethod aql-lang :FILTER 161 | [op expr] (format "FILTER %s" (serialize expr))) 162 | 163 | (defmethod aql-lang :SORT 164 | [op & exprs] 165 | (format "SORT %s" (str/join 166 | "," 167 | (map (fn [x] 168 | (if-not (vector? x) 169 | (str x) 170 | (let [k (first x) 171 | direction (second x)] 172 | (str x (str/upper-case (name direction)))))) 173 | exprs)))) 174 | 175 | (defmethod aql-lang :LIMIT 176 | ([op cnt] (format "LIMIT %s" cnt)) 177 | ([op offset cnt] (format "LIMIT %s, %s" offset cnt))) 178 | 179 | ;;; infix 180 | 181 | (defmethod aql-lang :AND 182 | [op expr-1 expr-2] 183 | (format "(%s AND %s)" (serialize expr-1) (serialize expr-2))) 184 | 185 | (defmethod aql-lang :OR 186 | [op expr-1 expr-2] 187 | (format "(%s OR %s)" (serialize expr-1) (serialize expr-2))) 188 | 189 | (defmethod aql-lang :EQ 190 | [op expr-1 expr-2] 191 | (format "(%s == %s)" (serialize expr-1) (serialize expr-2))) 192 | 193 | (defmethod aql-lang :LT 194 | [op expr-1 expr-2] 195 | (format "(%s < %s)" (serialize expr-1) (serialize expr-2))) 196 | 197 | (defmethod aql-lang :GT 198 | [op expr-1 expr-2] 199 | (format "(%s > %s)" (serialize expr-1) (serialize expr-2))) 200 | 201 | (defmethod aql-lang :LTEQ 202 | [op expr-1 expr-2] 203 | (format "(%s <= %s)" (serialize expr-1) (serialize expr-2))) 204 | 205 | (defmethod aql-lang :GTEQ 206 | [op expr-1 expr-2] 207 | (format "(%s >= %s)" (serialize expr-1) (serialize expr-2))) 208 | 209 | (defmethod aql-lang :NE 210 | [op expr-1 expr-2] 211 | (format "(%s != %s)" (serialize expr-1) (serialize expr-2))) 212 | 213 | (defmethod aql-lang :default 214 | [op & args] 215 | (format "%s(%s)" (name op) (str/join "," (map serialize args)))) 216 | -------------------------------------------------------------------------------- /src/clojure/clj_arangodb/arangodb/collections.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.collections 2 | (:require [clj-arangodb.arangodb.adapter :as ad] 3 | [clj-arangodb.arangodb.options :as options]) 4 | (:import [java.util 5 | ArrayList 6 | Collection] 7 | [com.arangodb 8 | ArangoCollection] 9 | [com.arangodb.entity 10 | CollectionEntity 11 | IndexEntity 12 | MultiDocumentEntity 13 | DocumentCreateEntity 14 | DocumentUpdateEntity 15 | DocumentDeleteEntity 16 | DocumentImportEntity 17 | CollectionPropertiesEntity 18 | CollectionRevisionEntity] 19 | [com.arangodb.model 20 | CollectionPropertiesOptions 21 | SkiplistIndexOptions 22 | GeoIndexOptions 23 | FulltextIndexOptions 24 | PersistentIndexOptions 25 | HashIndexOptions 26 | DocumentCreateOptions 27 | DocumentReadOptions 28 | DocumentUpdateOptions 29 | DocumentDeleteOptions 30 | DocumentReplaceOptions 31 | DocumentImportOptions]) 32 | (:refer-clojure :exclude [drop load])) 33 | 34 | (defn get-info ^CollectionEntity 35 | [^ArangoCollection coll] 36 | (ad/from-entity (.getInfo coll))) 37 | 38 | (defn get-properties ^CollectionPropertiesEntity 39 | [^ArangoCollection coll] 40 | (ad/from-entity (.getProperties coll))) 41 | 42 | (defn get-revision ^CollectionRevisionEntity 43 | [^ArangoCollection coll] 44 | (ad/from-entity (.getRevision coll))) 45 | 46 | (defn exists? ^Boolean 47 | [^ArangoCollection coll] 48 | (.exists coll)) 49 | 50 | (defn rename ^CollectionEntity 51 | [^ArangoCollection coll ^String new-name] 52 | (ad/from-entity (.rename coll new-name))) 53 | 54 | (defn load ^CollectionEntity 55 | [^ArangoCollection coll] 56 | (ad/from-entity (.load coll))) 57 | 58 | (defn unload ^CollectionEntity 59 | [^ArangoCollection coll] 60 | (ad/from-entity (.unload coll))) 61 | 62 | (defn change-properties 63 | ^CollectionEntity 64 | [^ArangoCollection coll ^CollectionPropertiesOptions options] 65 | (ad/from-entity (.changeProperties coll (options/build CollectionPropertiesOptions options)))) 66 | 67 | ;; (defn truncate ^CollectionEntity 68 | ;; [^ArangoCollection coll] 69 | ;; (ad/from-entity (.tuncate coll))) 70 | 71 | (defn drop ;;void 72 | ([^ArangoCollection coll] (.drop coll)) 73 | ([^ArangoCollection coll ^Boolean flag] (.drop coll flag))) 74 | 75 | (defn ensure-hash-index ^IndexEntity 76 | [^ArangoCollection coll ^java.lang.Iterable fields ^HashIndexOptions options] 77 | (ad/from-entity (.ensureHashIndex coll fields (options/build HashIndexOptions options)))) 78 | 79 | (defn ^IndexEntity ensure-skip-list-index 80 | [^ArangoCollection coll ^java.lang.Iterable fields ^SkiplistIndexOptions options] 81 | (ad/from-entity (.ensureSkiplistIndex coll fields (options/build SkiplistIndexOptions options)))) 82 | 83 | (defn ensure-geo-index ^IndexEntity 84 | [^ArangoCollection coll ^java.lang.Iterable fields ^GeoIndexOptions options] 85 | (ad/from-entity (.ensureGeoIndex coll fields (options/build GeoIndexOptions options)))) 86 | 87 | (defn ensure-full-text-index ^IndexEntity 88 | [^ArangoCollection coll ^java.lang.Iterable fields ^FulltextIndexOptions options] 89 | (ad/from-entity (.ensureFulltextIndex coll fields (options/build FulltextIndexOptions options)))) 90 | 91 | (defn ensure-persistent-index ^IndexEntity 92 | [^ArangoCollection coll ^java.lang.Iterable fields ^PersistentIndexOptions options] 93 | (ad/from-entity (.ensurePersistentIndex coll fields (options/build PersistentIndexOptions options)))) 94 | 95 | (defn get-index ^IndexEntity 96 | [^ArangoCollection coll ^String index] 97 | (ad/from-entity (.getIndex coll index))) 98 | 99 | (defn get-indexes ^java.util.Collection 100 | ;; collection of IndexEntity 101 | [^ArangoCollection coll] 102 | (map ad/from-entity (.getIndexes coll))) 103 | 104 | (defn delete-index ^String 105 | [^ArangoCollection coll ^String index] 106 | (.deleteIndex coll index)) 107 | 108 | (defn get-document 109 | " 110 | Class represents the class of the returned document. 111 | `String` will return a json encoding 112 | `VpackSlice` will return a arangodb velocypack slice 113 | `BaseDocument` will return a java object 114 | `Map` will return a java map 115 | " 116 | ([^ArangoCollection coll ^String key] 117 | (get-document coll key ad/*default-doc-class*)) 118 | ([^ArangoCollection coll ^String key ^Class as] 119 | (ad/deserialize-doc (.getDocument coll key as))) 120 | ([^ArangoCollection coll ^String key ^Class as ^DocumentReadOptions options] 121 | (ad/deserialize-doc (.getDocument coll key as (options/build DocumentReadOptions options))))) 122 | 123 | (defn get-documents 124 | " 125 | Class represents the class of the returned document. 126 | `String` will return a json encoding 127 | `VpackSlice` will return a arangodb velocypack slice 128 | `BaseDocument` will return a java object 129 | `Map` will return a java map 130 | " 131 | ^MultiDocumentEntity 132 | ([^ArangoCollection coll ^Collection keys] 133 | (get-documents coll keys ad/*default-doc-class*)) 134 | ([^ArangoCollection coll ^Collection keys ^Class as] 135 | (ad/from-entity (.getDocuments coll keys as)))) 136 | 137 | (defn insert-document ^DocumentCreateEntity 138 | ([^ArangoCollection coll ^Object doc] 139 | (ad/from-entity (.insertDocument coll (ad/serialize-doc doc)))) 140 | ([^ArangoCollection coll ^Object doc ^DocumentCreateOptions options] 141 | (ad/from-entity (.insertDocument coll (ad/serialize-doc doc) 142 | (options/build DocumentCreateOptions options))))) 143 | 144 | (defn insert-documents ^MultiDocumentEntity 145 | ([^ArangoCollection coll docs] 146 | (ad/from-entity (.insertDocuments coll ^Collection (map ad/serialize-doc docs)))) 147 | ([^ArangoCollection coll docs ^DocumentCreateOptions options] 148 | (ad/from-entity (.insertDocuments coll ^Collection (map ad/serialize-doc docs) 149 | (options/build DocumentCreateOptions options))))) 150 | 151 | ;; (defn import-documents ^DocumentImportEntity 152 | ;; ([^ArangoCollection coll ^Collection docs] 153 | ;; (ad/from-entity (.importDocuments coll docs))) 154 | ;; ([^ArangoCollection coll ^Collection docs ^DocumentImportOptions options] 155 | ;; (ad/from-entity (.importDocuments coll docs (options/build DocumentImportOptions options))))) 156 | 157 | (defn update-document ^DocumentUpdateEntity 158 | ([^ArangoCollection coll ^String key ^Object doc] 159 | (ad/from-entity (.updateDocument coll key (ad/serialize-doc doc)))) 160 | ([^ArangoCollection coll ^String key doc ^DocumentUpdateOptions options] 161 | (ad/from-entity (.updateDocument coll key (ad/serialize-doc doc) 162 | (options/build DocumentUpdateOptions options))))) 163 | 164 | (defn update-documents ^MultiDocumentEntity 165 | ([^ArangoCollection coll docs] 166 | (ad/from-entity (.updateDocuments coll ^Collection (map ad/serialize-doc docs)))) 167 | ([^ArangoCollection coll docs ^DocumentUpdateOptions options] 168 | (ad/from-entity (.updateDocuments coll ^Collection (map ad/serialize-doc docs) 169 | (options/build DocumentUpdateOptions options))))) 170 | 171 | (defn replace-document ^DocumentUpdateEntity 172 | ([^ArangoCollection coll ^String key ^Object doc] 173 | (ad/from-entity (.replaceDocument coll key doc))) 174 | ([^ArangoCollection coll ^String key ^Object doc ^DocumentReplaceOptions options] 175 | (ad/from-entity (.replaceDocument coll key doc (options/build DocumentReplaceOptions options))))) 176 | 177 | (defn replace-documents ^MultiDocumentEntity 178 | ([^ArangoCollection coll docs] 179 | (ad/from-entity (.replaceDocuments coll ^Collection (map ad/serialize-doc docs)))) 180 | ([^ArangoCollection coll docs ^DocumentReplaceOptions options] 181 | (ad/from-entity (.replaceDocuments coll ^Collection (map ad/serialize-doc docs) 182 | (options/build DocumentReplaceOptions options))))) 183 | 184 | (defn delete-document ^DocumentDeleteEntity 185 | ([^ArangoCollection coll ^String key] 186 | (ad/from-entity (.deleteDocument coll key))) 187 | ([^ArangoCollection coll ^String key ^Class as ^DocumentDeleteOptions options] 188 | (ad/from-entity (.deleteDocument coll key as (options/build DocumentDeleteOptions options))))) 189 | 190 | (defn delete-documents ^MultiDocumentEntity 191 | ([^ArangoCollection coll ^Collection keys] 192 | (ad/from-entity (.deleteDocuments coll keys))) 193 | ([^ArangoCollection coll ^Collection keys ^Class as ^DocumentDeleteOptions options] 194 | (ad/from-entity (.deleteDocuments coll keys as 195 | (options/build DocumentDeleteOptions options))))) 196 | -------------------------------------------------------------------------------- /src/clojure/clj_arangodb/arangodb/core.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.core 2 | (:require [clojure.set :as set] 3 | [clj-arangodb.arangodb.options :as options]) 4 | (:import [com.arangodb 5 | ArangoDB$Builder 6 | ArangoDB 7 | ArangoDatabase])) 8 | 9 | (defn ^ArangoDB connect 10 | " 11 | Takes an optional map that may contain the following: 12 | keys have the same names as the java methods. 13 | :host a pair default is ['127.0.0.1' 8529] 14 | :user a String default is 'root' 15 | :password String by default no password is used 16 | :use-protocol vst | http-json | http-vpack (:vst by default) 17 | :ssl-context SSlContext not used 18 | :timeout Integer | Long 19 | :chunksize Integer | Long 20 | :max-connections Integer | Long 21 | " 22 | ([] (connect {})) 23 | ([options] (.build ^ArangoDB$Builder (options/build ArangoDB$Builder options)))) 24 | 25 | (defn shutdown [^ArangoDB conn] (.shutdown conn)) 26 | 27 | (defn ^Boolean create-database 28 | "returns `true` on success else `ArangoDBException`" 29 | [^ArangoDB conn ^String db-name] 30 | (.createDatabase conn db-name)) 31 | 32 | (defn ^ArangoDatabase db 33 | "Always returns a new `ArrangoDatabase` even if no such database exists 34 | the returned object can be used if a database is created at a later time" 35 | [^ArangoDB conn ^String db-name] 36 | (.db conn db-name)) 37 | 38 | (def get-database db) 39 | 40 | (defn ^Boolean create-and-get-database 41 | "" 42 | [^ArangoDB conn ^String db-name] 43 | (do (.createDatabase conn db-name) 44 | (.db conn db-name))) 45 | 46 | (defn get-databases 47 | "returns a `vec` of strings corresponding to the names of databases" 48 | [^ArangoDB conn] (vec (.getDatabases conn))) 49 | 50 | (defn database? 51 | "returns true if `db-name` is an existsing db" 52 | [^ArangoDB conn ^String db-name] 53 | (boolean (some #{db-name} (get-databases conn)))) 54 | -------------------------------------------------------------------------------- /src/clojure/clj_arangodb/arangodb/cursor.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.cursor 2 | (:require [clj-arangodb.arangodb.adapter :as ad]) 3 | (:import [com.arangodb 4 | Predicate 5 | Function 6 | Consumer 7 | ArangoCursor] 8 | [com.arangodb.entity 9 | CursorEntity$Stats]) 10 | (:refer-clojure :exclude [next first map filter count])) 11 | 12 | (defn- as-pred ^com.arangodb.Predicate [pred] 13 | (reify com.arangodb.Predicate 14 | (test [this x] (pred x)))) 15 | 16 | (defn- as-fn ^com.arangodb.Function [f] 17 | (reify com.arangodb.Function 18 | (apply [this x] (f x)))) 19 | 20 | (defn- as-consumer ^java.util.function.Consumer [f] 21 | (reify java.util.function.Consumer 22 | (accept [this x] (f x)))) 23 | 24 | (defn has-next ^Boolean 25 | [^ArangoCursor cursor] 26 | (.hasNext cursor)) 27 | 28 | (defn get-type [^ArangoCursor cursor] 29 | (.getType cursor)) 30 | 31 | (defn next [^ArangoCursor cursor] 32 | (.next cursor)) 33 | 34 | (defn first [^ArangoCursor cursor] 35 | (.first cursor)) 36 | 37 | (defn foreach [^ArangoCursor cursor consume-fn] 38 | (.forEach cursor (as-consumer consume-fn))) 39 | 40 | (defn map ^Iterable [^ArangoCursor cursor f] 41 | (.map cursor (as-fn f))) 42 | 43 | (defn filter [^ArangoCursor cursor pred] 44 | (.filter cursor (as-pred pred))) 45 | 46 | (defn any-match [^ArangoCursor cursor pred] 47 | (.anyMatch cursor (as-pred pred))) 48 | 49 | (defn all-match [^ArangoCursor cursor pred] 50 | (.allMatch cursor (as-pred pred))) 51 | 52 | (defn none-match [^ArangoCursor cursor pred] 53 | (.noneMatch cursor (as-pred pred))) 54 | 55 | (defn collect-into [^ArangoCursor cursor target] 56 | (.collectInto cursor target)) 57 | 58 | (defn iterator [^ArangoCursor cursor] 59 | (.iterator cursor)) 60 | 61 | (defn as-list-remaining [^ArangoCursor cursor] 62 | (.asListRemaining cursor)) 63 | 64 | (defn get-count [^ArangoCursor cursor] 65 | (.getCount cursor)) 66 | 67 | (defn count ^Long 68 | [^ArangoCursor cursor] 69 | (.count cursor)) 70 | 71 | (defn get-stats ^CursorEntity$Stats 72 | [^ArangoCursor cursor] 73 | (ad/from-entity ^CursorEntity$Stats (.getStats cursor))) 74 | 75 | (defn get-warnings [^ArangoCursor cursor] 76 | (ad/from-entity (.getWarnings cursor))) 77 | 78 | (defn is-cached ^Boolean 79 | [^ArangoCursor cursor] 80 | (.isCached cursor)) 81 | -------------------------------------------------------------------------------- /src/clojure/clj_arangodb/arangodb/databases.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.databases 2 | (:require [clj-arangodb.velocypack.core :as vpack] 3 | [clj-arangodb.arangodb.graph :as graph] 4 | [clj-arangodb.arangodb.aql :as aql] 5 | [clj-arangodb.arangodb.adapter :as ad] 6 | [clj-arangodb.arangodb.options :as options]) 7 | (:import [com.arangodb 8 | ArangoDB 9 | ArangoCursor 10 | ArangoDatabase 11 | ArangoGraph 12 | ArangoCollection] 13 | [com.arangodb.entity 14 | GraphEntity 15 | CollectionEntity 16 | DatabaseEntity] 17 | [com.arangodb.model 18 | DocumentReadOptions 19 | CollectionCreateOptions 20 | CollectionsReadOptions 21 | GraphCreateOptions 22 | AqlQueryOptions]) 23 | (:refer-clojure :exclude [drop])) 24 | 25 | (defn exists? ^Boolean 26 | [^ArangoDatabase db] (.exists db)) 27 | 28 | (defn drop ^Boolean 29 | [^ArangoDatabase db] (.drop db)) 30 | 31 | (defn get-info ^DatabaseEntity 32 | [^ArangoDatabase db] (ad/from-entity (.getInfo db))) 33 | 34 | (defn get-document 35 | " 36 | Class represents the class of the returned document. 37 | `String` will return a json encoding 38 | `VpackSlice` will return a arangodb velocypack slice 39 | `BaseDocument` will return a java object 40 | " 41 | ([^ArangoDatabase db ^String id] 42 | (get-document db id ad/*default-doc-class*)) 43 | ([^ArangoDatabase db ^String id ^Class as] 44 | (ad/deserialize-doc (.getDocument db id as))) 45 | ([^ArangoDatabase db ^String id ^Class as ^DocumentReadOptions options] 46 | (ad/deserialize-doc (.getDocument db id as (options/build DocumentReadOptions options))))) 47 | 48 | (defn ^CollectionEntity create-collection 49 | "create a new collection entity" 50 | ([^ArangoDatabase db ^String coll-name] 51 | (ad/from-entity (.createCollection db coll-name))) 52 | ([^ArangoDatabase db ^String coll-name ^CollectionCreateOptions options] 53 | (ad/from-entity (.createCollection db coll-name 54 | (options/build CollectionCreateOptions options))))) 55 | 56 | (defn collection ^ArangoCollection 57 | ([^ArangoDatabase db ^String coll-name] 58 | (.collection db coll-name))) 59 | 60 | (def get-collection collection) 61 | 62 | (defn create-and-get-collection ^ArangoCollection 63 | ([^ArangoDatabase db ^String coll-name] 64 | (do (.createCollection db coll-name) 65 | (.collection db coll-name))) 66 | ([^ArangoDatabase db ^String coll-name ^CollectionCreateOptions options] 67 | (do (.createCollection db coll-name (options/build CollectionCreateOptions options)) 68 | (.collection db coll-name)))) 69 | 70 | (defn get-collections 71 | ;; returns a collection of CollectionEntity 72 | ([^ArangoDatabase db] 73 | (vec (map ad/from-entity (.getCollections db)))) 74 | ([^ArangoDatabase db ^CollectionsReadOptions options] 75 | (vec (map ad/from-entity 76 | (.getCollections db (options/build CollectionsReadOptions options)))))) 77 | 78 | (defn get-collection-names 79 | ([^ArangoDatabase db] 80 | (vec (map #(.getName ^CollectionEntity %) (.getCollections db))))) 81 | 82 | (defn get-graphs 83 | ;; returns a collection of GraphEntity 84 | [^ArangoDatabase db] 85 | (vec (map ad/from-entity (.getGraphs db)))) 86 | 87 | (defn collection-exists? [^ArangoDatabase db collection-name] 88 | (some #(= collection-name (.getName ^CollectionEntity %)) (.getCollections db))) 89 | 90 | (defn collection? [^ArangoDatabase db collection-name] 91 | (boolean (collection-exists? db collection-name))) 92 | 93 | (defn graph-exists? [^ArangoDatabase db graph-name] 94 | (some #(= graph-name (.getName ^GraphEntity %)) (.getGraphs db))) 95 | 96 | (defn create-graph 97 | "Create a new graph `graph-name`. edge-definitions must be a non empty 98 | sequence of maps `{:name 'relationName' :from ['collA'...] :to [collB...]}` 99 | if the names in sources and targets do not exist on the database, 100 | then new collections will be created." 101 | ^GraphEntity 102 | [^ArangoDatabase db ^String name edge-definitions ^GraphCreateOptions options] 103 | (ad/from-entity (.createGraph db name 104 | (map #(if (map? %) (graph/edge-definition %) %) 105 | edge-definitions) 106 | (options/build GraphCreateOptions options)))) 107 | 108 | (defn graph ^ArangoGraph 109 | ([^ArangoDatabase db ^String graph-name] 110 | (.graph db graph-name))) 111 | 112 | (def get-graph graph) 113 | 114 | (defn query ^ArangoCursor 115 | ;; can pass java.util.Map / java.util.List as well 116 | ([^ArangoDatabase db aql-query] 117 | (query db aql-query nil nil ad/*default-doc-class*)) 118 | ([^ArangoDatabase db aql-query ^Class as] 119 | (query db aql-query nil nil as)) 120 | ([^ArangoDatabase db aql-query ^AqlQueryOptions options ^Class as] 121 | (query db aql-query nil options as)) 122 | ([^ArangoDatabase db aql-query bindvars ^AqlQueryOptions options ^Class as] 123 | (.query db ^String (aql/serialize aql-query) bindvars (options/build AqlQueryOptions options) as))) 124 | -------------------------------------------------------------------------------- /src/clojure/clj_arangodb/arangodb/graph.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.graph 2 | (:require [clj-arangodb.arangodb.adapter :as ad] 3 | [clj-arangodb.arangodb.options :as options]) 4 | (:import [com.arangodb.entity 5 | GraphEntity 6 | VertexEntity 7 | EdgeEntity 8 | EdgeDefinition 9 | VertexUpdateEntity 10 | EdgeUpdateEntity] 11 | [com.arangodb 12 | ArangoGraph 13 | ArangoVertexCollection 14 | ArangoEdgeCollection] 15 | [com.arangodb.model 16 | VertexCreateOptions 17 | VertexDeleteOptions 18 | VertexReplaceOptions 19 | VertexUpdateOptions 20 | EdgeCreateOptions 21 | EdgeDeleteOptions 22 | EdgeReplaceOptions 23 | EdgeUpdateOptions 24 | DocumentCreateOptions 25 | DocumentReadOptions 26 | DocumentReplaceOptions 27 | DocumentUpdateOptions]) 28 | (:refer-clojure :exclude [drop])) 29 | 30 | (defn ^Boolean exists? [^ArangoGraph x] (.exists x)) 31 | 32 | (defn ^GraphEntity get-info [^ArangoGraph x] (ad/from-entity (.getInfo x))) 33 | 34 | (defn drop [^ArangoGraph x] (.drop x)) 35 | 36 | (defn get-vertex-collections [^ArangoGraph graph] 37 | ;; collection of strings 38 | (.getVertexCollections graph)) 39 | 40 | (defn ^ArangoVertexCollection vertex-collection 41 | "get the actual collection" 42 | [^ArangoGraph graph ^String name] 43 | (.vertexCollection graph name)) 44 | 45 | (defn ^GraphEntity add-vertex-collection [^ArangoGraph graph ^String name] 46 | ;; returns ArangoDBException Response: 400, Error: 1938 - collection used in orphans if 47 | ;; you try adding the collection twice 48 | ;; arangoDB.db("myDatabase").graph("myGraph").drop(); 49 | (ad/from-entity (.addVertexCollection graph name))) 50 | 51 | (defn get-edge-definitions [^ArangoGraph graph] 52 | ;; collection of strings 53 | (.getEdgeDefinitions graph)) 54 | 55 | (defn ^ArangoEdgeCollection edge-collection 56 | "get the actual collection" 57 | [^ArangoGraph graph ^String name] 58 | (.edgeCollection graph name)) 59 | 60 | (defn ^EdgeDefinition edge-definition 61 | [{:keys [name from to] :as edge-definition}] 62 | (-> (new EdgeDefinition) 63 | (.collection name) 64 | (.from (into-array from)) 65 | (.to (into-array to)))) 66 | 67 | (defn ^GraphEntity add-edge-definition 68 | [^ArangoGraph graph ^EdgeDefinition definition] 69 | (ad/from-entity (.addEdgeDefinition graph definition))) 70 | 71 | (defn ^GraphEntity replace-edge-definition [^ArangoGraph graph ^EdgeDefinition definition] 72 | (ad/from-entity (.replaceEdgeDefinition graph definition))) 73 | 74 | (defn ^GraphEntity remove-edge-definition [^ArangoGraph graph ^String name] 75 | (ad/from-entity (.removeEdgeDefinition graph name))) 76 | 77 | (defn ^VertexEntity insert-vertex 78 | ([^ArangoVertexCollection coll doc] 79 | (ad/from-entity (.insertVertex coll (ad/serialize-doc doc)))) 80 | ([^ArangoVertexCollection coll doc ^VertexCreateOptions options] 81 | (ad/from-entity (.insertVertex coll (ad/serialize-doc doc) 82 | (options/build VertexCreateOptions options))))) 83 | 84 | (defn get-vertex 85 | ([^ArangoVertexCollection coll key] 86 | (get-vertex coll key ad/*default-doc-class*)) 87 | ([^ArangoVertexCollection coll ^String key ^Class as] 88 | (ad/deserialize-doc (.getVertex coll key as))) 89 | ([^ArangoVertexCollection coll ^String key ^Class as ^DocumentReadOptions options] 90 | (ad/deserialize-doc (.getVertex coll key as (options/build DocumentReadOptions options))))) 91 | 92 | (defn ^VertexUpdateEntity replace-vertex 93 | ([^ArangoVertexCollection coll ^String key ^Object doc] 94 | (ad/from-entity (.replaceVertex coll key (ad/serialize-doc doc)))) 95 | ([^ArangoVertexCollection coll ^String key ^Object doc ^VertexUpdateOptions options] 96 | (ad/from-entity (.replaceVertex coll key (ad/serialize-doc doc) 97 | (options/build VertexUpdateOptions options))))) 98 | 99 | (defn ^VertexUpdateEntity update-vertex 100 | ([^ArangoVertexCollection coll ^String key ^Object doc] 101 | (ad/from-entity (.updateVertex coll key (ad/serialize-doc doc)))) 102 | ([^ArangoVertexCollection coll ^String key ^Object doc ^VertexUpdateOptions options] 103 | (ad/from-entity (.updateVertex coll key (ad/serialize-doc doc) 104 | (options/build VertexUpdateOptions options))))) 105 | 106 | (defn delete-vertex ; void 107 | ([^ArangoVertexCollection coll ^String key] 108 | (.deleteVertex coll key)) 109 | ([^ArangoVertexCollection coll ^String key ^VertexDeleteOptions options] 110 | (.deleteVertex coll key (options/build VertexDeleteOptions options)))) 111 | 112 | (defn insert-edge ^EdgeEntity 113 | ([^ArangoEdgeCollection coll doc] 114 | (ad/from-entity (.insertEdge coll (ad/serialize-doc doc)))) 115 | ([^ArangoEdgeCollection coll doc ^EdgeCreateOptions options] 116 | (ad/from-entity (.insertEdge coll (ad/serialize-doc doc) 117 | (options/build EdgeCreateOptions options))))) 118 | 119 | (defn get-edge 120 | ([^ArangoEdgeCollection coll key] 121 | (get-edge coll key ad/*default-doc-class*)) 122 | ([^ArangoEdgeCollection coll ^String key ^Class as] 123 | (ad/deserialize-doc (.getEdge coll key as))) 124 | ([^ArangoEdgeCollection coll ^String key ^Class as ^DocumentReadOptions options] 125 | (ad/deserialize-doc (.getEdge coll key as (options/build DocumentReadOptions options))))) 126 | 127 | 128 | (defn replace-edge ^EdgeUpdateEntity 129 | ([^ArangoEdgeCollection coll ^String key ^Object doc] 130 | (ad/from-entity (.replaceEdge coll key (ad/serialize-doc doc)))) 131 | ([^ArangoEdgeCollection coll ^String key ^Object doc ^EdgeUpdateOptions options] 132 | (ad/from-entity (.replaceEdge coll key (ad/serialize-doc doc) 133 | (options/build EdgeUpdateOptions options))))) 134 | 135 | (defn update-edge ^EdgeUpdateEntity 136 | ([^ArangoEdgeCollection coll ^String key ^Object doc] 137 | (ad/from-entity (.updateEdge coll key (ad/serialize-doc doc)))) 138 | ([^ArangoEdgeCollection coll ^String key ^Object doc ^EdgeUpdateOptions options] 139 | (ad/from-entity (.updateEdge coll key (ad/serialize-doc doc) 140 | (options/build EdgeUpdateOptions options))))) 141 | 142 | (defn delete-edge ; void 143 | ([^ArangoEdgeCollection coll ^String key] 144 | (.deleteEdge coll key)) 145 | ([^ArangoEdgeCollection coll ^String key ^EdgeDeleteOptions options] 146 | (.deleteEdge coll key (options/build EdgeDeleteOptions options)))) 147 | -------------------------------------------------------------------------------- /src/clojure/clj_arangodb/arangodb/options.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.options) 2 | 3 | (defn class->symbol [^Class c] 4 | (symbol (.substring (.toString c) 6))) 5 | 6 | (defn- fn-builder 7 | "given a map entry that represents a method call where the key (keyword) 8 | is the method name and the `val` are args (modulo the implicit object) 9 | returns a partial function that takes a single `object` argument." 10 | ;; to ensure that objects can be passed as args, we need to create a 11 | ;; partial function so that we do not need to eval any args directly! 12 | ;; Arguments single multiple arguments should be in a seq 13 | [class-symbol [method-key args]] 14 | (let [method-name (symbol (name method-key)) 15 | args (flatten (list args)) 16 | params (vec (repeatedly (inc (count args)) #(gensym))) 17 | ;; add meta to the object so that we dont get reflection warnings! 18 | obj-param (with-meta (peek params) {:tag class-symbol}) 19 | arg-params (pop params) 20 | f (eval `(fn ~params (. ~obj-param ~method-name ~@arg-params)))] 21 | (apply partial f args))) 22 | 23 | (defn build 24 | "given a class name and map of options create a sequence of function calls 25 | and call them sequentially using a new `object` of class `class`" 26 | [class options] 27 | (if (map? options) 28 | (reduce (fn [acc f] 29 | (f acc)) (eval `(new ~class)) 30 | (map (partial fn-builder (class->symbol class)) options)) 31 | options)) 32 | -------------------------------------------------------------------------------- /src/clojure/clj_arangodb/velocypack/core.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.velocypack.core 2 | (:require [clj-arangodb.velocypack.utils :as utils] 3 | [cheshire.core :as json]) 4 | (:import [com.arangodb.velocypack 5 | ObjectIterator 6 | ArrayIterator 7 | VPackSlice 8 | ValueType 9 | VPackBuilder 10 | VPackParser$Builder])) 11 | 12 | (defn unpack 13 | ([^VPackSlice slice key-fn] 14 | (let [builder (new VPackParser$Builder) 15 | data (.toJson (.build builder) slice)] 16 | (json/parse-string data key-fn))) 17 | ([^VPackSlice slice] 18 | (unpack slice utils/str->key))) 19 | 20 | 21 | (defn unpack-strict 22 | ([^VPackSlice slice key-fn] 23 | (let [builder (new VPackParser$Builder) 24 | data (.toJson (.build builder) slice)] 25 | (json/parse-string-strict data key-fn))) 26 | ([^VPackSlice slice] 27 | (unpack-strict slice utils/str->key))) 28 | 29 | 30 | (defprotocol VPackable 31 | (pack [this]) 32 | (add-item [this builder]) 33 | (add-entry [this k builder])) 34 | 35 | (extend-protocol VPackable 36 | 37 | (Class/forName "[B") 38 | (pack [^bytes this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^bytes this))) 39 | (add-item [^bytes this ^VPackBuilder builder] (.add builder ^bytes this)) 40 | (add-entry [^bytes this ^String k ^VPackBuilder builder] 41 | (.add builder ^String k ^bytes this)) 42 | 43 | nil 44 | ;; a hack - we pretend that we are passing an Object, but pass it nil 45 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^Integer this))) 46 | (add-item [this ^VPackBuilder builder] (.add builder ^Integer this)) 47 | (add-entry [this ^String k ^VPackBuilder builder] 48 | (.add builder ^String k ^Integer this)) 49 | 50 | String 51 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^String this))) 52 | (add-item [^String this ^VPackBuilder builder] (.add builder this)) 53 | (add-entry [^String this ^String k ^VPackBuilder builder] 54 | (.add builder ^String k this)) 55 | 56 | clojure.lang.Keyword 57 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^String (name this)))) 58 | (add-item [this ^VPackBuilder builder] (.add builder ^String (name this))) 59 | (add-entry [this ^String k ^VPackBuilder builder] 60 | (.add builder ^String k ^String (name this))) 61 | 62 | Long 63 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^Long this))) 64 | (add-item [^Long this ^VPackBuilder builder] (.add builder this)) 65 | (add-entry [^Long this ^String k ^VPackBuilder builder] 66 | (.add builder ^String k this)) 67 | 68 | VPackSlice 69 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^VPackSlice this))) 70 | (add-item [^VPackSlice this ^VPackBuilder builder] (.add builder this)) 71 | (add-entry [^VPackSlice this ^String k ^VPackBuilder builder] 72 | (.add builder ^String k this)) 73 | 74 | Short 75 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^Short this))) 76 | (add-item [^Short this ^VPackBuilder builder] (.add builder this)) 77 | (add-entry [^Short this ^String k ^VPackBuilder builder] 78 | (.add builder ^String k this)) 79 | 80 | BigInteger 81 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^BigInteger this))) 82 | (add-item [^BigInteger this ^VPackBuilder builder] (.add builder this)) 83 | (add-entry [^BigInteger this ^String k ^VPackBuilder builder] 84 | (.add builder ^String k this)) 85 | 86 | BigDecimal 87 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^BigDecimal this))) 88 | (add-item [^BigDecimal this ^VPackBuilder builder] (.add builder this)) 89 | (add-entry [^BigDecimal this ^String k ^VPackBuilder builder] 90 | (.add builder ^String k this)) 91 | 92 | java.util.Date 93 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^java.util.Date this))) 94 | (add-item [^java.util.Date this ^VPackBuilder builder] (.add builder this)) 95 | (add-entry [^java.util.Date this ^String k ^VPackBuilder builder] 96 | (.add builder ^String k this)) 97 | 98 | java.sql.Date 99 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^java.sql.Date this))) 100 | (add-item [^java.sql.Date this ^VPackBuilder builder] (.add builder this)) 101 | (add-entry [^java.sql.Date this ^String k ^VPackBuilder builder] 102 | (.add builder ^String k this)) 103 | 104 | Integer 105 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^Integer this))) 106 | (add-item [^Integer this ^VPackBuilder builder] (.add builder this)) 107 | (add-entry [^Integer this ^String k ^VPackBuilder builder] 108 | (.add builder ^String k this)) 109 | 110 | Character 111 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^Character this))) 112 | (add-item [^Character this ^VPackBuilder builder] (.add builder this)) 113 | (add-entry [^Character this ^String k ^VPackBuilder builder] 114 | (.add builder ^String k this)) 115 | 116 | Double 117 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^Double this))) 118 | (add-item [^Double this ^VPackBuilder builder] (.add builder this)) 119 | (add-entry [^Double this ^String k ^VPackBuilder builder] 120 | (.add builder ^String k this)) 121 | 122 | Float 123 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^Float this))) 124 | (add-item [^Float this ^VPackBuilder builder] (.add builder this)) 125 | (add-entry [^Float this ^String k ^VPackBuilder builder] 126 | (.add builder ^String k this)) 127 | 128 | Boolean 129 | (pack [this] ^VPackSlice (.slice ^VPackBuilder (.add (VPackBuilder.) ^Boolean this))) 130 | (add-item [^Boolean this ^VPackBuilder builder] (.add builder this)) 131 | (add-entry [^Boolean this ^String k ^VPackBuilder builder] 132 | (.add builder ^String k this)) 133 | 134 | clojure.lang.Sequential 135 | (pack [this] ^VPackSlice 136 | (.slice (.close ^VPackBuilder 137 | (reduce (fn [^VPackBuilder b x] 138 | (add-item x b)) 139 | (.add (VPackBuilder.) ^ValueType ValueType/ARRAY) 140 | this)))) 141 | (add-item [this ^VPackBuilder builder] 142 | (.close ^VPackBuilder 143 | (reduce (fn [^VPackBuilder builder elem] 144 | (add-item elem builder)) 145 | (.add builder ^ValueType ValueType/ARRAY) 146 | this))) 147 | (add-entry [this ^String k ^VPackBuilder builder] 148 | (.close ^VPackBuilder 149 | (reduce (fn [^VPackBuilder b x] 150 | (add-item x b)) 151 | (.add builder ^String k ^ValueType ValueType/ARRAY) 152 | this))) 153 | 154 | clojure.lang.IPersistentSet 155 | (pack [this] (pack (seq this))) 156 | (add-item [this builder] (add-item (seq this) builder)) 157 | (add-entry [this k builder] (add-entry (seq this) k builder)) 158 | 159 | clojure.lang.IPersistentMap 160 | (pack [this] ^VPackSlice 161 | (.slice (.close ^VPackBuilder 162 | (reduce (fn [^VPackBuilder b [k v]] 163 | (add-entry v (if (instance? clojure.lang.Keyword k) (. ^clojure.lang.Named k (getName)) 164 | (. k (toString))) b)) 165 | (.add (VPackBuilder.) ^ValueType ValueType/OBJECT) 166 | this)))) 167 | 168 | (add-item [this ^VPackBuilder builder] 169 | (.close ^VPackBuilder 170 | (reduce (fn [^VPackBuilder b [k v]] 171 | (add-entry v (if (instance? clojure.lang.Keyword k) (. ^clojure.lang.Named k (getName)) 172 | (. k (toString))) b)) 173 | (.add builder ^ValueType ValueType/OBJECT) 174 | this))) 175 | 176 | (add-entry [this ^String k ^VPackBuilder builder] 177 | (.close ^VPackBuilder 178 | (reduce (fn [^VPackBuilder b [k v]] 179 | (add-entry v (if (instance? clojure.lang.Keyword k) (. ^clojure.lang.Named k (getName)) 180 | (. k (toString))) b)) 181 | (.add builder ^String k ^ValueType ValueType/OBJECT) 182 | this)))) 183 | -------------------------------------------------------------------------------- /src/clojure/clj_arangodb/velocypack/utils.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.velocypack.utils) 2 | 3 | (defn key->str ^String [k] 4 | {:inline (fn [k] `(if (keyword? ~k) (name ~k) (str ~k)))} 5 | (if (keyword? k) (name k) (str k))) 6 | 7 | (defn str->key [^String k] 8 | (if-not (Character/isDigit (.charAt k 0)) 9 | (keyword k) 10 | (try (Integer. k) 11 | (catch Exception _ 12 | (try (Float. k) 13 | (catch Exception _ 14 | (keyword k))))))) 15 | -------------------------------------------------------------------------------- /test/clj_arangodb/arangodb/aql_test.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.aql-test 2 | (:require [clj-arangodb.arangodb.databases :as d] 3 | [clj-arangodb.arangodb.aql :as aql] 4 | [clj-arangodb.arangodb.helper :as h] 5 | [clj-arangodb.arangodb.cursor :as cursor] 6 | [clj-arangodb.arangodb.adapter :as adapter] 7 | [clojure.test :refer :all])) 8 | 9 | (deftest let-test-1 10 | (h/with-temp-db [db "testDB"] 11 | (let [qry [:LET ["x" [:SUM [1 2 3 4 5 6 7 8 9 10]] 12 | "y" [:LENGTH ["\"a\"" "\"b\"" "\"c\""]] 13 | "z" 100] 14 | [:RETURN [:SUM ["x" "y" "z"]]]]] 15 | (is (= 158 (cursor/first (d/query db qry Integer))))))) 16 | 17 | (deftest let-test-2 18 | (h/with-temp-db [db "testDB"] 19 | (let [qry [:LET ["x" [:SUM [1 2 3 4 5 6 7 8 9 10]] 20 | "y" [:LENGTH ["\"a\"" "\"b\"" "\"c\""]] 21 | "z" [1 2 3 4 5]] 22 | [:RETURN [:SUM ["x" "y" [:SUM "z"]]]]]] 23 | (is (= 73 (cursor/first (d/query db qry Integer))))))) 24 | 25 | (deftest can-encode-nested-funs 26 | (is (= "sum([sum([1,2,3]),sum([2,3,4])])" 27 | (aql/serialize [:sum [[:sum [1 2 3]] [:sum [2 3 4]]]]))) 28 | (h/with-temp-db [db "testDB"] 29 | (let [query [:return [:sum [[:sum [1 2 3]] [:sum [2 3 4]]]]]] 30 | (testing "default deserialization returns a float" 31 | (let [res (-> db 32 | (d/query query) 33 | cursor/first 34 | adapter/deserialize-doc)] 35 | (is (= 15.0 res)))) 36 | (testing "passing an Integer class returns an integer" 37 | (let [res (-> db 38 | (d/query query Integer) 39 | cursor/first)] 40 | (is (= 15 res))))))) 41 | 42 | (deftest obj-test 43 | (h/with-temp-db [db "testDB"] 44 | (let [query-1 [:LET ["a" [:SUM [1 2 3]] 45 | "b" "a"] 46 | [:RETURN {"a" [:SUM [1 2 3]] "b" "b"}]] 47 | query-2 [:LET ["a" [:SUM [1 2 3]] 48 | "b" "a"] 49 | [:RETURN {:a [:SUM [1 2 3]] :b "b"}]]] 50 | (is (= (-> (d/query db query-1) 51 | cursor/first 52 | adapter/deserialize-doc) 53 | (-> (d/query db query-2) 54 | cursor/first 55 | adapter/deserialize-doc) 56 | {:a 6.0 :b 6.0}))))) 57 | -------------------------------------------------------------------------------- /test/clj_arangodb/arangodb/collections_test.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.collections-test 2 | (:require [clojure.set :as set] 3 | [clj-arangodb.arangodb.databases :as d] 4 | [clj-arangodb.arangodb.collections :as c] 5 | [clj-arangodb.arangodb.aql :as aql] 6 | [clj-arangodb.arangodb.adapter :as adapter] 7 | [clj-arangodb.arangodb.cursor :as cursor] 8 | [clj-arangodb.arangodb.helper :as h] 9 | [clj-arangodb.arangodb.test-data :as td] 10 | [clj-arangodb.velocypack.core :as v] 11 | [clojure.test :refer :all] 12 | [cheshire.core :as json])) 13 | 14 | (defn game-of-thrones-test-fixture [tests] 15 | (td/init-game-of-thrones-db) 16 | (tests) 17 | (td/drop-game-of-thrones-db)) 18 | 19 | (use-fixtures :once game-of-thrones-test-fixture) 20 | 21 | (deftest insert-documents-test 22 | (h/with-temp-db [db "someDB"] 23 | (let [coll (d/create-and-get-collection db "someCollection") 24 | res (c/insert-documents coll [{:name "a" :age 1} {:name "b" :age 2}])] 25 | (is (= (:class res) 26 | com.arangodb.entity.MultiDocumentEntity))))) 27 | 28 | (deftest collection-size-test 29 | (h/with-db [db td/game-of-thrones-db-label] 30 | (let [query [:RETURN [:LENGTH "Characters"]]] 31 | (is (= (first (d/query db query Integer)) 32 | 43))))) 33 | 34 | (deftest neds-children-test 35 | (h/with-db [db td/game-of-thrones-db-label] 36 | (let [children #{"Robb" "Jon" "Bran" "Arya" "Sansa"} 37 | query [:FOR ["c" "Characters"] 38 | [:FILTER [:EQ "c.name" "\"Ned\""]] 39 | [:FOR ["v" {:start "c" 40 | :type :inbound 41 | :depth [1 1] 42 | :collections ["ChildOf"]}] 43 | [:RETURN "v.name"]]]] 44 | (is (= (set (d/query db query String)) 45 | children))))) 46 | 47 | (def x (json/parse-string (json/generate-string ["a" ["b" "c"]]))) 48 | 49 | (deftest group-test-1 50 | (h/with-db [db td/game-of-thrones-db-label] 51 | (is (= (->> [:FOR ["c" "Characters"] 52 | [:COLLECT ["surname" "c.surname"]] 53 | [:RETURN "surname"]] 54 | (d/query db) 55 | ((comp set (partial map adapter/deserialize-doc)))) 56 | (->> [:FOR ["c" "Characters"] 57 | [:RETURN-DISTINCT "c.surname"]] 58 | (d/query db) 59 | ((comp set (partial map adapter/deserialize-doc)))))))) 60 | 61 | (deftest group-test-2 62 | (h/with-db [db td/game-of-thrones-db-label] 63 | (let [result (->> [:FOR ["c" "Characters"] 64 | [:COLLECT 65 | ["surname" "c.surname"] {:into ["members" "c.name"]}] 66 | [:FILTER [:NE "surname" nil]] 67 | [:RETURN ["surname" "members"]]] 68 | (d/query db) 69 | (map v/unpack-strict) 70 | (into {}))] 71 | (is (= (count (get result "Lannister")) 4)) 72 | (is (= (count (get result "Stark")) 6))))) 73 | 74 | (deftest brans-parents-test 75 | (h/with-db [db td/game-of-thrones-db-label] 76 | (let [parents #{"Ned" "Catelyn"} 77 | query [:FOR ["c" "Characters"] 78 | [:FILTER [:EQ "c.name" "\"Bran\""]] 79 | [:FOR ["v" {:start "c" 80 | :type :outbound 81 | :depth [1 1] 82 | :collections ["ChildOf"]}] 83 | [:RETURN "v.name"]]]] 84 | (is (= (set (d/query db query String)) 85 | (set (map adapter/deserialize-doc (d/query db query))) 86 | parents))))) 87 | 88 | (deftest tywins-grandchildren-test 89 | (h/with-db [db td/game-of-thrones-db-label] 90 | (let [grandchildren #{"Joffrey"} 91 | query [:FOR ["c" "Characters"] 92 | [:FILTER [:EQ "c.name" "\"Tywin\""]] 93 | [:FOR ["v" {:start "c" 94 | :type :inbound 95 | :depth [2 2] 96 | :collections ["ChildOf"]}] 97 | [:RETURN-DISTINCT "v.name"]]]] 98 | (is (= (set (d/query db query String)) 99 | grandchildren))))) 100 | 101 | (deftest joffrey-parents-and-grandparents-test 102 | (h/with-db [db td/game-of-thrones-db-label] 103 | (let [grandparents #{"Tywin"} 104 | parents #{"Cersei" "Jaime"} 105 | query [:FOR ["c" "Characters"] 106 | [:FILTER [:EQ "c.name" "\"Joffrey\""]] 107 | [:FOR ["v" {:start "c" 108 | :type :outbound 109 | :depth [1 2] 110 | :collections ["ChildOf"]}] 111 | [:RETURN-DISTINCT "v.name"]]]] 112 | (is (= (set (d/query db query String)) 113 | (set/union parents grandparents)))))) 114 | -------------------------------------------------------------------------------- /test/clj_arangodb/arangodb/cursor_test.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.cursor-test 2 | (:require 3 | [clj-arangodb.arangodb.databases :as d] 4 | [clj-arangodb.arangodb.aql :as aql] 5 | [clj-arangodb.arangodb.adapter :as adapter] 6 | [clj-arangodb.arangodb.cursor :as cursor] 7 | [clj-arangodb.arangodb.helper :as h] 8 | [clojure.test :refer :all])) 9 | 10 | (deftest cursor-stats-test 11 | (h/with-temp-db [db "testDB"] 12 | (let [res (d/query db [:RETURN true]) 13 | stats (cursor/get-stats res)] 14 | (is (= (:class stats) 15 | com.arangodb.entity.CursorEntity$Stats)) 16 | (is (= (set (keys stats)) 17 | #{:class :executionTime :filtered :fullCount 18 | :scannedFull :scannedIndex :writesExecuted 19 | :writesIgnored :peakMemoryUsage}))))) 20 | 21 | (deftest cursor-count-test 22 | (h/with-temp-db [db "testDB"] 23 | (let [res (d/query db [:FOR ["x" "1..5"] [:RETURN "x"]] nil {:count true} Integer)] 24 | (is (= 5 (cursor/get-count res)))) 25 | (let [res (d/query db [:FOR ["x" "1..5"] [:RETURN "x"]])] 26 | (is (= 5 (cursor/count res)))))) 27 | 28 | (deftest cursor-cache-test 29 | (h/with-temp-db [db "testDB"] 30 | (let [res (d/query db [:FOR ["x" "1..5"] [:RETURN "x"]])] 31 | (is (= false (cursor/is-cached res)))))) 32 | 33 | (deftest cursor-collect-into-test 34 | (h/with-temp-db [db "testDB"] 35 | (let [res (d/query db [:FOR ["x" "1..5"] [:RETURN "x"]] nil nil Integer) 36 | xs (cursor/collect-into res (java.util.ArrayList.))] 37 | (is (= [1 2 3 4 5] xs)) 38 | (testing "collect-into consumes the contents of the cursor" 39 | (is (= false (cursor/has-next res))))) 40 | (let [res (d/query db [:FOR ["x" [1 1 2 2 3]] [:RETURN "x"]] nil nil Integer) 41 | xs (cursor/collect-into res (java.util.HashSet.))] 42 | (is (= #{1 2 3} xs))))) 43 | 44 | (deftest cursor-seq-test 45 | (h/with-temp-db [db "testDB"] 46 | (let [res (d/query db [:FOR ["x" "1..5"] [:RETURN "x"]] nil nil Integer) 47 | xs (seq res)] 48 | (is (= (type xs) clojure.lang.LazySeq)) 49 | (is (= [1 2 3 4 5] xs))) 50 | (testing "can map deserialize-doc" 51 | (let [res (d/query db [:FOR ["x" "1..5"] [:RETURN "x"]]) 52 | xs (map adapter/deserialize-doc res)] 53 | (is (= (type xs) clojure.lang.LazySeq)) 54 | (is (= [1 2 3 4 5] xs)))))) 55 | 56 | (deftest cursor-predicate-test 57 | (h/with-temp-db [db "testDB"] 58 | (let [res (d/query db [:FOR ["x" "1..5"] [:RETURN "x"]] nil nil Integer)] 59 | (is (= true (cursor/all-match res (partial >= 5)))) 60 | (testing "can only test once" 61 | (is (= false (cursor/all-match res nat-int?))))))) 62 | 63 | (deftest cursor-map-test 64 | (h/with-temp-db [db "testDB"] 65 | (let [res (d/query db [:FOR ["x" "1..5"] [:RETURN "x"]] nil nil Integer) 66 | xs (cursor/map res inc)] 67 | (is (= [1 2 3 4 5] (seq res))) 68 | (is (= nil (seq xs)))) 69 | (let [res (d/query db [:FOR ["x" "1..5"] [:RETURN "x"]] nil nil Integer) 70 | xs (cursor/map res inc)] 71 | (is (= [2 3 4 5 6] (seq xs))) 72 | (is (= (seq res) nil))) )) 73 | 74 | (deftest cursor-first-test 75 | (h/with-temp-db [db "testDB"] 76 | (let [res (d/query db [:FOR ["x" "1..3"] [:RETURN "x"]] nil nil Integer)] 77 | (is (= 1 (cursor/first res))) 78 | (is (= 2 (cursor/first res))) 79 | (is (= 3 (cursor/first res))) 80 | (is (nil? (cursor/first res)))))) 81 | 82 | (deftest cursor-foreach-test 83 | (h/with-temp-db [db "testDB"] 84 | (let [res (d/query db [:FOR ["x" "1..3"] [:RETURN "x"]] nil nil Integer) 85 | state (atom 0)] 86 | (cursor/foreach res (fn [x] (swap! state + x))) 87 | (is (nil? (cursor/first res))) 88 | (is (= @state 6))))) 89 | 90 | (deftest multi-batch-tests 91 | (h/with-temp-db [db "testDB"] 92 | (let [res (d/query db 93 | [:FOR ["x" "0..99"] [:RETURN "x"]] 94 | nil 95 | {:batchSize (int 5)} 96 | Integer)] 97 | (is (= (range 100) (seq res)))) 98 | (let [res (d/query db 99 | [:FOR ["x" "1..3"] [:RETURN "x"]] 100 | nil 101 | {:batchSize (int 1)} 102 | Integer)] 103 | (while (cursor/has-next res) 104 | (is (nat-int? (cursor/next res))))))) 105 | -------------------------------------------------------------------------------- /test/clj_arangodb/arangodb/databases_test.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.databases-test 2 | (:require [clj-arangodb.arangodb.databases :as d] 3 | [clj-arangodb.arangodb.core :as c] 4 | [clojure.test :refer :all] 5 | [clj-arangodb.arangodb.helper :as h])) 6 | 7 | (deftest collection-exists-test 8 | (h/with-temp-db [db "some_database"] 9 | (let [label "some_collection"] 10 | (d/create-collection db label) 11 | (is (contains? (set (d/get-collection-names db)) label)) 12 | (is (d/collection-exists? db label))))) 13 | -------------------------------------------------------------------------------- /test/clj_arangodb/arangodb/helper.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.helper 2 | (:require [clj-arangodb.arangodb.core :as a] 3 | [clj-arangodb.arangodb.databases :as d] 4 | [clj-arangodb.arangodb.collections :as c])) 5 | 6 | (defmacro with-conn 7 | [[conn spec] & body] 8 | `(let [conn# (a/connect ~spec) 9 | ~conn conn#] 10 | ~@body 11 | (a/shutdown conn#))) 12 | 13 | (defmacro with-test-conn 14 | [& body] 15 | `(let [conn# (a/connect {:user "test"})] 16 | ~@body 17 | (a/shutdown conn#))) 18 | 19 | (defmacro with-db 20 | [[db label] & body] 21 | `(let [conn# (a/connect {:user "test"}) 22 | ~db (a/db conn# ~label)] 23 | (when-not (d/exists? ~db) 24 | (a/create-database conn# ~label)) 25 | ~@body 26 | (a/shutdown conn#))) 27 | 28 | (defmacro with-temp-db 29 | [[db label] & body] 30 | `(let [conn# (a/connect {:user "test"}) 31 | ~db (a/db conn# ~label)] 32 | (try (d/drop ~db) 33 | (catch Exception _#)) 34 | (when-not (d/exists? ~db) 35 | (a/create-database conn# ~label)) 36 | ~@body 37 | (try (d/drop ~db) 38 | (catch Exception _#)) 39 | (a/shutdown conn#))) 40 | 41 | (defmacro with-db-and-coll 42 | [[db db-label 43 | coll coll-label] & body] 44 | `(let [conn# (a/connect {:user "test"}) 45 | ~db (a/db conn# ~db-label) 46 | ~coll (d/collection ~db ~coll-label)] 47 | (when-not (d/exists? ~db) 48 | (a/create-database conn# ~db-label)) 49 | (when-not (c/exists? ~coll) 50 | (d/create-collection ~db ~coll-label)) 51 | ~@body 52 | (try (d/drop ~db) 53 | (catch Exception _#)) 54 | (a/shutdown conn#))) 55 | 56 | (defmacro with-coll 57 | [[coll coll-label] & body] 58 | `(let [conn# (a/connect {:user "test"}) 59 | db-label# (str (gensym)) 60 | db# (a/db conn# db-label#) 61 | coll# (d/collection db# ~coll-label) 62 | ~coll coll#] 63 | (when-not (d/exists? db#) 64 | (a/create-database conn# db-label#)) 65 | (when-not (c/exists? coll#) 66 | (d/create-collection db# ~coll-label)) 67 | ~@body 68 | (try (d/drop db#) 69 | (catch Exception _#)) 70 | (a/shutdown conn#))) 71 | -------------------------------------------------------------------------------- /test/clj_arangodb/arangodb/test_data.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.arangodb.test-data 2 | (:require [clj-arangodb.arangodb.databases :as d] 3 | [clj-arangodb.arangodb.adapter :as adapter] 4 | [clj-arangodb.arangodb.aql :as aql] 5 | [clj-arangodb.arangodb.helper :as h])) 6 | 7 | (def ^:const game-of-thrones-db-label "gameOfThronesDB") 8 | 9 | (def ^:private characters 10 | (adapter/double-quote-strings 11 | [{:name "Robert" :surname "Baratheon" :alive false :traits ["A" "H" "C"]} 12 | {:name "Jaime" :surname "Lannister" :alive true :age 36 :traits ["A" "F" "B"]} 13 | {:name "Ned" :surname "Stark" :alive true :age 41 :traits ["A" "H" "C" "N" "P"]} 14 | {:name "Catelyn" :surname "Stark" :alive false :age 40 :traits ["D" "H" "C"]} 15 | {:name "Cersei" :surname "Lannister" :alive true :age 36 :traits ["H" "E" "F"]} 16 | {:name "Daenerys" :surname "Targaryen" :alive true :age 16 :traits ["D" "H" "C"]} 17 | {:name "Jorah" :surname "Mormont" :alive false :traits ["A" "B" "C" "F"]} 18 | {:name "Petyr" :surname "Baelish" :alive false :traits ["E" "G" "F"]} 19 | {:name "Viserys" :surname "Targaryen" :alive false :traits ["O" "L" "N"]} 20 | {:name "Jon" :surname "Snow" :alive true :age 16 :traits ["A" "B" "C" "F"]} 21 | {:name "Sansa" :surname "Stark" :alive true :age 13 :traits ["D" "I" "J"]} 22 | {:name "Arya" :surname "Stark" :alive true :age 11 :traits ["C" "K" "L"]} 23 | {:name "Robb" :surname "Stark" :alive false :traits ["A" "B" "C" "K"]} 24 | {:name "Theon" :surname "Greyjoy" :alive true :age 16 :traits ["E" "R" "K"]} 25 | {:name "Bran" :surname "Stark" :alive true :age 10 :traits ["L" "J"]} 26 | {:name "Joffrey" :surname "Baratheon" :alive false :age 19 :traits ["I" "L" "O"]} 27 | {:name "Sandor" :surname "Clegane" :alive true :traits ["A" "P" "K" "F"]} 28 | {:name "Tyrion" :surname "Lannister" :alive true :age 32 :traits ["F" "K" "M" "N"]} 29 | {:name "Khal" :surname "Drogo" :alive false :traits ["A" "C" "O" "P"]} 30 | {:name "Tywin" :surname "Lannister" :alive false :traits ["O" "M" "H" "F"]} 31 | {:name "Davos" :surname "Seaworth" :alive true :age 49 :traits ["C" "K" "P" "F"]} 32 | {:name "Samwell" :surname "Tarly" :alive true :age 17 :traits ["C" "L" "I"]} 33 | {:name "Stannis" :surname "Baratheon" :alive false :traits ["H" "O" "P" "M"]} 34 | {:name "Melisandre" :alive true :traits ["G" "E" "H"]} 35 | {:name "Margaery" :surname "Tyrell" :alive false :traits ["M" "D" "B"]} 36 | {:name "Jeor" :surname "Mormont" :alive false :traits ["C" "H" "M" "P"]} 37 | {:name "Bronn" :alive true :traits ["K" "E" "C"]} 38 | {:name "Varys" :alive true :traits ["M" "F" "N" "E"]} 39 | {:name "Shae" :alive false :traits ["M" "D" "G"]} 40 | {:name "Talisa" :surname "Maegyr" :alive false :traits ["D" "C" "B"]} 41 | {:name "Gendry" :alive false :traits ["K" "C" "A"]} 42 | {:name "Ygritte" :alive false :traits ["A" "P" "K"]} 43 | {:name "Tormund" :surname "Giantsbane" :alive true :traits ["C" "P" "A" "I"]} 44 | {:name "Gilly" :alive true :traits ["L" "J"]} 45 | {:name "Brienne" :surname "Tarth" :alive true :age 32 :traits ["P" "C" "A" "K"]} 46 | {:name "Ramsay" :surname "Bolton" :alive true :traits ["E" "O" "G" "A"]} 47 | {:name "Ellaria" :surname "Sand" :alive true :traits ["P" "O" "A" "E"]} 48 | {:name "Daario" :surname "Naharis" :alive true :traits ["K" "P" "A"]} 49 | {:name "Missandei" :alive true :traits ["D" "L" "C" "M"]} 50 | {:name "Tommen" :surname "Baratheon" :alive true :traits ["I" "L" "B"]} 51 | {:name "Jaqen" :surname "H'ghar" :alive true :traits ["H" "F" "K"]} 52 | {:name "Roose" :surname "Bolton" :alive true :traits ["H" "E" "F" "A"]} 53 | {:name "The High Sparrow" :alive true :traits ["H" "M" "F" "O"]}])) 54 | 55 | (def ^:private traits 56 | (adapter/double-quote-strings 57 | [{:_key "A" :en "strong" :de "stark"} 58 | {:_key "B" :en "polite" :de "freundlich"} 59 | {:_key "C" :en "loyal" :de "loyal"} 60 | {:_key "D" :en "beautiful" :de "schön"} 61 | {:_key "E" :en "sneaky" :de "hinterlistig"} 62 | {:_key "F" :en "experienced" :de "erfahren"} 63 | {:_key "G" :en "corrupt" :de "korrupt"} 64 | {:_key "H" :en "powerful" :de "einflussreich"} 65 | {:_key "I" :en "naive" :de "naiv"} 66 | {:_key "J" :en "unmarried" :de "unverheiratet"} 67 | {:_key "K" :en "skillful" :de "geschickt"} 68 | {:_key "L" :en "young" :de "jung"} 69 | {:_key "M" :en "smart" :de "klug"} 70 | {:_key "N" :en "rational" :de "rational"} 71 | {:_key "O" :en "ruthless" :de "skrupellos"} 72 | {:_key "P" :en "brave" :de "mutig"} 73 | {:_key "Q" :en "mighty" :de "mächtig"} 74 | {:_key "R" :en "weak" :de "schwach" }])) 75 | 76 | (def ^:private child-of 77 | (adapter/double-quote-strings 78 | [{:parent {:name "Ned" :surname "Stark"} 79 | :child {:name "Robb" :surname "Stark"}} 80 | {:parent {:name "Ned" :surname "Stark"} 81 | :child {:name "Sansa" :surname "Stark"}} 82 | {:parent {:name "Ned" :surname "Stark"} 83 | :child {:name "Arya" :surname "Stark"}} 84 | {:parent {:name "Ned" :surname "Stark"} 85 | :child {:name "Bran" :surname "Stark"}} 86 | {:parent {:name "Catelyn" :surname "Stark"} 87 | :child {:name "Robb" :surname "Stark"}} 88 | {:parent {:name "Catelyn" :surname "Stark"} 89 | :child {:name "Sansa" :surname "Stark"}} 90 | {:parent {:name "Catelyn" :surname "Stark"} 91 | :child {:name "Arya" :surname "Stark"}} 92 | {:parent {:name "Catelyn" :surname "Stark"} 93 | :child {:name "Bran" :surname "Stark" }} 94 | {:parent {:name "Ned" :surname "Stark"} 95 | :child {:name "Jon" :surname "Snow" }} 96 | {:parent {:name "Tywin" :surname "Lannister"} 97 | :child {:name "Jaime" :surname "Lannister"}} 98 | {:parent {:name "Tywin" :surname "Lannister"} 99 | :child {:name "Cersei" :surname "Lannister"}} 100 | {:parent {:name "Tywin" :surname "Lannister"} 101 | :child {:name "Tyrion" :surname "Lannister"}} 102 | {:parent {:name "Cersei" :surname "Lannister"} 103 | :child {:name "Joffrey" :surname "Baratheon"}} 104 | {:parent {:name "Jaime" :surname "Lannister"} 105 | :child {:name "Joffrey" :surname "Baratheon"}}])) 106 | 107 | (defn- drop-db [label] 108 | (h/with-db 109 | [db label] 110 | (d/drop db))) 111 | 112 | (defn drop-game-of-thrones-db [] 113 | (drop-db game-of-thrones-db-label)) 114 | 115 | (defn init-game-of-thrones-db [] 116 | (drop-game-of-thrones-db) 117 | (h/with-db 118 | [db game-of-thrones-db-label] 119 | (d/create-collection db "Characters") 120 | (d/create-collection db "Traits") 121 | (d/create-collection db "ChildOf" {:type com.arangodb.entity.CollectionType/EDGES}) 122 | (->> [:LET ["data" characters] 123 | [:FOR ["c" "data"] 124 | [:INSERT "c" "Characters"]]] 125 | (d/query db)) 126 | (->> [:LET ["data" traits] 127 | [:FOR ["t" "data"] 128 | [:INSERT "t" "Traits"]]] 129 | (d/query db)) 130 | (->> [:LET ["data" child-of] 131 | [:FOR ["rel" "data"] 132 | [:LET ["parentId" [:FIRST 133 | [:FOR ["c" "Characters"] 134 | [:FILTER [:EQ "c.name" "rel.parent.name"]] 135 | [:FILTER [:EQ "c.surname" "rel.parent.surname"]] 136 | [:LIMIT 1] 137 | [:RETURN "c._id"]]] 138 | "childId" [:FIRST 139 | [:FOR ["c" "Characters"] 140 | [:FILTER [:EQ "c.name" "rel.child.name"]] 141 | [:FILTER [:EQ "c.surname" "rel.child.surname"]] 142 | [:LIMIT 1] 143 | [:RETURN "c._id"]]]] 144 | [:FILTER [:AND [:NE "parentId" nil] [:NE "childId" nil]]] 145 | [:INSERT {:_from "childId" :_to "parentId"} "ChildOf"] 146 | [:RETURN "NEW"]]]] 147 | (d/query db)))) 148 | -------------------------------------------------------------------------------- /test/clj_arangodb/velocypack/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns clj-arangodb.velocypack.core-test 2 | (:require [clj-arangodb.velocypack.core :as v] 3 | [clojure.test :refer :all])) 4 | 5 | (deftest pack-unpack-test 6 | (let [id (comp v/unpack v/pack)] 7 | (doseq [datum [1 8 | 8172 9 | 1872.283 10 | true 11 | false 12 | nil 13 | "hello world" 14 | [1 "foo" 2 "bar" {:a "a" :b 2}] 15 | {:a "a" :b "b"} 16 | {:a 1 :b 2} 17 | {:a "a" :b 2} 18 | {1 "one" 2 "two" 3.0 "three"} 19 | {:a "a" :b 2 :d {:e 3 :f "g"}}]] 20 | (is (= datum (id datum)))))) 21 | --------------------------------------------------------------------------------