Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

hsearch.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  */
00007 /*
00008  * Copyright (c) 1990, 1993
00009  *      Margo Seltzer.  All rights reserved.
00010  */
00011 /*
00012  * Copyright (c) 1990, 1993
00013  *      The Regents of the University of California.  All rights reserved.
00014  *
00015  * This code is derived from software contributed to Berkeley by
00016  * Margo Seltzer.
00017  *
00018  * Redistribution and use in source and binary forms, with or without
00019  * modification, are permitted provided that the following conditions
00020  * are met:
00021  * 1. Redistributions of source code must retain the above copyright
00022  *    notice, this list of conditions and the following disclaimer.
00023  * 2. Redistributions in binary form must reproduce the above copyright
00024  *    notice, this list of conditions and the following disclaimer in the
00025  *    documentation and/or other materials provided with the distribution.
00026  * 3. Neither the name of the University nor the names of its contributors
00027  *    may be used to endorse or promote products derived from this software
00028  *    without specific prior written permission.
00029  *
00030  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00031  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00032  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00033  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00034  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00035  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00036  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00037  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00038  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00039  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00040  * SUCH DAMAGE.
00041  *
00042  * $Id: hsearch.c,v 12.2 2005/06/16 20:22:56 bostic Exp $
00043  */
00044 
00045 #include "db_config.h"
00046 
00047 #ifndef NO_SYSTEM_INCLUDES
00048 #include <sys/types.h>
00049 
00050 #include <string.h>
00051 #endif
00052 
00053 #define DB_DBM_HSEARCH  1
00054 #include "db_int.h"
00055 
00056 static DB       *dbp;
00057 static ENTRY     retval;
00058 
00059 /*
00060  * Translate HSEARCH calls into DB calls so that DB doesn't step on the
00061  * application's name space.
00062  *
00063  * EXTERN: #if DB_DBM_HSEARCH != 0
00064  *
00065  * EXTERN: int __db_hcreate __P((size_t));
00066  * EXTERN: ENTRY *__db_hsearch __P((ENTRY, ACTION));
00067  * EXTERN: void __db_hdestroy __P((void));
00068  *
00069  * EXTERN: #endif
00070  */
00071 int
00072 __db_hcreate(nel)
00073         size_t nel;
00074 {
00075         int ret;
00076 
00077         if ((ret = db_create(&dbp, NULL, 0)) != 0) {
00078                 __os_set_errno(ret);
00079                 return (1);
00080         }
00081 
00082         if ((ret = dbp->set_pagesize(dbp, 512)) != 0 ||
00083             (ret = dbp->set_h_ffactor(dbp, 16)) != 0 ||
00084             (ret = dbp->set_h_nelem(dbp, (u_int32_t)nel)) != 0 ||
00085             (ret = dbp->open(dbp, NULL,
00086             NULL, NULL, DB_HASH, DB_CREATE, __db_omode(OWNER_RW))) != 0)
00087                 __os_set_errno(ret);
00088 
00089         /*
00090          * !!!
00091          * Hsearch returns 0 on error, not 1.
00092          */
00093         return (ret == 0 ? 1 : 0);
00094 }
00095 
00096 ENTRY *
00097 __db_hsearch(item, action)
00098         ENTRY item;
00099         ACTION action;
00100 {
00101         DBT key, val;
00102         int ret;
00103 
00104         if (dbp == NULL) {
00105                 __os_set_errno(EINVAL);
00106                 return (NULL);
00107         }
00108         memset(&key, 0, sizeof(key));
00109         memset(&val, 0, sizeof(val));
00110         key.data = item.key;
00111         key.size = (u_int32_t)strlen(item.key) + 1;
00112 
00113         switch (action) {
00114         case ENTER:
00115                 val.data = item.data;
00116                 val.size = (u_int32_t)strlen(item.data) + 1;
00117 
00118                 /*
00119                  * Try and add the key to the database.  If we fail because
00120                  * the key already exists, return the existing key.
00121                  */
00122                 if ((ret =
00123                     dbp->put(dbp, NULL, &key, &val, DB_NOOVERWRITE)) == 0)
00124                         break;
00125                 if (ret == DB_KEYEXIST &&
00126                     (ret = dbp->get(dbp, NULL, &key, &val, 0)) == 0)
00127                         break;
00128                 /*
00129                  * The only possible DB error is DB_NOTFOUND, and it can't
00130                  * happen.  Check for a DB error, and lie if we find one.
00131                  */
00132                 __os_set_errno(ret > 0 ? ret : EINVAL);
00133                 return (NULL);
00134         case FIND:
00135                 if ((ret = dbp->get(dbp, NULL, &key, &val, 0)) != 0) {
00136                         if (ret != DB_NOTFOUND)
00137                                 __os_set_errno(ret);
00138                         return (NULL);
00139                 }
00140                 item.data = (char *)val.data;
00141                 break;
00142         default:
00143                 __os_set_errno(EINVAL);
00144                 return (NULL);
00145         }
00146         retval.key = item.key;
00147         retval.data = item.data;
00148         return (&retval);
00149 }
00150 
00151 void
00152 __db_hdestroy()
00153 {
00154         if (dbp != NULL) {
00155                 (void)dbp->close(dbp, 0);
00156                 dbp = NULL;
00157         }
00158 }

Generated on Sun Dec 25 12:14:30 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2