]> gitweb.factorcode.org Git - factor.git/commitdiff
forestdb: Support more api calls.
authorDoug Coleman <doug.coleman@gmail.com>
Wed, 5 Nov 2014 18:03:52 +0000 (10:03 -0800)
committerDoug Coleman <doug.coleman@gmail.com>
Wed, 5 Nov 2014 18:04:09 +0000 (10:04 -0800)
extra/forestdb/ffi/ffi.factor
extra/forestdb/lib/lib-tests.factor
extra/forestdb/lib/lib.factor

index 5bc8e97844dae0b5b4e4d3c1bb8fe54c2864fb27..423cfdabe29f7dbdf9d2d05eddefb0f355d55422 100644 (file)
@@ -5,6 +5,8 @@ alien.libraries.finder alien.syntax classes.struct kernel
 unix.types ;
 IN: forestdb.ffi
 
+! Functions with LIBFDB_API are exported.
+
 << "forestdb" dup find-library cdecl add-library >>
 
 LIBRARY: forestdb
@@ -87,6 +89,7 @@ ENUM: fdb_status
     { FDB_RESULT_IN_USE_BY_COMPACTOR -30 }
     { FDB_RESULT_FAIL -100 } ;
 
+! cmp_fixed and cmp_variable have their own open() functions
 STRUCT: fdb_config
     { chunksize uint16_t }
     { blocksize uint32_t }
@@ -119,6 +122,9 @@ STRUCT: fdb_doc
     { body void* }
     { deleted bool } ;
 
+! filename is a pointer to the handle's filename
+! new_filename is a pointer to the handle's new_file
+
 STRUCT: fdb_info
     { filename char* }
     { new_filename char* }
@@ -128,26 +134,35 @@ STRUCT: fdb_info
     { file_size uint64_t } ;
 
 FUNCTION: fdb_status fdb_init ( fdb_config* config ) ;
+
 FUNCTION: fdb_status fdb_open ( fdb_handle** ptr_handle, c-string filename, fdb_config* fconfig ) ;
 FUNCTION: fdb_status fdb_open_cmp_fixed ( fdb_handle** ptr_handle, c-string filename, fdb_config* fconfig ) ;
 FUNCTION: fdb_status fdb_open_cmp_variable ( fdb_handle** ptr_handle, c-string filename, fdb_config* fconfig ) ;
+
 FUNCTION: fdb_status fdb_set_log_callback ( fdb_handle* handle, fdb_log_callback log_callback, void* ctx_data ) ;
+
+! doc is calloc'd
 FUNCTION: fdb_status fdb_doc_create ( fdb_doc** doc, c-string key, size_t keylen, c-string meta, size_t metalen, c-string body, size_t bodylen ) ;
 FUNCTION: fdb_status fdb_doc_update ( fdb_doc** doc, c-string meta, size_t metalen, c-string body, size_t bodylen ) ;
 FUNCTION: fdb_status fdb_doc_free ( fdb_doc* doc ) ;
+
 FUNCTION: fdb_status fdb_get ( fdb_handle* handle, fdb_doc* doc ) ;
 FUNCTION: fdb_status fdb_get_metaonly ( fdb_handle* handle, fdb_doc* doc ) ;
 FUNCTION: fdb_status fdb_get_byseq ( fdb_handle* handle, fdb_doc* doc ) ;
 FUNCTION: fdb_status fdb_get_metaonly_byseq ( fdb_handle* handle, fdb_doc* doc ) ;
 FUNCTION: fdb_status fdb_get_byoffset ( fdb_handle* handle, fdb_doc* doc ) ;
+
 FUNCTION: fdb_status fdb_set ( fdb_handle* handle, fdb_doc* doc ) ;
 FUNCTION: fdb_status fdb_del ( fdb_handle* handle, fdb_doc* doc ) ;
+
 FUNCTION: fdb_status fdb_get_kv ( fdb_handle* handle, c-string key, size_t keylen, void** value_out, size_t* valuelen_out ) ;
 FUNCTION: fdb_status fdb_set_kv ( fdb_handle* handle, c-string key, size_t keylen, c-string value, size_t valuelen ) ;
 FUNCTION: fdb_status fdb_del_kv ( fdb_handle* handle, c-string key, size_t keylen ) ;
+
 FUNCTION: fdb_status fdb_commit ( fdb_handle* handle, fdb_commit_opt_t opt ) ;
 FUNCTION: fdb_status fdb_snapshot_open ( fdb_handle* handle_in, fdb_handle** handle_out, fdb_seqnum_t snapshot_seqnum ) ;
 FUNCTION: fdb_status fdb_rollback ( fdb_handle** handle_ptr, fdb_seqnum_t rollback_seqnum ) ;
+
 FUNCTION: fdb_status fdb_iterator_init ( fdb_handle* handle, fdb_iterator** iterator, c-string start_key, size_t start_keylen, c-string end_key, size_t end_keylen, fdb_iterator_opt_t opt ) ;
 FUNCTION: fdb_status fdb_iterator_sequence_init ( fdb_handle* handle, fdb_iterator** iterator, fdb_seqnum_t start_seq, fdb_seqnum_t end_seq, fdb_iterator_opt_t opt ) ;
 FUNCTION: fdb_status fdb_iterator_prev ( fdb_iterator* iterator, fdb_doc** doc ) ;
@@ -155,12 +170,15 @@ FUNCTION: fdb_status fdb_iterator_next ( fdb_iterator* iterator, fdb_doc** doc )
 FUNCTION: fdb_status fdb_iterator_next_metaonly ( fdb_iterator* iterator, fdb_doc** doc ) ;
 FUNCTION: fdb_status fdb_iterator_seek ( fdb_iterator* iterator, c-string seek_key, size_t seek_keylen ) ;
 FUNCTION: fdb_status fdb_iterator_close ( fdb_iterator* iterator ) ;
+
 FUNCTION: fdb_status fdb_compact ( fdb_handle* handle, c-string new_filename ) ;
 FUNCTION: fdb_status fdb_get_dbinfo ( fdb_handle* handle, fdb_info* info ) ;
 FUNCTION: fdb_status fdb_switch_compaction_mode ( fdb_handle* handle, fdb_compaction_mode_t mode, size_t new_threshold ) ;
+
 FUNCTION: fdb_status fdb_close ( fdb_handle* handle ) ;
 FUNCTION: fdb_status fdb_destroy ( c-string filename, fdb_compaction_mode_t mode, size_t new_threshold ) ;
 FUNCTION: fdb_status fdb_shutdown ( ) ;
+
 FUNCTION: fdb_status fdb_begin_transaction ( fdb_handle* handle, fdb_isolation_level_t isolation_level ) ;
 FUNCTION: fdb_status fdb_end_transaction ( fdb_handle* handle, fdb_commit_opt_t opt ) ;
 FUNCTION: fdb_status fdb_abort_transaction ( fdb_handle* handle ) ;
index af2b666d955702ca508801204e8acbd2fcf64777..ec7d11be1034bbeec8873df33ad9fee28da53b95 100644 (file)
@@ -10,14 +10,37 @@ IN: forestdb.lib
 
 { "val123" } [
     forestdb-test-path [
-       "key123" "val123" fdb-set
-       "key123" fdb-get
+       "key123" "val123" fdb-set-kv
+       "key123" fdb-get-kv
     ] with-forestdb
 ] unit-test
 
 { "val12345" } [
     forestdb-test-path [
-       "key123" "val12345" fdb-set
-       "key123" fdb-get
+       "key123" "val12345" fdb-set-kv
+       "key123" fdb-get-kv
+    ] with-forestdb
+] unit-test
+
+
+{ f } [
+    ! Filename is only valid inside with-forestdb
+    forestdb-test-path [
+        get-current-db-info filename>> alien>native-string empty?
+    ] with-forestdb
+] unit-test
+
+{ 6 9 9 } [
+    forestdb-test-path [
+       "key123" "meta blah" "some body" fdb-doc-create
+        [ keylen>> ] [ metalen>> ] [ bodylen>> ] tri
+    ] with-forestdb
+] unit-test
+
+{ 7 8 15 } [
+    forestdb-test-path [
+       "key1234" "meta blah" "some body" fdb-doc-create
+        dup "new meta" "some other body" fdb-doc-update
+        [ keylen>> ] [ metalen>> ] [ bodylen>> ] tri
     ] with-forestdb
 ] unit-test
