summaryrefslogtreecommitdiffstats
path: root/reftable/generic.c
blob: 28ae26145e6ef64ad970e66a6fa935092faba785 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
Copyright 2020 Google LLC

Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/

#include "constants.h"
#include "record.h"
#include "generic.h"
#include "reftable-iterator.h"
#include "reftable-generic.h"

void table_init_iter(struct reftable_table *tab,
		     struct reftable_iterator *it,
		     uint8_t typ)
{

	tab->ops->init_iter(tab->table_arg, it, typ);
}

void reftable_table_init_ref_iter(struct reftable_table *tab,
				  struct reftable_iterator *it)
{
	table_init_iter(tab, it, BLOCK_TYPE_REF);
}

void reftable_table_init_log_iter(struct reftable_table *tab,
				  struct reftable_iterator *it)
{
	table_init_iter(tab, it, BLOCK_TYPE_LOG);
}

int reftable_iterator_seek_ref(struct reftable_iterator *it,
			       const char *name)
{
	struct reftable_record want = {
		.type = BLOCK_TYPE_REF,
		.u.ref = {
			.refname = (char *)name,
		},
	};
	return it->ops->seek(it->iter_arg, &want);
}

int reftable_iterator_seek_log_at(struct reftable_iterator *it,
				  const char *name, uint64_t update_index)
{
	struct reftable_record want = {
		.type = BLOCK_TYPE_LOG,
		.u.log = {
			.refname = (char *)name,
			.update_index = update_index,
		},
	};
	return it->ops->seek(it->iter_arg, &want);
}

int reftable_iterator_seek_log(struct reftable_iterator *it,
			       const char *name)
{
	return reftable_iterator_seek_log_at(it, name, ~((uint64_t) 0));
}

int reftable_table_read_ref(struct reftable_table *tab, const char *name,
			    struct reftable_ref_record *ref)
{
	struct reftable_iterator it = { NULL };
	int err;

	reftable_table_init_ref_iter(tab, &it);

	err = reftable_iterator_seek_ref(&it, name);
	if (err)
		goto done;

	err = reftable_iterator_next_ref(&it, ref);
	if (err)
		goto done;

	if (strcmp(ref->refname, name) ||
	    reftable_ref_record_is_deletion(ref)) {
		reftable_ref_record_release(ref);
		err = 1;
		goto done;
	}

done:
	reftable_iterator_destroy(&it);
	return err;
}

int reftable_table_print(struct reftable_table *tab) {
	struct reftable_iterator it = { NULL };
	struct reftable_ref_record ref = { NULL };
	struct reftable_log_record log = { NULL };
	uint32_t hash_id = reftable_table_hash_id(tab);
	int err;

	reftable_table_init_ref_iter(tab, &it);

	err = reftable_iterator_seek_ref(&it, "");
	if (err < 0)
		return err;

	while (1) {
		err = reftable_iterator_next_ref(&it, &ref);
		if (err > 0) {
			break;
		}
		if (err < 0) {
			return err;
		}
		reftable_ref_record_print(&ref, hash_id);
	}
	reftable_iterator_destroy(&it);
	reftable_ref_record_release(&ref);

	reftable_table_init_log_iter(tab, &it);

	err = reftable_iterator_seek_log(&it, "");
	if (err < 0)
		return err;

	while (1) {
		err = reftable_iterator_next_log(&it, &log);
		if (err > 0) {
			break;
		}
		if (err < 0) {
			return err;
		}
		reftable_log_record_print(&log, hash_id);
	}
	reftable_iterator_destroy(&it);
	reftable_log_record_release(&log);
	return 0;
}

uint64_t reftable_table_max_update_index(struct reftable_table *tab)
{
	return tab->ops->max_update_index(tab->table_arg);
}

uint64_t reftable_table_min_update_index(struct reftable_table *tab)
{
	return tab->ops->min_update_index(tab->table_arg);
}

uint32_t reftable_table_hash_id(struct reftable_table *tab)
{
	return tab->ops->hash_id(tab->table_arg);
}

void reftable_iterator_destroy(struct reftable_iterator *it)
{
	if (!it->ops) {
		return;
	}
	it->ops->close(it->iter_arg);
	it->ops = NULL;
	FREE_AND_NULL(it->iter_arg);
}

int reftable_iterator_next_ref(struct reftable_iterator *it,
			       struct reftable_ref_record *ref)
{
	struct reftable_record rec = {
		.type = BLOCK_TYPE_REF,
		.u = {
			.ref = *ref
		},
	};
	int err = iterator_next(it, &rec);
	*ref = rec.u.ref;
	return err;
}

int reftable_iterator_next_log(struct reftable_iterator *it,
			       struct reftable_log_record *log)
{
	struct reftable_record rec = {
		.type = BLOCK_TYPE_LOG,
		.u = {
			.log = *log,
		},
	};
	int err = iterator_next(it, &rec);
	*log = rec.u.log;
	return err;
}

int iterator_seek(struct reftable_iterator *it, struct reftable_record *want)
{
	return it->ops->seek(it->iter_arg, want);
}

int iterator_next(struct reftable_iterator *it, struct reftable_record *rec)
{
	return it->ops->next(it->iter_arg, rec);
}

static int empty_iterator_seek(void *arg, struct reftable_record *want)
{
	return 0;
}

static int empty_iterator_next(void *arg, struct reftable_record *rec)
{
	return 1;
}

static void empty_iterator_close(void *arg)
{
}

static struct reftable_iterator_vtable empty_vtable = {
	.seek = &empty_iterator_seek,
	.next = &empty_iterator_next,
	.close = &empty_iterator_close,
};

void iterator_set_empty(struct reftable_iterator *it)
{
	assert(!it->ops);
	it->iter_arg = NULL;
	it->ops = &empty_vtable;
}