From: Russ Allbery Date: Mon, 22 Mar 1999 04:26:26 +0000 (-0800) Subject: [5.005_56] Add EXISTS to SDBM_File and libsdbm X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f4b9d8806d76b352b24a8b7684a45c4120e28af1;p=p5sagit%2Fp5-mst-13.2.git [5.005_56] Add EXISTS to SDBM_File and libsdbm Message-ID: p4raw-id: //depot/perl@3161 --- diff --git a/ext/SDBM_File/SDBM_File.xs b/ext/SDBM_File/SDBM_File.xs index 38eaebf..789e5c8 100644 --- a/ext/SDBM_File/SDBM_File.xs +++ b/ext/SDBM_File/SDBM_File.xs @@ -8,6 +8,7 @@ typedef DBM* SDBM_File; #define sdbm_FETCH(db,key) sdbm_fetch(db,key) #define sdbm_STORE(db,key,value,flags) sdbm_store(db,key,value,flags) #define sdbm_DELETE(db,key) sdbm_delete(db,key) +#define sdbm_EXISTS(db,key) sdbm_exists(db,key) #define sdbm_FIRSTKEY(db) sdbm_firstkey(db) #define sdbm_NEXTKEY(db,key) sdbm_nextkey(db) @@ -52,6 +53,11 @@ sdbm_DELETE(db, key) SDBM_File db datum key +int +sdbm_EXISTS(db,key) + SDBM_File db + datum key + datum sdbm_FIRSTKEY(db) SDBM_File db diff --git a/ext/SDBM_File/sdbm/README.too b/ext/SDBM_File/sdbm/README.too index c2d0959..1fec315 100644 --- a/ext/SDBM_File/sdbm/README.too +++ b/ext/SDBM_File/sdbm/README.too @@ -7,3 +7,8 @@ Fri Apr 15 10:15:30 EDT 1994. Additional portability/configuration changes for libsdbm by Andy Dougherty doughera@lafcol.lafayette.edu. + + +Mon Mar 22 03:24:47 PST 1999. + +sdbm_exists added to the library by Russ Allbery . diff --git a/ext/SDBM_File/sdbm/pair.c b/ext/SDBM_File/sdbm/pair.c index a9a805a..d03bfb3 100644 --- a/ext/SDBM_File/sdbm/pair.c +++ b/ext/SDBM_File/sdbm/pair.c @@ -102,6 +102,17 @@ getpair(char *pag, datum key) return val; } +int +exipair(char *pag, datum key) +{ + register short *ino = (short *) pag; + + if (ino[0] == 0) + return 0; + + return (seepair(pag, ino[0], key.dptr, key.dsize) != 0); +} + #ifdef SEEDUPS int duppair(char *pag, datum key) diff --git a/ext/SDBM_File/sdbm/pair.h b/ext/SDBM_File/sdbm/pair.h index 8a675b9..b6944ed 100644 --- a/ext/SDBM_File/sdbm/pair.h +++ b/ext/SDBM_File/sdbm/pair.h @@ -2,6 +2,7 @@ #define chkpage sdbm__chkpage #define delpair sdbm__delpair #define duppair sdbm__duppair +#define exipair sdbm__exipair #define fitpair sdbm__fitpair #define getnkey sdbm__getnkey #define getpair sdbm__getpair @@ -11,6 +12,7 @@ extern int fitpair proto((char *, int)); extern void putpair proto((char *, datum, datum)); extern datum getpair proto((char *, datum)); +extern int exipair proto((char *, datum)); extern int delpair proto((char *, datum)); extern int chkpage proto((char *)); extern datum getnkey proto((char *, int)); diff --git a/ext/SDBM_File/sdbm/sdbm.3 b/ext/SDBM_File/sdbm/sdbm.3 index 7e5c176..fe6fe76 100644 --- a/ext/SDBM_File/sdbm/sdbm.3 +++ b/ext/SDBM_File/sdbm/sdbm.3 @@ -1,7 +1,7 @@ .\" $Id: sdbm.3,v 1.2 90/12/13 13:00:57 oz Exp $ .TH SDBM 3 "1 March 1990" .SH NAME -sdbm, sdbm_open, sdbm_prep, sdbm_close, sdbm_fetch, sdbm_store, sdbm_delete, sdbm_firstkey, sdbm_nextkey, sdbm_hash, sdbm_rdonly, sdbm_error, sdbm_clearerr, sdbm_dirfno, sdbm_pagfno \- data base subroutines +sdbm, sdbm_open, sdbm_prep, sdbm_close, sdbm_fetch, sdbm_store, sdbm_delete, sdbm_exists, sdbm_firstkey, sdbm_nextkey, sdbm_hash, sdbm_rdonly, sdbm_error, sdbm_clearerr, sdbm_dirfno, sdbm_pagfno \- data base subroutines .SH SYNOPSIS .nf .ft B @@ -26,6 +26,8 @@ int sdbm_store(\s-1DBM\s0 *db, datum key, datum val, int flags) .sp int sdbm_delete(\s-1DBM\s0 *db, datum key) .sp +int sdbm_exists(\s-1DBM\s0 *db, datum key) +.sp datum sdbm_firstkey(\s-1DBM\s0 *db) .sp datum sdbm_nextkey(\s-1DBM\s0 *db) @@ -47,6 +49,7 @@ int sdbm_pagfno(\s-1DBM\s0 *db) .IX sdbm_fetch "" "\fLsdbm_fetch\fR \(em fetch \fLsdbm\fR database data" .IX sdbm_store "" "\fLsdbm_store\fR \(em add data to \fLsdbm\fR database" .IX sdbm_delete "" "\fLsdbm_delete\fR \(em remove data from \fLsdbm\fR database" +.IX sdbm_exists "" "\fLsdbm_exists\fR \(em test \fLsdbm\fR key existence" .IX sdbm_firstkey "" "\fLsdbm_firstkey\fR \(em access \fLsdbm\fR database" .IX sdbm_nextkey "" "\fLsdbm_nextkey\fR \(em access \fLsdbm\fR database" .IX sdbm_hash "" "\fLsdbm_hash\fR \(em string hash for \fLsdbm\fR database" @@ -149,6 +152,8 @@ Given a handle, one can retrieve data associated with a key by using the routine, and associate data with a key by using the .BR sdbm_store (\|) routine. +.BR sdbm_exists (\|) +will say whether a given key exists in the database. .LP The values of the .I flags diff --git a/ext/SDBM_File/sdbm/sdbm.c b/ext/SDBM_File/sdbm/sdbm.c index c147e45..47de0b9 100644 --- a/ext/SDBM_File/sdbm/sdbm.c +++ b/ext/SDBM_File/sdbm/sdbm.c @@ -182,6 +182,18 @@ sdbm_fetch(register DBM *db, datum key) } int +sdbm_exists(register DBM *db, datum key) +{ + if (db == NULL || bad(key)) + return errno = EINVAL, -1; + + if (getpage(db, exhash(key))) + return exipair(db->pagbuf, key); + + return ioerr(db), -1; +} + +int sdbm_delete(register DBM *db, datum key) { if (db == NULL || bad(key)) diff --git a/ext/SDBM_File/sdbm/sdbm.h b/ext/SDBM_File/sdbm/sdbm.h index 4dc1a22..c3c2a6d 100644 --- a/ext/SDBM_File/sdbm/sdbm.h +++ b/ext/SDBM_File/sdbm/sdbm.h @@ -79,6 +79,7 @@ extern int sdbm_delete proto((DBM *, datum)); extern int sdbm_store proto((DBM *, datum, datum, int)); extern datum sdbm_firstkey proto((DBM *)); extern datum sdbm_nextkey proto((DBM *)); +extern int sdbm_exists proto((DBM *, datum)); /* * other diff --git a/pod/perldelta.pod b/pod/perldelta.pod index 215e68d..beb25c7 100644 --- a/pod/perldelta.pod +++ b/pod/perldelta.pod @@ -351,6 +351,13 @@ act as mutators (accessor $z->Re(), mutator $z->Re(3)). A little bit of radial trigonometry (cylindrical and spherical) added, radial coordinate conversions and the great circle distance. +=item SDBM_File + +An EXISTS method has been added to this module (and sdbm_exists() has +been added to the underlying sdbm library), so one can now call exists +on an SDBM_File tied hash and get the correct result rather than a +runtime error. + =item Time::Local The timelocal() and timegm() functions used to silently return bogus diff --git a/t/lib/sdbm.t b/t/lib/sdbm.t index 70f0b74..af796c1 100755 --- a/t/lib/sdbm.t +++ b/t/lib/sdbm.t @@ -15,7 +15,7 @@ require SDBM_File; #If Fcntl is not available, try 0x202 or 0x102 for O_RDWR|O_CREAT use Fcntl; -print "1..18\n"; +print "1..20\n"; unlink ; @@ -122,13 +122,6 @@ print join(':',200..400) eq join(':',@foo) ? "ok 10\n" : "not ok 10\n"; print ($h{'foo'} eq '' ? "ok 11\n" : "not ok 11\n"); print ($h{''} eq 'bar' ? "ok 12\n" : "not ok 12\n"); -untie %h; -if ($^O eq 'VMS') { - unlink 'Op_dbmx.sdbm_dir', $Dfile; -} else { - unlink 'Op_dbmx.dir', $Dfile; -} - sub ok { @@ -210,3 +203,13 @@ EOM unlink "SubDB.pm", ; } + +ok(19, !exists $h{'goner1'}); +ok(20, exists $h{'foo'}); + +untie %h; +if ($^O eq 'VMS') { + unlink 'Op_dbmx.sdbm_dir', $Dfile; +} else { + unlink 'Op_dbmx.dir', $Dfile; +}