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
11 #include "constants.h"
17 #include "reftable-merged.h"
18 #include "reftable-error.h"
21 static int merged_iter_init(struct merged_iter
*mi
)
24 for (i
= 0; i
< mi
->stack_len
; i
++) {
25 struct reftable_record rec
= reftable_new_record(mi
->typ
);
26 int err
= iterator_next(&mi
->stack
[i
], &rec
);
32 reftable_iterator_destroy(&mi
->stack
[i
]);
33 reftable_record_release(&rec
);
39 merged_iter_pqueue_add(&mi
->pq
, &e
);
46 static void merged_iter_close(void *p
)
48 struct merged_iter
*mi
= p
;
50 merged_iter_pqueue_release(&mi
->pq
);
51 for (i
= 0; i
< mi
->stack_len
; i
++) {
52 reftable_iterator_destroy(&mi
->stack
[i
]);
54 reftable_free(mi
->stack
);
57 static int merged_iter_advance_nonnull_subiter(struct merged_iter
*mi
,
61 .rec
= reftable_new_record(mi
->typ
),
64 int err
= iterator_next(&mi
->stack
[idx
], &e
.rec
);
69 reftable_iterator_destroy(&mi
->stack
[idx
]);
70 reftable_record_release(&e
.rec
);
74 merged_iter_pqueue_add(&mi
->pq
, &e
);
78 static int merged_iter_advance_subiter(struct merged_iter
*mi
, size_t idx
)
80 if (iterator_is_null(&mi
->stack
[idx
]))
82 return merged_iter_advance_nonnull_subiter(mi
, idx
);
85 static int merged_iter_next_entry(struct merged_iter
*mi
,
86 struct reftable_record
*rec
)
88 struct strbuf entry_key
= STRBUF_INIT
;
89 struct pq_entry entry
= { 0 };
92 if (merged_iter_pqueue_is_empty(mi
->pq
))
95 entry
= merged_iter_pqueue_remove(&mi
->pq
);
96 err
= merged_iter_advance_subiter(mi
, entry
.index
);
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
, &entry_key
);
109 while (!merged_iter_pqueue_is_empty(mi
->pq
)) {
110 struct pq_entry top
= merged_iter_pqueue_top(mi
->pq
);
111 struct strbuf k
= STRBUF_INIT
;
112 int err
= 0, cmp
= 0;
114 reftable_record_key(&top
.rec
, &k
);
116 cmp
= strbuf_cmp(&k
, &entry_key
);
123 merged_iter_pqueue_remove(&mi
->pq
);
124 err
= merged_iter_advance_subiter(mi
, top
.index
);
128 reftable_record_release(&top
.rec
);
131 reftable_record_copy_from(rec
, &entry
.rec
, hash_size(mi
->hash_id
));
132 reftable_record_release(&entry
.rec
);
133 strbuf_release(&entry_key
);
137 static int merged_iter_next(struct merged_iter
*mi
, struct reftable_record
*rec
)
140 int err
= merged_iter_next_entry(mi
, rec
);
141 if (err
== 0 && mi
->suppress_deletions
&&
142 reftable_record_is_deletion(rec
)) {
150 static int merged_iter_next_void(void *p
, struct reftable_record
*rec
)
152 struct merged_iter
*mi
= p
;
153 if (merged_iter_pqueue_is_empty(mi
->pq
))
156 return merged_iter_next(mi
, rec
);
159 static struct reftable_iterator_vtable merged_iter_vtable
= {
160 .next
= &merged_iter_next_void
,
161 .close
= &merged_iter_close
,
164 static void iterator_from_merged_iter(struct reftable_iterator
*it
,
165 struct merged_iter
*mi
)
169 it
->ops
= &merged_iter_vtable
;
172 int reftable_new_merged_table(struct reftable_merged_table
**dest
,
173 struct reftable_table
*stack
, int n
,
176 struct reftable_merged_table
*m
= NULL
;
177 uint64_t last_max
= 0;
178 uint64_t first_min
= 0;
180 for (i
= 0; i
< n
; i
++) {
181 uint64_t min
= reftable_table_min_update_index(&stack
[i
]);
182 uint64_t max
= reftable_table_max_update_index(&stack
[i
]);
184 if (reftable_table_hash_id(&stack
[i
]) != hash_id
) {
185 return REFTABLE_FORMAT_ERROR
;
187 if (i
== 0 || min
< first_min
) {
190 if (i
== 0 || max
> last_max
) {
195 m
= reftable_calloc(sizeof(struct reftable_merged_table
));
200 m
->hash_id
= hash_id
;
205 /* clears the list of subtable, without affecting the readers themselves. */
206 void merged_table_release(struct reftable_merged_table
*mt
)
208 FREE_AND_NULL(mt
->stack
);
212 void reftable_merged_table_free(struct reftable_merged_table
*mt
)
217 merged_table_release(mt
);
222 reftable_merged_table_max_update_index(struct reftable_merged_table
*mt
)
228 reftable_merged_table_min_update_index(struct reftable_merged_table
*mt
)
233 static int reftable_table_seek_record(struct reftable_table
*tab
,
234 struct reftable_iterator
*it
,
235 struct reftable_record
*rec
)
237 return tab
->ops
->seek_record(tab
->table_arg
, it
, rec
);
240 static int merged_table_seek_record(struct reftable_merged_table
*mt
,
241 struct reftable_iterator
*it
,
242 struct reftable_record
*rec
)
244 struct reftable_iterator
*iters
= reftable_calloc(
245 sizeof(struct reftable_iterator
) * mt
->stack_len
);
246 struct merged_iter merged
= {
248 .typ
= reftable_record_type(rec
),
249 .hash_id
= mt
->hash_id
,
250 .suppress_deletions
= mt
->suppress_deletions
,
255 for (i
= 0; i
< mt
->stack_len
&& err
== 0; i
++) {
256 int e
= reftable_table_seek_record(&mt
->stack
[i
], &iters
[n
],
267 for (i
= 0; i
< n
; i
++) {
268 reftable_iterator_destroy(&iters
[i
]);
270 reftable_free(iters
);
274 merged
.stack_len
= n
;
275 err
= merged_iter_init(&merged
);
277 merged_iter_close(&merged
);
280 struct merged_iter
*p
=
281 reftable_malloc(sizeof(struct merged_iter
));
283 iterator_from_merged_iter(it
, p
);
288 int reftable_merged_table_seek_ref(struct reftable_merged_table
*mt
,
289 struct reftable_iterator
*it
,
292 struct reftable_record rec
= {
293 .type
= BLOCK_TYPE_REF
,
295 .refname
= (char *)name
,
298 return merged_table_seek_record(mt
, it
, &rec
);
301 int reftable_merged_table_seek_log_at(struct reftable_merged_table
*mt
,
302 struct reftable_iterator
*it
,
303 const char *name
, uint64_t update_index
)
305 struct reftable_record rec
= { .type
= BLOCK_TYPE_LOG
,
307 .refname
= (char *)name
,
308 .update_index
= update_index
,
310 return merged_table_seek_record(mt
, it
, &rec
);
313 int reftable_merged_table_seek_log(struct reftable_merged_table
*mt
,
314 struct reftable_iterator
*it
,
317 uint64_t max
= ~((uint64_t)0);
318 return reftable_merged_table_seek_log_at(mt
, it
, name
, max
);
321 uint32_t reftable_merged_table_hash_id(struct reftable_merged_table
*mt
)
326 static int reftable_merged_table_seek_void(void *tab
,
327 struct reftable_iterator
*it
,
328 struct reftable_record
*rec
)
330 return merged_table_seek_record(tab
, it
, rec
);
333 static uint32_t reftable_merged_table_hash_id_void(void *tab
)
335 return reftable_merged_table_hash_id(tab
);
338 static uint64_t reftable_merged_table_min_update_index_void(void *tab
)
340 return reftable_merged_table_min_update_index(tab
);
343 static uint64_t reftable_merged_table_max_update_index_void(void *tab
)
345 return reftable_merged_table_max_update_index(tab
);
348 static struct reftable_table_vtable merged_table_vtable
= {
349 .seek_record
= reftable_merged_table_seek_void
,
350 .hash_id
= reftable_merged_table_hash_id_void
,
351 .min_update_index
= reftable_merged_table_min_update_index_void
,
352 .max_update_index
= reftable_merged_table_max_update_index_void
,
355 void reftable_table_from_merged_table(struct reftable_table
*tab
,
356 struct reftable_merged_table
*merged
)
359 tab
->ops
= &merged_table_vtable
;
360 tab
->table_arg
= merged
;