1 ;;; BongoDB, an embedded document-based engine
2 ;;; Copyright (C) 2016 by Javier Sancho Fernandez <jsf at jsancho dot org>
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;;; GNU General Public License for more details.
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 (define-module (bongodb)
19 #:use-module (ice-9 receive)
20 #:use-module (ice-9 vlist)
21 #:use-module (srfi srfi-9)
22 #:use-module (srfi srfi-9 gnu)
23 #:export (make-collection
36 ;;; Collection Definition
38 (define-record-type collection
39 (make-collection-record table)
43 (define (make-collection)
44 (make-collection-record vlist-null))
47 (vlist-length (get-table col)))
49 (set-record-type-printer! collection
52 "#<collection with ~a documents>"
56 ;;; Working with documents
58 (define (format-document document)
59 "Return a vhash document ready to store in a collection"
61 (cond ((vhash? document)
64 (alist->vhash document)))))
66 (define (document-with-id document)
67 "Return always a document with an id"
68 (or (and (vhash-assoc '_id document)
70 (vhash-cons '_id (gensym) document)))
72 (define (insert col . documents)
73 "Insert documents into the collection and return the new collection"
74 (cond ((null? documents)
77 (let* ((document (format-document (car documents)))
78 (docid (cdr (vhash-assoc '_id document)))
79 (newcol (make-collection-record (vhash-cons docid document (get-table col)))))
80 (receive (rescol docids)
81 (apply insert (cons newcol (cdr documents)))
82 (values rescol (cons docid docids)))))))
84 (define (find col filter)
85 "Query the collection and return the documents that match with filter"
86 (map vhash->alist (inner-find col filter)))
88 (define (inner-find col filter)
89 "Query the collection and return the documents that match with filter"
90 (let ((table (get-table col)))
92 (lambda (key document result)
93 (cond ((match-document? document filter)
94 (cons document result))
100 (define (match-document? document filter)
101 "Try to match the given document with a given filter"
102 (cond ((equal? filter #t)
107 (define (update col filter . changes)
108 "Update selected documents using the given filter"
109 (let ((documents (inner-find col filter)))
110 (apply insert (cons col (map (lambda (doc) (update-document doc changes)) documents)))))
112 (define (update-document document changes)
113 "Update a document with the appropiate changes"
114 (cond ((null? changes)
117 (update-document ((car changes) document) (cdr changes)))))
122 (define ($eq field value)
124 (let ((stored (vhash-assoc field document)))
126 (equal? (cdr stored) value)))))
128 (define ($exists field)
130 (and (vhash-assoc field document)
135 (not (expr document))))
147 (define ($set field value)
149 (vhash-cons field value document)))
154 (define (vhash->alist vhash)
156 (lambda (key value result) (assoc-set! result key value))