Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/reftable/reftable-stack.h # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / reftable / reftable-stack.h` 185 lines ยท 6264 bytes Raw Blame History 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_STACK_H 10 #define REFTABLE_STACK_H 11 12 #include "reftable-system.h" 13 #include "reftable-writer.h" 14 15 /* 16 * The stack presents an interface to a mutable sequence of reftables. 17 18 * A stack can be mutated by pushing a table to the top of the stack. 19 20 * The reftable_stack automatically compacts files on disk to ensure good 21 * amortized performance. 22 * 23 * For windows and other platforms that cannot have open files as rename 24 * destinations, concurrent access from multiple processes needs the rand() 25 * random seed to be randomized. 26 */ 27 struct reftable_stack; 28 29 /* Options related to opening a stack. */ 30 struct reftable_stack_options { 31 /* 32 * 4-byte identifier ("sha1", "s256") of the hash. Defaults to SHA1 if 33 * unset. 34 */ 35 enum reftable_hash hash_id; 36 37 /* 38 * Callback function to execute whenever the stack is being reloaded. 39 * This can be used e.g. to discard cached information that relies on 40 * the old stack's data. The payload data will be passed as argument to 41 * the callback. 42 */ 43 void (*on_reload)(void *payload); 44 void *on_reload_payload; 45 46 int suppress_deletions; 47 }; 48 49 /* open a new reftable stack. The tables along with the table list will be 50 * stored in 'dir'. Typically, this should be .git/reftables. 51 */ 52 int reftable_new_stack(struct reftable_stack **dest, const char *dir, 53 const struct reftable_stack_options *opts); 54 55 /* returns the update_index at which a next table should be written. */ 56 uint64_t reftable_stack_next_update_index(struct reftable_stack *st); 57 58 /* holds a transaction to add tables at the top of a stack. */ 59 struct reftable_addition; 60 61 /* 62 * returns a new transaction to add reftables to the given stack. As a side 63 * effect, the ref database is locked. 64 */ 65 int reftable_stack_addition_new(struct reftable_addition **dest, 66 struct reftable_stack *st, 67 const struct reftable_write_options *opts); 68 69 /* Adds a reftable to transaction. */ 70 int reftable_addition_add(struct reftable_addition *add, 71 int (*write_table)(struct reftable_writer *wr, 72 void *arg), 73 void *arg); 74 75 /* Commits the transaction, releasing the lock. After calling this, 76 * reftable_addition_destroy should still be called. 77 */ 78 int reftable_addition_commit(struct reftable_addition *add); 79 80 /* Release all non-committed data from the transaction, and deallocate the 81 * transaction. Releases the lock if held. */ 82 void reftable_addition_destroy(struct reftable_addition *add); 83 84 /* 85 * Add a new table to the stack. The write_table function must call 86 * reftable_writer_set_limits, add refs and return an error value. 87 */ 88 int reftable_stack_add(struct reftable_stack *st, 89 int (*write_table)(struct reftable_writer *wr, 90 void *write_arg), 91 void *write_arg, 92 const struct reftable_write_options *opts); 93 94 struct reftable_iterator; 95 96 /* 97 * Initialize an iterator for the merged tables contained in the stack that can 98 * be used to iterate through refs. The iterator is valid until the next reload 99 * or write. 100 */ 101 int reftable_stack_init_ref_iterator(struct reftable_stack *st, 102 struct reftable_iterator *it); 103 104 /* 105 * Initialize an iterator for the merged tables contained in the stack that can 106 * be used to iterate through logs. The iterator is valid until the next reload 107 * or write. 108 */ 109 int reftable_stack_init_log_iterator(struct reftable_stack *st, 110 struct reftable_iterator *it); 111 112 /* returns the merged_table for seeking. This table is valid until the 113 * next write or reload, and should not be closed or deleted. 114 */ 115 struct reftable_merged_table * 116 reftable_stack_merged_table(struct reftable_stack *st); 117 118 /* frees all resources associated with the stack. */ 119 void reftable_stack_destroy(struct reftable_stack *st); 120 121 /* Reloads the stack if necessary. This is very cheap to run if the stack was up 122 * to date */ 123 int reftable_stack_reload(struct reftable_stack *st); 124 125 /* Policy for expiring reflog entries. */ 126 struct reftable_log_expiry_config { 127 /* Drop entries older than this timestamp */ 128 uint64_t time; 129 130 /* Drop older entries */ 131 uint64_t min_update_index; 132 }; 133 134 /* compacts all reftables into a giant table. Expire reflog entries if config is 135 * non-NULL */ 136 int reftable_stack_compact_all(struct reftable_stack *st, 137 const struct reftable_write_options *opts, 138 struct reftable_log_expiry_config *config); 139 140 /* 141 * Check if compaction is required. 142 * 143 * When `use_heuristics` is false, check if all tables can be compacted to a 144 * single table. If true, use heuristics to determine if the tables need to be 145 * compacted to maintain geometric progression. 146 */ 147 int reftable_stack_compaction_required(struct reftable_stack *st, 148 const struct reftable_write_options *opts, 149 bool use_heuristics, 150 bool *required); 151 152 /* heuristically compact unbalanced table stack. */ 153 int reftable_stack_auto_compact(struct reftable_stack *st, 154 const struct reftable_write_options *opts); 155 156 /* delete stale .ref tables. */ 157 int reftable_stack_clean(struct reftable_stack *st); 158 159 /* convenience function to read a single ref. Returns < 0 for error, 0 for 160 * success, and 1 if ref not found. */ 161 int reftable_stack_read_ref(struct reftable_stack *st, const char *refname, 162 struct reftable_ref_record *ref); 163 164 /* convenience function to read a single log. Returns < 0 for error, 0 for 165 * success, and 1 if ref not found. */ 166 int reftable_stack_read_log(struct reftable_stack *st, const char *refname, 167 struct reftable_log_record *log); 168 169 /* statistics on past compactions. */ 170 struct reftable_compaction_stats { 171 uint64_t bytes; /* total number of bytes written */ 172 uint64_t entries_written; /* total number of entries written, including 173 failures. */ 174 int attempts; /* how often we tried to compact */ 175 int failures; /* failures happen on concurrent updates */ 176 }; 177 178 /* return statistics for compaction up till now. */ 179 struct reftable_compaction_stats * 180 reftable_stack_compaction_stats(struct reftable_stack *st); 181 182 /* Return the hash of the stack. */ 183 enum reftable_hash reftable_stack_hash_id(struct reftable_stack *st); 184 185 #endif