Initial bits of monger.gridfs
This commit is contained in:
parent
3c813132d8
commit
eab4405012
4 changed files with 122 additions and 2 deletions
|
|
@ -28,7 +28,7 @@
|
|||
[org.bson.types ObjectId]))
|
||||
|
||||
(defprotocol ConvertToDBObject
|
||||
(^DBObject to-db-object [input] "Converts given piece of Clojure data to BasicDBObject MongoDB Java driver uses"))
|
||||
(to-db-object [input] "Converts given piece of Clojure data to BasicDBObject MongoDB Java driver uses"))
|
||||
|
||||
(extend-protocol ConvertToDBObject
|
||||
nil
|
||||
|
|
|
|||
77
src/monger/gridfs.clj
Normal file
77
src/monger/gridfs.clj
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
(ns monger.gridfs
|
||||
(:refer-clojure :exclude [remove])
|
||||
(:require [monger.core]
|
||||
[clojure.java.io :as io])
|
||||
(:use [monger.conversion])
|
||||
(:import [com.mongodb DBObject]
|
||||
[com.mongodb.gridfs GridFS GridFSInputFile]))
|
||||
|
||||
;;
|
||||
;; Implementation
|
||||
;;
|
||||
|
||||
(def
|
||||
^{:doc "Type object for a Java primitive byte array."
|
||||
:private true
|
||||
}
|
||||
byte-array-type (class (make-array Byte/TYPE 0)))
|
||||
|
||||
(def ^:dynamic *chunk-size* (* 2 1024 1024))
|
||||
|
||||
(defn filename
|
||||
[m ^String s]
|
||||
(merge m { :filename s }))
|
||||
|
||||
(defn content-type
|
||||
[m ^String s]
|
||||
(merge m { :content-type s }))
|
||||
|
||||
(defn- exec
|
||||
[{ :keys [input filename content-type] }]
|
||||
)
|
||||
|
||||
;; ...
|
||||
|
||||
|
||||
|
||||
;;
|
||||
;; API
|
||||
;;
|
||||
|
||||
|
||||
(defn remove
|
||||
([]
|
||||
(remove {}))
|
||||
([query]
|
||||
(.remove ^GridFS monger.core/*mongodb-gridfs* ^DBObject (to-db-object query))))
|
||||
|
||||
(defn remove-all
|
||||
[]
|
||||
(remove {}))
|
||||
|
||||
(defn all-files
|
||||
([]
|
||||
(.getFileList ^GridFS monger.core/*mongodb-gridfs*))
|
||||
([query]
|
||||
(.getFileList ^GridFS monger.core/*mongodb-gridfs* query)))
|
||||
|
||||
|
||||
(defprotocol GridFSInputFileFactory
|
||||
(^GridFSInputFile make-input-file [input] "Makes GridFSInputFile out of given input"))
|
||||
|
||||
(extend byte-array-type
|
||||
GridFSInputFileFactory
|
||||
{ :make-input-file (fn [^bytes input]
|
||||
(.createFile ^GridFS monger.core/*mongodb-gridfs* input)) })
|
||||
|
||||
(extend-protocol GridFSInputFileFactory
|
||||
String
|
||||
(make-input-file [^String input]
|
||||
(.createFile ^GridFS monger.core/*mongodb-gridfs* (.getBytes input))))
|
||||
|
||||
|
||||
|
||||
|
||||
(defmacro store
|
||||
[^GridFSInputFile input & body]
|
||||
`(.save ^GridFSInputFile (doto ~input ~@body) *chunk-size*))
|
||||
42
test/monger/test/gridfs.clj
Normal file
42
test/monger/test/gridfs.clj
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
(ns monger.test.gridfs
|
||||
(:refer-clojure :exclude [count remove find])
|
||||
(:use [clojure.test]
|
||||
[monger.core :only [count]]
|
||||
[monger.test.fixtures]
|
||||
[monger.operators]
|
||||
[monger.gridfs :only (store make-input-file)])
|
||||
(:require [monger.gridfs :as gridfs]
|
||||
[monger.test.helper :as helper]
|
||||
[clojure.java.io :as io])
|
||||
(:import [java.io InputStream]
|
||||
[com.mongodb.gridfs GridFS GridFSInputFile]))
|
||||
|
||||
|
||||
(defn purge-gridfs
|
||||
[f]
|
||||
(gridfs/remove-all)
|
||||
(f)
|
||||
(gridfs/remove-all))
|
||||
|
||||
(use-fixtures :each purge-gridfs)
|
||||
|
||||
(helper/connect!)
|
||||
|
||||
|
||||
|
||||
(deftest test-storing-strings-to-gridfs
|
||||
(let [input "A string"]
|
||||
(is (= 0 (count (gridfs/all-files))))
|
||||
(gridfs/store (gridfs/make-input-file input)
|
||||
(.setFilename "monger.test.gridfs.file1")
|
||||
(.setContentType "application/octet-stream"))
|
||||
(is (= 1 (count (gridfs/all-files))))))
|
||||
|
||||
|
||||
(deftest test-storing-bytes-to-gridfs
|
||||
(let [input (.getBytes "A string")]
|
||||
(is (= 0 (count (gridfs/all-files))))
|
||||
(store (make-input-file input)
|
||||
(.setFilename "monger.test.gridfs.file1")
|
||||
(.setContentType "application/octet-stream"))
|
||||
(is (= 1 (count (gridfs/all-files))))))
|
||||
|
|
@ -6,7 +6,8 @@
|
|||
(:import [org.joda.time DateTime ReadableInstant]
|
||||
[org.joda.time.format ISODateTimeFormat]
|
||||
[java.io StringWriter PrintWriter]
|
||||
[org.bson.types ObjectId])
|
||||
[org.bson.types ObjectId]
|
||||
[com.mongodb DBObject])
|
||||
(:require [clojure.data.json :as json]
|
||||
[clj-time.core :as t]))
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue