]> gitweb.factorcode.org Git - factor.git/blob - extra/rocksdb/lib/lib.factor
Switch to https urls
[factor.git] / extra / rocksdb / lib / lib.factor
1 ! Copyright (C) 2020 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data alien.strings
4 constructors destructors io.encodings.string io.encodings.utf8
5 kernel namespaces rocksdb.ffi sequences ;
6 IN: rocksdb.lib
7
8 ! https://github.com/facebook/rocksdb/blob/c08c0ae73131457a2ac74507da58ff49870c1ee6/db/c_test.c
9
10 : rocksdb-ptr-error ( ptr/f error-ptrptr -- ptr/f )
11     over
12     [ drop ]
13     [ void* deref alien>native-string throw ] if ;
14
15 TUPLE: rocksdb-handle < disposable ptr ;
16 CONSTRUCTOR: <rocksdb-handle> rocksdb-handle ( ptr -- handle ) ;
17 M: rocksdb-handle dispose
18     ptr>> rocksdb_close ;
19
20 TUPLE: rocksdb-options < disposable ptr ;
21 CONSTRUCTOR: <rocksdb-options> rocksdb-options ( ptr -- options ) ;
22 M: rocksdb-options dispose
23     ptr>> rocksdb_options_destroy ;
24
25 : set-create-if-missing ( options -- options )
26     [ 1 rocksdb_options_set_create_if_missing ] keep ;
27
28 : create-rocksdb ( path -- ptr )
29     [
30         rocksdb_options_create [ <rocksdb-options> &dispose drop ] keep
31         set-create-if-missing
32     ] dip f void* <ref>
33     [ rocksdb_open ] keep rocksdb-ptr-error ;
34
35 SYMBOL: rocksdb-handle-var
36
37 : with-rocksdb ( path quot -- )
38     [
39         [
40             create-rocksdb [ <rocksdb-handle> &dispose drop ] keep
41             dup rocksdb-handle-var
42         ] dip
43          with-variable
44     ] with-destructors ; inline
45
46 ! Writing
47 SYMBOL: rocksdb-write-options-var
48
49 TUPLE: rocksdb-write-options < disposable ptr ;
50 CONSTRUCTOR: <rocksdb-write-options> rocksdb-write-options ( ptr -- options ) ;
51 M: rocksdb-write-options dispose
52     ptr>> rocksdb_writeoptions_destroy ;
53
54 : make-write-options-sync ( -- write-options )
55     rocksdb_writeoptions_create [ 1 rocksdb_writeoptions_set_sync ] keep
56     [ <rocksdb-write-options> &dispose drop ] keep ;
57
58 : make-write-options-async ( -- write-options )
59     rocksdb_writeoptions_create [ 0 rocksdb_writeoptions_set_sync ] keep
60     [ <rocksdb-write-options> &dispose drop ] keep ;
61
62 : rocksdb-put* ( db write-options key value -- error/f )
63     [ utf8 encode dup length ] bi@
64     f void* <ref>
65     [ rocksdb_put ] keep
66     dup [ void* deref alien>native-string ] when ;
67
68 ! Reading
69
70 SYMBOL: rocksdb-read-options-var
71
72 TUPLE: rocksdb-read-options < disposable ptr ;
73 CONSTRUCTOR: <rocksdb-read-options> rocksdb-read-options ( ptr -- options ) ;
74 M: rocksdb-read-options dispose
75     ptr>> rocksdb_readoptions_destroy ;
76
77
78 : make-read-options ( -- read-options )
79     rocksdb_readoptions_create
80     [ <rocksdb-read-options> &dispose drop ] keep ;
81
82 ! : with-default-rocksdb-read-options ( quot -- )
83 !    [ make-read-options rocksdb-read-options-var ] dip with-variable ; inline
84
85 : rocksdb-get* ( db read-options key -- value/f error/f )
86     utf8 encode dup length
87     0 size_t <ref>
88     f void* <ref>
89     [ rocksdb_get ] 2keep
90     dup [ void* deref alien>native-string ] when
91     dup [
92         [ 2drop f ] dip
93     ] [
94         [ size_t deref memory>byte-array ] dip
95     ] if ;
96
97 ! Deleting
98
99 : rocksdb-delete* ( db write-options key -- error/f )
100     utf8 encode dup length
101     f void* <ref>
102     [ rocksdb_delete ] keep
103     dup [ void* deref alien>native-string ] when ;