collection?
count
insert
- find))
+ find
+ $eq
+ $exists
+ $not))
;;; Collection Definition
(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
- (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
+ ((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)