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