]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dbcache.c
fix typo
[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-2008 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 #ifdef BDB_INDEX_USE_HASH
32 /* Pass-thru hash function. Since the indexer is already giving us hash
33  * values as keys, we don't need BDB to re-hash them.
34  */
35 static u_int32_t
36 bdb_db_hash(
37         DB *db,
38         const void *bytes,
39         u_int32_t length
40 )
41 {
42         u_int32_t ret = 0;
43         unsigned char *dst = (unsigned char *)&ret;
44         const unsigned char *src = (const unsigned char *)bytes;
45
46         if ( length > sizeof(u_int32_t) )
47                 length = sizeof(u_int32_t);
48
49         while ( length ) {
50                 *dst++ = *src++;
51                 length--;
52         }
53         return ret;
54 }
55 #define BDB_INDEXTYPE   DB_HASH
56 #else
57 #define BDB_INDEXTYPE   DB_BTREE
58 #endif
59
60 int
61 bdb_db_cache(
62         Backend *be,
63         struct berval *name,
64         DB **dbout )
65 {
66         int i, flags;
67         int rc;
68         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
69         struct bdb_db_info *db;
70         char *file;
71
72         *dbout = NULL;
73
74         for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
75                 if( !ber_bvcmp( &bdb->bi_databases[i]->bdi_name, name) ) {
76                         *dbout = bdb->bi_databases[i]->bdi_db;
77                         return 0;
78                 }
79         }
80
81         ldap_pvt_thread_mutex_lock( &bdb->bi_database_mutex );
82
83         /* check again! may have been added by another thread */
84         for( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
85                 if( !ber_bvcmp( &bdb->bi_databases[i]->bdi_name, name) ) {
86                         *dbout = bdb->bi_databases[i]->bdi_db;
87                         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
88                         return 0;
89                 }
90         }
91
92         if( i >= BDB_INDICES ) {
93                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
94                 return -1;
95         }
96
97         db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
98
99         ber_dupbv( &db->bdi_name, name );
100
101         rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
102         if( rc != 0 ) {
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                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
107                 ch_free( db );
108                 return rc;
109         }
110
111         if( !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
112                 rc = db->bdi_db->set_flags( db->bdi_db, DB_ENCRYPT );
113                 if ( rc ) {
114                         Debug( LDAP_DEBUG_ANY,
115                                 "bdb_db_cache: db set_flags(DB_ENCRYPT)(%s) failed: %s (%d)\n",
116                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
117                         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
118                         db->bdi_db->close( db->bdi_db, 0 );
119                         ch_free( db );
120                         return rc;
121                 }
122         }
123
124         rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
125 #ifdef BDB_INDEX_USE_HASH
126         rc = db->bdi_db->set_h_hash( db->bdi_db, bdb_db_hash );
127 #endif
128         rc = db->bdi_db->set_flags( db->bdi_db, DB_DUP | DB_DUPSORT );
129
130         file = ch_malloc( db->bdi_name.bv_len + sizeof(BDB_SUFFIX) );
131         strcpy( file, db->bdi_name.bv_val );
132         strcpy( file+db->bdi_name.bv_len, BDB_SUFFIX );
133
134 #ifdef HAVE_EBCDIC
135         __atoe( file );
136 #endif
137         flags = DB_CREATE | DB_THREAD;
138 #ifdef DB_AUTO_COMMIT
139         if ( !( slapMode & SLAP_TOOL_QUICK ))
140                 flags |= DB_AUTO_COMMIT;
141 #endif
142         /* Cannot Truncate when Transactions are in use */
143         if ( (slapMode & (SLAP_TOOL_QUICK|SLAP_TRUNCATE_MODE)) ==
144                 (SLAP_TOOL_QUICK|SLAP_TRUNCATE_MODE))
145                         flags |= DB_TRUNCATE;
146
147         rc = DB_OPEN( db->bdi_db,
148                 file, NULL /* name */,
149                 BDB_INDEXTYPE, bdb->bi_db_opflags | flags, bdb->bi_dbenv_mode );
150
151         ch_free( file );
152
153         if( rc != 0 ) {
154                 Debug( LDAP_DEBUG_ANY,
155                         "bdb_db_cache: db_open(%s) failed: %s (%d)\n",
156                         name->bv_val, db_strerror(rc), rc );
157                 ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
158                 return rc;
159         }
160
161         bdb->bi_databases[i] = db;
162         bdb->bi_ndatabases = i+1;
163
164         *dbout = db->bdi_db;
165
166         ldap_pvt_thread_mutex_unlock( &bdb->bi_database_mutex );
167         return 0;
168 }