Subject: Re: db(1) show size of key and/or value (with patch)
To: Luke Mewburn <lukem@NetBSD.org>
From: Jeremy C. Reed <reed@reedmedia.net>
List: tech-userlevel
Date: 08/31/2007 08:35:50
On Fri, 31 Aug 2007, Luke Mewburn wrote:
> On Thu, Aug 30, 2007 at 06:01:38AM -0500, Jeremy C. Reed wrote:
> | Any thoughts on this db(1) -s addition:
> |
> | $ db -S b -T w -s hash /var/db/services.db | head -5
> | 37 (3) gopher\04070/tcp\040nicname\040 (23)
> | \M-~pop3s (7) 197 (4)
> | 286 (4) afs3-resserver\0407010/udp\040icq\040 (29)
> | \M^?87/tcp (8) 43 (3)
> | 38 (3) gopher\04070/udp\040nicname\040 (23)
>
> I'm curious; what did you need this for that a post-processor
> program (hi awk(1) :) couldn't do?
I guess I could, but then I couldn't easily use the vis(3) feature and
would need to pipe through vis(1) at end.
$ db hash /home/reed/tmp/abc.db | awk '{ print $1, length($1), $2, length($2); }'
key 3 value 5
key2 4 0
(My key2 was made with an "" empty value.)
Now here is pipe through vis:
length($2); }' | vis -w
$ db hash /home/reed/tmp/abc.db | \
awk '{ print $1, length($1), $2, length($2); }' | vis -w
key\0403\040value\0405\012key2\0404\040\0400\012glacier:/usr/src/usr.bin/db$
Notice my prompt above and default tab delimiter became literal "\040".
It just seemed easier in db(1) versus doing more awk scripting to get it
right.
> | Is it okay that the size is one larger? For example:
> |
> | $ db -w hash abc.db key value
> | Added key `key'
> | $ db -s hash abc.db
> | key (4) value (6)
>
> The size is including the trailing NUL.
> Does it return the correct length when using -N ?
No.
So I did printf("%zd", key->size - 1);
> | Would you prefer different output format?
>
> If we implemented this, I'd remove the surrounding parenthesis
> to make the number easy to parse.
Okay:
$ /usr/src/usr.bin/db/obj/db -S b -T w -s hash /var/db/services.db | head -5
37 2 gopher\04070/tcp\040nicname\040 22
\M-~pop3s 6 197 3
286 3 afs3-resserver\0407010/udp\040icq\040 28
\M^?87/tcp 7 43 2
38 2 gopher\04070/udp\040nicname\040 22
Thanks for your feedback. (It is nice to receive feedback from the
AUTHOR :) db(1) sure is useful -- is your db(1) included with any other
operating systems?
Jeremy C. Reed