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