index dbe156ae96c097bc5ca4d5a2b297ff739d99fdc3..319c3894aa43f1c0a84b8f04875501e2275cbc21 100644 (file)
@@ -1,8 +1,9 @@
 ! Copyright (C) 2014 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors alien.c-types alien.data alien.strings
-combinators destructors forestdb.ffi fry io.encodings.string
-io.encodings.utf8 io.pathnames kernel libc namespaces sequences ;
+classes.struct combinators destructors forestdb.ffi fry
+generalizations io.encodings.string io.encodings.utf8
+io.pathnames kernel libc namespaces sequences ;
 IN: forestdb.lib
 
 ERROR: fdb-error error ;
@@ -22,25 +23,37 @@ TUPLE: fdb-handle < disposable handle ;
 M: fdb-handle dispose*
     handle>> fdb_close check-forestdb-error ;
 
+
+TUPLE: fdb-doc < disposable doc ;
+M: fdb-doc dispose*
+    fdb_doc_free check-forestdb-error ;
+
+
 : open-default-forestdb ( path -- handle )
     [ f void* <ref> ] dip
     absolute-path f
     [ fdb_open check-forestdb-error ] 3keep 2drop void* deref <fdb-handle> ;
 
+: ret>string ( void** len -- string )
+    [ void* deref ] [ size_t deref ] bi*
+    [ memory>byte-array utf8 decode ] [ drop (free) ] 2bi ;
+
 SYMBOL: current-forestdb
 
 : get-handle ( -- handle )
     current-forestdb get handle>> ;
 
-: fdb-set ( key value -- )
+: fdb-set-kv ( key value -- )
     [ get-handle ] 2dip
     [ dup length ] bi@ fdb_set_kv check-forestdb-error ;
 
-: ret>string ( void** len -- string )
-    [ void* deref ] [ size_t deref ] bi*
-    [ memory>byte-array utf8 decode ] [ drop (free) ] 2bi ;
+: fdb-set ( doc -- )
+    [ get-handle ] dip fdb_set check-forestdb-error ;
+
+: fdb-del ( doc -- )
+    [ get-handle ] dip fdb_del check-forestdb-error ;
 
-: fdb-get ( key -- value/f )
+: fdb-get-kv ( key -- value/f )
     [ get-handle ] dip
     dup length f void* <ref> 0 size_t <ref>
     [ fdb_get_kv ] 2keep
@@ -50,6 +63,28 @@ SYMBOL: current-forestdb
         [ fdb-error ]
     } case ;
 
+: fdb-del-kv ( key -- )
+    [ get-handle ] dip dup length fdb_del_kv check-forestdb-error ;
+
+: fdb-doc-create ( key meta body -- doc )
+    [ f void* <ref> ] 3dip
+    [ dup length ] tri@
+    [ fdb_doc_create check-forestdb-error ] 7 nkeep 6 ndrop
+    void* deref fdb_doc memory>struct ;
+
+: fdb-doc-update ( doc meta body -- )
+    [ void* <ref> ] 2dip
+    [ dup length ] bi@
+    fdb_doc_update check-forestdb-error ;
+
+: fdb-doc-free ( doc -- )
+    fdb_doc_free check-forestdb-error ;
+
+
+: get-current-db-info ( -- info )
+    get-handle
+    fdb_info <struct> [ fdb_get_dbinfo check-forestdb-error ] keep ;
+
 : commit-forestdb ( -- )
     get-handle FDB_COMMIT_NORMAL fdb_commit check-forestdb-error ;