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
10 #include "constants.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
,
21 .refname
= (char *)name
,
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
,
31 .refname
= (char *)name
,
32 .update_index
= ~((uint64_t)0),
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
);
45 err
= reftable_iterator_next_ref(&it
, ref
);
49 if (strcmp(ref
->refname
, name
) ||
50 reftable_ref_record_is_deletion(ref
)) {
51 reftable_ref_record_release(ref
);
57 reftable_iterator_destroy(&it
);
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
, "");
72 err
= reftable_iterator_next_ref(&it
, &ref
);
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
, "");
89 err
= reftable_iterator_next_log(&it
, &log
);
96 reftable_log_record_print(&log
, hash_id
);
98 reftable_iterator_destroy(&it
);
99 reftable_log_record_release(&log
);
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
)
123 it
->ops
->close(it
->iter_arg
);
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
,
137 int err
= iterator_next(it
, &rec
);
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
,
151 int err
= iterator_next(it
, &rec
);
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
)
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
)
179 it
->ops
= &empty_vtable
;