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.
24 /* Offset of the global header. Nonzero in the first block only. */
27 /* How often to restart keys. */
31 /* Offset of next uint8_t to write. */
37 struct strbuf last_key
;
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
);
63 /* offset of the block header; nonzero for the first block in a
67 /* the memory block */
68 struct reftable_block block
;
71 /* Uncompressed data for log entries. */
73 unsigned char *uncompressed_data
;
74 size_t uncompressed_cap
;
76 /* size of the data, excluding restart data. */
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
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
,
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 */
101 /* offset within the block of the next entry to read. */
103 const unsigned char *block
;
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
);