ldb_kv: Skip @ records early in a search full scan
[ambi/samba-autobuild/.git] / lib / ldb / ldb_key_value / ldb_kv.h
1 #include "replace.h"
2 #include "system/filesys.h"
3 #include "system/time.h"
4 #include "tdb.h"
5 #include "ldb_module.h"
6
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);
14
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 (*iterate_range)(struct ldb_kv_private *ldb_kv,
36                              struct ldb_val start_key,
37                              struct ldb_val end_key,
38                              ldb_kv_traverse_fn fn,
39                              void *ctx);
40         int (*lock_read)(struct ldb_module *);
41         int (*unlock_read)(struct ldb_module *);
42         int (*begin_write)(struct ldb_kv_private *);
43         int (*prepare_write)(struct ldb_kv_private *);
44         int (*abort_write)(struct ldb_kv_private *);
45         int (*finish_write)(struct ldb_kv_private *);
46         int (*error)(struct ldb_kv_private *ldb_kv);
47         const char *(*errorstr)(struct ldb_kv_private *ldb_kv);
48         const char *(*name)(struct ldb_kv_private *ldb_kv);
49         bool (*has_changed)(struct ldb_kv_private *ldb_kv);
50         bool (*transaction_active)(struct ldb_kv_private *ldb_kv);
51         size_t (*get_size)(struct ldb_kv_private *ldb_kv);
52 };
53
54 /* this private structure is used by the key value backends in the
55    ldb_context */
56 struct ldb_kv_private {
57         const struct kv_db_ops *kv_ops;
58         struct ldb_module *module;
59         TDB_CONTEXT *tdb;
60         struct lmdb_private *lmdb_private;
61         unsigned int connect_flags;
62
63         unsigned long long sequence_number;
64
65         /* the low level tdb seqnum - used to avoid loading BASEINFO when
66            possible */
67         int tdb_seqnum;
68
69         struct ldb_kv_cache {
70                 struct ldb_message *indexlist;
71                 bool one_level_indexes;
72                 bool attribute_indexes;
73                 const char *GUID_index_attribute;
74                 const char *GUID_index_dn_component;
75         } *cache;
76
77
78         bool check_base;
79         bool disallow_dn_filter;
80         struct ldb_kv_idxptr *idxptr;
81         bool prepared_commit;
82         int read_lock_count;
83
84         bool warn_unindexed;
85         bool warn_reindex;
86
87         bool read_only;
88
89         bool reindex_failed;
90
91         const struct ldb_schema_syntax *GUID_index_syntax;
92
93         /*
94          * Maximum index key length.  If non zero keys longer than this length
95          * will be truncated for non unique indexes. Keys for unique indexes
96          * greater than this length will be rejected.
97          */
98         unsigned max_key_length;
99
100         /*
101          * To allow testing that ensures the DB does not fall back
102          * to a full scan
103          */
104         bool disable_full_db_scan;
105
106         /*
107          * The PID that opened this database so we don't work in a
108          * fork()ed child.
109          */
110         pid_t pid;
111
112         /*
113          * The size to be used for the index transaction cache
114          */
115         size_t index_transaction_cache_size;
116 };
117
118 struct ldb_kv_context {
119         struct ldb_module *module;
120         struct ldb_request *req;
121
122         bool request_terminated;
123         struct ldb_kv_req_spy *spy;
124
125         /* search stuff */
126         const struct ldb_parse_tree *tree;
127         struct ldb_dn *base;
128         enum ldb_scope scope;
129         const char * const *attrs;
130         struct tevent_timer *timeout_event;
131
132         /* error handling */
133         int error;
134 };
135
136 struct ldb_kv_reindex_context {
137         struct ldb_module *module;
138         int error;
139         uint32_t count;
140 };
141
142
143 /* special record types */
144 #define LDB_KV_INDEX      "@INDEX"
145 #define LDB_KV_INDEXLIST  "@INDEXLIST"
146 #define LDB_KV_IDX        "@IDX"
147 #define LDB_KV_IDXVERSION "@IDXVERSION"
148 #define LDB_KV_IDXATTR    "@IDXATTR"
149 #define LDB_KV_IDXONE     "@IDXONE"
150 #define LDB_KV_IDXDN     "@IDXDN"
151 #define LDB_KV_IDXGUID    "@IDXGUID"
152 #define LDB_KV_IDX_DN_GUID "@IDX_DN_GUID"
153
154 /*
155  * This will be used to indicate when a new, yet to be developed
156  * sub-database version of the indicies are in use, to ensure we do
157  * not load future databases unintentionally.
158  */
159
160 #define LDB_KV_IDX_LMDB_SUBDB "@IDX_LMDB_SUBDB"
161
162 #define LDB_KV_BASEINFO   "@BASEINFO"
163 #define LDB_KV_OPTIONS    "@OPTIONS"
164 #define LDB_KV_ATTRIBUTES "@ATTRIBUTES"
165
166 /* special attribute types */
167 #define LDB_KV_SEQUENCE_NUMBER "sequenceNumber"
168 #define LDB_KV_CHECK_BASE "checkBaseOnSearch"
169 #define LDB_KV_DISALLOW_DN_FILTER "disallowDNFilter"
170 #define LDB_KV_MOD_TIMESTAMP "whenChanged"
171 #define LDB_KV_OBJECTCLASS "objectClass"
172
173 /* DB keys */
174 #define LDB_KV_GUID_KEY_PREFIX "GUID="
175 #define LDB_KV_GUID_SIZE 16
176 #define LDB_KV_GUID_KEY_SIZE (LDB_KV_GUID_SIZE + sizeof(LDB_KV_GUID_KEY_PREFIX) - 1)
177
178 /*
179  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_cache.c
180  */
181
182 int ldb_kv_cache_reload(struct ldb_module *module);
183 int ldb_kv_cache_load(struct ldb_module *module);
184 int ldb_kv_increase_sequence_number(struct ldb_module *module);
185 int ldb_kv_check_at_attributes_values(const struct ldb_val *value);
186
187 /*
188  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_index.c
189  */
190
191 /*
192  * The default size of the in memory TDB used to cache index records
193  * The value chosen gives a prime modulo for the hash table and keeps the
194  * tdb memory overhead under 4 kB
195  */
196 #define DEFAULT_INDEX_CACHE_SIZE 491
197
198 struct ldb_parse_tree;
199
200 int ldb_kv_search_indexed(struct ldb_kv_context *ctx, uint32_t *);
201 int ldb_kv_index_add_new(struct ldb_module *module,
202                          struct ldb_kv_private *ldb_kv,
203                          const struct ldb_message *msg);
204 int ldb_kv_index_delete(struct ldb_module *module,
205                         const struct ldb_message *msg);
206 int ldb_kv_index_del_element(struct ldb_module *module,
207                              struct ldb_kv_private *ldb_kv,
208                              const struct ldb_message *msg,
209                              struct ldb_message_element *el);
210 int ldb_kv_index_add_element(struct ldb_module *module,
211                              struct ldb_kv_private *ldb_kv,
212                              const struct ldb_message *msg,
213                              struct ldb_message_element *el);
214 int ldb_kv_index_del_value(struct ldb_module *module,
215                            struct ldb_kv_private *ldb_kv,
216                            const struct ldb_message *msg,
217                            struct ldb_message_element *el,
218                            unsigned int v_idx);
219 int ldb_kv_reindex(struct ldb_module *module);
220 int ldb_kv_index_transaction_start(
221         struct ldb_module *module,
222         size_t cache_size);
223 int ldb_kv_index_transaction_commit(struct ldb_module *module);
224 int ldb_kv_index_transaction_cancel(struct ldb_module *module);
225 int ldb_kv_key_dn_from_idx(struct ldb_module *module,
226                            struct ldb_kv_private *ldb_kv,
227                            TALLOC_CTX *mem_ctx,
228                            struct ldb_dn *dn,
229                           struct ldb_val *key);
230
231 /*
232  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_search.c
233  */
234 int ldb_kv_search_dn1(struct ldb_module *module,
235                       struct ldb_dn *dn,
236                       struct ldb_message *msg,
237                       unsigned int unpack_flags);
238 int ldb_kv_search_base(struct ldb_module *module,
239                        TALLOC_CTX *mem_ctx,
240                        struct ldb_dn *dn,
241                        struct ldb_dn **ret_dn);
242 int ldb_kv_search_key(struct ldb_module *module,
243                       struct ldb_kv_private *ldb_kv,
244                       const struct ldb_val ldb_key,
245                       struct ldb_message *msg,
246                       unsigned int unpack_flags);
247 int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
248                         const struct ldb_message *msg,
249                         const char *const *attrs,
250                         struct ldb_message **filtered_msg);
251 int ldb_kv_search(struct ldb_kv_context *ctx);
252
253 /*
254  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv.c  */
255 /*
256  * Determine if this key could hold a normal record.  We allow the new
257  * GUID index, the old DN index and a possible future ID= but not
258  * DN=@.
259  */
260 bool ldb_kv_key_is_normal_record(struct ldb_val key);
261 struct ldb_val ldb_kv_key_dn(struct ldb_module *module,
262                              TALLOC_CTX *mem_ctx,
263                              struct ldb_dn *dn);
264 struct ldb_val ldb_kv_key_msg(struct ldb_module *module,
265                              TALLOC_CTX *mem_ctx,
266                               const struct ldb_message *msg);
267 int ldb_kv_guid_to_key(struct ldb_module *module,
268                        struct ldb_kv_private *ldb_kv,
269                        const struct ldb_val *GUID_val,
270                        struct ldb_val *key);
271 int ldb_kv_idx_to_key(struct ldb_module *module,
272                       struct ldb_kv_private *ldb_kv,
273                       TALLOC_CTX *mem_ctx,
274                       const struct ldb_val *idx_val,
275                       struct ldb_val *key);
276 int ldb_kv_store(struct ldb_module *module,
277                  const struct ldb_message *msg,
278                  int flgs);
279 int ldb_kv_modify_internal(struct ldb_module *module,
280                            const struct ldb_message *msg,
281                            struct ldb_request *req);
282 int ldb_kv_delete_noindex(struct ldb_module *module,
283                           const struct ldb_message *msg);
284 int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
285                       const char *name,
286                       struct ldb_context *ldb,
287                       const char *options[],
288                       struct ldb_module **_module);
289 #endif /* __LDB_KV_H__ */