Sync with 'maint'
[alt-git.git] / reftable / iter.h
blob40f98893b85f6d754dd320e6e964e6e91823f78c
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 #ifndef ITER_H
10 #define ITER_H
12 #include "system.h"
13 #include "block.h"
14 #include "record.h"
16 #include "reftable-iterator.h"
19 * The virtual function table for implementing generic reftable iterators.
21 struct reftable_iterator_vtable {
22 int (*seek)(void *iter_arg, struct reftable_record *want);
23 int (*next)(void *iter_arg, struct reftable_record *rec);
24 void (*close)(void *iter_arg);
28 * Position the iterator at the wanted record such that a call to
29 * `iterator_next()` would return that record, if it exists.
31 int iterator_seek(struct reftable_iterator *it, struct reftable_record *want);
34 * Yield the next record and advance the iterator. Returns <0 on error, 0 when
35 * a record was yielded, and >0 when the iterator hit an error.
37 int iterator_next(struct reftable_iterator *it, struct reftable_record *rec);
40 * Set up the iterator such that it behaves the same as an iterator with no
41 * entries.
43 void iterator_set_empty(struct reftable_iterator *it);
45 /* iterator that produces only ref records that point to `oid` */
46 struct filtering_ref_iterator {
47 struct reftable_buf oid;
48 struct reftable_iterator it;
50 #define FILTERING_REF_ITERATOR_INIT \
51 { \
52 .oid = REFTABLE_BUF_INIT \
55 void iterator_from_filtering_ref_iterator(struct reftable_iterator *,
56 struct filtering_ref_iterator *);
58 /* iterator that produces only ref records that point to `oid`,
59 * but using the object index.
61 struct indexed_table_ref_iter {
62 struct reftable_reader *r;
63 struct reftable_buf oid;
65 /* mutable */
66 uint64_t *offsets;
68 /* Points to the next offset to read. */
69 int offset_idx;
70 int offset_len;
71 struct block_reader block_reader;
72 struct block_iter cur;
73 int is_finished;
76 #define INDEXED_TABLE_REF_ITER_INIT { \
77 .cur = BLOCK_ITER_INIT, \
78 .oid = REFTABLE_BUF_INIT, \
81 void iterator_from_indexed_table_ref_iter(struct reftable_iterator *it,
82 struct indexed_table_ref_iter *itr);
84 /* Takes ownership of `offsets` */
85 int indexed_table_ref_iter_new(struct indexed_table_ref_iter **dest,
86 struct reftable_reader *r, uint8_t *oid,
87 int oid_len, uint64_t *offsets, int offset_len);
89 #endif