dbwrap: Avoid passing lp_ctx to tdb_wrap_open in db_open_tdb
[kai/samba-autobuild/.git] / lib / dbwrap / dbwrap_cache.c
1 /*
2    Unix SMB/CIFS implementation.
3    Cache db contents for parse_record based on seqnum
4    Copyright (C) Volker Lendecke 2012
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/dbwrap/dbwrap.h"
22 #include "lib/dbwrap/dbwrap_private.h"
23 #include "lib/dbwrap/dbwrap_rbt.h"
24 #include "lib/dbwrap/dbwrap_cache.h"
25
26 struct db_cache_ctx {
27         int seqnum;
28         struct db_context *backing;
29         struct db_context *positive;
30         struct db_context *negative;
31 };
32
33 static bool dbwrap_cache_validate(struct db_cache_ctx *ctx)
34 {
35         int backing_seqnum;
36
37         backing_seqnum = dbwrap_get_seqnum(ctx->backing);
38         if (backing_seqnum == ctx->seqnum) {
39                 return true;
40         }
41
42         TALLOC_FREE(ctx->positive);
43         ctx->positive = db_open_rbt(ctx);
44         if (ctx->positive == NULL) {
45                 return false;
46         }
47
48         TALLOC_FREE(ctx->negative);
49         ctx->negative = db_open_rbt(ctx);
50         if (ctx->negative == NULL) {
51                 return false;
52         }
53
54         ctx->seqnum = backing_seqnum;
55         return true;
56 }
57
58 static NTSTATUS dbwrap_cache_parse_record(
59         struct db_context *db, TDB_DATA key,
60         void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data),
61         void *private_data)
62 {
63         struct db_cache_ctx *ctx = talloc_get_type_abort(
64                 db->private_data, struct db_cache_ctx);
65         TDB_DATA value;
66         NTSTATUS status;
67
68         if (!dbwrap_cache_validate(ctx)) {
69                 return NT_STATUS_NO_MEMORY;
70         }
71
72         if (dbwrap_exists(ctx->negative, key)) {
73                 return NT_STATUS_NOT_FOUND;
74         }
75         status = dbwrap_parse_record(ctx->positive, key, parser, private_data);
76         if (NT_STATUS_IS_OK(status)) {
77                 return status;
78         }
79
80         status = dbwrap_fetch(ctx->backing, talloc_tos(), key, &value);
81
82         if (NT_STATUS_IS_OK(status)) {
83                 dbwrap_store(ctx->positive, key, value, 0);
84                 parser(key, value, private_data);
85                 TALLOC_FREE(value.dptr);
86                 return NT_STATUS_OK;
87         }
88
89         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
90                 char c = '\0';
91                 value.dptr = (uint8_t *)&c;
92                 value.dsize = sizeof(c);
93                 dbwrap_store(ctx->negative, key, value, 0);
94                 return NT_STATUS_NOT_FOUND;
95         }
96         return status;
97 }
98
99 static struct db_record *dbwrap_cache_fetch_locked(
100         struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key)
101 {
102         struct db_cache_ctx *ctx = talloc_get_type_abort(
103                 db->private_data, struct db_cache_ctx);
104         return dbwrap_fetch_locked(ctx->backing, mem_ctx, key);
105 }
106
107 static int dbwrap_cache_traverse(struct db_context *db,
108                                  int (*f)(struct db_record *rec,
109                                           void *private_data),
110                                  void *private_data)
111 {
112         struct db_cache_ctx *ctx = talloc_get_type_abort(
113                 db->private_data, struct db_cache_ctx);
114         NTSTATUS status;
115         int ret;
116         status = dbwrap_traverse(ctx->backing, f, private_data, &ret);
117         if (!NT_STATUS_IS_OK(status)) {
118                 return -1;
119         }
120         return ret;
121 }
122
123 static int dbwrap_cache_traverse_read(struct db_context *db,
124                                       int (*f)(struct db_record *rec,
125                                                void *private_data),
126                                       void *private_data)
127 {
128         struct db_cache_ctx *ctx = talloc_get_type_abort(
129                 db->private_data, struct db_cache_ctx);
130         NTSTATUS status;
131         int ret;
132         status = dbwrap_traverse_read(ctx->backing, f, private_data, &ret);
133         if (!NT_STATUS_IS_OK(status)) {
134                 return -1;
135         }
136         return ret;
137 }
138
139 static int dbwrap_cache_get_seqnum(struct db_context *db)
140 {
141         struct db_cache_ctx *ctx = talloc_get_type_abort(
142                 db->private_data, struct db_cache_ctx);
143         return dbwrap_get_seqnum(ctx->backing);
144 }
145
146 static int dbwrap_cache_transaction_start(struct db_context *db)
147 {
148         struct db_cache_ctx *ctx = talloc_get_type_abort(
149                 db->private_data, struct db_cache_ctx);
150         return dbwrap_transaction_start(ctx->backing);
151 }
152
153 static int dbwrap_cache_transaction_commit(struct db_context *db)
154 {
155         struct db_cache_ctx *ctx = talloc_get_type_abort(
156                 db->private_data, struct db_cache_ctx);
157         return dbwrap_transaction_commit(ctx->backing);
158 }
159
160 static int dbwrap_cache_transaction_cancel(struct db_context *db)
161 {
162         struct db_cache_ctx *ctx = talloc_get_type_abort(
163                 db->private_data, struct db_cache_ctx);
164         return dbwrap_transaction_cancel(ctx->backing);
165 }
166
167 static int dbwrap_cache_exists(struct db_context *db, TDB_DATA key)
168 {
169         struct db_cache_ctx *ctx = talloc_get_type_abort(
170                 db->private_data, struct db_cache_ctx);
171
172         if (ctx->positive && dbwrap_exists(ctx->positive, key)) {
173                 return true;
174         }
175         if (ctx->negative && dbwrap_exists(ctx->negative, key)) {
176                 return false;
177         }
178         return dbwrap_exists(ctx->backing, key);
179 }
180
181 static void dbwrap_cache_id(struct db_context *db, const uint8_t **id,
182                             size_t *idlen)
183 {
184         struct db_cache_ctx *ctx = talloc_get_type_abort(
185                 db->private_data, struct db_cache_ctx);
186         dbwrap_db_id(ctx->backing, id, idlen);
187 }
188
189 struct db_context *db_open_cache(TALLOC_CTX *mem_ctx,
190                                  struct db_context *backing)
191 {
192         struct db_context *db;
193         struct db_cache_ctx *ctx;
194
195         db = talloc_zero(mem_ctx, struct db_context);
196         if (db == NULL) {
197                 return NULL;
198         }
199         ctx = talloc_zero(db, struct db_cache_ctx);
200         if (ctx == NULL) {
201                 TALLOC_FREE(db);
202                 return NULL;
203         }
204
205         ctx->seqnum = -1;
206         ctx->backing = talloc_move(ctx, &backing);
207         db->private_data = ctx;
208         if (!dbwrap_cache_validate(ctx)) {
209                 TALLOC_FREE(db);
210                 return NULL;
211         }
212
213         db->fetch_locked = dbwrap_cache_fetch_locked;
214         db->traverse = dbwrap_cache_traverse;
215         db->traverse_read = dbwrap_cache_traverse_read;
216         db->get_seqnum = dbwrap_cache_get_seqnum;
217         db->transaction_start = dbwrap_cache_transaction_start;
218         db->transaction_commit = dbwrap_cache_transaction_commit;
219         db->transaction_cancel = dbwrap_cache_transaction_cancel;
220         db->parse_record = dbwrap_cache_parse_record;
221         db->exists = dbwrap_cache_exists;
222         db->id = dbwrap_cache_id;
223         db->name = dbwrap_name(ctx->backing);
224         db->hash_size = dbwrap_hash_size(ctx->backing);
225         return db;
226 }