ldb: make key/value backends expose if there is an active transaction
[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 ltdb_private;
8 typedef int (*ldb_kv_traverse_fn)(struct ltdb_private *ltdb,
9                                   struct ldb_val key, struct ldb_val data,
10                                   void *ctx);
11
12 struct kv_db_ops {
13         int (*store)(struct ltdb_private *ltdb, struct ldb_val key, struct ldb_val data, int flags);
14         int (*delete)(struct ltdb_private *ltdb, struct ldb_val key);
15         int (*iterate)(struct ltdb_private *ltdb, ldb_kv_traverse_fn fn, void *ctx);
16         int (*update_in_iterate)(struct ltdb_private *ltdb, struct ldb_val key,
17                                  struct ldb_val key2, struct ldb_val data, void *ctx);
18         int (*fetch_and_parse)(struct ltdb_private *ltdb, 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 ltdb_private *);
25         int (*prepare_write)(struct ltdb_private *);
26         int (*abort_write)(struct ltdb_private *);
27         int (*finish_write)(struct ltdb_private *);
28         int (*error)(struct ltdb_private *ltdb);
29         const char * (*errorstr)(struct ltdb_private *ltdb);
30         const char * (*name)(struct ltdb_private *ltdb);
31         bool (*has_changed)(struct ltdb_private *ltdb);
32         bool (*transaction_active)(struct ltdb_private *ltdb);
33 };
34
35 /* this private structure is used by the ltdb backend in the
36    ldb_context */
37 struct ltdb_private {
38         const struct kv_db_ops *kv_ops;
39         TDB_CONTEXT *tdb;
40         unsigned int connect_flags;
41         
42         unsigned long long sequence_number;
43
44         /* the low level tdb seqnum - used to avoid loading BASEINFO when
45            possible */
46         int tdb_seqnum;
47
48         struct ltdb_cache {
49                 struct ldb_message *indexlist;
50                 bool one_level_indexes;
51                 bool attribute_indexes;
52                 const char *GUID_index_attribute;
53                 const char *GUID_index_dn_component;
54         } *cache;
55
56         int in_transaction;
57
58         bool check_base;
59         bool disallow_dn_filter;
60         struct ltdb_idxptr *idxptr;
61         bool prepared_commit;
62         int read_lock_count;
63
64         bool warn_unindexed;
65         bool warn_reindex;
66
67         bool read_only;
68
69         bool reindex_failed;
70
71         const struct ldb_schema_syntax *GUID_index_syntax;
72
73         /*
74          * Maximum index key length.  If non zero keys longer than this length
75          * will be truncated for non unique indexes. Keys for unique indexes
76          * greater than this length will be rejected.
77          */
78         unsigned max_key_length;
79 };
80
81 struct ltdb_context {
82         struct ldb_module *module;
83         struct ldb_request *req;
84
85         bool request_terminated;
86         struct ltdb_req_spy *spy;
87
88         /* search stuff */
89         const struct ldb_parse_tree *tree;
90         struct ldb_dn *base;
91         enum ldb_scope scope;
92         const char * const *attrs;
93         struct tevent_timer *timeout_event;
94
95         /* error handling */
96         int error;
97 };
98
99 struct ltdb_reindex_context {
100         struct ldb_module *module;
101         int error;
102         uint32_t count;
103 };
104
105
106 /* special record types */
107 #define LTDB_INDEX      "@INDEX"
108 #define LTDB_INDEXLIST  "@INDEXLIST"
109 #define LTDB_IDX        "@IDX"
110 #define LTDB_IDXVERSION "@IDXVERSION"
111 #define LTDB_IDXATTR    "@IDXATTR"
112 #define LTDB_IDXONE     "@IDXONE"
113 #define LTDB_IDXDN     "@IDXDN"
114 #define LTDB_IDXGUID    "@IDXGUID"
115 #define LTDB_IDX_DN_GUID "@IDX_DN_GUID"
116 #define LTDB_BASEINFO   "@BASEINFO"
117 #define LTDB_OPTIONS    "@OPTIONS"
118 #define LTDB_ATTRIBUTES "@ATTRIBUTES"
119
120 /* special attribute types */
121 #define LTDB_SEQUENCE_NUMBER "sequenceNumber"
122 #define LTDB_CHECK_BASE "checkBaseOnSearch"
123 #define LTDB_DISALLOW_DN_FILTER "disallowDNFilter"
124 #define LTDB_MOD_TIMESTAMP "whenChanged"
125 #define LTDB_OBJECTCLASS "objectClass"
126
127 /* DB keys */
128 #define LTDB_GUID_KEY_PREFIX "GUID="
129 #define LTDB_GUID_SIZE 16
130 #define LTDB_GUID_KEY_SIZE (LTDB_GUID_SIZE + sizeof(LTDB_GUID_KEY_PREFIX) - 1)
131
132 /* The following definitions come from lib/ldb/ldb_tdb/ldb_cache.c  */
133
134 int ltdb_cache_reload(struct ldb_module *module);
135 int ltdb_cache_load(struct ldb_module *module);
136 int ltdb_increase_sequence_number(struct ldb_module *module);
137 int ltdb_check_at_attributes_values(const struct ldb_val *value);
138
139 /* The following definitions come from lib/ldb/ldb_tdb/ldb_index.c  */
140
141 struct ldb_parse_tree;
142
143 int ltdb_search_indexed(struct ltdb_context *ctx, uint32_t *);
144 int ltdb_index_add_new(struct ldb_module *module,
145                        struct ltdb_private *ltdb,
146                        const struct ldb_message *msg);
147 int ltdb_index_delete(struct ldb_module *module, const struct ldb_message *msg);
148 int ltdb_index_del_element(struct ldb_module *module,
149                            struct ltdb_private *ltdb,
150                            const struct ldb_message *msg,
151                            struct ldb_message_element *el);
152 int ltdb_index_add_element(struct ldb_module *module,
153                            struct ltdb_private *ltdb,
154                            const struct ldb_message *msg,
155                            struct ldb_message_element *el);
156 int ltdb_index_del_value(struct ldb_module *module,
157                          struct ltdb_private *ltdb,
158                          const struct ldb_message *msg,
159                          struct ldb_message_element *el, unsigned int v_idx);
160 int ltdb_reindex(struct ldb_module *module);
161 int ltdb_index_transaction_start(struct ldb_module *module);
162 int ltdb_index_transaction_commit(struct ldb_module *module);
163 int ltdb_index_transaction_cancel(struct ldb_module *module);
164 int ltdb_key_dn_from_idx(struct ldb_module *module,
165                          struct ltdb_private *ltdb,
166                          TALLOC_CTX *mem_ctx,
167                          struct ldb_dn *dn,
168                          TDB_DATA *tdb_key);
169
170 /* The following definitions come from lib/ldb/ldb_tdb/ldb_search.c  */
171
172 int ltdb_has_wildcard(struct ldb_module *module, const char *attr_name, 
173                       const struct ldb_val *val);
174 void ltdb_search_dn1_free(struct ldb_module *module, struct ldb_message *msg);
175 int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg,
176                     unsigned int unpack_flags);
177 int ltdb_search_base(struct ldb_module *module,
178                      TALLOC_CTX *mem_ctx,
179                      struct ldb_dn *dn,
180                      struct ldb_dn **ret_dn);
181 int ltdb_search_key(struct ldb_module *module, struct ltdb_private *ltdb,
182                     struct TDB_DATA tdb_key,
183                     struct ldb_message *msg,
184                     unsigned int unpack_flags);
185 int ltdb_filter_attrs(TALLOC_CTX *mem_ctx,
186                       const struct ldb_message *msg, const char * const *attrs,
187                       struct ldb_message **filtered_msg);
188 int ltdb_search(struct ltdb_context *ctx);
189
190 /* The following definitions come from lib/ldb/ldb_tdb/ldb_tdb.c  */
191 /* 
192  * Determine if this key could hold a record.  We allow the new GUID
193  * index, the old DN index and a possible future ID=
194  */
195 bool ltdb_key_is_record(TDB_DATA key);
196 TDB_DATA ltdb_key_dn(struct ldb_module *module, TALLOC_CTX *mem_ctx,
197                      struct ldb_dn *dn);
198 TDB_DATA ltdb_key_msg(struct ldb_module *module, TALLOC_CTX *mem_ctx,
199                       const struct ldb_message *msg);
200 int ltdb_guid_to_key(struct ldb_module *module,
201                      struct ltdb_private *ltdb,
202                      const struct ldb_val *GUID_val,
203                      TDB_DATA *key);
204 int ltdb_idx_to_key(struct ldb_module *module,
205                     struct ltdb_private *ltdb,
206                     TALLOC_CTX *mem_ctx,
207                     const struct ldb_val *idx_val,
208                     TDB_DATA *key);
209 TDB_DATA ltdb_key(struct ldb_module *module, struct ldb_dn *dn);
210 int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs);
211 int ltdb_modify_internal(struct ldb_module *module, const struct ldb_message *msg, struct ldb_request *req);
212 int ltdb_delete_noindex(struct ldb_module *module,
213                         const struct ldb_message *msg);
214 int ltdb_err_map(enum TDB_ERROR tdb_code);
215
216 struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
217                                    const char *path, int hash_size, int tdb_flags,
218                                    int open_flags, mode_t mode,
219                                    struct ldb_context *ldb);
220 int init_store(struct ltdb_private *ltdb, const char *name,
221                struct ldb_context *ldb, const char *options[],
222                struct ldb_module **_module);
223
224 int ltdb_connect(struct ldb_context *ldb, const char *url,
225                  unsigned int flags, const char *options[],
226                  struct ldb_module **_module);