s3:build: move lib/dbwrap/dbwrap_util.o to the other dbwrap objects...
[kai/samba-autobuild/.git] / source3 / 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         if (ctx->seqnum == dbwrap_get_seqnum(ctx->backing)) {
36                 return true;
37         }
38         TALLOC_FREE(ctx->positive);
39         ctx->positive = db_open_rbt(ctx);
40         TALLOC_FREE(ctx->negative);
41         ctx->negative = db_open_rbt(ctx);
42
43         return ((ctx->positive != NULL) && (ctx->negative != NULL));
44 }
45
46 static NTSTATUS dbwrap_cache_parse_record(
47         struct db_context *db, TDB_DATA key,
48         void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data),
49         void *private_data)
50 {
51         struct db_cache_ctx *ctx = talloc_get_type_abort(
52                 db->private_data, struct db_cache_ctx);
53         TDB_DATA value;
54         NTSTATUS status;
55
56         if (!dbwrap_cache_validate(ctx)) {
57                 return NT_STATUS_NO_MEMORY;
58         }
59
60         status = dbwrap_parse_record(ctx->positive, key, parser, private_data);
61         if (NT_STATUS_IS_OK(status)) {
62                 return status;
63         }
64         if (dbwrap_exists(ctx->negative, key)) {
65                 return NT_STATUS_NOT_FOUND;
66         }
67
68         status = dbwrap_fetch(ctx->backing, talloc_tos(), key, &value);
69
70         if (NT_STATUS_IS_OK(status)) {
71                 dbwrap_store(ctx->positive, key, value, 0);
72                 parser(key, value, private_data);
73                 TALLOC_FREE(value.dptr);
74                 return NT_STATUS_OK;
75         }
76
77         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
78                 char c = '\0';
79                 value.dptr = (uint8_t *)&c;
80                 value.dsize = sizeof(c);
81                 dbwrap_store(ctx->negative, key, value, 0);
82                 return NT_STATUS_NOT_FOUND;
83         }
84         return status;
85 }
86
87 static struct db_record *dbwrap_cache_fetch_locked(
88         struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key)
89 {
90         struct db_cache_ctx *ctx = talloc_get_type_abort(
91                 db->private_data, struct db_cache_ctx);
92         return dbwrap_fetch_locked(ctx->backing, mem_ctx, key);
93 }
94
95 static int dbwrap_cache_traverse(struct db_context *db,
96                                  int (*f)(struct db_record *rec,
97                                           void *private_data),
98                                  void *private_data)
99 {
100         struct db_cache_ctx *ctx = talloc_get_type_abort(
101                 db->private_data, struct db_cache_ctx);
102         NTSTATUS status;
103         int ret;
104         status = dbwrap_traverse(ctx->backing, f, private_data, &ret);
105         if (!NT_STATUS_IS_OK(status)) {
106                 return -1;
107         }
108         return ret;
109 }
110
111 static int dbwrap_cache_traverse_read(struct db_context *db,
112                                       int (*f)(struct db_record *rec,
113                                                void *private_data),
114                                       void *private_data)
115 {
116         struct db_cache_ctx *ctx = talloc_get_type_abort(
117                 db->private_data, struct db_cache_ctx);
118         NTSTATUS status;
119         int ret;
120         status = dbwrap_traverse_read(ctx->backing, f, private_data, &ret);
121         if (!NT_STATUS_IS_OK(status)) {
122                 return -1;
123         }
124         return ret;
125 }
126
127 static int dbwrap_cache_get_seqnum(struct db_context *db)
128 {
129         struct db_cache_ctx *ctx = talloc_get_type_abort(
130                 db->private_data, struct db_cache_ctx);
131         return dbwrap_get_seqnum(ctx->backing);
132 }
133
134 static int dbwrap_cache_get_flags(struct db_context *db)
135 {
136         struct db_cache_ctx *ctx = talloc_get_type_abort(
137                 db->private_data, struct db_cache_ctx);
138         return dbwrap_get_flags(ctx->backing);
139 }
140
141 static int dbwrap_cache_transaction_start(struct db_context *db)
142 {
143         struct db_cache_ctx *ctx = talloc_get_type_abort(
144                 db->private_data, struct db_cache_ctx);
145         return dbwrap_transaction_start(ctx->backing);
146 }
147
148 static int dbwrap_cache_transaction_commit(struct db_context *db)
149 {
150         struct db_cache_ctx *ctx = talloc_get_type_abort(
151                 db->private_data, struct db_cache_ctx);
152         return dbwrap_transaction_commit(ctx->backing);
153 }
154
155 static int dbwrap_cache_transaction_cancel(struct db_context *db)
156 {
157         struct db_cache_ctx *ctx = talloc_get_type_abort(
158                 db->private_data, struct db_cache_ctx);
159         return dbwrap_transaction_cancel(ctx->backing);
160 }
161
162 static int dbwrap_cache_exists(struct db_context *db, TDB_DATA key)
163 {
164         struct db_cache_ctx *ctx = talloc_get_type_abort(
165                 db->private_data, struct db_cache_ctx);
166
167         if (ctx->positive && dbwrap_exists(ctx->positive, key)) {
168                 return true;
169         }
170         if (ctx->negative && dbwrap_exists(ctx->negative, key)) {
171                 return false;
172         }
173         return dbwrap_exists(ctx->backing, key);
174 }
175
176 static void dbwrap_cache_id(struct db_context *db, const uint8_t **id,
177                             size_t *idlen)
178 {
179         struct db_cache_ctx *ctx = talloc_get_type_abort(
180                 db->private_data, struct db_cache_ctx);
181         dbwrap_db_id(ctx->backing, id, idlen);
182 }
183
184 struct db_context *db_open_cache(TALLOC_CTX *mem_ctx,
185                                  struct db_context *backing)
186 {
187         struct db_context *db;
188         struct db_cache_ctx *ctx;
189
190         db = talloc(mem_ctx, struct db_context);
191         if (db == NULL) {
192                 return NULL;
193         }
194         ctx = talloc_zero(db, struct db_cache_ctx);
195         if (ctx == NULL) {
196                 TALLOC_FREE(db);
197                 return NULL;
198         }
199
200         ctx->seqnum = -1;
201         ctx->backing = talloc_move(ctx, &backing);
202         db->private_data = ctx;
203         if (!dbwrap_cache_validate(ctx)) {
204                 TALLOC_FREE(db);
205                 return NULL;
206         }
207
208         db->fetch_locked = dbwrap_cache_fetch_locked;
209         db->try_fetch_locked = NULL;
210         db->traverse = dbwrap_cache_traverse;
211         db->traverse_read = dbwrap_cache_traverse_read;
212         db->get_seqnum = dbwrap_cache_get_seqnum;
213         db->get_flags = dbwrap_cache_get_flags;
214         db->transaction_start = dbwrap_cache_transaction_start;
215         db->transaction_commit = dbwrap_cache_transaction_commit;
216         db->transaction_cancel = dbwrap_cache_transaction_cancel;
217         db->parse_record = dbwrap_cache_parse_record;
218         db->exists = dbwrap_cache_exists;
219         db->id = dbwrap_cache_id;
220         db->stored_callback = NULL;
221         db->wipe = NULL;
222         db->lock_order = 0;
223         db->persistent = false;
224         return db;
225 }