Home | History | Annotate | Line # | Download | only in common
ypdb.c revision 1.3
      1  1.3    lukem /*	$NetBSD: ypdb.c,v 1.3 1997/10/07 14:39:06 lukem Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (c) 1990, 1993
      5  1.1  thorpej  *	The Regents of the University of California.  All rights reserved.
      6  1.1  thorpej  * All rights reserved.
      7  1.1  thorpej  *
      8  1.1  thorpej  * This code is derived from software contributed to Berkeley by
      9  1.1  thorpej  * Margo Seltzer.
     10  1.1  thorpej  *
     11  1.1  thorpej  * This code is derived from ndbm module of BSD4.4 db (hash) by
     12  1.1  thorpej  * Mats O Jansson
     13  1.1  thorpej  *
     14  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     15  1.1  thorpej  * modification, are permitted provided that the following conditions
     16  1.1  thorpej  * are met:
     17  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     18  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     19  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     20  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     21  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     22  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     23  1.1  thorpej  *    must display the following acknowledgement:
     24  1.1  thorpej  *	This product includes software developed by the University of
     25  1.1  thorpej  *	California, Berkeley and its contributors.
     26  1.1  thorpej  * 4. Neither the name of the University nor the names of its contributors
     27  1.1  thorpej  *    may be used to endorse or promote products derived from this software
     28  1.1  thorpej  *    without specific prior written permission.
     29  1.1  thorpej  *
     30  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     31  1.1  thorpej  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     32  1.1  thorpej  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     34  1.1  thorpej  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     35  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     36  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     37  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     38  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     39  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     40  1.1  thorpej  * SUCH DAMAGE.
     41  1.1  thorpej  */
     42  1.1  thorpej 
     43  1.1  thorpej #include <sys/param.h>
     44  1.1  thorpej #include <sys/types.h>
     45  1.1  thorpej #include <stdio.h>
     46  1.1  thorpej #include <string.h>
     47  1.1  thorpej 
     48  1.2  thorpej #define __DBINTERFACE_PRIVATE
     49  1.2  thorpej #include <db.h>
     50  1.2  thorpej 
     51  1.1  thorpej #include "ypdb.h"
     52  1.1  thorpej 
     53  1.1  thorpej /*
     54  1.1  thorpej  * Returns:
     55  1.1  thorpej  * 	*DBM on success
     56  1.1  thorpej  *	 NULL on failure
     57  1.1  thorpej  */
     58  1.1  thorpej 
     59  1.3    lukem DBM *
     60  1.1  thorpej ypdb_open(file, flags, mode)
     61  1.1  thorpej 	const char *file;
     62  1.1  thorpej 	int flags, mode;
     63  1.1  thorpej {
     64  1.3    lukem 	char path[MAXPATHLEN];
     65  1.1  thorpej #ifdef YPDB_PATCH
     66  1.1  thorpej 	HASHINFO info;
     67  1.1  thorpej 
     68  1.1  thorpej 	info.bsize = 4096;
     69  1.1  thorpej 	info.ffactor = 40;
     70  1.1  thorpej 	info.nelem = 1;
     71  1.1  thorpej 	info.cachesize = NULL;
     72  1.1  thorpej 	info.hash = NULL;
     73  1.1  thorpej 	info.lorder = 0;
     74  1.1  thorpej 	snprintf(path, sizeof(path), "%s%s", file, YPDB_SUFFIX);
     75  1.1  thorpej 	return ((DBM *)__hash_open(path, flags, mode, &info, 0));
     76  1.1  thorpej #else
     77  1.1  thorpej 	BTREEINFO info;
     78  1.1  thorpej 
     79  1.1  thorpej 	info.flags = 0;
     80  1.1  thorpej 	info.cachesize = 0;
     81  1.1  thorpej 	info.maxkeypage = 0;
     82  1.1  thorpej 	info.minkeypage = 0;
     83  1.1  thorpej 	info.psize = 0;
     84  1.1  thorpej 	info.compare = NULL;
     85  1.1  thorpej 	info.prefix = NULL;
     86  1.1  thorpej 	info.lorder = 0;
     87  1.1  thorpej 	snprintf(path, sizeof(path), "%s%s", file, YPDB_SUFFIX);
     88  1.3    lukem 	return ((DBM *)__bt_open(path, flags, mode, &info, 0));
     89  1.1  thorpej #endif
     90  1.1  thorpej }
     91  1.1  thorpej 
     92  1.3    lukem void
     93  1.1  thorpej ypdb_close(db)
     94  1.1  thorpej 	DBM *db;
     95  1.1  thorpej {
     96  1.1  thorpej 	(void)(db->close)(db);
     97  1.1  thorpej }
     98  1.1  thorpej 
     99  1.1  thorpej /*
    100  1.1  thorpej  * Returns:
    101  1.1  thorpej  *	DATUM on success
    102  1.1  thorpej  *	NULL on failure
    103  1.1  thorpej  */
    104  1.1  thorpej 
    105  1.3    lukem datum
    106  1.1  thorpej ypdb_fetch(db, key)
    107  1.1  thorpej 	DBM *db;
    108  1.1  thorpej 	datum key;
    109  1.1  thorpej {
    110  1.1  thorpej 	datum retval;
    111  1.1  thorpej 	int status;
    112  1.1  thorpej 
    113  1.1  thorpej 	status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
    114  1.1  thorpej 	if (status) {
    115  1.1  thorpej 		retval.dptr = NULL;
    116  1.1  thorpej 		retval.dsize = 0;
    117  1.1  thorpej 	}
    118  1.1  thorpej 	return (retval);
    119  1.1  thorpej }
    120  1.1  thorpej 
    121  1.1  thorpej /*
    122  1.1  thorpej  * Returns:
    123  1.1  thorpej  *	DATUM on success
    124  1.1  thorpej  *	NULL on failure
    125  1.1  thorpej  */
    126  1.1  thorpej 
    127  1.3    lukem datum
    128  1.1  thorpej ypdb_firstkey(db)
    129  1.1  thorpej 	DBM *db;
    130  1.1  thorpej {
    131  1.1  thorpej 	int status;
    132  1.1  thorpej 	datum retdata, retkey;
    133  1.1  thorpej 
    134  1.1  thorpej 	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
    135  1.1  thorpej 	if (status)
    136  1.1  thorpej 		retkey.dptr = NULL;
    137  1.1  thorpej 	return (retkey);
    138  1.1  thorpej }
    139  1.1  thorpej 
    140  1.1  thorpej /*
    141  1.1  thorpej  * Returns:
    142  1.1  thorpej  *	DATUM on success
    143  1.1  thorpej  *	NULL on failure
    144  1.1  thorpej  */
    145  1.1  thorpej 
    146  1.3    lukem datum
    147  1.1  thorpej ypdb_nextkey(db)
    148  1.1  thorpej 	DBM *db;
    149  1.1  thorpej {
    150  1.1  thorpej 	int status;
    151  1.1  thorpej 	datum retdata, retkey;
    152  1.1  thorpej 
    153  1.1  thorpej 	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
    154  1.1  thorpej 	if (status)
    155  1.1  thorpej 		retkey.dptr = NULL;
    156  1.1  thorpej 	return (retkey);
    157  1.1  thorpej }
    158  1.1  thorpej 
    159  1.1  thorpej /*
    160  1.1  thorpej  * Returns:
    161  1.1  thorpej  *	DATUM on success
    162  1.1  thorpej  *	NULL on failure
    163  1.1  thorpej  */
    164  1.1  thorpej 
    165  1.3    lukem datum
    166  1.1  thorpej ypdb_setkey(db, key)
    167  1.1  thorpej 	DBM *db;
    168  1.1  thorpej         datum key;
    169  1.1  thorpej {
    170  1.1  thorpej 	int status;
    171  1.1  thorpej 	datum retdata;
    172  1.1  thorpej #ifdef YPDB_PATCH
    173  1.1  thorpej 	datum retkey;
    174  1.1  thorpej 
    175  1.1  thorpej 	status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
    176  1.1  thorpej 	if (status)
    177  1.1  thorpej 		retkey.dptr = NULL;
    178  1.1  thorpej 	while ((retkey.dptr != NULL) &&
    179  1.1  thorpej 	       ((retkey.dsize != key.dsize) ||
    180  1.1  thorpej 		(strncmp(key.dptr,retkey.dptr,retkey.dsize) != 0))) {
    181  1.1  thorpej 	  status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
    182  1.1  thorpej 	  if (status)
    183  1.1  thorpej 	  	retkey.dptr = NULL;
    184  1.1  thorpej 	};
    185  1.1  thorpej 	return (retkey);
    186  1.1  thorpej #else
    187  1.1  thorpej 	status = (db->seq)(db, (DBT *)&key, (DBT *)&retdata, R_CURSOR);
    188  1.1  thorpej 	if (status)
    189  1.1  thorpej 		key.dptr = NULL;
    190  1.1  thorpej 	return (key);
    191  1.1  thorpej #endif
    192  1.1  thorpej }
    193  1.1  thorpej 
    194  1.1  thorpej /*
    195  1.1  thorpej  * Returns:
    196  1.1  thorpej  *	 0 on success
    197  1.1  thorpej  *	<0 failure
    198  1.1  thorpej  */
    199  1.1  thorpej 
    200  1.1  thorpej int
    201  1.1  thorpej ypdb_delete(db, key)
    202  1.1  thorpej 	DBM *db;
    203  1.1  thorpej 	datum key;
    204  1.1  thorpej {
    205  1.1  thorpej 	int status;
    206  1.1  thorpej 
    207  1.1  thorpej 	status = (db->del)(db, (DBT *)&key, 0);
    208  1.1  thorpej 	if (status)
    209  1.1  thorpej 		return (-1);
    210  1.1  thorpej 	else
    211  1.1  thorpej 		return (0);
    212  1.1  thorpej }
    213  1.1  thorpej 
    214  1.1  thorpej /*
    215  1.1  thorpej  * Returns:
    216  1.1  thorpej  *	 0 on success
    217  1.1  thorpej  *	<0 failure
    218  1.1  thorpej  *	 1 if YPDB_INSERT and entry exists
    219  1.1  thorpej  */
    220  1.1  thorpej 
    221  1.1  thorpej int
    222  1.1  thorpej ypdb_store(db, key, content, flags)
    223  1.1  thorpej 	DBM *db;
    224  1.1  thorpej 	datum key, content;
    225  1.1  thorpej 	int flags;
    226  1.1  thorpej {
    227  1.1  thorpej 	return ((db->put)(db, (DBT *)&key, (DBT *)&content,
    228  1.1  thorpej 	    (flags == YPDB_INSERT) ? R_NOOVERWRITE : 0));
    229  1.1  thorpej }
    230