Merge branch 'js/apply-partial-clone-filters-recursively'
[alt-git.git] / reftable / generic.c
blob57f8032db941ca77f5c61d0e6d38268c56958be9
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 "constants.h"
11 #include "record.h"
12 #include "generic.h"
13 #include "reftable-iterator.h"
14 #include "reftable-generic.h"
16 int reftable_table_seek_ref(struct reftable_table *tab,
17 struct reftable_iterator *it, const char *name)
19 struct reftable_record rec = { .type = BLOCK_TYPE_REF,
20 .u.ref = {
21 .refname = (char *)name,
22 } };
23 return tab->ops->seek_record(tab->table_arg, it, &rec);
26 int reftable_table_seek_log(struct reftable_table *tab,
27 struct reftable_iterator *it, const char *name)
29 struct reftable_record rec = { .type = BLOCK_TYPE_LOG,
30 .u.log = {
31 .refname = (char *)name,
32 .update_index = ~((uint64_t)0),
33 } };
34 return tab->ops->seek_record(tab->table_arg, it, &rec);
37 int reftable_table_read_ref(struct reftable_table *tab, const char *name,
38 struct reftable_ref_record *ref)
40 struct reftable_iterator it = { NULL };
41 int err = reftable_table_seek_ref(tab, &it, name);
42 if (err)
43 goto done;
45 err = reftable_iterator_next_ref(&it, ref);
46 if (err)
47 goto done;
49 if (strcmp(ref->refname, name) ||
50 reftable_ref_record_is_deletion(ref)) {
51 reftable_ref_record_release(ref);
52 err = 1;
53 goto done;
56 done:
57 reftable_iterator_destroy(&it);
58 return err;
61 int reftable_table_print(struct reftable_table *tab) {
62 struct reftable_iterator it = { NULL };
63 struct reftable_ref_record ref = { NULL };
64 struct reftable_log_record log = { NULL };
65 uint32_t hash_id = reftable_table_hash_id(tab);
66 int err = reftable_table_seek_ref(tab, &it, "");
67 if (err < 0) {
68 return err;
71 while (1) {
72 err = reftable_iterator_next_ref(&it, &ref);
73 if (err > 0) {
74 break;
76 if (err < 0) {
77 return err;
79 reftable_ref_record_print(&ref, hash_id);
81 reftable_iterator_destroy(&it);
82 reftable_ref_record_release(&ref);
84 err = reftable_table_seek_log(tab, &it, "");
85 if (err < 0) {
86 return err;
88 while (1) {
89 err = reftable_iterator_next_log(&it, &log);
90 if (err > 0) {
91 break;
93 if (err < 0) {
94 return err;
96 reftable_log_record_print(&log, hash_id);
98 reftable_iterator_destroy(&it);
99 reftable_log_record_release(&log);
100 return 0;
103 uint64_t reftable_table_max_update_index(struct reftable_table *tab)
105 return tab->ops->max_update_index(tab->table_arg);
108 uint64_t reftable_table_min_update_index(struct reftable_table *tab)
110 return tab->ops->min_update_index(tab->table_arg);
113 uint32_t reftable_table_hash_id(struct reftable_table *tab)
115 return tab->ops->hash_id(tab->table_arg);
118 void reftable_iterator_destroy(struct reftable_iterator *it)
120 if (!it->ops) {
121 return;
123 it->ops->close(it->iter_arg);
124 it->ops = NULL;
125 FREE_AND_NULL(it->iter_arg);
128 int reftable_iterator_next_ref(struct reftable_iterator *it,
129 struct reftable_ref_record *ref)
131 struct reftable_record rec = {
132 .type = BLOCK_TYPE_REF,
133 .u = {
134 .ref = *ref
137 int err = iterator_next(it, &rec);
138 *ref = rec.u.ref;
139 return err;
142 int reftable_iterator_next_log(struct reftable_iterator *it,
143 struct reftable_log_record *log)
145 struct reftable_record rec = {
146 .type = BLOCK_TYPE_LOG,
147 .u = {
148 .log = *log,
151 int err = iterator_next(it, &rec);
152 *log = rec.u.log;
153 return err;
156 int iterator_next(struct reftable_iterator *it, struct reftable_record *rec)
158 return it->ops->next(it->iter_arg, rec);
161 static int empty_iterator_next(void *arg, struct reftable_record *rec)
163 return 1;
166 static void empty_iterator_close(void *arg)
170 static struct reftable_iterator_vtable empty_vtable = {
171 .next = &empty_iterator_next,
172 .close = &empty_iterator_close,
175 void iterator_set_empty(struct reftable_iterator *it)
177 assert(!it->ops);
178 it->iter_arg = NULL;
179 it->ops = &empty_vtable;