]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dbcache.c
Suck in HEAD changes since 2.1alpha
[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 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         const char *name,
50         DB **dbout )
51 {
52         int i;
53         int rc;
54         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
55         struct bdb_db_info *db;
56         char *file;
57
58         *dbout = NULL;
59
60         for( i=BDB_NDB; bdb->bi_databases[i]; i++ ) {
61                 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
62                         *dbout = bdb->bi_databases[i]->bdi_db;
63                         return 0;
64                 }
65         }
66
67         ldap_pvt_thread_mutex_lock( &bdb->bi_database_mutex );
68
69         /* check again! may have been added by another thread */
70         for( i=BDB_NDB; bdb->bi_databases[i]; i++ ) {
71                 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
72                         *dbout = bdb->bi_databases[i]->bdi_db;
73                         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
74                         return 0;
75                 }
76         }
77
78         if( i >= BDB_INDICES ) {
79                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
80                 return -1;
81         }
82
83         db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
84
85         db->bdi_name = ch_strdup( name );
86
87         rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
88         if( rc != 0 ) {
89 #ifdef NEW_LOGGING
90                 LDAP_LOG (( "dbcache", LDAP_LEVEL_ERR, "bdb_db_cache: db_create(%s) failed: %s (%d)\n", bdb->bi_dbenv_home, db_strerror(rc), rc ));
91 #else
92                 Debug( LDAP_DEBUG_ANY,
93                         "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
94                         bdb->bi_dbenv_home, db_strerror(rc), rc );
95 #endif
96                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
97                 return rc;
98         }
99
100         rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
101         rc = db->bdi_db->set_h_hash( db->bdi_db, bdb_db_hash );
102 #ifdef BDB_IDL_MULTI
103         rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
104         rc = db->bdi_db->set_dup_compare( db->bdi_db, bdb_bt_compare );
105 #endif
106
107         file = ch_malloc( strlen( name ) + sizeof(BDB_SUFFIX) );
108         sprintf( file, "%s" BDB_SUFFIX, name );
109
110         rc = db->bdi_db->open( db->bdi_db,
111                 file, name,
112                 DB_HASH, bdb->bi_db_opflags | DB_CREATE | DB_THREAD,
113                 bdb->bi_dbenv_mode );
114
115         ch_free( file );
116
117         if( rc != 0 ) {
118 #ifdef NEW_LOGGING
119                 LDAP_LOG (( "dbcache", LDAP_LEVEL_ERR, "bdb_db_cache: db_open(%s) failed: %s (%d)\n", name, db_strerror(rc), rc ));
120 #else
121                 Debug( LDAP_DEBUG_ANY,
122                         "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
123                         name, db_strerror(rc), rc );
124 #endif
125                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
126                 return rc;
127         }
128
129         bdb->bi_databases[i+1] = NULL;
130         bdb->bi_databases[i] = db;
131         bdb->bi_ndatabases = i+1;
132
133         *dbout = db->bdi_db;
134
135         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
136         return 0;
137 }