Skip to content

Commit

Permalink
Merge pull request #6 from naryl/indexes
Browse files Browse the repository at this point in the history
Indexes
  • Loading branch information
archimag committed Sep 26, 2013
2 parents 13dd63d + c318145 commit 62e583c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
1 change: 1 addition & 0 deletions driver/collection.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
(defclass collection ()
((database :initarg :database :reader collection-database)
(name :initarg :name :reader collection-name)
(indexes :initform nil :accessor collection-indexes)
(fullname :reader fullname)))

(defun collection (database name)
Expand Down
54 changes: 46 additions & 8 deletions driver/indexes.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,50 @@

(in-package #:mongo-cl-driver)

;; TODO
;;
;; createIndex
;; ensureIndex
;; dropIndex
;; reIndex
;; indexInformation
;; indexes
(defun make-name (keys)
(format nil "~{~A_~A~^_~}" (hash-table-plist keys)))

(defun index-collection (collection)
(collection (collection-database collection)
"system.indexes"))

(defun create-index (collection keys &optional (options (son)))
(let ((index (copy-hash-table options))
(index-collection (index-collection collection))
(name (make-name keys)))
(setf (gethash "key" index) keys
(gethash "ns" index) (fullname collection)
(gethash "name" index) (gethash "name" index name))
(pushnew name (collection-indexes collection) :test #'equal)
(insert-op index-collection index)))

(defun ensure-index (collection keys &optional (options (son)))
(unless (find (make-name keys)
(collection-indexes collection)
:test #'equal)
(create-index collection keys options)))

(defun drop-indexes (collection &optional (name "*"))
(setf (collection-indexes collection) nil)
(let ((cmd (son "dropIndexes" (collection-name collection)
"index" name)))
(maybe-finished
(alet ((reply (run-command (collection-database collection) cmd)))
(case (truncate (gethash "ok" reply))
(0 (values nil (gethash "errmsg" reply)))
(1 t))))))

(defun reindex (collection)
(let ((cmd (son "reIndex" (collection-name collection))))
(maybe-finished
(alet ((reply (run-command (collection-database collection) cmd)))
reply))))

(defun index-information (collection name)
(find-one (index-collection collection)
:query (son "ns" (fullname collection)
"name" name)))

(defun indexes (collection)
(find-list (index-collection collection)
:query (son "ns" (fullname collection))))
8 changes: 8 additions & 0 deletions driver/packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,12 @@
#:iterate-cursor
#:docursor
#:with-cursor

;; indexes
#:create-index
#:ensure-index
#:drop-indexes
#:reindex
#:index-information
#:indexes
))

0 comments on commit 62e583c

Please sign in to comment.