completion: treat dangling symrefs as existing pseudorefs
[alt-git.git] / reftable / block.h
blob17481e6331979cc31972ee3dad576e9c594a1769
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 BLOCK_H
10 #define BLOCK_H
12 #include "basics.h"
13 #include "record.h"
14 #include "reftable-blocksource.h"
17 * Writes reftable blocks. The block_writer is reused across blocks to minimize
18 * allocation overhead.
20 struct block_writer {
21 uint8_t *buf;
22 uint32_t block_size;
24 /* Offset of the global header. Nonzero in the first block only. */
25 uint32_t header_off;
27 /* How often to restart keys. */
28 int restart_interval;
29 int hash_size;
31 /* Offset of next uint8_t to write. */
32 uint32_t next;
33 uint32_t *restarts;
34 uint32_t restart_len;
35 uint32_t restart_cap;
37 struct strbuf last_key;
38 int entries;
42 * initializes the blockwriter to write `typ` entries, using `buf` as temporary
43 * storage. `buf` is not owned by the block_writer. */
44 void block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *buf,
45 uint32_t block_size, uint32_t header_off, int hash_size);
47 /* returns the block type (eg. 'r' for ref records. */
48 uint8_t block_writer_type(struct block_writer *bw);
50 /* appends the record, or -1 if it doesn't fit. */
51 int block_writer_add(struct block_writer *w, struct reftable_record *rec);
53 /* appends the key restarts, and compress the block if necessary. */
54 int block_writer_finish(struct block_writer *w);
56 /* clears out internally allocated block_writer members. */
57 void block_writer_release(struct block_writer *bw);
59 /* Read a block. */
60 struct block_reader {
61 /* offset of the block header; nonzero for the first block in a
62 * reftable. */
63 uint32_t header_off;
65 /* the memory block */
66 struct reftable_block block;
67 int hash_size;
69 /* size of the data, excluding restart data. */
70 uint32_t block_len;
71 uint8_t *restart_bytes;
72 uint16_t restart_count;
74 /* size of the data in the file. For log blocks, this is the compressed
75 * size. */
76 uint32_t full_block_size;
79 /* Iterate over entries in a block */
80 struct block_iter {
81 /* offset within the block of the next entry to read. */
82 uint32_t next_off;
83 struct block_reader *br;
85 /* key for last entry we read. */
86 struct strbuf last_key;
87 struct strbuf key;
90 #define BLOCK_ITER_INIT { \
91 .last_key = STRBUF_INIT, \
92 .key = STRBUF_INIT, \
95 /* initializes a block reader. */
96 int block_reader_init(struct block_reader *br, struct reftable_block *bl,
97 uint32_t header_off, uint32_t table_block_size,
98 int hash_size);
100 /* Position `it` at start of the block */
101 void block_reader_start(struct block_reader *br, struct block_iter *it);
103 /* Position `it` to the `want` key in the block */
104 int block_reader_seek(struct block_reader *br, struct block_iter *it,
105 struct strbuf *want);
107 /* Returns the block type (eg. 'r' for refs) */
108 uint8_t block_reader_type(struct block_reader *r);
110 /* Decodes the first key in the block */
111 int block_reader_first_key(struct block_reader *br, struct strbuf *key);
113 void block_iter_copy_from(struct block_iter *dest, struct block_iter *src);
115 /* return < 0 for error, 0 for OK, > 0 for EOF. */
116 int block_iter_next(struct block_iter *it, struct reftable_record *rec);
118 /* Seek to `want` with in the block pointed to by `it` */
119 int block_iter_seek(struct block_iter *it, struct strbuf *want);
121 /* deallocate memory for `it`. The block reader and its block is left intact. */
122 void block_iter_close(struct block_iter *it);
124 /* size of file header, depending on format version */
125 int header_size(int version);
127 /* size of file footer, depending on format version */
128 int footer_size(int version);
130 /* returns a block to its source. */
131 void reftable_block_done(struct reftable_block *ret);
133 #endif