]> git.jsancho.org Git - bongodb.git/commitdiff
Basic searching support
authorJavier Sancho <jsf@jsancho.org>
Wed, 9 Mar 2016 16:25:26 +0000 (17:25 +0100)
committerJavier Sancho <jsf@jsancho.org>
Wed, 9 Mar 2016 16:25:26 +0000 (17:25 +0100)
src/bongodb.scm
tests/sample.scm

index 3cffa48d5ce6f7280958a00d1abf04ece8acc50b..e4df14bde9e1699503e91b4b9d6b9883446ba0b1 100644 (file)
   "Query the collection and return the documents that match with filter"
   (let ((table (get-table col)))
     (vhash-fold
-     (lambda (key value result) (cons (vhash->alist value) result))
+     (lambda (key document result)
+       (cond ((match-document? document filter)
+             (cons (vhash->alist document) result))
+            (else
+             result)))
      '()
      table)))
 
+(define (match-document? document filter)
+  "Try to match the given document with a list of conditions"
+  (cond ((null? filter)
+        #t)
+       (else
+        (and
+         (equal? (vhash-assoc (caar filter) document) (car filter))
+         (match-document? document (cdr filter))))))
+
 
 ;;; Tools
 
index 047a751ca444e363154adea47b044cee46a6fd96..899c6e0f710582625d1e8f17620c1bd7d67d2a54 100644 (file)
@@ -30,5 +30,7 @@
 
 ; Search
 (test-eqv 2 (length (find col '((a . 1)))))
+(test-eqv 1 (length (find col '((a . 1) (b . 2)))))
+(test-eqv 0 (length (find col '((a . "test")))))
 
 (test-end "bongodb")