Git fork
1/*
2 * Copyright 2020 Google LLC
3 *
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 */
8
9#ifndef REFTABLE_TABLE_H
10#define REFTABLE_TABLE_H
11
12#include "reftable-iterator.h"
13#include "reftable-block.h"
14#include "reftable-blocksource.h"
15
16/*
17 * Reading single tables
18 *
19 * The follow routines are for reading single files. For an
20 * application-level interface, skip ahead to struct
21 * reftable_merged_table and struct reftable_stack.
22 */
23
24/* Metadata for a block type. */
25struct reftable_table_offsets {
26 int is_present;
27 uint64_t offset;
28 uint64_t index_offset;
29};
30
31/* The table struct is a handle to an open reftable file. */
32struct reftable_table {
33 /* for convenience, associate a name with the instance. */
34 char *name;
35 struct reftable_block_source source;
36
37 /* Size of the file, excluding the footer. */
38 uint64_t size;
39
40 /* The hash function used for ref records. */
41 enum reftable_hash hash_id;
42
43 uint32_t block_size;
44 uint64_t min_update_index;
45 uint64_t max_update_index;
46 /* Length of the OID keys in the 'o' section */
47 int object_id_len;
48 int version;
49
50 struct reftable_table_offsets ref_offsets;
51 struct reftable_table_offsets obj_offsets;
52 struct reftable_table_offsets log_offsets;
53
54 uint64_t refcount;
55};
56
57/* reftable_table_new opens a reftable for reading. If successful,
58 * returns 0 code and sets pp. The name is used for creating a
59 * stack. Typically, it is the basename of the file. The block source
60 * `src` is owned by the table, and is closed on calling
61 * reftable_table_destroy(). On error, the block source `src` is
62 * closed as well.
63 */
64int reftable_table_new(struct reftable_table **out,
65 struct reftable_block_source *src, const char *name);
66
67/*
68 * Manage the reference count of the reftable table. A newly initialized
69 * table starts with a refcount of 1 and will be deleted once the refcount has
70 * reached 0.
71 *
72 * This is required because tables may have longer lifetimes than the stack
73 * they belong to. The stack may for example be reloaded while the old tables
74 * are still being accessed by an iterator.
75 */
76void reftable_table_incref(struct reftable_table *table);
77void reftable_table_decref(struct reftable_table *table);
78
79/* Initialize a reftable iterator for reading refs. */
80int reftable_table_init_ref_iterator(struct reftable_table *t,
81 struct reftable_iterator *it);
82
83/* Initialize a reftable iterator for reading logs. */
84int reftable_table_init_log_iterator(struct reftable_table *t,
85 struct reftable_iterator *it);
86
87/* returns the hash ID used in this table. */
88enum reftable_hash reftable_table_hash_id(struct reftable_table *t);
89
90/* return an iterator for the refs pointing to `oid`. */
91int reftable_table_refs_for(struct reftable_table *t,
92 struct reftable_iterator *it, uint8_t *oid);
93
94/* return the max_update_index for a table */
95uint64_t reftable_table_max_update_index(struct reftable_table *t);
96
97/* return the min_update_index for a table */
98uint64_t reftable_table_min_update_index(struct reftable_table *t);
99
100/*
101 * An iterator that iterates through the blocks contained in a given table.
102 */
103struct reftable_table_iterator {
104 void *iter_arg;
105};
106
107int reftable_table_iterator_init(struct reftable_table_iterator *it,
108 struct reftable_table *t);
109
110void reftable_table_iterator_release(struct reftable_table_iterator *it);
111
112int reftable_table_iterator_next(struct reftable_table_iterator *it,
113 const struct reftable_block **out);
114
115#endif