{ $values { "key" object } { "?" boolean } }
{ $description "Searches for a particular key without retreiving it." } ;
+HELP: each-key
+{ $values { "quot" quotation } }
+{ $description "Applies the quotation to the each key in the database." } ;
+
+HELP: each-value
+{ $values { "quot" quotation } }
+{ $description "Applies the quotation to the each value in the database." } ;
+
+HELP: each-record
+{ $values { "quot" quotation } }
+{ $description "Applies the quotation to the each key-value pair in the database." } ;
+
HELP: gdbm-file-descriptor
{ $values { "desc" integer } }
{ $description "Returns the file descriptor of the database. This is used for manual database locking if it was opened with " { $snippet "nolock" } " flag set to " { $link t } "." } ;
{ $subsections insert exists? fetch delete }
{ $heading "Sequential access" }
-"It is possible to iterate through all records in the database with."
+"It is possible to iterate through all records in the database with"
{ $subsections first-key next-key }
+"The following combinators, however, provide more convenient way to do that:"
+{ $subsections each-key each-value each-record }
"The order in which records are accessed has nothing to do with the order in which records have been stored. Note that these words can only be used in read-only algorithms since delete operation re-arranges the hash table."
;
! Copyright (C) 2010 Dmitry Shubin.
! See http://factorcode.org/license.txt for BSD license.
-USING: accessors continuations gdbm gdbm.ffi io.directories
+USING: accessors arrays continuations gdbm gdbm.ffi io.directories
io.files.temp kernel sequences sets tools.test ;
IN: gdbm.tests
[ t ]
[
- V{ }
- [
- first-key
- [ next-key* ] [ [ swap push ] 2keep ] do while drop
- ] with-test.db
- V{ "foo" "bar" "baz" } set=
+ V{ } [ [ 2array append ] each-record ] with-test.db
+ V{ "foo" "bar" "baz" 42 43 44 } set=
] unit-test
: first-key ( -- key/f ) first-key* drop ;
: next-key ( key -- key/f ) next-key* drop ;
+:: each-key ( ... quot: ( ... key -- ... ) -- ... )
+ first-key*
+ [ [ next-key* ] [ quot keep ] do while ] when drop ; inline
+
+: each-value ( ... quot: ( ... value -- ... ) -- ... )
+ [ fetch ] prepose each-key ; inline
+
+: each-record ( ... quot: ( ... key value -- ... ) -- ... )
+ [ dup fetch ] prepose each-key ; inline
+
: reorganize ( -- ) dbf gdbm_reorganize check-error ;
: synchronize ( -- ) dbf gdbm_sync ;