Subject: db(1) show size of key and/or value (with patch)
To: None <tech-userlevel@netbsd.org>
From: Jeremy C. Reed <reed@reedmedia.net>
List: tech-userlevel
Date: 08/30/2007 06:01:38
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)

Another example:

$ db -s btree /var/db/pkg/pkgdb.byfile.db | head -3
/usr/pkg//bin/ccmakedep (24)    xorg-imake-6.9.0nb1     (20)
/usr/pkg//bin/cleanlinks        (25)    xorg-imake-6.9.0nb1     (20)
/usr/pkg//bin/gccmakedep        (25)    xorg-imake-6.9.0nb1     (20)

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)

Would you prefer different output format?

Here is the patch:

Index: usr.bin/db/db.1
===================================================================
RCS file: /cvsroot/src/usr.bin/db/db.1,v
retrieving revision 1.17
diff -u -r1.17 db.1
--- usr.bin/db/db.1	20 Jun 2005 02:53:38 -0000	1.17
+++ usr.bin/db/db.1	30 Aug 2007 10:53:11 -0000
@@ -231,6 +231,9 @@
 Defaults to a single tab
 .Pq Sq \et .
 .
+.It Fl s
+Display size of key and/or data in parentheses.
+.
 .It Fl S Ar visitem
 Specify items to
 .Xr strvis 3
Index: usr.bin/db/db.c
===================================================================
RCS file: /cvsroot/src/usr.bin/db/db.c,v
retrieving revision 1.15
diff -u -r1.15 db.c
--- usr.bin/db/db.c	3 Apr 2007 04:52:32 -0000	1.15
+++ usr.bin/db/db.c	30 Aug 2007 10:53:11 -0000
@@ -59,6 +59,7 @@
 	F_DELETE	= 1<<1,
 	F_SHOW_KEY	= 1<<2,
 	F_SHOW_VALUE	= 1<<3,
+	F_SHOW_SIZE	= 1<<4,
 	F_QUIET		= 1<<10,
 	F_IGNORECASE	= 1<<11,
 	F_ENDIAN_BIG	= 1<<12,
@@ -120,7 +121,7 @@
 
 				/* parse arguments */
 	while ( (ch = getopt(argc, argv,
-			     "CDdE:F:f:iKm:NO:qRS:T:U:VwX:")) != -1) {
+			     "CDdE:F:f:iKm:NO:qRsS:T:U:VwX:")) != -1) {
 		switch (ch) {
 
 		case 'C':
@@ -202,6 +203,10 @@
 				    optarg);
 			break;
 
+		case 's':
+			flags |= F_SHOW_SIZE;
+			break;
+
 		case 'T':
 			encflags = parse_encode_option(&optarg);
 			if (! encflags)
@@ -370,6 +375,10 @@
 			data = (char *)key->data;
 		}
 		printf("%.*s", len, data);
+		if (flags & F_SHOW_SIZE) {
+			printf("%s", outputsep);
+			printf("(%zd)", key->size);
+		}
 	}
 	if ((flags & F_SHOW_KEY) && (flags & F_SHOW_VALUE))
 		printf("%s", outputsep);
@@ -382,6 +391,10 @@
 			data = (char *)val->data;
 		}
 		printf("%.*s", len, data);
+		if (flags & F_SHOW_SIZE) {
+			printf("%s", outputsep);
+			printf("(%zd)", val->size);
+		}
 	}
 	printf("\n");
 }

  Jeremy C. Reed