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
);
61 /* offset of the block header; nonzero for the first block in a
65 /* the memory block */
66 struct reftable_block block
;
69 /* size of the data, excluding restart data. */
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
76 uint32_t full_block_size
;
79 /* Iterate over entries in a block */
81 /* offset within the block of the next entry to read. */
83 struct block_reader
*br
;
85 /* key for last entry we read. */
86 struct strbuf last_key
;
90 #define BLOCK_ITER_INIT { \
91 .last_key = STRBUF_INIT, \
95 /* initializes a block reader. */
96 int block_reader_init(struct block_reader
*br
, struct reftable_block
*bl
,
97 uint32_t header_off
, uint32_t table_block_size
,
100 /* Position `it` at start of the block */
101 void block_reader_start(struct block_reader
*br
, struct block_iter
*it
);
103 /* Position `it` to the `want` key in the block */
104 int block_reader_seek(struct block_reader
*br
, struct block_iter
*it
,
105 struct strbuf
*want
);
107 /* Returns the block type (eg. 'r' for refs) */
108 uint8_t block_reader_type(struct block_reader
*r
);
110 /* Decodes the first key in the block */
111 int block_reader_first_key(struct block_reader
*br
, struct strbuf
*key
);
113 void block_iter_copy_from(struct block_iter
*dest
, struct block_iter
*src
);
115 /* return < 0 for error, 0 for OK, > 0 for EOF. */
116 int block_iter_next(struct block_iter
*it
, struct reftable_record
*rec
);
118 /* Seek to `want` with in the block pointed to by `it` */
119 int block_iter_seek(struct block_iter
*it
, struct strbuf
*want
);
121 /* deallocate memory for `it`. The block reader and its block is left intact. */
122 void block_iter_close(struct block_iter
*it
);
124 /* size of file header, depending on format version */
125 int header_size(int version
);
127 /* size of file footer, depending on format version */
128 int footer_size(int version
);
130 /* returns a block to its source. */
131 void reftable_block_done(struct reftable_block
*ret
);