From 1be756644f233cf92f54886fd260615c13927ed1 Mon Sep 17 00:00:00 2001 From: jkh Date: Tue, 22 Apr 1997 18:02:52 +0000 Subject: Update to version 1.9. Submitted by: Shigio Yamaguchi --- usr.bin/global/btreeop/btreeop.1 | 46 ++++++++++++++----- usr.bin/global/btreeop/btreeop.c | 96 +++++++++++++++++++++++++++++++++------- 2 files changed, 115 insertions(+), 27 deletions(-) (limited to 'usr.bin/global/btreeop') diff --git a/usr.bin/global/btreeop/btreeop.1 b/usr.bin/global/btreeop/btreeop.1 index b6baa6d..08bbf51 100644 --- a/usr.bin/global/btreeop/btreeop.1 +++ b/usr.bin/global/btreeop/btreeop.1 @@ -28,7 +28,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd April 21, 1996 +.Dd April 21, 1997 .Dt BTREEOP 1 .Os BSD 4 .Sh NAME @@ -36,7 +36,9 @@ .Nd btree database maintenance tool .Sh SYNOPSIS .Nm btreeop +.Op Fl A .Op Fl C +.Op Fl D Ar key .Op Fl K Ar key .Op Fl b .Op Fl c Ar cashesize @@ -49,16 +51,21 @@ execute simple operations for .Xr btree 3 database. .Nm Btreeop -can create database and read (sequential or index) from it . +can create database, write record, read record (sequential or index) and +delete record from it. Duplicate entries are allowed. .Sh OPTIONS A capital letter means a command. If no command specified then it assume sequential read operation. .Bl -tag -width Ds +.It Fl A +append records. If database doesn't exist, btreeop creates it. .It Fl C -create database. +create database and write records to it. +.It Fl D Ar key +delete records by the key. .It Fl K Ar key -search data by the key. +search records by the key. .It Fl b assume BIG_ENDIAN byte order. default is LITTLE_ENDIAN. .It Fl c Ar cashesize @@ -74,7 +81,7 @@ of BTREEINFO. (see btree(3)) .It Ar dbname database name. default is 'btree'. .Sh DATA FORMAT -To creat database, +To creat (or append) database, .Nm btreeop read data from stdin. The format of the data is the following. @@ -96,6 +103,10 @@ Key cannot include blank. Data can include blank. .It Null Data not allowed. +.It +Additionally, META record is available. META record has a key that start with +a blank. You can read this record only by indexed search (with -K option). +Usage is unlimited by Btreeop. .El .Sh EXAMPLES Create database. @@ -103,11 +114,18 @@ Create database. % btreeop -C key1 data1 key2 data2 - key2 data2-2 key3 data3 ^D % +Append records. + + % btreeop -A + __.VERSION 2 + key2 data2-2 + ^D + % + Sequential read. % btreeop @@ -122,6 +140,14 @@ Indexed read. % btreeop -K key2 key2 data2-2 key2 data2 + % btreeop -K ' __.VERSION' + __.VERSION 2 + % + +Delete record. + + % btreeop -D ' __.VERSION' + % btreeop -K ' __.VERSION' % .Sh FILES @@ -133,10 +159,10 @@ default database name. .Nm Btreeop exits with a value of 1 if an error occurred, 0 otherwise. .Sh SEE ALSO -.Xr btree 3 , -.Sh BUGS -.Nm btreeop -cannot utilize all features of .Xr btree 3 .Sh AUTHOR Shigio Yamaguchi (shigio@wafu.netgate.net) +.Sh HISTORY +The +.Nm +command appeared in FreeBSD 2.2. diff --git a/usr.bin/global/btreeop/btreeop.c b/usr.bin/global/btreeop/btreeop.c index 1d2faeb..a202578 100644 --- a/usr.bin/global/btreeop/btreeop.c +++ b/usr.bin/global/btreeop/btreeop.c @@ -28,7 +28,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * btreeop.c 5-Apr-97 + * btreeop.c 21-Apr-97 * */ #include @@ -56,9 +56,12 @@ void die __P((char *)); void usage __P((void)); void entab __P((char *)); void main __P((int, char **)); -int dbcreate __P((DB *)); +int dbwrite __P((DB *)); int dbkey __P((DB *, char *)); int dbscan __P((DB *)); +int dbdel __P((DB *, char *)); +DB *db; +char *key; #ifndef LITTLE_ENDIAN #define LITTLE_ENDIAN 1234 @@ -78,7 +81,7 @@ char *s; void usage() { fprintf(stderr, - "usage: %s [-C][-K key][-b][-c cachesize][-l][-p psize][dbname]\n", + "usage: %s [-A][-C][-D key][-K key][-b][-c cachesize][-l][-p psize][dbname]\n", progname); exit(1); } @@ -126,11 +129,14 @@ main(argc, argv) int argc; char *argv[]; { - char command = 0; + char command = 'R'; char *key = NULL; DB *db; BTREEINFO info; int c; + int flags; + extern char *optarg; + extern int optind; info.flags = R_DUP; /* allow duplicate entries */ info.cachesize = 500000; @@ -141,11 +147,15 @@ char *argv[]; info.prefix = NULL; info.lorder = LITTLE_ENDIAN; - while ((c = getopt(argc, argv, "CK:bc:lp:")) != EOF) { + while ((c = getopt(argc, argv, "ACD:K:bc:lp:")) != EOF) { switch (c) { case 'K': + case 'D': key = optarg; + case 'A': case 'C': + if (command != 'R') + usage(); command = c; break; case 'b': @@ -166,21 +176,35 @@ char *argv[]; } dbname = (optind < argc) ? argv[optind] : dbdefault; - db = dbopen(dbname, command == 'C' ? - O_RDWR|O_CREAT|O_TRUNC : O_RDONLY, - 0644, DB_BTREE, &info); - + switch (command) { + case 'A': + case 'D': + flags = O_RDWR|O_CREAT; + break; + case 'C': + flags = O_RDWR|O_CREAT|O_TRUNC; + break; + case 'K': + case 'R': + flags = O_RDONLY; + break; + } + db = dbopen(dbname, flags, 0644, DB_BTREE, &info); if (db == NULL) { die("dbopen failed."); } switch (command) { + case 'A': /* Append records */ case 'C': /* Create database */ - dbcreate(db); + dbwrite(db); + break; + case 'D': /* Delete records */ + dbdel(db, key); break; - case 'K': /* Keyed search */ + case 'K': /* Keyed (indexed) read */ dbkey(db, key); break; - default: /* Scan all data */ + case 'R': /* sequencial Read */ dbscan(db); break; } @@ -190,13 +214,13 @@ char *argv[]; exit(0); } /* - * dbcreate: create database + * dbwrite: write to database * * i) db * r) 0: normal */ int -dbcreate(db) +dbwrite(db) DB *db; { DBT key, dat; @@ -216,6 +240,12 @@ DB *db; * - Key cannot include blank. * - Data can include blank. * - Null Data not allowed. + * + * META record: + * You can write meta record by making key start with a ' '. + * You can read this record only by indexed read ('-K' option). + * +------------------ + * | __.VERSION 2 */ while (fgets(buf, BUFSIZ, stdin)) { if (buf[strlen(buf)-1] == '\n') /* chop(buf) */ @@ -223,7 +253,12 @@ DB *db; else while (fgetc(stdin) != '\n') ; - for (c = buf; *c && !isspace(*c); c++) /* skip key part */ + c = buf; + if (*c == ' ') { /* META record */ + if (*++c == ' ') + die("illegal format."); + } + for (; *c && !isspace(*c); c++) /* skip key part */ ; if (*c == 0) die("data part not found."); @@ -277,14 +312,13 @@ char *skey; status = (*db->seq)(db, &key, &dat, R_NEXT)) { (void)fprintf(stdout, "%s\n", (char *)dat.data); } - if (status == RET_ERROR) die("db->seq failed."); return (0); } /* - * dbscan: Scan all data + * dbscan: Scan all records * * i) db * r) 0: normal @@ -300,9 +334,37 @@ DB *db; for (status = (*db->seq)(db, &key, &dat, R_FIRST); status == RET_SUCCESS; status = (*db->seq)(db, &key, &dat, R_NEXT)) { + /* skip META record */ + if (*(char *)key.data == ' ') + continue; (void)fprintf(stdout, "%s\n", (char *)dat.data); } if (status == RET_ERROR) die("db->seq failed."); return (0); } + +/* + * dbdel: Delete records + * + * i) db + * i) key key + * r) 0: normal + * 1: not found + */ +int +dbdel(db, skey) +DB *db; +char *skey; +{ + DBT key; + int status; + + key.data = skey; + key.size = strlen(skey)+1; + + status = (*db->del)(db, &key, 0); + if (status == RET_ERROR) + die("db->del failed."); + return (0); +} -- cgit v1.1