Merge branch 'jk/test-lsan-improvements'
[git/gitster.git] / reftable / block.h
blob1c8f25ee6eb6e5c400594f2b3e9b8d3687dbb837
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 z_stream *zstream;
22 unsigned char *compressed;
23 size_t compressed_cap;
25 uint8_t *buf;
26 uint32_t block_size;
28 /* Offset of the global header. Nonzero in the first block only. */
29 uint32_t header_off;
31 /* How often to restart keys. */
32 uint16_t restart_interval;
33 int hash_size;
35 /* Offset of next uint8_t to write. */
36 uint32_t next;
37 uint32_t *restarts;
38 uint32_t restart_len;
39 uint32_t restart_cap;
41 struct strbuf last_key;
42 int entries;
46 * initializes the blockwriter to write `typ` entries, using `buf` as temporary
47 * storage. `buf` is not owned by the block_writer. */
48 void block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *buf,
49 uint32_t block_size, uint32_t header_off, int hash_size);
51 /* returns the block type (eg. 'r' for ref records. */
52 uint8_t block_writer_type(struct block_writer *bw);
54 /* appends the record, or -1 if it doesn't fit. */
55 int block_writer_add(struct block_writer *w, struct reftable_record *rec);
57 /* appends the key restarts, and compress the block if necessary. */
58 int block_writer_finish(struct block_writer *w);
60 /* clears out internally allocated block_writer members. */
61 void block_writer_release(struct block_writer *bw);
63 struct z_stream;
65 /* Read a block. */
66 struct block_reader {
67 /* offset of the block header; nonzero for the first block in a
68 * reftable. */
69 uint32_t header_off;
71 /* the memory block */
72 struct reftable_block block;
73 int hash_size;
75 /* Uncompressed data for log entries. */
76 z_stream *zstream;
77 unsigned char *uncompressed_data;
78 size_t uncompressed_cap;
80 /* size of the data, excluding restart data. */
81 uint32_t block_len;
82 uint8_t *restart_bytes;
83 uint16_t restart_count;
85 /* size of the data in the file. For log blocks, this is the compressed
86 * size. */
87 uint32_t full_block_size;
90 /* initializes a block reader. */
91 int block_reader_init(struct block_reader *br, struct reftable_block *bl,
92 uint32_t header_off, uint32_t table_block_size,
93 int hash_size);
95 void block_reader_release(struct block_reader *br);
97 /* Returns the block type (eg. 'r' for refs) */
98 uint8_t block_reader_type(const struct block_reader *r);
100 /* Decodes the first key in the block */
101 int block_reader_first_key(const struct block_reader *br, struct strbuf *key);
103 /* Iterate over entries in a block */
104 struct block_iter {
105 /* offset within the block of the next entry to read. */
106 uint32_t next_off;
107 const unsigned char *block;
108 size_t block_len;
109 int hash_size;
111 /* key for last entry we read. */
112 struct strbuf last_key;
113 struct strbuf scratch;
116 #define BLOCK_ITER_INIT { \
117 .last_key = STRBUF_INIT, \
118 .scratch = STRBUF_INIT, \
121 /* Position `it` at start of the block */
122 void block_iter_seek_start(struct block_iter *it, const struct block_reader *br);
124 /* Position `it` to the `want` key in the block */
125 int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
126 struct strbuf *want);
128 /* return < 0 for error, 0 for OK, > 0 for EOF. */
129 int block_iter_next(struct block_iter *it, struct reftable_record *rec);
131 /* Reset the block iterator to pristine state without releasing its memory. */
132 void block_iter_reset(struct block_iter *it);
134 /* deallocate memory for `it`. The block reader and its block is left intact. */
135 void block_iter_close(struct block_iter *it);
137 /* size of file header, depending on format version */
138 int header_size(int version);
140 /* size of file footer, depending on format version */
141 int footer_size(int version);
143 /* returns a block to its source. */
144 void reftable_block_done(struct reftable_block *ret);
146 #endif