Git fork
at reftables-rust 115 lines 3.6 kB view raw
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-block.h" 15#include "reftable-blocksource.h" 16 17/* 18 * Writes reftable blocks. The block_writer is reused across blocks to minimize 19 * allocation overhead. 20 */ 21struct block_writer { 22 struct z_stream_s *zstream; 23 unsigned char *compressed; 24 size_t compressed_cap; 25 26 uint8_t *block; 27 uint32_t block_size; 28 29 /* Offset of the global header. Nonzero in the first block only. */ 30 uint32_t header_off; 31 32 /* How often to restart keys. */ 33 uint16_t restart_interval; 34 uint32_t hash_size; 35 36 /* Offset of next uint8_t to write. */ 37 uint32_t next; 38 uint32_t *restarts; 39 uint32_t restart_len; 40 uint32_t restart_cap; 41 42 struct reftable_buf last_key; 43 /* Scratch buffer used to avoid allocations. */ 44 struct reftable_buf scratch; 45 int entries; 46}; 47 48/* 49 * initializes the blockwriter to write `typ` entries, using `block` as temporary 50 * storage. `block` is not owned by the block_writer. */ 51int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block, 52 uint32_t block_size, uint32_t header_off, uint32_t hash_size); 53 54/* returns the block type (eg. 'r' for ref records. */ 55uint8_t block_writer_type(struct block_writer *bw); 56 57/* Attempts to append the record. Returns 0 on success or error code on failure. */ 58int block_writer_add(struct block_writer *w, struct reftable_record *rec); 59 60/* appends the key restarts, and compress the block if necessary. */ 61int block_writer_finish(struct block_writer *w); 62 63/* clears out internally allocated block_writer members. */ 64void block_writer_release(struct block_writer *bw); 65 66/* Iterator for records contained in a single block. */ 67struct block_iter { 68 /* offset within the block of the next entry to read. */ 69 uint32_t next_off; 70 const struct reftable_block *block; 71 72 /* key for last entry we read. */ 73 struct reftable_buf last_key; 74 struct reftable_buf scratch; 75}; 76 77#define BLOCK_ITER_INIT { \ 78 .last_key = REFTABLE_BUF_INIT, \ 79 .scratch = REFTABLE_BUF_INIT, \ 80} 81 82/* 83 * Initialize the block iterator with the given block. The iterator will be 84 * positioned at the first record contained in the block. The block must remain 85 * valid until the end of the iterator's lifetime. It is valid to re-initialize 86 * iterators multiple times. 87 */ 88void block_iter_init(struct block_iter *it, const struct reftable_block *block); 89 90/* Position the initialized iterator at the first record of its block. */ 91void block_iter_seek_start(struct block_iter *it); 92 93/* 94 * Position the initialized iterator at the desired record key. It is not an 95 * error in case the record cannot be found. If so, a subsequent call to 96 * `block_iter_next()` will indicate that the iterator is exhausted. 97 */ 98int block_iter_seek_key(struct block_iter *it, struct reftable_buf *want); 99 100/* return < 0 for error, 0 for OK, > 0 for EOF. */ 101int block_iter_next(struct block_iter *it, struct reftable_record *rec); 102 103/* Reset the block iterator to pristine state without releasing its memory. */ 104void block_iter_reset(struct block_iter *it); 105 106/* deallocate memory for `it`. The block reader and its block is left intact. */ 107void block_iter_close(struct block_iter *it); 108 109/* size of file header, depending on format version */ 110size_t header_size(int version); 111 112/* size of file footer, depending on format version */ 113size_t footer_size(int version); 114 115#endif