diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2021-11-14 09:58:53 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2021-11-14 09:58:53 +0200 |
commit | b35f6d298ecf4f2c7e967b92e7a4d23609f89ea4 (patch) | |
tree | 060966512381fdeb55a9f1c7e9eaa790aa3fa1bb | |
parent | ed163b6c8e56de85ee67f218a5da4a1fc6ee6312 (diff) | |
download | gdbm-dircache.tar.gz |
New "introspection" option codes.dircache
* doc/gdbm.texi: Document new option codes.
* src/gdbm.h.in (GDBM_GETDBFORMAT, GDBM_GETDIRDEPTH)
(GDBM_GETBUCKETSIZE): New option codes.
* src/gdbmsetopt.c: Handle new option codes.
(setopt_gdbm_getflags): Return the state of GDBM_CLOEXEC and GDBM_NUMSYNC
bits.
-rw-r--r-- | doc/gdbm.texi | 34 | ||||
-rw-r--r-- | src/gdbm.h.in | 10 | ||||
-rw-r--r-- | src/gdbmsetopt.c | 60 |
3 files changed, 95 insertions, 9 deletions
diff --git a/doc/gdbm.texi b/doc/gdbm.texi index 6bc3d01..50d06ba 100644 --- a/doc/gdbm.texi +++ b/doc/gdbm.texi @@ -2090,9 +2090,9 @@ point to a @code{size_t} holding the desired cache size, or the constant @code{GDBM_CACHE_AUTO}, to adjust the cache size automatically. -By default, a newly open database is configured to adapt the cache -size to the number of index buckets in the database file. This -provides for the best performance. +By default, a newly open database is configured to dynamically +accommodate the cache size to the number of index buckets in the +database file. This provides for the best performance. If another @var{value} is set, it is adjusted to the nearest larger power of two. @@ -2109,7 +2109,8 @@ gdbm_bucket_count (dbf, &bn); ret = gdbm_setopt (dbf, GDBM_SETCACHESIZE, &bn, sizeof (bn)); @end example -To set the best cache size, use the constant @code{GDBM_CACHE_AUTO}: +To request the automatically adjustable cache size, use the constant +@code{GDBM_CACHE_AUTO}: @example size_t bn = GDBM_CACHE_AUTO; @@ -2118,8 +2119,9 @@ ret = gdbm_setopt (dbf, GDBM_SETCACHESIZE, &bn, sizeof (bn)); @end defvr @defvr {Option} GDBM_GETCACHESIZE -Return the size of the internal bucket cache. The @var{value} should -point to a @code{size_t} variable, where the size will be stored. +Return the actual size of the internal bucket cache. The @var{value} +should point to a @code{size_t} variable, where the size will be +stored. @end defvr @defvr {Option} GDBM_SETCACHEAUTO @@ -2153,6 +2155,26 @@ its value will be similar to the flags used when opening the database (which may have been altered by another calls to @code{gdbm_setopt}). @end defvr +@defvr {Option} GDBM_GETDBFORMAT +Return the database format. The @var{value} should point to an +@code{int} variable. Upon successful return, it will be set to +@samp{0} if the database is in standard format and @code{GDBM_NUMSYNC} +if it is in extended format. @xref{Database format}. +@end defvr + +@defvr {Option} GDBM_GETDIRDEPTH +Returns the @dfn{directory depth}: the number of initial (most significant) +bits in hash value that are interpreted as index to the directory. The +actual directory size can be computed as @code{1 << @var{value}}. + +The @var{value} argument should point to an @code{int}. +@end defvr + +@defvr {Option} GDBM_GETBUCKETSIZE +Returns the @dfn{bucket capacity}: maximum number of keys per bucket +(@code{int}). +@end defvr + @defvr {Option} GDBM_FASTMODE Enable or disable the @dfn{fast writes mode}, i.e.@: writes without subsequent synchronization. The @var{value} should point diff --git a/src/gdbm.h.in b/src/gdbm.h.in index 97763fe..e371e9b 100644 --- a/src/gdbm.h.in +++ b/src/gdbm.h.in @@ -88,9 +88,13 @@ extern "C" { # define GDBM_GETMAXMAPSIZE 14 /* Get maximum mapped memory size */ # define GDBM_GETDBNAME 15 /* Return database file name */ # define GDBM_GETBLOCKSIZE 16 /* Return block size */ - -# define GDBM_GETCACHEAUTO 17 /* Get the value of cache auto-adjustment */ -# define GDBM_SETCACHEAUTO 18 /* Set the value of cache auto-adjustment */ +# define GDBM_GETDBFORMAT 17 /* Return the database format */ +# define GDBM_GETDIRDEPTH 18 /* Directory depth: number of initial (most + significant) bits in hash interpreted as + index to the directory. */ +# define GDBM_GETBUCKETSIZE 19 /* Get number of elements per bucket */ +# define GDBM_GETCACHEAUTO 20 /* Get the value of cache auto-adjustment */ +# define GDBM_SETCACHEAUTO 21 /* Set the value of cache auto-adjustment */ # define GDBM_CACHE_AUTO 0 diff --git a/src/gdbmsetopt.c b/src/gdbmsetopt.c index 52f3211..15c7ada 100644 --- a/src/gdbmsetopt.c +++ b/src/gdbmsetopt.c @@ -280,14 +280,24 @@ setopt_gdbm_getflags (GDBM_FILE dbf, void *optval, int optlen) else { int flags = dbf->read_write; + if (!dbf->fast_write) flags |= GDBM_SYNC; + if (!dbf->file_locking) flags |= GDBM_NOLOCK; + if (!dbf->memory_mapping) flags |= GDBM_NOMMAP; else if (dbf->mmap_preread) flags |= GDBM_PREREAD; + + if (dbf->cloexec) + flags |= GDBM_CLOEXEC; + + if (dbf->header->header_magic == GDBM_NUMSYNC_MAGIC) + flags |= GDBM_NUMSYNC; + *(int*) optval = flags; } return 0; @@ -326,6 +336,53 @@ setopt_gdbm_getblocksize (GDBM_FILE dbf, void *optval, int optlen) GDBM_SET_ERRNO (dbf, GDBM_OPT_BADVAL, FALSE); return -1; } + +static int +setopt_gdbm_getdbformat (GDBM_FILE dbf, void *optval, int optlen) +{ + if (optval && optlen == sizeof (int)) + { + switch (dbf->header->header_magic) + { + case GDBM_OMAGIC: + case GDBM_MAGIC: + *(int*)optval = 0; + break; + + case GDBM_NUMSYNC_MAGIC: + *(int*)optval = GDBM_NUMSYNC; + } + } + + GDBM_SET_ERRNO (dbf, GDBM_OPT_BADVAL, FALSE); + return -1; +} + +static int +setopt_gdbm_getdirdepth (GDBM_FILE dbf, void *optval, int optlen) +{ + if (optval && optlen == sizeof (int)) + { + *(int*) optval = dbf->header->dir_bits; + return 0; + } + + GDBM_SET_ERRNO (dbf, GDBM_OPT_BADVAL, FALSE); + return -1; +} + +static int +setopt_gdbm_getbucketsize (GDBM_FILE dbf, void *optval, int optlen) +{ + if (optval && optlen == sizeof (int)) + { + *(int*) optval = dbf->header->bucket_elems; + return 0; + } + + GDBM_SET_ERRNO (dbf, GDBM_OPT_BADVAL, FALSE); + return -1; +} typedef int (*setopt_handler) (GDBM_FILE, void *, int); @@ -348,6 +405,9 @@ static setopt_handler setopt_handler_tab[] = { [GDBM_GETFLAGS] = setopt_gdbm_getflags, [GDBM_GETDBNAME] = setopt_gdbm_getdbname, [GDBM_GETBLOCKSIZE] = setopt_gdbm_getblocksize, + [GDBM_GETDBFORMAT] = setopt_gdbm_getdbformat, + [GDBM_GETDIRDEPTH] = setopt_gdbm_getdirdepth, + [GDBM_GETBUCKETSIZE] = setopt_gdbm_getbucketsize, [GDBM_GETCACHEAUTO] = setopt_gdbm_getcacheauto, [GDBM_SETCACHEAUTO] = setopt_gdbm_setcacheauto, }; |