Sync with 2.34.4
[git/debian.git] / reftable / reftable.c
blob0e4607a7cd6c95daf040d9759850cd9819e8578e
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 "basics.h"
10 #include "record.h"
11 #include "generic.h"
12 #include "reftable-iterator.h"
13 #include "reftable-generic.h"
15 int reftable_table_seek_ref(struct reftable_table *tab,
16 struct reftable_iterator *it, const char *name)
18 struct reftable_ref_record ref = {
19 .refname = (char *)name,
21 struct reftable_record rec = { NULL };
22 reftable_record_from_ref(&rec, &ref);
23 return tab->ops->seek_record(tab->table_arg, it, &rec);
26 int reftable_table_read_ref(struct reftable_table *tab, const char *name,
27 struct reftable_ref_record *ref)
29 struct reftable_iterator it = { NULL };
30 int err = reftable_table_seek_ref(tab, &it, name);
31 if (err)
32 goto done;
34 err = reftable_iterator_next_ref(&it, ref);
35 if (err)
36 goto done;
38 if (strcmp(ref->refname, name) ||
39 reftable_ref_record_is_deletion(ref)) {
40 reftable_ref_record_release(ref);
41 err = 1;
42 goto done;
45 done:
46 reftable_iterator_destroy(&it);
47 return err;
50 uint64_t reftable_table_max_update_index(struct reftable_table *tab)
52 return tab->ops->max_update_index(tab->table_arg);
55 uint64_t reftable_table_min_update_index(struct reftable_table *tab)
57 return tab->ops->min_update_index(tab->table_arg);
60 uint32_t reftable_table_hash_id(struct reftable_table *tab)
62 return tab->ops->hash_id(tab->table_arg);
65 void reftable_iterator_destroy(struct reftable_iterator *it)
67 if (!it->ops) {
68 return;
70 it->ops->close(it->iter_arg);
71 it->ops = NULL;
72 FREE_AND_NULL(it->iter_arg);
75 int reftable_iterator_next_ref(struct reftable_iterator *it,
76 struct reftable_ref_record *ref)
78 struct reftable_record rec = { NULL };
79 reftable_record_from_ref(&rec, ref);
80 return iterator_next(it, &rec);
83 int reftable_iterator_next_log(struct reftable_iterator *it,
84 struct reftable_log_record *log)
86 struct reftable_record rec = { NULL };
87 reftable_record_from_log(&rec, log);
88 return iterator_next(it, &rec);
91 int iterator_next(struct reftable_iterator *it, struct reftable_record *rec)
93 return it->ops->next(it->iter_arg, rec);
96 static int empty_iterator_next(void *arg, struct reftable_record *rec)
98 return 1;
101 static void empty_iterator_close(void *arg)
105 static struct reftable_iterator_vtable empty_vtable = {
106 .next = &empty_iterator_next,
107 .close = &empty_iterator_close,
110 void iterator_set_empty(struct reftable_iterator *it)
112 assert(!it->ops);
113 it->iter_arg = NULL;
114 it->ops = &empty_vtable;