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
12 #include "reftable-iterator.h"
13 #include "reftable-generic.h"
15 void table_init_iter(struct reftable_table
*tab
,
16 struct reftable_iterator
*it
,
20 tab
->ops
->init_iter(tab
->table_arg
, it
, typ
);
23 void reftable_table_init_ref_iter(struct reftable_table
*tab
,
24 struct reftable_iterator
*it
)
26 table_init_iter(tab
, it
, BLOCK_TYPE_REF
);
29 void reftable_table_init_log_iter(struct reftable_table
*tab
,
30 struct reftable_iterator
*it
)
32 table_init_iter(tab
, it
, BLOCK_TYPE_LOG
);
35 int reftable_iterator_seek_ref(struct reftable_iterator
*it
,
38 struct reftable_record want
= {
39 .type
= BLOCK_TYPE_REF
,
41 .refname
= (char *)name
,
44 return it
->ops
->seek(it
->iter_arg
, &want
);
47 int reftable_iterator_seek_log_at(struct reftable_iterator
*it
,
48 const char *name
, uint64_t update_index
)
50 struct reftable_record want
= {
51 .type
= BLOCK_TYPE_LOG
,
53 .refname
= (char *)name
,
54 .update_index
= update_index
,
57 return it
->ops
->seek(it
->iter_arg
, &want
);
60 int reftable_iterator_seek_log(struct reftable_iterator
*it
,
63 return reftable_iterator_seek_log_at(it
, name
, ~((uint64_t) 0));
66 int reftable_table_read_ref(struct reftable_table
*tab
, const char *name
,
67 struct reftable_ref_record
*ref
)
69 struct reftable_iterator it
= { NULL
};
72 reftable_table_init_ref_iter(tab
, &it
);
74 err
= reftable_iterator_seek_ref(&it
, name
);
78 err
= reftable_iterator_next_ref(&it
, ref
);
82 if (strcmp(ref
->refname
, name
) ||
83 reftable_ref_record_is_deletion(ref
)) {
84 reftable_ref_record_release(ref
);
90 reftable_iterator_destroy(&it
);
94 int reftable_table_print(struct reftable_table
*tab
) {
95 struct reftable_iterator it
= { NULL
};
96 struct reftable_ref_record ref
= { NULL
};
97 struct reftable_log_record log
= { NULL
};
98 uint32_t hash_id
= reftable_table_hash_id(tab
);
101 reftable_table_init_ref_iter(tab
, &it
);
103 err
= reftable_iterator_seek_ref(&it
, "");
108 err
= reftable_iterator_next_ref(&it
, &ref
);
115 reftable_ref_record_print(&ref
, hash_id
);
117 reftable_iterator_destroy(&it
);
118 reftable_ref_record_release(&ref
);
120 reftable_table_init_log_iter(tab
, &it
);
122 err
= reftable_iterator_seek_log(&it
, "");
127 err
= reftable_iterator_next_log(&it
, &log
);
134 reftable_log_record_print(&log
, hash_id
);
136 reftable_iterator_destroy(&it
);
137 reftable_log_record_release(&log
);
141 uint64_t reftable_table_max_update_index(struct reftable_table
*tab
)
143 return tab
->ops
->max_update_index(tab
->table_arg
);
146 uint64_t reftable_table_min_update_index(struct reftable_table
*tab
)
148 return tab
->ops
->min_update_index(tab
->table_arg
);
151 uint32_t reftable_table_hash_id(struct reftable_table
*tab
)
153 return tab
->ops
->hash_id(tab
->table_arg
);
156 void reftable_iterator_destroy(struct reftable_iterator
*it
)
161 it
->ops
->close(it
->iter_arg
);
163 FREE_AND_NULL(it
->iter_arg
);
166 int reftable_iterator_next_ref(struct reftable_iterator
*it
,
167 struct reftable_ref_record
*ref
)
169 struct reftable_record rec
= {
170 .type
= BLOCK_TYPE_REF
,
175 int err
= iterator_next(it
, &rec
);
180 int reftable_iterator_next_log(struct reftable_iterator
*it
,
181 struct reftable_log_record
*log
)
183 struct reftable_record rec
= {
184 .type
= BLOCK_TYPE_LOG
,
189 int err
= iterator_next(it
, &rec
);
194 int iterator_seek(struct reftable_iterator
*it
, struct reftable_record
*want
)
196 return it
->ops
->seek(it
->iter_arg
, want
);
199 int iterator_next(struct reftable_iterator
*it
, struct reftable_record
*rec
)
201 return it
->ops
->next(it
->iter_arg
, rec
);
204 static int empty_iterator_seek(void *arg
, struct reftable_record
*want
)
209 static int empty_iterator_next(void *arg
, struct reftable_record
*rec
)
214 static void empty_iterator_close(void *arg
)
218 static struct reftable_iterator_vtable empty_vtable
= {
219 .seek
= &empty_iterator_seek
,
220 .next
= &empty_iterator_next
,
221 .close
= &empty_iterator_close
,
224 void iterator_set_empty(struct reftable_iterator
*it
)
228 it
->ops
= &empty_vtable
;