]> git.jsancho.org Git - bongodb.git/commitdiff
Advanced queries support
authorJavier Sancho <jsf@jsancho.org>
Tue, 15 Mar 2016 00:10:40 +0000 (01:10 +0100)
committerJavier Sancho <jsf@jsancho.org>
Tue, 15 Mar 2016 00:10:40 +0000 (01:10 +0100)
src/bongodb.scm
tests/sample.scm

index e4df14bde9e1699503e91b4b9d6b9883446ba0b1..6fc16fe0fcfababa251b2e7686263640e2a2cd18 100644 (file)
            collection?
            count
            insert
-           find))
+           find
+           $eq
+           $exists
+           $not))
 
 
 ;;; Collection Definition
@@ -75,7 +78,7 @@
               (apply insert (cons newcol (cdr documents)))
             (values rescol (cons docid docids)))))))
 
-(define (find col filter)
+(define (find col filter)
   "Query the collection and return the documents that match with filter"
   (let ((table (get-table col)))
     (vhash-fold
         #t)
        (else
         (and
-         (equal? (vhash-assoc (caar filter) document) (car filter))
+         ((car filter) document)
          (match-document? document (cdr filter))))))
 
 
+;;; Queries
+
+(define ($eq field value)
+  (lambda (document)
+    (let ((stored (vhash-assoc field document)))
+      (and stored
+          (equal? (cdr stored) value)))))
+
+
+(define ($exists field)
+  (lambda (document)
+    (and (vhash-assoc field document)
+        #t)))
+
+
+(define ($not expr)
+  (lambda (document)
+    (not (expr document))))
+
+            
 ;;; Tools
 
 (define (vhash->alist vhash)
index 899c6e0f710582625d1e8f17620c1bd7d67d2a54..5054ffeab15680c003ec607677ba94d4d96920c0 100644 (file)
 (test-assert (collection? col))
 
 ; Insert
-(set! col (insert col '((a . 1) (b . 2)) '((a . 10) (b . 20)) '((a . 1) (b . "hello world"))))
+(set! col (insert col
+                 '((a . 1) (b . 2))
+                 '((a . 10) (b . 20))
+                 '((a . 1) (c . "hello world"))))
 (test-eqv 3 (count col))
 
 ; 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-eqv 3 (length (find col)))
+(test-eqv 2 (length (find col ($eq 'a 1))))
+(test-eqv 1 (length (find col ($eq 'a 1) ($eq 'b 2))))
+(test-eqv 0 (length (find col ($eq 'a "test"))))
+
+(test-eqv 2 (length (find col ($exists 'b))))
+(test-eqv 2 (length (find col ($not ($exists 'c)))))
 
 (test-end "bongodb")