aboutsummaryrefslogtreecommitdiff
path: root/eris/blocks/gdbm.scm
blob: 82f268568ffb23d34a92eea503faedf9ec3a6e04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
; SPDX-FileCopyrightText: 2021 pukkamustard <pukkamustard@posteo.net>
;
; SPDX-License-Identifier: GPL-3.0-or-later

(define-module (eris blocks gdbm)
  #:use-module (eris)
  #:use-module (eris utils base32)

  #:use-module (gdbm)

  #:export (eris-blocks-gdbm-reducer
	    eris-blocks-gdbm-ref))


(define* (eris-blocks-gdbm-reducer db-file #:key
				   (block-size %eris-block-size-large)
				   (flags GDBM_WRCREAT))
  "Returns a block reducer that stores blocks in the GDBM database DB-FILE."
  (case-lambda
    ;; open db connection
    (() (gdbm-open db-file flags))

    ;; Completion. Close the db
    ((db) (gdbm-close db))

    ;; store a block
    ((db ref-block)
     (begin
       (gdbm-set! db
		  (base32-encode (car ref-block))
		  (base32-encode (cdr ref-block)))
       db))))

(define* (eris-blocks-gdbm-ref db)
  (lambda (ref)
    (base32-decode
     (gdbm-ref db (base32-encode ref)))))