Advertisement
Shinmera

Radiance Interface Function for MongoDB

Jul 4th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.60 KB | None | 0 0
  1. ;; Looks nice but is shit in terms of performance.
  2. (defmethod db-iterate ((db mongodb) (collection string) query function &key (skip 0) (limit 0) sort)
  3.   "Iterate over a set of data. The collected return values are returned."
  4.   (if sort (setf query (kv (kv "query" query) (kv "orderby" (alist->document sort)))))
  5.   (let ((result (db.find collection query :limit limit :skip skip)))
  6.     (multiple-value-bind (iterator collection docs) (cl-mongo::db.iterator result)
  7.       (loop
  8.          for next = '(NIL NIL) then (db.next collection iter)
  9.          for iter = iterator then (nth-value 0 (cl-mongo::db.iterator next))
  10.          for idoc = docs then (append (cdr idoc) (second next))
  11.          for doc = (car idoc)
  12.          while doc
  13.          collect (funcall function doc)))))
  14.  
  15. ;; Two loops and the performance is increased by a factor of ~30.
  16. (defmethod db-iterate ((db mongodb) (collection string) query function &key (skip 0) (limit 0) sort)
  17.   "Iterate over a set of data. The collected return values are returned."
  18.   (if sort (setf query (kv (kv "query" query) (kv "orderby" (alist->document sort)))))
  19.   (let ((result (db.find collection query :limit limit :skip skip)))
  20.     (multiple-value-bind (iterator collection docs) (cl-mongo::db.iterator result)
  21.       (loop
  22.          for next = '(NIL (0 1)) then (db.next collection iter)
  23.          for iter = iterator then (nth-value 0 (cl-mongo::db.iterator next))
  24.          until (zerop (length (second next)))
  25.          for idocs = docs then (append idocs (second next))
  26.          finally (setf docs idocs))
  27.       (loop for doc in docs collect (funcall function doc)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement