forestdb.lib: fdb_doc has to be malloc'd. Iterators have to fdb_doc_free docs. Add with-doc and with-create-doc combinators. Unit testing fdb-get etc.

db4
Doug Coleman 2014-11-07 01:56:37 -08:00
parent 9348cb48ef
commit 2f2cd7b299
2 changed files with 83 additions and 7 deletions

View File

@ -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 <seqnum-doc> [
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 <seqnum-doc> [
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

View File

@ -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> ( key -- doc )
fdb_doc malloc-struct
swap [ utf8 malloc-string >>key ] [ length >>keylen ] bi ;
: <seqnum-doc> ( 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 <struct> [ fdb_get_dbinfo fdb-check-error ] keep ;
@ -183,7 +204,7 @@ M: fdb-iterator dispose*
: fdb-iterate ( iterator word -- doc )
'[
fdb_doc <struct> fdb_doc <ref>
fdb_doc malloc-struct fdb_doc <ref>
[ _ execute ] keep swap check-iterate-result
] call ; inline