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