The eleventh batch
[git.git] / reftable / block.h
blob87c77539b5bd6ab0b9909c51801719258507b424
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;
89 /* initializes a block reader. */
90 int block_reader_init(struct block_reader *br, struct reftable_block *bl,
91 uint32_t header_off, uint32_t table_block_size,
92 int hash_size);
94 /* Position `it` at start of the block */
95 void block_reader_start(struct block_reader *br, struct block_iter *it);
97 /* Position `it` to the `want` key in the block */
98 int block_reader_seek(struct block_reader *br, struct block_iter *it,
99 struct strbuf *want);
101 /* Returns the block type (eg. 'r' for refs) */
102 uint8_t block_reader_type(struct block_reader *r);
104 /* Decodes the first key in the block */
105 int block_reader_first_key(struct block_reader *br, struct strbuf *key);
107 void block_iter_copy_from(struct block_iter *dest, struct block_iter *src);
109 /* return < 0 for error, 0 for OK, > 0 for EOF. */
110 int block_iter_next(struct block_iter *it, struct reftable_record *rec);
112 /* Seek to `want` with in the block pointed to by `it` */
113 int block_iter_seek(struct block_iter *it, struct strbuf *want);
115 /* deallocate memory for `it`. The block reader and its block is left intact. */
116 void block_iter_close(struct block_iter *it);
118 /* size of file header, depending on format version */
119 int header_size(int version);
121 /* size of file footer, depending on format version */
122 int footer_size(int version);
124 /* returns a block to its source. */
125 void reftable_block_done(struct reftable_block *ret);
127 #endif