The sixth batch
[alt-git.git] / reftable / block.h
blobbef2b8a4c5c31edf26c18d586d9ec1abfe1906d9
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 *block;
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 uint32_t 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 reftable_buf last_key;
42 /* Scratch buffer used to avoid allocations. */
43 struct reftable_buf scratch;
44 int entries;
48 * initializes the blockwriter to write `typ` entries, using `block` as temporary
49 * storage. `block` is not owned by the block_writer. */
50 int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,
51 uint32_t block_size, uint32_t header_off, uint32_t hash_size);
53 /* returns the block type (eg. 'r' for ref records. */
54 uint8_t block_writer_type(struct block_writer *bw);
56 /* appends the record, or -1 if it doesn't fit. */
57 int block_writer_add(struct block_writer *w, struct reftable_record *rec);
59 /* appends the key restarts, and compress the block if necessary. */
60 int block_writer_finish(struct block_writer *w);
62 /* clears out internally allocated block_writer members. */
63 void block_writer_release(struct block_writer *bw);
65 struct z_stream;
67 /* Read a block. */
68 struct block_reader {
69 /* offset of the block header; nonzero for the first block in a
70 * reftable. */
71 uint32_t header_off;
73 /* the memory block */
74 struct reftable_block block;
75 uint32_t hash_size;
77 /* Uncompressed data for log entries. */
78 z_stream *zstream;
79 unsigned char *uncompressed_data;
80 size_t uncompressed_cap;
82 /* size of the data, excluding restart data. */
83 uint32_t block_len;
84 uint8_t *restart_bytes;
85 uint16_t restart_count;
87 /* size of the data in the file. For log blocks, this is the compressed
88 * size. */
89 uint32_t full_block_size;
92 /* initializes a block reader. */
93 int block_reader_init(struct block_reader *br, struct reftable_block *bl,
94 uint32_t header_off, uint32_t table_block_size,
95 uint32_t hash_size);
97 void block_reader_release(struct block_reader *br);
99 /* Returns the block type (eg. 'r' for refs) */
100 uint8_t block_reader_type(const struct block_reader *r);
102 /* Decodes the first key in the block */
103 int block_reader_first_key(const struct block_reader *br, struct reftable_buf *key);
105 /* Iterate over entries in a block */
106 struct block_iter {
107 /* offset within the block of the next entry to read. */
108 uint32_t next_off;
109 const unsigned char *block;
110 size_t block_len;
111 uint32_t hash_size;
113 /* key for last entry we read. */
114 struct reftable_buf last_key;
115 struct reftable_buf scratch;
118 #define BLOCK_ITER_INIT { \
119 .last_key = REFTABLE_BUF_INIT, \
120 .scratch = REFTABLE_BUF_INIT, \
123 /* Position `it` at start of the block */
124 void block_iter_seek_start(struct block_iter *it, const struct block_reader *br);
126 /* Position `it` to the `want` key in the block */
127 int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
128 struct reftable_buf *want);
130 /* return < 0 for error, 0 for OK, > 0 for EOF. */
131 int block_iter_next(struct block_iter *it, struct reftable_record *rec);
133 /* Reset the block iterator to pristine state without releasing its memory. */
134 void block_iter_reset(struct block_iter *it);
136 /* deallocate memory for `it`. The block reader and its block is left intact. */
137 void block_iter_close(struct block_iter *it);
139 /* size of file header, depending on format version */
140 size_t header_size(int version);
142 /* size of file footer, depending on format version */
143 size_t footer_size(int version);
145 /* returns a block to its source. */
146 void reftable_block_done(struct reftable_block *ret);
148 #endif