lib ldb key_value: Set index cache size on open
[Samba.git] / lib / ldb / ldb_key_value / ldb_kv.h
bloba4aa5ed9e62ae4ca73c748f0386b1b93e60811aa
1 #include "replace.h"
2 #include "system/filesys.h"
3 #include "system/time.h"
4 #include "tdb.h"
5 #include "ldb_module.h"
7 #ifndef __LDB_KV_H__
8 #define __LDB_KV_H__
9 struct ldb_kv_private;
10 typedef int (*ldb_kv_traverse_fn)(struct ldb_kv_private *ldb_kv,
11 struct ldb_val key,
12 struct ldb_val data,
13 void *ctx);
15 struct kv_db_ops {
16 int (*store)(struct ldb_kv_private *ldb_kv,
17 struct ldb_val key,
18 struct ldb_val data,
19 int flags);
20 int (*delete)(struct ldb_kv_private *ldb_kv, struct ldb_val key);
21 int (*iterate)(struct ldb_kv_private *ldb_kv,
22 ldb_kv_traverse_fn fn,
23 void *ctx);
24 int (*update_in_iterate)(struct ldb_kv_private *ldb_kv,
25 struct ldb_val key,
26 struct ldb_val key2,
27 struct ldb_val data,
28 void *ctx);
29 int (*fetch_and_parse)(struct ldb_kv_private *ldb_kv,
30 struct ldb_val key,
31 int (*parser)(struct ldb_val key,
32 struct ldb_val data,
33 void *private_data),
34 void *ctx);
35 int (*lock_read)(struct ldb_module *);
36 int (*unlock_read)(struct ldb_module *);
37 int (*begin_write)(struct ldb_kv_private *);
38 int (*prepare_write)(struct ldb_kv_private *);
39 int (*abort_write)(struct ldb_kv_private *);
40 int (*finish_write)(struct ldb_kv_private *);
41 int (*error)(struct ldb_kv_private *ldb_kv);
42 const char *(*errorstr)(struct ldb_kv_private *ldb_kv);
43 const char *(*name)(struct ldb_kv_private *ldb_kv);
44 bool (*has_changed)(struct ldb_kv_private *ldb_kv);
45 bool (*transaction_active)(struct ldb_kv_private *ldb_kv);
46 size_t (*get_size)(struct ldb_kv_private *ldb_kv);
49 /* this private structure is used by the key value backends in the
50 ldb_context */
51 struct ldb_kv_private {
52 const struct kv_db_ops *kv_ops;
53 struct ldb_module *module;
54 TDB_CONTEXT *tdb;
55 struct lmdb_private *lmdb_private;
56 unsigned int connect_flags;
58 unsigned long long sequence_number;
60 /* the low level tdb seqnum - used to avoid loading BASEINFO when
61 possible */
62 int tdb_seqnum;
64 struct ldb_kv_cache {
65 struct ldb_message *indexlist;
66 bool one_level_indexes;
67 bool attribute_indexes;
68 const char *GUID_index_attribute;
69 const char *GUID_index_dn_component;
70 } *cache;
73 bool check_base;
74 bool disallow_dn_filter;
75 struct ldb_kv_idxptr *idxptr;
76 bool prepared_commit;
77 int read_lock_count;
79 bool warn_unindexed;
80 bool warn_reindex;
82 bool read_only;
84 bool reindex_failed;
86 const struct ldb_schema_syntax *GUID_index_syntax;
89 * Maximum index key length. If non zero keys longer than this length
90 * will be truncated for non unique indexes. Keys for unique indexes
91 * greater than this length will be rejected.
93 unsigned max_key_length;
96 * To allow testing that ensures the DB does not fall back
97 * to a full scan
99 bool disable_full_db_scan;
102 * The PID that opened this database so we don't work in a
103 * fork()ed child.
105 pid_t pid;
108 * The size to be used for the index transaction cache
110 size_t index_transaction_cache_size;
113 struct ldb_kv_context {
114 struct ldb_module *module;
115 struct ldb_request *req;
117 bool request_terminated;
118 struct ldb_kv_req_spy *spy;
120 /* search stuff */
121 const struct ldb_parse_tree *tree;
122 struct ldb_dn *base;
123 enum ldb_scope scope;
124 const char * const *attrs;
125 struct tevent_timer *timeout_event;
127 /* error handling */
128 int error;
131 struct ldb_kv_reindex_context {
132 struct ldb_module *module;
133 int error;
134 uint32_t count;
138 /* special record types */
139 #define LDB_KV_INDEX "@INDEX"
140 #define LDB_KV_INDEXLIST "@INDEXLIST"
141 #define LDB_KV_IDX "@IDX"
142 #define LDB_KV_IDXVERSION "@IDXVERSION"
143 #define LDB_KV_IDXATTR "@IDXATTR"
144 #define LDB_KV_IDXONE "@IDXONE"
145 #define LDB_KV_IDXDN "@IDXDN"
146 #define LDB_KV_IDXGUID "@IDXGUID"
147 #define LDB_KV_IDX_DN_GUID "@IDX_DN_GUID"
150 * This will be used to indicate when a new, yet to be developed
151 * sub-database version of the indicies are in use, to ensure we do
152 * not load future databases unintentionally.
155 #define LDB_KV_IDX_LMDB_SUBDB "@IDX_LMDB_SUBDB"
157 #define LDB_KV_BASEINFO "@BASEINFO"
158 #define LDB_KV_OPTIONS "@OPTIONS"
159 #define LDB_KV_ATTRIBUTES "@ATTRIBUTES"
161 /* special attribute types */
162 #define LDB_KV_SEQUENCE_NUMBER "sequenceNumber"
163 #define LDB_KV_CHECK_BASE "checkBaseOnSearch"
164 #define LDB_KV_DISALLOW_DN_FILTER "disallowDNFilter"
165 #define LDB_KV_MOD_TIMESTAMP "whenChanged"
166 #define LDB_KV_OBJECTCLASS "objectClass"
168 /* DB keys */
169 #define LDB_KV_GUID_KEY_PREFIX "GUID="
170 #define LDB_KV_GUID_SIZE 16
171 #define LDB_KV_GUID_KEY_SIZE (LDB_KV_GUID_SIZE + sizeof(LDB_KV_GUID_KEY_PREFIX) - 1)
174 * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_cache.c
177 int ldb_kv_cache_reload(struct ldb_module *module);
178 int ldb_kv_cache_load(struct ldb_module *module);
179 int ldb_kv_increase_sequence_number(struct ldb_module *module);
180 int ldb_kv_check_at_attributes_values(const struct ldb_val *value);
183 * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_index.c
187 * The default size of the in memory TDB used to cache index records
188 * The value chosen gives a prime modulo for the hash table and keeps the
189 * tdb memory overhead under 4 kB
191 #define DEFAULT_INDEX_CACHE_SIZE 491
193 struct ldb_parse_tree;
195 int ldb_kv_search_indexed(struct ldb_kv_context *ctx, uint32_t *);
196 int ldb_kv_index_add_new(struct ldb_module *module,
197 struct ldb_kv_private *ldb_kv,
198 const struct ldb_message *msg);
199 int ldb_kv_index_delete(struct ldb_module *module,
200 const struct ldb_message *msg);
201 int ldb_kv_index_del_element(struct ldb_module *module,
202 struct ldb_kv_private *ldb_kv,
203 const struct ldb_message *msg,
204 struct ldb_message_element *el);
205 int ldb_kv_index_add_element(struct ldb_module *module,
206 struct ldb_kv_private *ldb_kv,
207 const struct ldb_message *msg,
208 struct ldb_message_element *el);
209 int ldb_kv_index_del_value(struct ldb_module *module,
210 struct ldb_kv_private *ldb_kv,
211 const struct ldb_message *msg,
212 struct ldb_message_element *el,
213 unsigned int v_idx);
214 int ldb_kv_reindex(struct ldb_module *module);
215 int ldb_kv_index_transaction_start(
216 struct ldb_module *module,
217 size_t cache_size);
218 int ldb_kv_index_transaction_commit(struct ldb_module *module);
219 int ldb_kv_index_transaction_cancel(struct ldb_module *module);
220 int ldb_kv_key_dn_from_idx(struct ldb_module *module,
221 struct ldb_kv_private *ldb_kv,
222 TALLOC_CTX *mem_ctx,
223 struct ldb_dn *dn,
224 struct ldb_val *key);
227 * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_search.c
229 int ldb_kv_search_dn1(struct ldb_module *module,
230 struct ldb_dn *dn,
231 struct ldb_message *msg,
232 unsigned int unpack_flags);
233 int ldb_kv_search_base(struct ldb_module *module,
234 TALLOC_CTX *mem_ctx,
235 struct ldb_dn *dn,
236 struct ldb_dn **ret_dn);
237 int ldb_kv_search_key(struct ldb_module *module,
238 struct ldb_kv_private *ldb_kv,
239 const struct ldb_val ldb_key,
240 struct ldb_message *msg,
241 unsigned int unpack_flags);
242 int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
243 const struct ldb_message *msg,
244 const char *const *attrs,
245 struct ldb_message **filtered_msg);
246 int ldb_kv_search(struct ldb_kv_context *ctx);
249 * The following definitions come from lib/ldb/ldb_key_value/ldb_kv.c */
251 * Determine if this key could hold a record. We allow the new GUID
252 * index, the old DN index and a possible future ID=
254 bool ldb_kv_key_is_record(struct ldb_val key);
255 struct ldb_val ldb_kv_key_dn(struct ldb_module *module,
256 TALLOC_CTX *mem_ctx,
257 struct ldb_dn *dn);
258 struct ldb_val ldb_kv_key_msg(struct ldb_module *module,
259 TALLOC_CTX *mem_ctx,
260 const struct ldb_message *msg);
261 int ldb_kv_guid_to_key(struct ldb_module *module,
262 struct ldb_kv_private *ldb_kv,
263 const struct ldb_val *GUID_val,
264 struct ldb_val *key);
265 int ldb_kv_idx_to_key(struct ldb_module *module,
266 struct ldb_kv_private *ldb_kv,
267 TALLOC_CTX *mem_ctx,
268 const struct ldb_val *idx_val,
269 struct ldb_val *key);
270 int ldb_kv_store(struct ldb_module *module,
271 const struct ldb_message *msg,
272 int flgs);
273 int ldb_kv_modify_internal(struct ldb_module *module,
274 const struct ldb_message *msg,
275 struct ldb_request *req);
276 int ldb_kv_delete_noindex(struct ldb_module *module,
277 const struct ldb_message *msg);
278 int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
279 const char *name,
280 struct ldb_context *ldb,
281 const char *options[],
282 struct ldb_module **_module);
283 #endif /* __LDB_KV_H__ */