Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; Looks nice but is shit in terms of performance.
- (defmethod db-iterate ((db mongodb) (collection string) query function &key (skip 0) (limit 0) sort)
- "Iterate over a set of data. The collected return values are returned."
- (if sort (setf query (kv (kv "query" query) (kv "orderby" (alist->document sort)))))
- (let ((result (db.find collection query :limit limit :skip skip)))
- (multiple-value-bind (iterator collection docs) (cl-mongo::db.iterator result)
- (loop
- for next = '(NIL NIL) then (db.next collection iter)
- for iter = iterator then (nth-value 0 (cl-mongo::db.iterator next))
- for idoc = docs then (append (cdr idoc) (second next))
- for doc = (car idoc)
- while doc
- collect (funcall function doc)))))
- ;; Two loops and the performance is increased by a factor of ~30.
- (defmethod db-iterate ((db mongodb) (collection string) query function &key (skip 0) (limit 0) sort)
- "Iterate over a set of data. The collected return values are returned."
- (if sort (setf query (kv (kv "query" query) (kv "orderby" (alist->document sort)))))
- (let ((result (db.find collection query :limit limit :skip skip)))
- (multiple-value-bind (iterator collection docs) (cl-mongo::db.iterator result)
- (loop
- for next = '(NIL (0 1)) then (db.next collection iter)
- for iter = iterator then (nth-value 0 (cl-mongo::db.iterator next))
- until (zerop (length (second next)))
- for idocs = docs then (append idocs (second next))
- finally (setf docs idocs))
- (loop for doc in docs collect (funcall function doc)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement