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