diff --git a/extra/gdbm/gdbm-docs.factor b/extra/gdbm/gdbm-docs.factor index e712da3c99..18e5d5cf33 100644 --- a/extra/gdbm/gdbm-docs.factor +++ b/extra/gdbm/gdbm-docs.factor @@ -44,6 +44,18 @@ HELP: exists? { $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 } "." } ; @@ -125,8 +137,10 @@ $nl { $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." ; diff --git a/extra/gdbm/gdbm-tests.factor b/extra/gdbm/gdbm-tests.factor index 373ae1c624..b720dfc0f7 100644 --- a/extra/gdbm/gdbm-tests.factor +++ b/extra/gdbm/gdbm-tests.factor @@ -1,6 +1,6 @@ ! 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 @@ -49,12 +49,8 @@ CLEANUP [ 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 diff --git a/extra/gdbm/gdbm.factor b/extra/gdbm/gdbm.factor index 021fadd514..6223a6b79e 100644 --- a/extra/gdbm/gdbm.factor +++ b/extra/gdbm/gdbm.factor @@ -93,6 +93,16 @@ PRIVATE> : 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 ;