Skip to content

Commit

Permalink
20100117 fixed exports
Browse files Browse the repository at this point in the history
  • Loading branch information
fons committed Jan 18, 2010
1 parent ce94dd3 commit 2c905d3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/document
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bson.lisp: (ht->document (to-hash-map array)))
bson.lisp: (values (ht->document ht) rest))))
save-document.lisp:(defun ht->document (ht)
45 changes: 45 additions & 0 deletions src/document.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,48 @@
(print-hash (elements document) stream)
"no elements set..")
(format stream "}~%"))

(defun ht->document (ht)
(multiple-value-bind (oid oid-supplied) (gethash "_id" ht)
(let ((doc (make-document :oid (if oid-supplied oid nil))))
(when oid-supplied (remhash "_id" ht))
(with-hash-table-iterator (iterator ht)
(dotimes (repeat (hash-table-count ht))
(multiple-value-bind (exists-p key value) (iterator)
(if exists-p (add-element key value doc)))))
doc)))


(defgeneric bson-encode-container ( container &key )
(:documentation "encode a container of key-value pairs.."))

;;(defmethod bson-encode-container ( (container array) )
;; container)

(defmethod bson-encode-container ( (container document) &key (array nil) (size 10) )
(setf (gethash "_id" (elements container)) (_id container))
(bson-encode-container (elements container) :array array :size size))

(defmethod bson-encode-container ( (container hash-table) &key (array nil) (size nil) )
(let* ((size (or size 10))
(array (or array (make-octet-vector size))))
(add-octets (int32-to-octet 0) array )
(with-hash-table-iterator (iterator container)
(dotimes (repeat (hash-table-count container))
(multiple-value-bind (exists-p key value) (iterator)
(if exists-p (add-octets (bson-encode key value) array :start 4 :from-end 1)))))
(normalize-array array)))

(defmethod bson-encode ( (key string) (value hash-table) &key (array nil array-supplied-p)
(size 10 size-supplied-p)
(type nil) (encoder nil))
(declare (ignore encoder) (ignore array) (ignore type) (ignore size)
(ignore size-supplied-p) (ignore array-supplied-p) )
(bson-encode key (bson-encode-container value :array (make-octet-vector (* (hash-table-count value) 12)))))

(defmethod bson-encode ( (key string) (value document) &key (array nil array-supplied-p)
(size 10 size-supplied-p)
(type nil) (encoder nil))
(declare (ignore encoder) (ignore array) (ignore type) (ignore size)
(ignore size-supplied-p) (ignore array-supplied-p) )
(bson-encode key (bson-encode-container value )))
21 changes: 20 additions & 1 deletion src/packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,35 @@
(:use #:common-lisp #:babel #:uuid #:usocket)
(:export

;;keywords
:all
:listdatabases
:serverstatus
:deleteindexes

;;commands
:mongo
:kv
:db.use
:db.insert
:db.find
:db.next
:db.iter
:db.stop
:db.delete
:db.ensure-index
:db.run-command
:db.indexes
:db.collections
:db.count
:close-all-connections

;; shell commands
:nwd
:cwd
:pp
:iter
:nd
:rm
:docs
))
11 changes: 0 additions & 11 deletions src/save-document.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,4 @@
(ignore size-supplied-p) (ignore array-supplied-p) )
(bson-encode key (bson-encode-container value )))

(defun ht->document (ht)
(multiple-value-bind (oid oid-supplied) (gethash "_id" ht)
(let ((doc (make-document :oid (if oid-supplied oid nil))))
(when oid-supplied (remhash "_id" ht))
(with-hash-table-iterator (iterator ht)
(dotimes (repeat (hash-table-count ht))
(multiple-value-bind (exists-p key value) (iterator)
(if exists-p (add-element key value doc)))))
doc)))



0 comments on commit 2c905d3

Please sign in to comment.