]> git.ipfire.org Git - thirdparty/git.git/blob - reftable/record.h
Merge branch 'ps/t0610-umask-fix'
[thirdparty/git.git] / reftable / record.h
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 static inline void string_view_consume(struct string_view *s, int n)
29 {
30 s->buf += n;
31 s->len -= n;
32 }
33
34 /* utilities for de/encoding varints */
35
36 int get_var_int(uint64_t *dest, struct string_view *in);
37 int put_var_int(struct string_view *dest, uint64_t val);
38
39 /* Methods for records. */
40 struct reftable_record_vtable {
41 /* encode the key of to a uint8_t strbuf. */
42 void (*key)(const void *rec, struct strbuf *dest);
43
44 /* The record type of ('r' for ref). */
45 uint8_t type;
46
47 void (*copy_from)(void *dest, const void *src, int hash_size);
48
49 /* a value of [0..7], indicating record subvariants (eg. ref vs. symref
50 * vs ref deletion) */
51 uint8_t (*val_type)(const void *rec);
52
53 /* encodes rec into dest, returning how much space was used. */
54 int (*encode)(const void *rec, struct string_view dest, int hash_size);
55
56 /* decode data from `src` into the record. */
57 int (*decode)(void *rec, struct strbuf key, uint8_t extra,
58 struct string_view src, int hash_size,
59 struct strbuf *scratch);
60
61 /* deallocate and null the record. */
62 void (*release)(void *rec);
63
64 /* is this a tombstone? */
65 int (*is_deletion)(const void *rec);
66
67 /* Are two records equal? This assumes they have the same type. Returns 0 for non-equal. */
68 int (*equal)(const void *a, const void *b, int hash_size);
69
70 /*
71 * Compare keys of two records with each other. The records must have
72 * the same type.
73 */
74 int (*cmp)(const void *a, const void *b);
75
76 /* Print on stdout, for debugging. */
77 void (*print)(const void *rec, int hash_size);
78 };
79
80 /* returns true for recognized block types. Block start with the block type. */
81 int reftable_is_block_type(uint8_t typ);
82
83 /* Encode `key` into `dest`. Sets `is_restart` to indicate a restart. Returns
84 * number of bytes written. */
85 int reftable_encode_key(int *is_restart, struct string_view dest,
86 struct strbuf prev_key, struct strbuf key,
87 uint8_t extra);
88
89 /* Decode a record's key lengths. */
90 int reftable_decode_keylen(struct string_view in,
91 uint64_t *prefix_len,
92 uint64_t *suffix_len,
93 uint8_t *extra);
94
95 /*
96 * Decode into `last_key` and `extra` from `in`. `last_key` is expected to
97 * contain the decoded key of the preceding record, if any.
98 */
99 int reftable_decode_key(struct strbuf *last_key, uint8_t *extra,
100 struct string_view in);
101
102 /* reftable_index_record are used internally to speed up lookups. */
103 struct reftable_index_record {
104 uint64_t offset; /* Offset of block */
105 struct strbuf last_key; /* Last key of the block. */
106 };
107
108 /* reftable_obj_record stores an object ID => ref mapping. */
109 struct reftable_obj_record {
110 uint8_t *hash_prefix; /* leading bytes of the object ID */
111 int hash_prefix_len; /* number of leading bytes. Constant
112 * across a single table. */
113 uint64_t *offsets; /* a vector of file offsets. */
114 int offset_len;
115 };
116
117 /* record is a generic wrapper for different types of records. It is normally
118 * created on the stack, or embedded within another struct. If the type is
119 * known, a fresh instance can be initialized explicitly. Otherwise, use
120 * `reftable_record_init()` to initialize generically (as the index_record is
121 * not valid as 0-initialized structure)
122 */
123 struct reftable_record {
124 uint8_t type;
125 union {
126 struct reftable_ref_record ref;
127 struct reftable_log_record log;
128 struct reftable_obj_record obj;
129 struct reftable_index_record idx;
130 } u;
131 };
132
133 /* Initialize the reftable record for the given type */
134 void reftable_record_init(struct reftable_record *rec, uint8_t typ);
135
136 /* see struct record_vtable */
137 int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b);
138 int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size);
139 void reftable_record_print(struct reftable_record *rec, int hash_size);
140 void reftable_record_key(struct reftable_record *rec, struct strbuf *dest);
141 void reftable_record_copy_from(struct reftable_record *rec,
142 struct reftable_record *src, int hash_size);
143 uint8_t reftable_record_val_type(struct reftable_record *rec);
144 int reftable_record_encode(struct reftable_record *rec, struct string_view dest,
145 int hash_size);
146 int reftable_record_decode(struct reftable_record *rec, struct strbuf key,
147 uint8_t extra, struct string_view src,
148 int hash_size, struct strbuf *scratch);
149 int reftable_record_is_deletion(struct reftable_record *rec);
150
151 static inline uint8_t reftable_record_type(struct reftable_record *rec)
152 {
153 return rec->type;
154 }
155
156 /* frees and zeroes out the embedded record */
157 void reftable_record_release(struct reftable_record *rec);
158
159 /* for qsort. */
160 int reftable_ref_record_compare_name(const void *a, const void *b);
161
162 /* for qsort. */
163 int reftable_log_record_compare_key(const void *a, const void *b);
164
165 #endif