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