Merge branch 'rj/launch-editor-error-message'
[alt-git.git] / reftable / block.h
blobea4384a7e2f99f8455075bbb5e7fb9e6148a901f
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 struct z_stream;
61 /* Read a block. */
62 struct block_reader {
63 /* offset of the block header; nonzero for the first block in a
64 * reftable. */
65 uint32_t header_off;
67 /* the memory block */
68 struct reftable_block block;
69 int hash_size;
71 /* Uncompressed data for log entries. */
72 z_stream *zstream;
73 unsigned char *uncompressed_data;
74 size_t uncompressed_cap;
76 /* size of the data, excluding restart data. */
77 uint32_t block_len;
78 uint8_t *restart_bytes;
79 uint16_t restart_count;
81 /* size of the data in the file. For log blocks, this is the compressed
82 * size. */
83 uint32_t full_block_size;
86 /* initializes a block reader. */
87 int block_reader_init(struct block_reader *br, struct reftable_block *bl,
88 uint32_t header_off, uint32_t table_block_size,
89 int hash_size);
91 void block_reader_release(struct block_reader *br);
93 /* Returns the block type (eg. 'r' for refs) */
94 uint8_t block_reader_type(const struct block_reader *r);
96 /* Decodes the first key in the block */
97 int block_reader_first_key(const struct block_reader *br, struct strbuf *key);
99 /* Iterate over entries in a block */
100 struct block_iter {
101 /* offset within the block of the next entry to read. */
102 uint32_t next_off;
103 const unsigned char *block;
104 size_t block_len;
105 int hash_size;
107 /* key for last entry we read. */
108 struct strbuf last_key;
109 struct strbuf scratch;
112 #define BLOCK_ITER_INIT { \
113 .last_key = STRBUF_INIT, \
114 .scratch = STRBUF_INIT, \
117 /* Position `it` at start of the block */
118 void block_iter_seek_start(struct block_iter *it, const struct block_reader *br);
120 /* Position `it` to the `want` key in the block */
121 int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
122 struct strbuf *want);
124 /* return < 0 for error, 0 for OK, > 0 for EOF. */
125 int block_iter_next(struct block_iter *it, struct reftable_record *rec);
127 /* Reset the block iterator to pristine state without releasing its memory. */
128 void block_iter_reset(struct block_iter *it);
130 /* deallocate memory for `it`. The block reader and its block is left intact. */
131 void block_iter_close(struct block_iter *it);
133 /* size of file header, depending on format version */
134 int header_size(int version);
136 /* size of file footer, depending on format version */
137 int footer_size(int version);
139 /* returns a block to its source. */
140 void reftable_block_done(struct reftable_block *ret);
142 #endif