diff --git a/extra/forestdb/lib/lib-tests.factor b/extra/forestdb/lib/lib-tests.factor index 2658e91117..1de118b7bb 100644 --- a/extra/forestdb/lib/lib-tests.factor +++ b/extra/forestdb/lib/lib-tests.factor @@ -4,7 +4,7 @@ USING: accessors alien.c-types alien.data alien.strings arrays assocs combinators continuations destructors forestdb.ffi fry io.directories io.files.temp io.pathnames kernel libc make math.parser math.ranges multiline namespaces sequences -tools.test ; +tools.test classes.struct ; IN: forestdb.lib : test-db-0 ( -- path ) "0.forestdb.0" temp-file ; @@ -52,6 +52,59 @@ IN: forestdb.lib ] with-forestdb-path ] unit-test +! Get +{ + { "key1" "val" } +} [ + delete-test-db-1 test-db-1 [ + 5 set-kv-n + fdb-commit-normal + "key1" "meta" "val" [ + fdb_doc>doc [ key>> ] [ body>> ] bi 2array + ] with-create-doc + ] with-forestdb-path +] unit-test + +{ + { "key1" f "val1" } +} [ + delete-test-db-1 test-db-1 [ + 5 set-kv-n + fdb-commit-normal + "key1" "no meta" "going away" [ + fdb-get + fdb_doc>doc [ key>> ] [ meta>> ] [ body>> ] tri 3array + ] with-create-doc + ] with-forestdb-path +] unit-test + + +{ + { "key2" f "val2" } +} [ + delete-test-db-1 test-db-1 [ + 5 set-kv-n + fdb-commit-normal + 2 [ + fdb-get-byseq fdb_doc>doc + [ key>> ] [ meta>> ] [ body>> ] tri 3array + ] with-doc + ] with-forestdb-path +] unit-test + +{ + { "key2" f "val2" } +} [ + delete-test-db-1 test-db-1 [ + 5 set-kv-n + fdb-commit-normal + 2 [ + fdb-get-byseq fdb_doc>doc + [ key>> ] [ meta>> ] [ body>> ] tri 3array + ] with-doc + ] with-forestdb-path +] unit-test + ! Filename is only valid inside with-forestdb { f } [ @@ -65,17 +118,19 @@ IN: forestdb.lib { 6 9 9 } [ delete-test-db-0 test-db-0 [ - "key123" "meta blah" "some body" fdb-doc-create - [ keylen>> ] [ metalen>> ] [ bodylen>> ] tri + "key123" "meta blah" "some body" [ + [ keylen>> ] [ metalen>> ] [ bodylen>> ] tri + ] with-create-doc ] with-forestdb-path ] unit-test { 7 8 15 } [ delete-test-db-0 test-db-0 [ - "key1234" "meta blah" "some body" fdb-doc-create - dup "new meta" "some other body" fdb-doc-update - [ keylen>> ] [ metalen>> ] [ bodylen>> ] tri + "key1234" "meta blah" "some body" [ + [ "new meta" "some other body" fdb-doc-update ] + [ [ keylen>> ] [ metalen>> ] [ bodylen>> ] tri ] bi + ] with-create-doc ] with-forestdb-path ] unit-test diff --git a/extra/forestdb/lib/lib.factor b/extra/forestdb/lib/lib.factor index b76d6eb46b..0acc3af5ae 100644 --- a/extra/forestdb/lib/lib.factor +++ b/extra/forestdb/lib/lib.factor @@ -14,6 +14,8 @@ IN: forestdb.lib ! 3) build on macosx doesn't include -L/usr/local/lib when it finds snappy ! - link_directories(/usr/local/lib) or some other fix ! 4) byseq iteration doesn't have bodies, weird. +! 5) Get byseq ignores seqnum and uses key instead if key is set + */ ERROR: fdb-error error ; @@ -59,6 +61,14 @@ SYMBOL: fdb-current [ get-handle ] 2dip [ dup length ] bi@ fdb_set_kv fdb-check-error ; +: ( key -- doc ) + fdb_doc malloc-struct + swap [ utf8 malloc-string >>key ] [ length >>keylen ] bi ; + +: ( seqnum -- doc ) + fdb_doc malloc-struct + swap >>seqnum ; + ! Fill in document by exemplar : fdb-get ( doc -- doc ) [ get-handle ] dip [ fdb_get fdb-check-error ] keep ; @@ -110,6 +120,17 @@ SYMBOL: fdb-current : fdb-doc-free ( doc -- ) fdb_doc_free fdb-check-error ; +: clear-doc-key ( doc -- doc ) + [ dup [ (free) f ] when ] change-key + 0 >>keylen ; + +: with-doc ( doc quot: ( doc -- ) -- ) + over '[ _ _ [ _ fdb-doc-free rethrow ] recover ] call ; inline + +: with-create-doc ( key meta body quot: ( doc -- ) -- ) + [ fdb-doc-create ] dip with-doc ; inline + + : fdb-info ( -- info ) get-handle fdb_info [ fdb_get_dbinfo fdb-check-error ] keep ; @@ -183,7 +204,7 @@ M: fdb-iterator dispose* : fdb-iterate ( iterator word -- doc ) '[ - fdb_doc fdb_doc + fdb_doc malloc-struct fdb_doc [ _ execute ] keep swap check-iterate-result ] call ; inline