Merge branch 'dd/t9604-use-posix-timezones'
[alt-git.git] / reftable / reftable-writer.h
blob155bf0bbe2a3b0310f6c5a8bd0db11545f73d6dd
1 /*
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
7 */
9 #ifndef REFTABLE_WRITER_H
10 #define REFTABLE_WRITER_H
12 #include "reftable-record.h"
14 #include <stdint.h>
15 #include <unistd.h> /* ssize_t */
17 /* Writing single reftables */
19 /* reftable_write_options sets options for writing a single reftable. */
20 struct reftable_write_options {
21 /* boolean: do not pad out blocks to block size. */
22 unsigned unpadded : 1;
24 /* the blocksize. Should be less than 2^24. */
25 uint32_t block_size;
27 /* boolean: do not generate a SHA1 => ref index. */
28 unsigned skip_index_objects : 1;
30 /* how often to write complete keys in each block. */
31 int restart_interval;
33 /* 4-byte identifier ("sha1", "s256") of the hash.
34 * Defaults to SHA1 if unset
36 uint32_t hash_id;
38 /* Default mode for creating files. If unset, use 0666 (+umask) */
39 unsigned int default_permissions;
41 /* boolean: do not check ref names for validity or dir/file conflicts.
43 unsigned skip_name_check : 1;
45 /* boolean: copy log messages exactly. If unset, check that the message
46 * is a single line, and add '\n' if missing.
48 unsigned exact_log_message : 1;
50 /* boolean: Prevent auto-compaction of tables. */
51 unsigned disable_auto_compact : 1;
54 /* reftable_block_stats holds statistics for a single block type */
55 struct reftable_block_stats {
56 /* total number of entries written */
57 int entries;
58 /* total number of key restarts */
59 int restarts;
60 /* total number of blocks */
61 int blocks;
62 /* total number of index blocks */
63 int index_blocks;
64 /* depth of the index */
65 int max_index_level;
67 /* offset of the first block for this type */
68 uint64_t offset;
69 /* offset of the top level index block for this type, or 0 if not
70 * present */
71 uint64_t index_offset;
74 /* stats holds overall statistics for a single reftable */
75 struct reftable_stats {
76 /* total number of blocks written. */
77 int blocks;
78 /* stats for ref data */
79 struct reftable_block_stats ref_stats;
80 /* stats for the SHA1 to ref map. */
81 struct reftable_block_stats obj_stats;
82 /* stats for index blocks */
83 struct reftable_block_stats idx_stats;
84 /* stats for log blocks */
85 struct reftable_block_stats log_stats;
87 /* disambiguation length of shortened object IDs. */
88 int object_id_len;
91 /* reftable_new_writer creates a new writer */
92 struct reftable_writer *
93 reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t),
94 int (*flush_func)(void *),
95 void *writer_arg, struct reftable_write_options *opts);
97 /* Set the range of update indices for the records we will add. When writing a
98 table into a stack, the min should be at least
99 reftable_stack_next_update_index(), or REFTABLE_API_ERROR is returned.
101 For transactional updates to a stack, typically min==max, and the
102 update_index can be obtained by inspeciting the stack. When converting an
103 existing ref database into a single reftable, this would be a range of
104 update-index timestamps.
106 void reftable_writer_set_limits(struct reftable_writer *w, uint64_t min,
107 uint64_t max);
110 Add a reftable_ref_record. The record should have names that come after
111 already added records.
113 The update_index must be within the limits set by
114 reftable_writer_set_limits(), or REFTABLE_API_ERROR is returned. It is an
115 REFTABLE_API_ERROR error to write a ref record after a log record.
117 int reftable_writer_add_ref(struct reftable_writer *w,
118 struct reftable_ref_record *ref);
121 Convenience function to add multiple reftable_ref_records; the function sorts
122 the records before adding them, reordering the records array passed in.
124 int reftable_writer_add_refs(struct reftable_writer *w,
125 struct reftable_ref_record *refs, int n);
128 adds reftable_log_records. Log records are keyed by (refname, decreasing
129 update_index). The key for the record added must come after the already added
130 log records.
132 int reftable_writer_add_log(struct reftable_writer *w,
133 struct reftable_log_record *log);
136 Convenience function to add multiple reftable_log_records; the function sorts
137 the records before adding them, reordering records array passed in.
139 int reftable_writer_add_logs(struct reftable_writer *w,
140 struct reftable_log_record *logs, int n);
142 /* reftable_writer_close finalizes the reftable. The writer is retained so
143 * statistics can be inspected. */
144 int reftable_writer_close(struct reftable_writer *w);
146 /* writer_stats returns the statistics on the reftable being written.
148 This struct becomes invalid when the writer is freed.
150 const struct reftable_stats *reftable_writer_stats(struct reftable_writer *w);
152 /* reftable_writer_free deallocates memory for the writer */
153 void reftable_writer_free(struct reftable_writer *w);
155 #endif