930405d0f63935fddb2728221d8efbda221bd52e
[samba.git] / lib / ldb / ldb_tdb / ldb_tdb.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 struct ldb_kv_private;
8 typedef int (*ldb_kv_traverse_fn)(struct ldb_kv_private *ldb_kv,
9                                   struct ldb_val key, struct ldb_val data,
10                                   void *ctx);
11
12 struct kv_db_ops {
13         int (*store)(struct ldb_kv_private *ldb_kv, struct ldb_val key, struct ldb_val data, int flags);
14         int (*delete)(struct ldb_kv_private *ldb_kv, struct ldb_val key);
15         int (*iterate)(struct ldb_kv_private *ldb_kv, ldb_kv_traverse_fn fn, void *ctx);
16         int (*update_in_iterate)(struct ldb_kv_private *ldb_kv, struct ldb_val key,
17                                  struct ldb_val key2, struct ldb_val data, void *ctx);
18         int (*fetch_and_parse)(struct ldb_kv_private *ldb_kv, struct ldb_val key,
19                                int (*parser)(struct ldb_val key, struct ldb_val data,
20                                              void *private_data),
21                                void *ctx);
22         int (*lock_read)(struct ldb_module *);
23         int (*unlock_read)(struct ldb_module *);
24         int (*begin_write)(struct ldb_kv_private *);
25         int (*prepare_write)(struct ldb_kv_private *);
26         int (*abort_write)(struct ldb_kv_private *);
27         int (*finish_write)(struct ldb_kv_private *);
28         int (*error)(struct ldb_kv_private *ldb_kv);
29         const char * (*errorstr)(struct ldb_kv_private *ldb_kv);
30         const char * (*name)(struct ldb_kv_private *ldb_kv);
31         bool (*has_changed)(struct ldb_kv_private *ldb_kv);
32         bool (*transaction_active)(struct ldb_kv_private *ldb_kv);
33 };
34
35 /* this private structure is used by the key value backends in the
36    ldb_context */
37 struct ldb_kv_private {
38         const struct kv_db_ops *kv_ops;
39         struct ldb_module *module;
40         TDB_CONTEXT *tdb;
41         struct lmdb_private *lmdb_private;
42         unsigned int connect_flags;
43         
44         unsigned long long sequence_number;
45
46         /* the low level tdb seqnum - used to avoid loading BASEINFO when
47            possible */
48         int tdb_seqnum;
49
50         struct ltdb_cache {
51                 struct ldb_message *indexlist;
52                 bool one_level_indexes;
53                 bool attribute_indexes;
54                 const char *GUID_index_attribute;
55                 const char *GUID_index_dn_component;
56         } *cache;
57
58
59         bool check_base;
60         bool disallow_dn_filter;
61         struct ltdb_idxptr *idxptr;
62         bool prepared_commit;
63         int read_lock_count;
64
65         bool warn_unindexed;
66         bool warn_reindex;
67
68         bool read_only;
69
70         bool reindex_failed;
71
72         const struct ldb_schema_syntax *GUID_index_syntax;
73
74         /*
75          * Maximum index key length.  If non zero keys longer than this length
76          * will be truncated for non unique indexes. Keys for unique indexes
77          * greater than this length will be rejected.
78          */
79         unsigned max_key_length;
80
81         /*
82          * To allow testing that ensures the DB does not fall back
83          * to a full scan
84          */
85         bool disable_full_db_scan;
86
87         /*
88          * The PID that opened this database so we don't work in a
89          * fork()ed child.
90          */
91         pid_t pid;
92 };
93
94 struct ldb_kv_context {
95         struct ldb_module *module;
96         struct ldb_request *req;
97
98         bool request_terminated;
99         struct ldb_kv_req_spy *spy;
100
101         /* search stuff */
102         const struct ldb_parse_tree *tree;
103         struct ldb_dn *base;
104         enum ldb_scope scope;
105         const char * const *attrs;
106         struct tevent_timer *timeout_event;
107
108         /* error handling */
109         int error;
110 };
111
112 struct ldb_kv_reindex_context {
113         struct ldb_module *module;
114         int error;
115         uint32_t count;
116 };
117
118
119 /* special record types */
120 #define LTDB_INDEX      "@INDEX"
121 #define LTDB_INDEXLIST  "@INDEXLIST"
122 #define LTDB_IDX        "@IDX"
123 #define LTDB_IDXVERSION "@IDXVERSION"
124 #define LTDB_IDXATTR    "@IDXATTR"
125 #define LTDB_IDXONE     "@IDXONE"
126 #define LTDB_IDXDN     "@IDXDN"
127 #define LTDB_IDXGUID    "@IDXGUID"
128 #define LTDB_IDX_DN_GUID "@IDX_DN_GUID"
129
130 /*
131  * This will be used to indicate when a new, yet to be developed
132  * sub-database version of the indicies are in use, to ensure we do
133  * not load future databases unintentionally.
134  */
135
136 #define LTDB_IDX_LMDB_SUBDB "@IDX_LMDB_SUBDB"
137
138 #define LTDB_BASEINFO   "@BASEINFO"
139 #define LTDB_OPTIONS    "@OPTIONS"
140 #define LTDB_ATTRIBUTES "@ATTRIBUTES"
141
142 /* special attribute types */
143 #define LTDB_SEQUENCE_NUMBER "sequenceNumber"
144 #define LTDB_CHECK_BASE "checkBaseOnSearch"
145 #define LTDB_DISALLOW_DN_FILTER "disallowDNFilter"
146 #define LTDB_MOD_TIMESTAMP "whenChanged"
147 #define LTDB_OBJECTCLASS "objectClass"
148
149 /* DB keys */
150 #define LTDB_GUID_KEY_PREFIX "GUID="
151 #define LTDB_GUID_SIZE 16
152 #define LTDB_GUID_KEY_SIZE (LTDB_GUID_SIZE + sizeof(LTDB_GUID_KEY_PREFIX) - 1)
153
154 /* The following definitions come from lib/ldb/ldb_tdb/ldb_cache.c  */
155
156 int ldb_kv_cache_reload(struct ldb_module *module);
157 int ldb_kv_cache_load(struct ldb_module *module);
158 int ldb_kv_increase_sequence_number(struct ldb_module *module);
159 int ldb_kv_check_at_attributes_values(const struct ldb_val *value);
160
161 /* The following definitions come from lib/ldb/ldb_tdb/ldb_index.c  */
162
163 struct ldb_parse_tree;
164
165 int ldb_kv_search_indexed(struct ldb_kv_context *ctx, uint32_t *);
166 int ldb_kv_index_add_new(struct ldb_module *module,
167                          struct ldb_kv_private *ldb_kv,
168                          const struct ldb_message *msg);
169 int ldb_kv_index_delete(struct ldb_module *module,
170                         const struct ldb_message *msg);
171 int ldb_kv_index_del_element(struct ldb_module *module,
172                              struct ldb_kv_private *ldb_kv,
173                              const struct ldb_message *msg,
174                              struct ldb_message_element *el);
175 int ldb_kv_index_add_element(struct ldb_module *module,
176                              struct ldb_kv_private *ldb_kv,
177                              const struct ldb_message *msg,
178                              struct ldb_message_element *el);
179 int ldb_kv_index_del_value(struct ldb_module *module,
180                            struct ldb_kv_private *ldb_kv,
181                            const struct ldb_message *msg,
182                            struct ldb_message_element *el,
183                            unsigned int v_idx);
184 int ldb_kv_reindex(struct ldb_module *module);
185 int ldb_kv_index_transaction_start(struct ldb_module *module);
186 int ldb_kv_index_transaction_commit(struct ldb_module *module);
187 int ldb_kv_index_transaction_cancel(struct ldb_module *module);
188 int ldb_kv_key_dn_from_idx(struct ldb_module *module,
189                            struct ldb_kv_private *ldb_kv,
190                            TALLOC_CTX *mem_ctx,
191                            struct ldb_dn *dn,
192                            TDB_DATA *tdb_key);
193
194 /* The following definitions come from lib/ldb/ldb_tdb/ldb_search.c  */
195
196 int ltdb_has_wildcard(struct ldb_module *module, const char *attr_name, 
197                       const struct ldb_val *val);
198 void ltdb_search_dn1_free(struct ldb_module *module, struct ldb_message *msg);
199 int ldb_kv_search_dn1(struct ldb_module *module,
200                       struct ldb_dn *dn,
201                       struct ldb_message *msg,
202                       unsigned int unpack_flags);
203 int ldb_kv_search_base(struct ldb_module *module,
204                        TALLOC_CTX *mem_ctx,
205                        struct ldb_dn *dn,
206                        struct ldb_dn **ret_dn);
207 int ldb_kv_search_key(struct ldb_module *module,
208                       struct ldb_kv_private *ldb_kv,
209                       struct TDB_DATA tdb_key,
210                       struct ldb_message *msg,
211                       unsigned int unpack_flags);
212 int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
213                         const struct ldb_message *msg,
214                         const char *const *attrs,
215                         struct ldb_message **filtered_msg);
216 int ldb_kv_search(struct ldb_kv_context *ctx);
217
218 /* The following definitions come from lib/ldb/ldb_tdb/ldb_tdb.c  */
219 /* 
220  * Determine if this key could hold a record.  We allow the new GUID
221  * index, the old DN index and a possible future ID=
222  */
223 bool ldb_kv_key_is_record(TDB_DATA key);
224 TDB_DATA ldb_kv_key_dn(struct ldb_module *module,
225                        TALLOC_CTX *mem_ctx,
226                        struct ldb_dn *dn);
227 TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
228                         TALLOC_CTX *mem_ctx,
229                         const struct ldb_message *msg);
230 int ldb_kv_guid_to_key(struct ldb_module *module,
231                        struct ldb_kv_private *ldb_kv,
232                        const struct ldb_val *GUID_val,
233                        TDB_DATA *key);
234 int ldb_kv_idx_to_key(struct ldb_module *module,
235                       struct ldb_kv_private *ldb_kv,
236                       TALLOC_CTX *mem_ctx,
237                       const struct ldb_val *idx_val,
238                       TDB_DATA *key);
239 TDB_DATA ltdb_key(struct ldb_module *module, struct ldb_dn *dn);
240 int ldb_kv_store(struct ldb_module *module,
241                  const struct ldb_message *msg,
242                  int flgs);
243 int ldb_kv_modify_internal(struct ldb_module *module,
244                            const struct ldb_message *msg,
245                            struct ldb_request *req);
246 int ldb_kv_delete_noindex(struct ldb_module *module,
247                           const struct ldb_message *msg);
248 int ltdb_err_map(enum TDB_ERROR tdb_code);
249
250 struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
251                                    const char *path, int hash_size, int tdb_flags,
252                                    int open_flags, mode_t mode,
253                                    struct ldb_context *ldb);
254 int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
255                       const char *name,
256                       struct ldb_context *ldb,
257                       const char *options[],
258                       struct ldb_module **_module);
259
260 int ltdb_connect(struct ldb_context *ldb, const char *url,
261                  unsigned int flags, const char *options[],
262                  struct ldb_module **_module);