gdbm: each-key - higher order combinator for sequential access

db4
Dmitry Shubin 2010-06-28 03:30:52 +04:00
parent 6705f4f466
commit 0c020a47d8
3 changed files with 28 additions and 8 deletions

View File

@ -44,6 +44,18 @@ HELP: exists?
{ $values { "key" object } { "?" boolean } } { $values { "key" object } { "?" boolean } }
{ $description "Searches for a particular key without retreiving it." } ; { $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 HELP: gdbm-file-descriptor
{ $values { "desc" integer } } { $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 } "." } ; { $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 } { $subsections insert exists? fetch delete }
{ $heading "Sequential access" } { $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 } { $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." "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."
; ;

View File

@ -1,6 +1,6 @@
! Copyright (C) 2010 Dmitry Shubin. ! Copyright (C) 2010 Dmitry Shubin.
! See http://factorcode.org/license.txt for BSD license. ! 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 ; io.files.temp kernel sequences sets tools.test ;
IN: gdbm.tests IN: gdbm.tests
@ -49,12 +49,8 @@ CLEANUP
[ t ] [ t ]
[ [
V{ } V{ } [ [ 2array append ] each-record ] with-test.db
[ V{ "foo" "bar" "baz" 42 43 44 } set=
first-key
[ next-key* ] [ [ swap push ] 2keep ] do while drop
] with-test.db
V{ "foo" "bar" "baz" } set=
] unit-test ] unit-test

View File

@ -93,6 +93,16 @@ PRIVATE>
: first-key ( -- key/f ) first-key* drop ; : first-key ( -- key/f ) first-key* drop ;
: next-key ( key -- key/f ) next-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 ; : reorganize ( -- ) dbf gdbm_reorganize check-error ;
: synchronize ( -- ) dbf gdbm_sync ; : synchronize ( -- ) dbf gdbm_sync ;