]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dbcache.c
Lots of fixes from HEAD
[openldap] / servers / slapd / back-bdb / dbcache.c
1 /* dbcache.c - manage cache of open databases */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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 static u_int32_t
26 bdb_db_hash(
27         DB *db,
28         const void *bytes,
29         u_int32_t length
30 )
31 {
32         u_int32_t ret = 0;
33         unsigned char *dst = (unsigned char *)&ret;
34         const unsigned char *src = (const unsigned char *)bytes;
35
36         if ( length > sizeof(u_int32_t) )
37                 length = sizeof(u_int32_t);
38
39         while ( length ) {
40                 *dst++ = *src++;
41                 length--;
42         }
43         return ret;
44 }
45
46 int
47 bdb_db_cache(
48         Backend *be,
49         DB_TXN *tid,
50         const char *name,
51         DB **dbout )
52 {
53         int i;
54         int rc;
55         int flags;
56         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
57         struct bdb_db_info *db;
58         char *file;
59
60         *dbout = NULL;
61
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                         return 0;
66                 }
67         }
68
69         ldap_pvt_thread_mutex_lock( &bdb->bi_database_mutex );
70
71         /* check again! may have been added by another thread */
72         for( i=BDB_NDB; bdb->bi_databases[i]; i++ ) {
73                 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
74                         *dbout = bdb->bi_databases[i]->bdi_db;
75                         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
76                         return 0;
77                 }
78         }
79
80         if( i >= BDB_INDICES ) {
81                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
82                 return -1;
83         }
84
85         db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
86
87         db->bdi_name = ch_strdup( name );
88
89         rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
90         if( rc != 0 ) {
91 #ifdef NEW_LOGGING
92                 LDAP_LOG ( CACHE, ERR, 
93                         "bdb_db_cache: db_create(%s) failed: %s (%d)\n", 
94                         bdb->bi_dbenv_home, db_strerror(rc), rc );
95 #else
96                 Debug( LDAP_DEBUG_ANY,
97                         "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
98                         bdb->bi_dbenv_home, db_strerror(rc), rc );
99 #endif
100                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
101                 return rc;
102         }
103
104         rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
105         rc = db->bdi_db->set_h_hash( db->bdi_db, bdb_db_hash );
106         rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
107         rc = db->bdi_db->set_dup_compare( db->bdi_db, bdb_bt_compare );
108
109         file = ch_malloc( strlen( name ) + sizeof(BDB_SUFFIX) );
110         sprintf( file, "%s" BDB_SUFFIX, name );
111
112 #ifdef HAVE_EBCDIC
113         __atoe( file );
114 #endif
115         flags = bdb->bi_db_opflags | DB_CREATE | DB_THREAD;
116         if ( !tid ) flags |= DB_AUTO_COMMIT;
117         rc = DB_OPEN( db->bdi_db, tid,
118                 file, name,
119                 DB_HASH, flags,
120                 bdb->bi_dbenv_mode );
121
122         ch_free( file );
123
124         if( rc != 0 ) {
125 #ifdef NEW_LOGGING
126                 LDAP_LOG ( CACHE, ERR, 
127                         "bdb_db_cache: db_open(%s) failed: %s (%d)\n", 
128                         name, db_strerror(rc), rc );
129 #else
130                 Debug( LDAP_DEBUG_ANY,
131                         "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
132                         name, db_strerror(rc), rc );
133 #endif
134                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
135                 return rc;
136         }
137
138         bdb->bi_databases[i+1] = NULL;
139         bdb->bi_databases[i] = db;
140         bdb->bi_ndatabases = i+1;
141
142         *dbout = db->bdi_db;
143
144         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
145         return 0;
146 }