]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/back-bdb.h
18d2ab941999e43e04352a8956cc099844d17316
[openldap] / servers / slapd / back-bdb / back-bdb.h
1 /* back-bdb.h - bdb back-end header file */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 2000-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #ifndef _BACK_BDB_H_
9 #define _BACK_BDB_H_
10
11 #include <portable.h>
12 #include <db.h>
13
14 #include "slap.h"
15
16 LDAP_BEGIN_DECL
17
18 #define BDB_IDL_MULTI           1
19 /* #define BDB_HIER             1 */
20
21 #define DN_BASE_PREFIX          SLAP_INDEX_EQUALITY_PREFIX
22 #define DN_ONE_PREFIX           '%'
23 #define DN_SUBTREE_PREFIX       '@'
24
25 #define DBTzero(t)                      (memset((t), 0, sizeof(DBT)))
26 #define DBT2bv(t,bv)            ((bv)->bv_val = (t)->data, \
27                                                                 (bv)->bv_len = (t)->size)
28 #define bv2DBT(bv,t)            ((t)->data = (bv)->bv_val, \
29                                                                 (t)->size = (bv)->bv_len )
30
31 #define BDB_TXN_RETRIES         16
32
33 #define BDB_MAX_ADD_LOOP        30
34
35 #ifdef BDB_SUBDIRS
36 #define BDB_TMP_SUBDIR  LDAP_DIRSEP "tmp"
37 #define BDB_LG_SUBDIR   LDAP_DIRSEP "log"
38 #define BDB_DATA_SUBDIR LDAP_DIRSEP "data"
39 #endif
40
41 #define BDB_SUFFIX              ".bdb"
42 #define BDB_ID2ENTRY    0
43 #ifdef BDB_HIER
44 #define BDB_ID2PARENT           1
45 #else
46 #define BDB_DN2ID               1
47 #endif
48 #define BDB_NDB                 2
49
50 /* The bdb on-disk entry format is pretty space-inefficient. Average
51  * sized user entries are 3-4K each. You need at least two entries to
52  * fit into a single database page, more is better. 64K is BDB's
53  * upper bound. The same issues arise with IDLs in the index databases,
54  * but it's nearly impossible to avoid overflows there.
55  *
56  * When using BDB_IDL_MULTI, the IDL size is no longer an issue. Smaller
57  * pages are better for concurrency.
58  */
59 #ifndef BDB_ID2ENTRY_PAGESIZE
60 #define BDB_ID2ENTRY_PAGESIZE   16384
61 #endif
62
63 #ifndef BDB_PAGESIZE
64 #ifdef BDB_IDL_MULTI
65 #define BDB_PAGESIZE    4096    /* BDB's original default */
66 #else
67 #define BDB_PAGESIZE    16384
68 #endif
69 #endif
70
71 #define DEFAULT_CACHE_SIZE     1000
72
73 /* for the in-core cache of entries */
74 typedef struct bdb_cache {
75         int             c_maxsize;
76         int             c_cursize;
77         Avlnode         *c_dntree;
78         Avlnode         *c_idtree;
79         Entry           *c_lruhead;     /* lru - add accessed entries here */
80         Entry           *c_lrutail;     /* lru - rem lru entries from here */
81         ldap_pvt_thread_rdwr_t c_rwlock;
82         ldap_pvt_thread_mutex_t lru_mutex;
83 } Cache;
84  
85 #define CACHE_READ_LOCK                0
86 #define CACHE_WRITE_LOCK       1
87  
88 #define BDB_INDICES             128
89
90 struct bdb_db_info {
91         char            *bdi_name;
92         DB                      *bdi_db;
93 };
94
95 struct bdb_info {
96         DB_ENV          *bi_dbenv;
97
98         /* DB_ENV parameters */
99         /* The DB_ENV can be tuned via DB_CONFIG */
100         char            *bi_dbenv_home;
101         u_int32_t       bi_dbenv_xflags; /* extra flags */
102         int                     bi_dbenv_mode;
103
104         int                     bi_ndatabases;
105         struct bdb_db_info **bi_databases;
106         ldap_pvt_thread_mutex_t bi_database_mutex;
107         int             bi_db_opflags;  /* db-specific flags */
108
109         slap_mask_t     bi_defaultmask;
110         Cache           bi_cache;
111         Avlnode         *bi_attrs;
112 #ifdef BDB_HIER
113         Avlnode         *bi_tree;
114         ldap_pvt_thread_rdwr_t  bi_tree_rdwr;
115         void            *bi_troot;
116         int             bi_nrdns;
117 #endif
118
119         int                     bi_txn_cp;
120         u_int32_t       bi_txn_cp_min;
121         u_int32_t       bi_txn_cp_kbyte;
122
123 #ifndef NO_THREADS
124         int                     bi_lock_detect;
125         int                     bi_lock_detect_seconds;
126         ldap_pvt_thread_t       bi_lock_detect_tid;
127 #endif
128
129         ID                      bi_lastid;
130         ldap_pvt_thread_mutex_t bi_lastid_mutex;
131 };
132
133 #define bi_id2entry     bi_databases[BDB_ID2ENTRY]
134 #ifdef BDB_HIER
135 #define bi_id2parent    bi_databases[BDB_ID2PARENT]
136 #else
137 #define bi_dn2id        bi_databases[BDB_DN2ID]
138 #endif
139
140 struct bdb_op_info {
141         BackendDB*      boi_bdb;
142         DB_TXN*         boi_txn;
143         int                     boi_err;
144 };
145
146 #define DB_OPEN(db, file, name, type, flags, mode) \
147         (db)->open(db, file, name, type, flags, mode)
148
149 #if DB_VERSION_MAJOR < 4
150 #define LOCK_DETECT(env,f,t,a)          lock_detect(env, f, t, a)
151 #define LOCK_GET(env,i,f,o,m,l)         lock_get(env, i, f, o, m, l)
152 #define LOCK_PUT(env,l)                 lock_put(env, l)
153 #define TXN_CHECKPOINT(env,k,m,f)       txn_checkpoint(env, k, m, f)
154 #define TXN_BEGIN(env,p,t,f)            txn_begin((env), p, t, f)
155 #define TXN_PREPARE(txn,gid)            txn_prepare((txn), (gid))
156 #define TXN_COMMIT(txn,f)                       txn_commit((txn), (f))
157 #define TXN_ABORT(txn)                          txn_abort((txn))
158 #define TXN_ID(txn)                                     txn_id(txn)
159 #define XLOCK_ID(env, locker)           lock_id(env, locker)
160 #define XLOCK_ID_FREE(env, locker)      lock_id_free(env, locker)
161 #else
162 #define LOCK_DETECT(env,f,t,a)          (env)->lock_detect(env, f, t, a)
163 #define LOCK_GET(env,i,f,o,m,l)         (env)->lock_get(env, i, f, o, m, l)
164 #define LOCK_PUT(env,l)                 (env)->lock_put(env, l)
165 #define TXN_CHECKPOINT(env,k,m,f)       (env)->txn_checkpoint(env, k, m, f)
166 #define TXN_BEGIN(env,p,t,f)            (env)->txn_begin((env), p, t, f)
167 #define TXN_PREPARE(txn,g)                      (txn)->prepare((txn), (g))
168 #define TXN_COMMIT(txn,f)                       (txn)->commit((txn), (f))
169 #define TXN_ABORT(txn)                          (txn)->abort((txn))
170 #define TXN_ID(txn)                                     (txn)->id(txn)
171 #define XLOCK_ID(env, locker)           (env)->lock_id(env, locker)
172 #define XLOCK_ID_FREE(env, locker)      (env)->lock_id_free(env, locker)
173
174 /* BDB 4.1.17 adds txn arg to db->open */
175 #if DB_VERSION_MINOR > 1 || DB_VERSION_PATCH >= 17
176 #undef DB_OPEN
177 #define DB_OPEN(db, file, name, type, flags, mode) \
178         (db)->open(db, NULL, file, name, type, (flags)|DB_AUTO_COMMIT, mode)
179 #endif
180
181 #define BDB_REUSE_LOCKERS
182
183 #ifdef BDB_REUSE_LOCKERS
184 #define LOCK_ID_FREE(env, locker)
185 #define LOCK_ID(env, locker)    bdb_locker_id(op, env, locker)
186 #else
187 #define LOCK_ID_FREE(env, locker)       XLOCK_ID_FREE(env, locker)
188 #define LOCK_ID(env, locker)            XLOCK_ID(env, locker)
189 #endif
190
191 #endif
192
193 LDAP_END_DECL
194
195 #include "proto-bdb.h"
196
197 #endif /* _BACK_BDB_H_ */