]>
Commit | Line | Data |
---|---|---|
e581fd72 HWN |
1 | /* |
2 | Copyright 2020 Google LLC | |
3 | ||
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 | */ | |
8 | ||
9 | #ifndef BLOCK_H | |
10 | #define BLOCK_H | |
11 | ||
12 | #include "basics.h" | |
13 | #include "record.h" | |
14 | #include "reftable-blocksource.h" | |
15 | ||
16 | /* | |
17 | * Writes reftable blocks. The block_writer is reused across blocks to minimize | |
18 | * allocation overhead. | |
19 | */ | |
20 | struct block_writer { | |
21 | uint8_t *buf; | |
22 | uint32_t block_size; | |
23 | ||
019bd340 | 24 | /* Offset of the global header. Nonzero in the first block only. */ |
e581fd72 HWN |
25 | uint32_t header_off; |
26 | ||
27 | /* How often to restart keys. */ | |
28 | int restart_interval; | |
29 | int hash_size; | |
30 | ||
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; | |
36 | ||
37 | struct strbuf last_key; | |
38 | int entries; | |
39 | }; | |
40 | ||
41 | /* | |
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); | |
46 | ||
47 | /* returns the block type (eg. 'r' for ref records. */ | |
48 | uint8_t block_writer_type(struct block_writer *bw); | |
49 | ||
50 | /* appends the record, or -1 if it doesn't fit. */ | |
51 | int block_writer_add(struct block_writer *w, struct reftable_record *rec); | |
52 | ||
53 | /* appends the key restarts, and compress the block if necessary. */ | |
54 | int block_writer_finish(struct block_writer *w); | |
55 | ||
56 | /* clears out internally allocated block_writer members. */ | |
57 | void block_writer_release(struct block_writer *bw); | |
58 | ||
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; | |
64 | ||
65 | /* the memory block */ | |
66 | struct reftable_block block; | |
67 | int hash_size; | |
68 | ||
69 | /* size of the data, excluding restart data. */ | |
70 | uint32_t block_len; | |
71 | uint8_t *restart_bytes; | |
72 | uint16_t restart_count; | |
73 | ||
74 | /* size of the data in the file. For log blocks, this is the compressed | |
75 | * size. */ | |
76 | uint32_t full_block_size; | |
77 | }; | |
78 | ||
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; | |
84 | ||
85 | /* key for last entry we read. */ | |
86 | struct strbuf last_key; | |
87 | }; | |
88 | ||
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); | |
93 | ||
94 | /* Position `it` at start of the block */ | |
95 | void block_reader_start(struct block_reader *br, struct block_iter *it); | |
96 | ||
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); | |
100 | ||
101 | /* Returns the block type (eg. 'r' for refs) */ | |
102 | uint8_t block_reader_type(struct block_reader *r); | |
103 | ||
104 | /* Decodes the first key in the block */ | |
105 | int block_reader_first_key(struct block_reader *br, struct strbuf *key); | |
106 | ||
107 | void block_iter_copy_from(struct block_iter *dest, struct block_iter *src); | |
108 | ||
109 | /* return < 0 for error, 0 for OK, > 0 for EOF. */ | |
110 | int block_iter_next(struct block_iter *it, struct reftable_record *rec); | |
111 | ||
112 | /* Seek to `want` with in the block pointed to by `it` */ | |
113 | int block_iter_seek(struct block_iter *it, struct strbuf *want); | |
114 | ||
115 | /* deallocate memory for `it`. The block reader and its block is left intact. */ | |
116 | void block_iter_close(struct block_iter *it); | |
117 | ||
118 | /* size of file header, depending on format version */ | |
119 | int header_size(int version); | |
120 | ||
121 | /* size of file footer, depending on format version */ | |
122 | int footer_size(int version); | |
123 | ||
124 | /* returns a block to its source. */ | |
125 | void reftable_block_done(struct reftable_block *ret); | |
126 | ||
127 | #endif |