Han-Wen Nienhuys | e303bf2 | 2021-10-07 20:25:02 +0000 | [diff] [blame] | 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 RECORD_H |
| 10 | #define RECORD_H |
| 11 | |
| 12 | #include "system.h" |
| 13 | |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | #include "reftable-record.h" |
| 17 | |
| 18 | /* |
| 19 | * A substring of existing string data. This structure takes no responsibility |
| 20 | * for the lifetime of the data it points to. |
| 21 | */ |
| 22 | struct string_view { |
| 23 | uint8_t *buf; |
| 24 | size_t len; |
| 25 | }; |
| 26 | |
| 27 | /* Advance `s.buf` by `n`, and decrease length. */ |
| 28 | void string_view_consume(struct string_view *s, int n); |
| 29 | |
| 30 | /* utilities for de/encoding varints */ |
| 31 | |
| 32 | int get_var_int(uint64_t *dest, struct string_view *in); |
| 33 | int put_var_int(struct string_view *dest, uint64_t val); |
| 34 | |
| 35 | /* Methods for records. */ |
| 36 | struct reftable_record_vtable { |
| 37 | /* encode the key of to a uint8_t strbuf. */ |
| 38 | void (*key)(const void *rec, struct strbuf *dest); |
| 39 | |
| 40 | /* The record type of ('r' for ref). */ |
| 41 | uint8_t type; |
| 42 | |
| 43 | void (*copy_from)(void *dest, const void *src, int hash_size); |
| 44 | |
| 45 | /* a value of [0..7], indicating record subvariants (eg. ref vs. symref |
| 46 | * vs ref deletion) */ |
| 47 | uint8_t (*val_type)(const void *rec); |
| 48 | |
| 49 | /* encodes rec into dest, returning how much space was used. */ |
| 50 | int (*encode)(const void *rec, struct string_view dest, int hash_size); |
| 51 | |
| 52 | /* decode data from `src` into the record. */ |
| 53 | int (*decode)(void *rec, struct strbuf key, uint8_t extra, |
| 54 | struct string_view src, int hash_size); |
| 55 | |
| 56 | /* deallocate and null the record. */ |
| 57 | void (*release)(void *rec); |
| 58 | |
| 59 | /* is this a tombstone? */ |
| 60 | int (*is_deletion)(const void *rec); |
Han-Wen Nienhuys | c983374 | 2022-01-20 15:12:11 +0000 | [diff] [blame] | 61 | |
| 62 | /* Are two records equal? This assumes they have the same type. Returns 0 for non-equal. */ |
| 63 | int (*equal)(const void *a, const void *b, int hash_size); |
Han-Wen Nienhuys | 01033de | 2022-01-20 15:12:14 +0000 | [diff] [blame] | 64 | |
| 65 | /* Print on stdout, for debugging. */ |
| 66 | void (*print)(const void *rec, int hash_size); |
Han-Wen Nienhuys | e303bf2 | 2021-10-07 20:25:02 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Han-Wen Nienhuys | e303bf2 | 2021-10-07 20:25:02 +0000 | [diff] [blame] | 69 | /* returns true for recognized block types. Block start with the block type. */ |
| 70 | int reftable_is_block_type(uint8_t typ); |
| 71 | |
Han-Wen Nienhuys | 66c0dab | 2022-01-20 15:12:13 +0000 | [diff] [blame] | 72 | /* return an initialized record for the given type */ |
Han-Wen Nienhuys | e303bf2 | 2021-10-07 20:25:02 +0000 | [diff] [blame] | 73 | struct reftable_record reftable_new_record(uint8_t typ); |
| 74 | |
| 75 | /* Encode `key` into `dest`. Sets `is_restart` to indicate a restart. Returns |
| 76 | * number of bytes written. */ |
| 77 | int reftable_encode_key(int *is_restart, struct string_view dest, |
| 78 | struct strbuf prev_key, struct strbuf key, |
| 79 | uint8_t extra); |
| 80 | |
| 81 | /* Decode into `key` and `extra` from `in` */ |
| 82 | int reftable_decode_key(struct strbuf *key, uint8_t *extra, |
| 83 | struct strbuf last_key, struct string_view in); |
| 84 | |
| 85 | /* reftable_index_record are used internally to speed up lookups. */ |
| 86 | struct reftable_index_record { |
| 87 | uint64_t offset; /* Offset of block */ |
| 88 | struct strbuf last_key; /* Last key of the block. */ |
| 89 | }; |
| 90 | |
| 91 | /* reftable_obj_record stores an object ID => ref mapping. */ |
| 92 | struct reftable_obj_record { |
| 93 | uint8_t *hash_prefix; /* leading bytes of the object ID */ |
| 94 | int hash_prefix_len; /* number of leading bytes. Constant |
| 95 | * across a single table. */ |
| 96 | uint64_t *offsets; /* a vector of file offsets. */ |
| 97 | int offset_len; |
| 98 | }; |
| 99 | |
Han-Wen Nienhuys | 66c0dab | 2022-01-20 15:12:13 +0000 | [diff] [blame] | 100 | /* record is a generic wrapper for different types of records. It is normally |
| 101 | * created on the stack, or embedded within another struct. If the type is |
| 102 | * known, a fresh instance can be initialized explicitly. Otherwise, use |
| 103 | * reftable_new_record() to initialize generically (as the index_record is not |
| 104 | * valid as 0-initialized structure) |
| 105 | */ |
| 106 | struct reftable_record { |
| 107 | uint8_t type; |
| 108 | union { |
| 109 | struct reftable_ref_record ref; |
| 110 | struct reftable_log_record log; |
| 111 | struct reftable_obj_record obj; |
| 112 | struct reftable_index_record idx; |
| 113 | } u; |
| 114 | }; |
| 115 | |
Han-Wen Nienhuys | e303bf2 | 2021-10-07 20:25:02 +0000 | [diff] [blame] | 116 | /* see struct record_vtable */ |
Han-Wen Nienhuys | c983374 | 2022-01-20 15:12:11 +0000 | [diff] [blame] | 117 | int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size); |
Han-Wen Nienhuys | 01033de | 2022-01-20 15:12:14 +0000 | [diff] [blame] | 118 | void reftable_record_print(struct reftable_record *rec, int hash_size); |
Han-Wen Nienhuys | e303bf2 | 2021-10-07 20:25:02 +0000 | [diff] [blame] | 119 | void reftable_record_key(struct reftable_record *rec, struct strbuf *dest); |
| 120 | uint8_t reftable_record_type(struct reftable_record *rec); |
| 121 | void reftable_record_copy_from(struct reftable_record *rec, |
| 122 | struct reftable_record *src, int hash_size); |
| 123 | uint8_t reftable_record_val_type(struct reftable_record *rec); |
| 124 | int reftable_record_encode(struct reftable_record *rec, struct string_view dest, |
| 125 | int hash_size); |
| 126 | int reftable_record_decode(struct reftable_record *rec, struct strbuf key, |
| 127 | uint8_t extra, struct string_view src, |
| 128 | int hash_size); |
| 129 | int reftable_record_is_deletion(struct reftable_record *rec); |
| 130 | |
Han-Wen Nienhuys | 66c0dab | 2022-01-20 15:12:13 +0000 | [diff] [blame] | 131 | /* frees and zeroes out the embedded record */ |
Han-Wen Nienhuys | e303bf2 | 2021-10-07 20:25:02 +0000 | [diff] [blame] | 132 | void reftable_record_release(struct reftable_record *rec); |
| 133 | |
Han-Wen Nienhuys | e303bf2 | 2021-10-07 20:25:02 +0000 | [diff] [blame] | 134 | /* for qsort. */ |
| 135 | int reftable_ref_record_compare_name(const void *a, const void *b); |
| 136 | |
| 137 | /* for qsort. */ |
| 138 | int reftable_log_record_compare_key(const void *a, const void *b); |
| 139 | |
| 140 | #endif |