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
14 #include "reftable-blocksource.h"
17 * Writes reftable blocks. The block_writer is reused across blocks to minimize
18 * allocation overhead.
22 unsigned char *compressed
;
23 size_t compressed_cap
;
28 /* Offset of the global header. Nonzero in the first block only. */
31 /* How often to restart keys. */
32 uint16_t restart_interval
;
35 /* Offset of next uint8_t to write. */
41 struct strbuf last_key
;
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
);
67 /* offset of the block header; nonzero for the first block in a
71 /* the memory block */
72 struct reftable_block block
;
75 /* Uncompressed data for log entries. */
77 unsigned char *uncompressed_data
;
78 size_t uncompressed_cap
;
80 /* size of the data, excluding restart data. */
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
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
,
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 */
105 /* offset within the block of the next entry to read. */
107 const unsigned char *block
;
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
);