]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dbcache.c
Misc. cleanup, remove lint, remove unused deprecated functions, etc.
[openldap] / servers / slapd / back-bdb / dbcache.c
1 /* dbcache.c - manage cache of open databases */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/errno.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16 #include <sys/stat.h>
17
18 #include "slap.h"
19 #include "back-bdb.h"
20 #include "lutil_hash.h"
21
22 /* Pass-thru hash function. Since the indexer is already giving us hash
23  * values as keys, we don't need BDB to re-hash them.
24  */
25 #if LUTIL_HASH_BYTES == 4
26 static u_int32_t
27 bdb_db_hash(
28         DB *db,
29         const void *bytes,
30         u_int32_t length
31 )
32 {
33         u_int32_t *ret = (u_int32_t *)bytes;
34         return *ret;
35 }
36 #endif
37
38 int
39 bdb_db_cache(
40         Backend *be,
41         const char *name,
42         DB **dbout )
43 {
44         int i;
45         int rc;
46         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
47         struct bdb_db_info *db;
48         char *file;
49
50         *dbout = NULL;
51
52         for( i=BDB_NDB; bdb->bi_databases[i]; i++ ) {
53                 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
54                         *dbout = bdb->bi_databases[i]->bdi_db;
55                         return 0;
56                 }
57         }
58
59         ldap_pvt_thread_mutex_lock( &bdb->bi_database_mutex );
60
61         /* check again! may have been added by another thread */
62         for( i=BDB_NDB; bdb->bi_databases[i]; i++ ) {
63                 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
64                         *dbout = bdb->bi_databases[i]->bdi_db;
65                         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
66                         return 0;
67                 }
68         }
69
70         if( i >= BDB_INDICES ) {
71                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
72                 return -1;
73         }
74
75         db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
76
77         db->bdi_name = ch_strdup( name );
78
79         rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
80         if( rc != 0 ) {
81                 Debug( LDAP_DEBUG_ANY,
82                         "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
83                         bdb->bi_dbenv_home, db_strerror(rc), rc );
84                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
85                 return rc;
86         }
87
88         rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
89 #if LUTIL_HASH_BYTES == 4
90         rc = db->bdi_db->set_h_hash( db->bdi_db, bdb_db_hash );
91 #endif
92 #ifdef BDB_IDL_MULTI
93         rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
94         rc = db->bdi_db->set_dup_compare( db->bdi_db, bdb_bt_compare );
95 #endif
96
97         file = ch_malloc( strlen( name ) + sizeof(BDB_SUFFIX) );
98         sprintf( file, "%s" BDB_SUFFIX, name );
99
100         rc = db->bdi_db->open( db->bdi_db,
101                 file, name,
102                 DB_HASH, bdb->bi_db_opflags | DB_CREATE | DB_THREAD,
103                 bdb->bi_dbenv_mode );
104
105         ch_free( file );
106
107         if( rc != 0 ) {
108                 Debug( LDAP_DEBUG_ANY,
109                         "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
110                         name, db_strerror(rc), rc );
111                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
112                 return rc;
113         }
114
115         bdb->bi_databases[i+1] = NULL;
116         bdb->bi_databases[i] = db;
117         bdb->bi_ndatabases = i+1;
118
119         *dbout = db->bdi_db;
120
121         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
122         return 0;
123 }