Merge branch 'pb/complete-config'
[git.git] / reftable / merged.c
bloba0f222e07bdf7519ed1c0d8d43ccbb6151663c3a
1 /*
2 Copyright 2020 Google LLC
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://developers.google.com/open-source/licenses/bsd
7 */
9 #include "merged.h"
11 #include "constants.h"
12 #include "iter.h"
13 #include "pq.h"
14 #include "record.h"
15 #include "generic.h"
16 #include "reftable-merged.h"
17 #include "reftable-error.h"
18 #include "system.h"
20 static int merged_iter_init(struct merged_iter *mi)
22 for (size_t i = 0; i < mi->stack_len; i++) {
23 struct pq_entry e = {
24 .index = i,
26 int err;
28 reftable_record_init(&e.rec, mi->typ);
29 err = iterator_next(&mi->stack[i], &e.rec);
30 if (err < 0)
31 return err;
32 if (err > 0) {
33 reftable_iterator_destroy(&mi->stack[i]);
34 reftable_record_release(&e.rec);
35 continue;
38 merged_iter_pqueue_add(&mi->pq, &e);
41 return 0;
44 static void merged_iter_close(void *p)
46 struct merged_iter *mi = p;
48 merged_iter_pqueue_release(&mi->pq);
49 for (size_t i = 0; i < mi->stack_len; i++)
50 reftable_iterator_destroy(&mi->stack[i]);
51 reftable_free(mi->stack);
52 strbuf_release(&mi->key);
53 strbuf_release(&mi->entry_key);
56 static int merged_iter_advance_nonnull_subiter(struct merged_iter *mi,
57 size_t idx)
59 struct pq_entry e = {
60 .index = idx,
62 int err;
64 reftable_record_init(&e.rec, mi->typ);
65 err = iterator_next(&mi->stack[idx], &e.rec);
66 if (err < 0)
67 return err;
69 if (err > 0) {
70 reftable_iterator_destroy(&mi->stack[idx]);
71 reftable_record_release(&e.rec);
72 return 0;
75 merged_iter_pqueue_add(&mi->pq, &e);
76 return 0;
79 static int merged_iter_advance_subiter(struct merged_iter *mi, size_t idx)
81 if (iterator_is_null(&mi->stack[idx]))
82 return 0;
83 return merged_iter_advance_nonnull_subiter(mi, idx);
86 static int merged_iter_next_entry(struct merged_iter *mi,
87 struct reftable_record *rec)
89 struct pq_entry entry = { 0 };
90 int err = 0;
92 if (merged_iter_pqueue_is_empty(mi->pq))
93 return 1;
95 entry = merged_iter_pqueue_remove(&mi->pq);
96 err = merged_iter_advance_subiter(mi, entry.index);
97 if (err < 0)
98 return err;
101 One can also use reftable as datacenter-local storage, where the ref
102 database is maintained in globally consistent database (eg.
103 CockroachDB or Spanner). In this scenario, replication delays together
104 with compaction may cause newer tables to contain older entries. In
105 such a deployment, the loop below must be changed to collect all
106 entries for the same key, and return new the newest one.
108 reftable_record_key(&entry.rec, &mi->entry_key);
109 while (!merged_iter_pqueue_is_empty(mi->pq)) {
110 struct pq_entry top = merged_iter_pqueue_top(mi->pq);
111 int cmp = 0;
113 reftable_record_key(&top.rec, &mi->key);
115 cmp = strbuf_cmp(&mi->key, &mi->entry_key);
116 if (cmp > 0)
117 break;
119 merged_iter_pqueue_remove(&mi->pq);
120 err = merged_iter_advance_subiter(mi, top.index);
121 if (err < 0)
122 goto done;
123 reftable_record_release(&top.rec);
126 reftable_record_release(rec);
127 *rec = entry.rec;
129 done:
130 if (err)
131 reftable_record_release(&entry.rec);
132 return err;
135 static int merged_iter_next(struct merged_iter *mi, struct reftable_record *rec)
137 while (1) {
138 int err = merged_iter_next_entry(mi, rec);
139 if (err == 0 && mi->suppress_deletions &&
140 reftable_record_is_deletion(rec)) {
141 continue;
144 return err;
148 static int merged_iter_next_void(void *p, struct reftable_record *rec)
150 struct merged_iter *mi = p;
151 if (merged_iter_pqueue_is_empty(mi->pq))
152 return 1;
154 return merged_iter_next(mi, rec);
157 static struct reftable_iterator_vtable merged_iter_vtable = {
158 .next = &merged_iter_next_void,
159 .close = &merged_iter_close,
162 static void iterator_from_merged_iter(struct reftable_iterator *it,
163 struct merged_iter *mi)
165 assert(!it->ops);
166 it->iter_arg = mi;
167 it->ops = &merged_iter_vtable;
170 int reftable_new_merged_table(struct reftable_merged_table **dest,
171 struct reftable_table *stack, size_t n,
172 uint32_t hash_id)
174 struct reftable_merged_table *m = NULL;
175 uint64_t last_max = 0;
176 uint64_t first_min = 0;
178 for (size_t i = 0; i < n; i++) {
179 uint64_t min = reftable_table_min_update_index(&stack[i]);
180 uint64_t max = reftable_table_max_update_index(&stack[i]);
182 if (reftable_table_hash_id(&stack[i]) != hash_id) {
183 return REFTABLE_FORMAT_ERROR;
185 if (i == 0 || min < first_min) {
186 first_min = min;
188 if (i == 0 || max > last_max) {
189 last_max = max;
193 REFTABLE_CALLOC_ARRAY(m, 1);
194 m->stack = stack;
195 m->stack_len = n;
196 m->min = first_min;
197 m->max = last_max;
198 m->hash_id = hash_id;
199 *dest = m;
200 return 0;
203 /* clears the list of subtable, without affecting the readers themselves. */
204 void merged_table_release(struct reftable_merged_table *mt)
206 FREE_AND_NULL(mt->stack);
207 mt->stack_len = 0;
210 void reftable_merged_table_free(struct reftable_merged_table *mt)
212 if (!mt) {
213 return;
215 merged_table_release(mt);
216 reftable_free(mt);
219 uint64_t
220 reftable_merged_table_max_update_index(struct reftable_merged_table *mt)
222 return mt->max;
225 uint64_t
226 reftable_merged_table_min_update_index(struct reftable_merged_table *mt)
228 return mt->min;
231 static int reftable_table_seek_record(struct reftable_table *tab,
232 struct reftable_iterator *it,
233 struct reftable_record *rec)
235 return tab->ops->seek_record(tab->table_arg, it, rec);
238 static int merged_table_seek_record(struct reftable_merged_table *mt,
239 struct reftable_iterator *it,
240 struct reftable_record *rec)
242 struct merged_iter merged = {
243 .typ = reftable_record_type(rec),
244 .hash_id = mt->hash_id,
245 .suppress_deletions = mt->suppress_deletions,
246 .key = STRBUF_INIT,
247 .entry_key = STRBUF_INIT,
249 struct merged_iter *p;
250 int err;
252 REFTABLE_CALLOC_ARRAY(merged.stack, mt->stack_len);
253 for (size_t i = 0; i < mt->stack_len; i++) {
254 err = reftable_table_seek_record(&mt->stack[i],
255 &merged.stack[merged.stack_len], rec);
256 if (err < 0)
257 goto out;
258 if (!err)
259 merged.stack_len++;
262 err = merged_iter_init(&merged);
263 if (err < 0)
264 goto out;
266 p = reftable_malloc(sizeof(struct merged_iter));
267 *p = merged;
268 iterator_from_merged_iter(it, p);
270 out:
271 if (err < 0)
272 merged_iter_close(&merged);
273 return err;
276 int reftable_merged_table_seek_ref(struct reftable_merged_table *mt,
277 struct reftable_iterator *it,
278 const char *name)
280 struct reftable_record rec = {
281 .type = BLOCK_TYPE_REF,
282 .u.ref = {
283 .refname = (char *)name,
286 return merged_table_seek_record(mt, it, &rec);
289 int reftable_merged_table_seek_log_at(struct reftable_merged_table *mt,
290 struct reftable_iterator *it,
291 const char *name, uint64_t update_index)
293 struct reftable_record rec = { .type = BLOCK_TYPE_LOG,
294 .u.log = {
295 .refname = (char *)name,
296 .update_index = update_index,
297 } };
298 return merged_table_seek_record(mt, it, &rec);
301 int reftable_merged_table_seek_log(struct reftable_merged_table *mt,
302 struct reftable_iterator *it,
303 const char *name)
305 uint64_t max = ~((uint64_t)0);
306 return reftable_merged_table_seek_log_at(mt, it, name, max);
309 uint32_t reftable_merged_table_hash_id(struct reftable_merged_table *mt)
311 return mt->hash_id;
314 static int reftable_merged_table_seek_void(void *tab,
315 struct reftable_iterator *it,
316 struct reftable_record *rec)
318 return merged_table_seek_record(tab, it, rec);
321 static uint32_t reftable_merged_table_hash_id_void(void *tab)
323 return reftable_merged_table_hash_id(tab);
326 static uint64_t reftable_merged_table_min_update_index_void(void *tab)
328 return reftable_merged_table_min_update_index(tab);
331 static uint64_t reftable_merged_table_max_update_index_void(void *tab)
333 return reftable_merged_table_max_update_index(tab);
336 static struct reftable_table_vtable merged_table_vtable = {
337 .seek_record = reftable_merged_table_seek_void,
338 .hash_id = reftable_merged_table_hash_id_void,
339 .min_update_index = reftable_merged_table_min_update_index_void,
340 .max_update_index = reftable_merged_table_max_update_index_void,
343 void reftable_table_from_merged_table(struct reftable_table *tab,
344 struct reftable_merged_table *merged)
346 assert(!tab->ops);
347 tab->ops = &merged_table_vtable;
348 tab->table_arg = merged;