diff --git a/extra/forestdb/ffi/ffi.factor b/extra/forestdb/ffi/ffi.factor index 778a8a8c8e..9c41dc92f0 100644 --- a/extra/forestdb/ffi/ffi.factor +++ b/extra/forestdb/ffi/ffi.factor @@ -232,7 +232,7 @@ FUNCTION: fdb_status fdb_end_transaction ( fdb_file_handle* fhandle, fdb_commit_ FUNCTION: fdb_status fdb_abort_transaction ( fdb_file_handle* fhandle ) ; FUNCTION: fdb_status fdb_kvs_open ( fdb_file_handle* fhandle, fdb_kvs_handle** ptr_handle, - char* kvs_name, + c-string kvs_name, fdb_kvs_config* config ) ; @@ -242,5 +242,5 @@ FUNCTION: fdb_status fdb_kvs_open_default ( fdb_file_handle* fhandle, FUNCTION: fdb_status fdb_kvs_close ( fdb_kvs_handle* handle ) ; -FUNCTION: fdb_status fdb_kvs_remove ( fdb_file_handle* fhandle, char* kvs_name ) ; +FUNCTION: fdb_status fdb_kvs_remove ( fdb_file_handle* fhandle, c-string kvs_name ) ; FUNCTION: char* fdb_error_msg ( fdb_status err_code ) ; diff --git a/extra/forestdb/lib/lib-tests.factor b/extra/forestdb/lib/lib-tests.factor index 2b12fad5cc..c9c9863abd 100644 --- a/extra/forestdb/lib/lib-tests.factor +++ b/extra/forestdb/lib/lib-tests.factor @@ -6,7 +6,7 @@ combinators.short-circuit constructors continuations destructors forestdb.ffi forestdb.utils fry generalizations io.directories io.encodings.string io.encodings.utf8 io.files.temp io.pathnames kernel layouts libc make math math.parser math.ranges multiline -namespaces sequences system tools.test ; +namespaces sequences system tools.test nested-comments ; IN: forestdb.lib { [ cell-bits 32 = ] [ os windows? ] } 0&& [ @@ -18,17 +18,21 @@ IN: forestdb.lib { "val123" } [ delete-test-db-0 test-db-0 [ - "key123" "val123" fdb-set-kv - "key123" fdb-get-kv - ] with-forestdb-path + "test123" [ + "key123" "val123" fdb-set-kv + "key123" fdb-get-kv + ] with-kvs + ] with-forestdb ] unit-test { "val12345" } [ delete-test-db-0 test-db-0 [ - "key123" "val12345" fdb-set-kv - "key123" fdb-get-kv - ] with-forestdb-path + "test123" [ + "key123" "val12345" fdb-set-kv + "key123" fdb-get-kv + ] with-kvs + ] with-forestdb ] unit-test ! Get @@ -42,9 +46,10 @@ IN: forestdb.lib "key1" "meta" "val" [ fdb_doc>doc [ key>> ] [ body>> ] bi 2array ] with-create-doc - ] with-forestdb-path + ] with-forestdb-tester ] unit-test + { { "key1" f "val1" } } [ @@ -55,7 +60,7 @@ IN: forestdb.lib fdb-get fdb_doc>doc [ key>> ] [ meta>> ] [ body>> ] tri 3array ] with-create-doc - ] with-forestdb-path + ] with-forestdb-tester ] unit-test @@ -69,7 +74,7 @@ IN: forestdb.lib fdb-get-byseq fdb_doc>doc [ key>> ] [ meta>> ] [ body>> ] tri 3array ] with-doc - ] with-forestdb-path + ] with-forestdb-tester ] unit-test { @@ -82,16 +87,15 @@ IN: forestdb.lib fdb-get-byseq fdb_doc>doc [ key>> ] [ meta>> ] [ body>> ] tri 3array ] with-doc - ] with-forestdb-path + ] with-forestdb-tester ] unit-test - ! Filename is only valid inside with-forestdb { f } [ delete-test-db-0 test-db-0 [ fdb-get-info filename>> alien>native-string empty? - ] with-forestdb-path + ] with-forestdb-tester ] unit-test ! Test fdb_doc_create @@ -101,7 +105,7 @@ IN: forestdb.lib "key123" "meta blah" "some body" [ [ keylen>> ] [ metalen>> ] [ bodylen>> ] tri ] with-create-doc - ] with-forestdb-path + ] with-forestdb-tester ] unit-test { 7 8 15 } [ @@ -111,7 +115,7 @@ IN: forestdb.lib [ "new meta" "some other body" fdb-doc-update ] [ [ keylen>> ] [ metalen>> ] [ bodylen>> ] tri ] bi ] with-create-doc - ] with-forestdb-path + ] with-forestdb-tester ] unit-test { 1 1 } [ @@ -120,7 +124,7 @@ IN: forestdb.lib 1 set-kv-n fdb-commit-normal fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi - ] with-forestdb-path + ] with-forestdb-tester ] unit-test { 6 5 } [ @@ -130,7 +134,7 @@ IN: forestdb.lib 5 set-kv-nth fdb-commit-normal fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi - ] with-forestdb-path + ] with-forestdb-tester ] unit-test { 5 5 } [ @@ -139,10 +143,12 @@ IN: forestdb.lib 5 set-kv-n fdb-commit-normal fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi - ] with-forestdb-path + ] with-forestdb-tester ] unit-test ! Snapshots + +/* { 5 5 } [ delete-test-db-1 test-db-1 [ @@ -151,8 +157,10 @@ IN: forestdb.lib FDB_SNAPSHOT_INMEM [ fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi ] with-forestdb-snapshot - ] with-forestdb-path + ] with-forestdb-tester ] unit-test +*/ + /* ! Snapshots can only occur on commits. If you commit five keys at once, @@ -167,11 +175,10 @@ IN: forestdb.lib FDB_SNAPSHOT_INMEM [ fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi ] with-forestdb-snapshot - ] with-forestdb-path + ] with-forestdb-tester ] [ T{ fdb-error { error FDB_RESULT_NO_DB_INSTANCE } } = ] must-fail-with -*/ ! Test that we take two snapshots and their seqnums/doc counts are right. ! XXX: Buggy, want to see the first snapshot's document count at 5 too @@ -194,9 +201,10 @@ IN: forestdb.lib FDB_SNAPSHOT_INMEM [ fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi 2array ] with-forestdb-snapshot - ] with-forestdb-path + ] with-forestdb-tester ] unit-test + ! Same test as above, but with buggy behavior for now so it passes { 7 @@ -217,10 +225,12 @@ IN: forestdb.lib FDB_SNAPSHOT_INMEM [ fdb-get-kvs-info last_seqnum>> ] with-forestdb-snapshot - ] with-forestdb-path + ] with-forestdb-tester ] unit-test + + ! Rollback test ! Make sure the doc_count is correct after a rollback { @@ -244,9 +254,11 @@ IN: forestdb.lib FDB_SNAPSHOT_INMEM [ fdb-get-kvs-info [ last_seqnum>> ] [ doc_count>> ] bi 2array ] with-forestdb-snapshot - ] with-forestdb-path + ] with-forestdb-tester ] unit-test +*/ + ! Iterators test ! No matching keys @@ -262,7 +274,7 @@ IN: forestdb.lib fdb_doc>doc [ seqnum>> ] [ key>> ] [ body>> ] tri 3array , ] with-fdb-normal-iterator ] { } make - ] with-forestdb-path + ] with-forestdb-tester ] unit-test ! All the keys @@ -284,7 +296,7 @@ IN: forestdb.lib fdb_doc>doc [ seqnum>> ] [ key>> ] [ body>> ] tri 3array , ] with-fdb-normal-iterator ] { } make - ] with-forestdb-path + ] with-forestdb-tester ] unit-test ! Test that keys at extremes get returned @@ -302,7 +314,7 @@ IN: forestdb.lib fdb_doc>doc [ seqnum>> ] [ key>> ] [ body>> ] tri 3array , ] with-fdb-normal-iterator ] { } make - ] with-forestdb-path + ] with-forestdb-tester ] unit-test { @@ -319,7 +331,7 @@ IN: forestdb.lib fdb_doc>doc [ seqnum>> ] [ key>> ] [ body>> ] tri 3array , ] with-fdb-normal-iterator ] { } make - ] with-forestdb-path + ] with-forestdb-tester ] unit-test @@ -335,7 +347,7 @@ IN: forestdb.lib fdb_doc>doc ] with-fdb-byseq-map [ seqnum>> ] map - ] with-forestdb-path + ] with-forestdb-tester ] unit-test ! Deleting 5 keys gives you 5 new seqnums that are those docs, but deleted @@ -351,7 +363,7 @@ IN: forestdb.lib fdb_doc>doc ] with-fdb-byseq-map [ [ seqnum>> ] [ deleted?>> ] bi 2array ] map - ] with-forestdb-path + ] with-forestdb-tester ] unit-test { @@ -373,7 +385,7 @@ IN: forestdb.lib [ [ key>> ] [ keylen>> ] bi alien/length>string ] bi 2array , ] with-fdb-byseq-each ] { } make - ] with-forestdb-path + ] with-forestdb-tester ] unit-test ] unless \ No newline at end of file diff --git a/extra/forestdb/lib/lib.factor b/extra/forestdb/lib/lib.factor index 0194597c62..4e76208298 100644 --- a/extra/forestdb/lib/lib.factor +++ b/extra/forestdb/lib/lib.factor @@ -10,8 +10,7 @@ IN: forestdb.lib /* ! Issues -! 4) byseq iteration doesn't have bodies, weird. -! 5) Get byseq ignores seqnum and uses key instead if key is set +! Get byseq ignores seqnum and uses key instead if key is set */ ERROR: fdb-error error ; @@ -19,6 +18,7 @@ ERROR: fdb-error error ; : fdb-check-error ( ret -- ) dup FDB_RESULT_SUCCESS = [ drop ] [ fdb-error ] if ; + TUPLE: fdb-kvs-handle < disposable handle ; : ( handle -- obj ) fdb-kvs-handle new-disposable @@ -36,35 +36,10 @@ TUPLE: fdb-file-handle < disposable handle ; M: fdb-file-handle dispose* handle>> fdb_close fdb-check-error ; + SYMBOL: current-fdb-file-handle SYMBOL: current-fdb-kvs-handle -: get-kvs-default-config ( -- kvs-config ) - S{ fdb_kvs_config - { create_if_missing t } - { custom_cmp f } - } clone ; - -: fdb-open-kvs' ( file-handle fdb-kvs-handle kvs-config -- file-handle handle ) - [ dup handle>> ] 2dip - [ handle>> ] dip - [ fdb_kvs_open_default fdb-check-error ] 2keep drop - void* deref ; - -: fdb-open-kvs ( fdb-file-handle kvs-config -- file-handle handle ) - [ f void* ] dip fdb-open-kvs' ; - -: fdb-open ( path config kvs-config -- file-handle handle ) - [ - [ f void* ] 2dip - [ absolute-path ensure-fdb-filename-directory ] dip - [ fdb_open fdb-check-error ] 3keep - 2drop void* deref - ] dip fdb-open-kvs ; - -: fdb-open-default-config ( path -- file-handle handle ) - fdb_get_default_config get-kvs-default-config fdb-open ; - : get-file-handle ( -- handle ) current-fdb-file-handle get handle>> ; @@ -174,16 +149,6 @@ SYMBOL: current-fdb-kvs-handle : fdb-compact-commit ( new-path -- ) fdb-compact fdb-commit-wal-flush ; -: fdb-swap-current-db ( new-path -- ) - current-fdb-kvs-handle [ dispose f ] change - fdb-open-default-config - [ current-fdb-file-handle set ] - [ current-fdb-kvs-handle set ] bi* ; - -: fdb-compact-and-swap-db ( path -- ) - next-vnode-version-name - [ fdb-compact fdb-commit-wal-flush ] - [ fdb-swap-current-db ] bi ; ! Call from within with-foresdb : fdb-open-snapshot ( seqnum -- handle ) @@ -328,6 +293,33 @@ T{ doc PRIVATE> + +: get-kvs-default-config ( -- kvs-config ) + S{ fdb_kvs_config + { create_if_missing t } + { custom_cmp f } + } clone ; + +: fdb-open ( path config -- file-handle ) + [ f void* ] 2dip + [ absolute-path ensure-fdb-filename-directory ] dip + [ fdb_open fdb-check-error ] 3keep + 2drop void* deref ; + +: fdb-open-default-config ( path -- file-handle ) + fdb_get_default_config fdb-open ; + +: fdb-kvs-open-config ( name config -- kvs-handle ) + [ + current-fdb-file-handle get handle>> + f void* + ] 2dip + [ fdb_kvs_open fdb-check-error ] 3keep 2drop + void* deref ; + +: fdb-kvs-open ( name -- kvs-handle ) + get-kvs-default-config fdb-kvs-open-config ; + : with-fdb-map ( start-key end-key fdb_iterator_opt_t iterator-init iterator-next quot: ( obj -- ) -- ) [ execute ] 2dip swap @@ -351,6 +343,27 @@ PRIVATE> [ FDB_ITR_NONE \ fdb-iterator-byseq-init \ fdb_iterator_next ] dip with-fdb-map ; inline + +: with-kvs ( name quot -- ) + [ + [ fdb-kvs-open &dispose current-fdb-kvs-handle ] dip with-variable + ] with-destructors ; inline + + +: with-default-kvs ( quot -- ) + [ "default" ] dip with-kvs ; inline + +: with-forestdb ( path quot -- ) + [ + [ fdb-open-default-config &dispose current-fdb-file-handle ] dip with-variable + ] with-destructors ; inline + +: with-forestdb-kvs ( path name quot -- ) + '[ + _ _ with-kvs + ] with-forestdb ; inline + +/* ! Do not try to commit here, as it will fail with FDB_RESULT_RONLY_VIOLATION ! fdb-current is weird, it gets replaced if you call fdb-rollback ! Therefore, only clean up fdb-current once, and clean it up at the end @@ -398,3 +411,4 @@ PRIVATE> : with-forestdb-path ( path quot -- ) [ absolute-path fdb-open-default-config ] dip with-forestdb-handles-commit-wal ; inline ! [ absolute-path fdb-open-default-config ] dip with-forestdb-handle-commit-normal ; inline +*/ \ No newline at end of file diff --git a/extra/forestdb/utils/utils.factor b/extra/forestdb/utils/utils.factor index edc5593c8d..36a440c92b 100644 --- a/extra/forestdb/utils/utils.factor +++ b/extra/forestdb/utils/utils.factor @@ -1,12 +1,17 @@ ! Copyright (C) 2014 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: assocs continuations forestdb.lib io.directories +USING: assocs continuations forestdb.lib fry io.directories io.files.temp kernel math.parser math.ranges sequences ; IN: forestdb.utils : test-db-0 ( -- path ) "0.forestdb.0" temp-file ; : test-db-1 ( -- path ) "1.forestdb.0" temp-file ; +: with-forestdb-tester ( path quot -- ) + '[ + "default" _ with-kvs + ] with-forestdb ; inline + : delete-test-db-0 ( -- ) [ test-db-0 delete-file ] ignore-errors ; : delete-test-db-1 ( -- ) [ test-db-1 delete-file ] ignore-errors ;