 IN: forestdb.lib
 
 /*
-! Possible bugs in foresdtb:
-
-! 1) why is meta set but metalen is 0?
-    ! also, there is no meta info in these docs
-    delete-test-db-1
-    test-db-1 [
-        5 set-kv-n
-        fdb-commit-normal
-        "key1" "key5" [
-              .
-        ] with-fdb-normal-meta-iterator
-    ] with-forestdb-path
-
-    ...
-    S{ fdb_doc
-        { keylen 4 }
-        { metalen 0 }
-        { bodylen 4 }
-        { size_ondisk 0 }
-        { key ALIEN: 12721e3f0 }
-        { seqnum 5 }
-        { offset 4256 }
-        { meta ALIEN: 1272308d0 }
-        { body f }
-        { deleted f }
-    }
-! 2) same for with-fdb-byseq-iterator, body and meta are messed up
-! 3) snapshot has doc_count of entire file, not in that snapshot
-! 4) build on macosx doesn't search /usr/local for libsnappy
-! 5) build on macosx doesn't include -L/usr/local/lib when it finds snappy
+! Issues
+! 1) bug - snapshot has doc_count of entire file, not just that snapshot's doc_count
+! 2) build on macosx doesn't search /usr/local for libsnappy
+! 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.
 */
 
 ERROR: fdb-error error ;


! fdb_doc key, meta, body only valid inside with-forestdb
! so make a helper word to preserve them outside
TUPLE: doc seqnum key meta body deleted? offset size-ondisk ;

CONSTRUCTOR: <doc> doc ( seqnum key meta body deleted? offset size-ondisk -- obj ) ;

/*
! Example fdb_doc and converted doc
S{ fdb_doc
    { keylen 4 }
    { metalen 0 }
    { bodylen 4 }
    { size_ondisk 0 }
    { key ALIEN: 111e003b0 }
    { seqnum 5 }
    { offset 4256 }
    { meta f }
    { body ALIEN: 111d11740 }
    { deleted f }
}
T{ doc
    { seqnum 5 }
    { key "key5" }
    { body "val5" }
    { offset 4256 }
    { size-ondisk 0 }
}
*/

: alien/length>string ( alien n -- string/f )
    [ drop f ] [ memory>byte-array utf8 decode ] if-zero ;

: fdb_doc>doc ( fdb_doc -- doc )
    {
        [ seqnum>> ]
        [ [ key>> ] [ keylen>> ] bi alien/length>string ]
        [ [ meta>> ] [ metalen>> ] bi alien/length>string ]
        [ [ body>> ] [ bodylen>> ] bi alien/length>string ]
        [ deleted>> >boolean ]
        [ offset>> ]

    [ FDB_ITR_NONE \ fdb-iterator-init \ fdb-iterator-next ] dip
    with-fdb-iterator ; inline

: with-fdb-normal-meta-iterator ( start-key end-key quot -- )
    [ FDB_ITR_NONE \ fdb-iterator-init \ fdb-iterator-next-meta-only ] dip
    with-fdb-iterator ; inline

: with-fdb-byseq-iterator ( start-seq end-seq quot -- )
    [ FDB_ITR_NONE \ fdb-iterator-byseq-init \ fdb-iterator-next-meta-only ] dip
    with-fdb-iterator ; 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
