]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dbcache.c
Remove broken MSVC build from REL_ENG branch.
[openldap] / servers / slapd / back-bdb / dbcache.c
1 /* dbcache.c - manage cache of open databases */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/errno.h>
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/time.h>
25 #include <sys/stat.h>
26
27 #include "slap.h"
28 #include "back-bdb.h"
29 #include "lutil_hash.h"
30
31 /* Pass-thru hash function. Since the indexer is already giving us hash
32  * values as keys, we don't need BDB to re-hash them.
33  */
34 static u_int32_t
35 bdb_db_hash(
36         DB *db,
37         const void *bytes,
38         u_int32_t length
39 )
40 {
41         u_int32_t ret = 0;
42         unsigned char *dst = (unsigned char *)&ret;
43         const unsigned char *src = (const unsigned char *)bytes;
44
45         if ( length > sizeof(u_int32_t) )
46                 length = sizeof(u_int32_t);
47
48         while ( length ) {
49                 *dst++ = *src++;
50                 length--;
51         }
52         return ret;
53 }
54
55 int
56 bdb_db_cache(
57         Backend *be,
58         const char *name,
59         DB **dbout )
60 {
61         int i;
62         int rc;
63         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
64         struct bdb_db_info *db;
65         char *file;
66
67         *dbout = NULL;
68
69         for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
70                 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
71                         *dbout = bdb->bi_databases[i]->bdi_db;
72                         return 0;
73                 }
74         }
75
76         ldap_pvt_thread_mutex_lock( &bdb->bi_database_mutex );
77
78         /* check again! may have been added by another thread */
79         for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
80                 if( !strcmp( bdb->bi_databases[i]->bdi_name, name) ) {
81                         *dbout = bdb->bi_databases[i]->bdi_db;
82                         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
83                         return 0;
84                 }
85         }
86
87         if( i >= BDB_INDICES ) {
88                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
89                 return -1;
90         }
91
92         db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
93
94         db->bdi_name = ch_strdup( name );
95
96         rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
97         if( rc != 0 ) {
98 #ifdef NEW_LOGGING
99                 LDAP_LOG ( CACHE, ERR, 
100                         "bdb_db_cache: db_create(%s) failed: %s (%d)\n", 
101                         bdb->bi_dbenv_home, db_strerror(rc), rc );
102 #else
103                 Debug( LDAP_DEBUG_ANY,
104                         "bdb_db_cache: db_create(%s) failed: %s (%d)\n",
105                         bdb->bi_dbenv_home, db_strerror(rc), rc );
106 #endif
107                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
108                 return rc;
109         }
110
111         rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
112         rc = db->bdi_db->set_h_hash( db->bdi_db, bdb_db_hash );
113         rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
114         rc = db->bdi_db->set_dup_compare( db->bdi_db, bdb_bt_compare );
115
116         file = ch_malloc( strlen( name ) + sizeof(BDB_SUFFIX) );
117         sprintf( file, "%s" BDB_SUFFIX, name );
118
119 #ifdef HAVE_EBCDIC
120         __atoe( file );
121 #endif
122         rc = DB_OPEN( db->bdi_db,
123                 file, NULL /* name */,
124                 DB_HASH, bdb->bi_db_opflags | DB_CREATE | DB_THREAD,
125                 bdb->bi_dbenv_mode );
126
127         ch_free( file );
128
129         if( rc != 0 ) {
130 #ifdef NEW_LOGGING
131                 LDAP_LOG ( CACHE, ERR, 
132                         "bdb_db_cache: db_open(%s) failed: %s (%d)\n", 
133                         name, db_strerror(rc), rc );
134 #else
135                 Debug( LDAP_DEBUG_ANY,
136                         "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
137                         name, db_strerror(rc), rc );
138 #endif
139                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
140                 return rc;
141         }
142
143         bdb->bi_databases[i] = db;
144         bdb->bi_ndatabases = i+1;
145
146         *dbout = db->bdi_db;
147
148         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
149         return 0;
150 }