]> git.ipfire.org Git - thirdparty/git.git/blob - object-store.h
Merge branch 'ab/reverse-midx-optim' into maint
[thirdparty/git.git] / object-store.h
1 #ifndef OBJECT_STORE_H
2 #define OBJECT_STORE_H
3
4 #include "cache.h"
5 #include "oidmap.h"
6 #include "list.h"
7 #include "oid-array.h"
8 #include "strbuf.h"
9 #include "thread-utils.h"
10 #include "khash.h"
11 #include "dir.h"
12 #include "oidtree.h"
13
14 struct object_directory {
15 struct object_directory *next;
16
17 /*
18 * Used to store the results of readdir(3) calls when we are OK
19 * sacrificing accuracy due to races for speed. That includes
20 * object existence with OBJECT_INFO_QUICK, as well as
21 * our search for unique abbreviated hashes. Don't use it for tasks
22 * requiring greater accuracy!
23 *
24 * Be sure to call odb_load_loose_cache() before using.
25 */
26 uint32_t loose_objects_subdir_seen[8]; /* 256 bits */
27 struct oidtree *loose_objects_cache;
28
29 /*
30 * Path to the alternative object store. If this is a relative path,
31 * it is relative to the current working directory.
32 */
33 char *path;
34 };
35
36 KHASH_INIT(odb_path_map, const char * /* key: odb_path */,
37 struct object_directory *, 1, fspathhash, fspatheq)
38
39 void prepare_alt_odb(struct repository *r);
40 char *compute_alternate_path(const char *path, struct strbuf *err);
41 typedef int alt_odb_fn(struct object_directory *, void *);
42 int foreach_alt_odb(alt_odb_fn, void*);
43 typedef void alternate_ref_fn(const struct object_id *oid, void *);
44 void for_each_alternate_ref(alternate_ref_fn, void *);
45
46 /*
47 * Add the directory to the on-disk alternates file; the new entry will also
48 * take effect in the current process.
49 */
50 void add_to_alternates_file(const char *dir);
51
52 /*
53 * Add the directory to the in-memory list of alternates (along with any
54 * recursive alternates it points to), but do not modify the on-disk alternates
55 * file.
56 */
57 void add_to_alternates_memory(const char *dir);
58
59 /*
60 * Populate and return the loose object cache array corresponding to the
61 * given object ID.
62 */
63 struct oidtree *odb_loose_cache(struct object_directory *odb,
64 const struct object_id *oid);
65
66 /* Empty the loose object cache for the specified object directory. */
67 void odb_clear_loose_cache(struct object_directory *odb);
68
69 struct packed_git {
70 struct hashmap_entry packmap_ent;
71 struct packed_git *next;
72 struct list_head mru;
73 struct pack_window *windows;
74 off_t pack_size;
75 const void *index_data;
76 size_t index_size;
77 uint32_t num_objects;
78 uint32_t num_bad_objects;
79 uint32_t crc_offset;
80 unsigned char *bad_object_sha1;
81 int index_version;
82 time_t mtime;
83 int pack_fd;
84 int index; /* for builtin/pack-objects.c */
85 unsigned pack_local:1,
86 pack_keep:1,
87 pack_keep_in_core:1,
88 freshened:1,
89 do_not_close:1,
90 pack_promisor:1,
91 multi_pack_index:1;
92 unsigned char hash[GIT_MAX_RAWSZ];
93 struct revindex_entry *revindex;
94 const uint32_t *revindex_data;
95 const uint32_t *revindex_map;
96 size_t revindex_size;
97 /* something like ".git/objects/pack/xxxxx.pack" */
98 char pack_name[FLEX_ARRAY]; /* more */
99 };
100
101 struct multi_pack_index;
102
103 static inline int pack_map_entry_cmp(const void *unused_cmp_data,
104 const struct hashmap_entry *entry,
105 const struct hashmap_entry *entry2,
106 const void *keydata)
107 {
108 const char *key = keydata;
109 const struct packed_git *pg1, *pg2;
110
111 pg1 = container_of(entry, const struct packed_git, packmap_ent);
112 pg2 = container_of(entry2, const struct packed_git, packmap_ent);
113
114 return strcmp(pg1->pack_name, key ? key : pg2->pack_name);
115 }
116
117 struct raw_object_store {
118 /*
119 * Set of all object directories; the main directory is first (and
120 * cannot be NULL after initialization). Subsequent directories are
121 * alternates.
122 */
123 struct object_directory *odb;
124 struct object_directory **odb_tail;
125 kh_odb_path_map_t *odb_by_path;
126
127 int loaded_alternates;
128
129 /*
130 * A list of alternate object directories loaded from the environment;
131 * this should not generally need to be accessed directly, but will
132 * populate the "odb" list when prepare_alt_odb() is run.
133 */
134 char *alternate_db;
135
136 /*
137 * Objects that should be substituted by other objects
138 * (see git-replace(1)).
139 */
140 struct oidmap *replace_map;
141 unsigned replace_map_initialized : 1;
142 pthread_mutex_t replace_mutex; /* protect object replace functions */
143
144 struct commit_graph *commit_graph;
145 unsigned commit_graph_attempted : 1; /* if loading has been attempted */
146
147 /*
148 * private data
149 *
150 * should only be accessed directly by packfile.c and midx.c
151 */
152 struct multi_pack_index *multi_pack_index;
153
154 /*
155 * private data
156 *
157 * should only be accessed directly by packfile.c
158 */
159
160 struct packed_git *packed_git;
161 /* A most-recently-used ordered version of the packed_git list. */
162 struct list_head packed_git_mru;
163
164 struct {
165 struct packed_git **packs;
166 unsigned flags;
167 } kept_pack_cache;
168
169 /*
170 * A map of packfiles to packed_git structs for tracking which
171 * packs have been loaded already.
172 */
173 struct hashmap pack_map;
174
175 /*
176 * A fast, rough count of the number of objects in the repository.
177 * These two fields are not meant for direct access. Use
178 * approximate_object_count() instead.
179 */
180 unsigned long approximate_object_count;
181 unsigned approximate_object_count_valid : 1;
182
183 /*
184 * Whether packed_git has already been populated with this repository's
185 * packs.
186 */
187 unsigned packed_git_initialized : 1;
188 };
189
190 struct raw_object_store *raw_object_store_new(void);
191 void raw_object_store_clear(struct raw_object_store *o);
192
193 /*
194 * Put in `buf` the name of the file in the local object database that
195 * would be used to store a loose object with the specified oid.
196 */
197 const char *loose_object_path(struct repository *r, struct strbuf *buf,
198 const struct object_id *oid);
199
200 void *map_loose_object(struct repository *r, const struct object_id *oid,
201 unsigned long *size);
202
203 void *read_object_file_extended(struct repository *r,
204 const struct object_id *oid,
205 enum object_type *type,
206 unsigned long *size, int lookup_replace);
207 static inline void *repo_read_object_file(struct repository *r,
208 const struct object_id *oid,
209 enum object_type *type,
210 unsigned long *size)
211 {
212 return read_object_file_extended(r, oid, type, size, 1);
213 }
214 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
215 #define read_object_file(oid, type, size) repo_read_object_file(the_repository, oid, type, size)
216 #endif
217
218 /* Read and unpack an object file into memory, write memory to an object file */
219 int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
220
221 int hash_object_file(const struct git_hash_algo *algo, const void *buf,
222 unsigned long len, const char *type,
223 struct object_id *oid);
224
225 int write_object_file(const void *buf, unsigned long len,
226 const char *type, struct object_id *oid);
227
228 int hash_object_file_literally(const void *buf, unsigned long len,
229 const char *type, struct object_id *oid,
230 unsigned flags);
231
232 /*
233 * Add an object file to the in-memory object store, without writing it
234 * to disk.
235 *
236 * Callers are responsible for calling write_object_file to record the
237 * object in persistent storage before writing any other new objects
238 * that reference it.
239 */
240 int pretend_object_file(void *, unsigned long, enum object_type,
241 struct object_id *oid);
242
243 int force_object_loose(const struct object_id *oid, time_t mtime);
244
245 /*
246 * Open the loose object at path, check its hash, and return the contents,
247 * type, and size. If the object is a blob, then "contents" may return NULL,
248 * to allow streaming of large blobs.
249 *
250 * Returns 0 on success, negative on error (details may be written to stderr).
251 */
252 int read_loose_object(const char *path,
253 const struct object_id *expected_oid,
254 enum object_type *type,
255 unsigned long *size,
256 void **contents);
257
258 /* Retry packed storage after checking packed and loose storage */
259 #define HAS_OBJECT_RECHECK_PACKED 1
260
261 /*
262 * Returns 1 if the object exists. This function will not lazily fetch objects
263 * in a partial clone.
264 */
265 int has_object(struct repository *r, const struct object_id *oid,
266 unsigned flags);
267
268 /*
269 * These macros and functions are deprecated. If checking existence for an
270 * object that is likely to be missing and/or whose absence is relatively
271 * inconsequential (or is consequential but the caller is prepared to handle
272 * it), use has_object(), which has better defaults (no lazy fetch in a partial
273 * clone and no rechecking of packed storage). In the unlikely event that a
274 * caller needs to assert existence of an object that it fully expects to
275 * exist, and wants to trigger a lazy fetch in a partial clone, use
276 * oid_object_info_extended() with a NULL struct object_info.
277 *
278 * These functions can be removed once all callers have migrated to
279 * has_object() and/or oid_object_info_extended().
280 */
281 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
282 #define has_sha1_file_with_flags(sha1, flags) repo_has_sha1_file_with_flags(the_repository, sha1, flags)
283 #define has_sha1_file(sha1) repo_has_sha1_file(the_repository, sha1)
284 #endif
285 int repo_has_object_file(struct repository *r, const struct object_id *oid);
286 int repo_has_object_file_with_flags(struct repository *r,
287 const struct object_id *oid, int flags);
288 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
289 #define has_object_file(oid) repo_has_object_file(the_repository, oid)
290 #define has_object_file_with_flags(oid, flags) repo_has_object_file_with_flags(the_repository, oid, flags)
291 #endif
292
293 /*
294 * Return true iff an alternate object database has a loose object
295 * with the specified name. This function does not respect replace
296 * references.
297 */
298 int has_loose_object_nonlocal(const struct object_id *);
299
300 void assert_oid_type(const struct object_id *oid, enum object_type expect);
301
302 /*
303 * Enabling the object read lock allows multiple threads to safely call the
304 * following functions in parallel: repo_read_object_file(), read_object_file(),
305 * read_object_file_extended(), read_object_with_reference(), read_object(),
306 * oid_object_info() and oid_object_info_extended().
307 *
308 * obj_read_lock() and obj_read_unlock() may also be used to protect other
309 * section which cannot execute in parallel with object reading. Since the used
310 * lock is a recursive mutex, these sections can even contain calls to object
311 * reading functions. However, beware that in these cases zlib inflation won't
312 * be performed in parallel, losing performance.
313 *
314 * TODO: oid_object_info_extended()'s call stack has a recursive behavior. If
315 * any of its callees end up calling it, this recursive call won't benefit from
316 * parallel inflation.
317 */
318 void enable_obj_read_lock(void);
319 void disable_obj_read_lock(void);
320
321 extern int obj_read_use_lock;
322 extern pthread_mutex_t obj_read_mutex;
323
324 static inline void obj_read_lock(void)
325 {
326 if(obj_read_use_lock)
327 pthread_mutex_lock(&obj_read_mutex);
328 }
329
330 static inline void obj_read_unlock(void)
331 {
332 if(obj_read_use_lock)
333 pthread_mutex_unlock(&obj_read_mutex);
334 }
335
336 struct object_info {
337 /* Request */
338 enum object_type *typep;
339 unsigned long *sizep;
340 off_t *disk_sizep;
341 struct object_id *delta_base_oid;
342 struct strbuf *type_name;
343 void **contentp;
344
345 /* Response */
346 enum {
347 OI_CACHED,
348 OI_LOOSE,
349 OI_PACKED,
350 OI_DBCACHED
351 } whence;
352 union {
353 /*
354 * struct {
355 * ... Nothing to expose in this case
356 * } cached;
357 * struct {
358 * ... Nothing to expose in this case
359 * } loose;
360 */
361 struct {
362 struct packed_git *pack;
363 off_t offset;
364 unsigned int is_delta;
365 } packed;
366 } u;
367 };
368
369 /*
370 * Initializer for a "struct object_info" that wants no items. You may
371 * also memset() the memory to all-zeroes.
372 */
373 #define OBJECT_INFO_INIT {NULL}
374
375 /* Invoke lookup_replace_object() on the given hash */
376 #define OBJECT_INFO_LOOKUP_REPLACE 1
377 /* Allow reading from a loose object file of unknown/bogus type */
378 #define OBJECT_INFO_ALLOW_UNKNOWN_TYPE 2
379 /* Do not retry packed storage after checking packed and loose storage */
380 #define OBJECT_INFO_QUICK 8
381 /* Do not check loose object */
382 #define OBJECT_INFO_IGNORE_LOOSE 16
383 /*
384 * Do not attempt to fetch the object if missing (even if fetch_is_missing is
385 * nonzero).
386 */
387 #define OBJECT_INFO_SKIP_FETCH_OBJECT 32
388 /*
389 * This is meant for bulk prefetching of missing blobs in a partial
390 * clone. Implies OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK
391 */
392 #define OBJECT_INFO_FOR_PREFETCH (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK)
393
394 int oid_object_info_extended(struct repository *r,
395 const struct object_id *,
396 struct object_info *, unsigned flags);
397
398 /*
399 * Iterate over the files in the loose-object parts of the object
400 * directory "path", triggering the following callbacks:
401 *
402 * - loose_object is called for each loose object we find.
403 *
404 * - loose_cruft is called for any files that do not appear to be
405 * loose objects. Note that we only look in the loose object
406 * directories "objects/[0-9a-f]{2}/", so we will not report
407 * "objects/foobar" as cruft.
408 *
409 * - loose_subdir is called for each top-level hashed subdirectory
410 * of the object directory (e.g., "$OBJDIR/f0"). It is called
411 * after the objects in the directory are processed.
412 *
413 * Any callback that is NULL will be ignored. Callbacks returning non-zero
414 * will end the iteration.
415 *
416 * In the "buf" variant, "path" is a strbuf which will also be used as a
417 * scratch buffer, but restored to its original contents before
418 * the function returns.
419 */
420 typedef int each_loose_object_fn(const struct object_id *oid,
421 const char *path,
422 void *data);
423 typedef int each_loose_cruft_fn(const char *basename,
424 const char *path,
425 void *data);
426 typedef int each_loose_subdir_fn(unsigned int nr,
427 const char *path,
428 void *data);
429 int for_each_file_in_obj_subdir(unsigned int subdir_nr,
430 struct strbuf *path,
431 each_loose_object_fn obj_cb,
432 each_loose_cruft_fn cruft_cb,
433 each_loose_subdir_fn subdir_cb,
434 void *data);
435 int for_each_loose_file_in_objdir(const char *path,
436 each_loose_object_fn obj_cb,
437 each_loose_cruft_fn cruft_cb,
438 each_loose_subdir_fn subdir_cb,
439 void *data);
440 int for_each_loose_file_in_objdir_buf(struct strbuf *path,
441 each_loose_object_fn obj_cb,
442 each_loose_cruft_fn cruft_cb,
443 each_loose_subdir_fn subdir_cb,
444 void *data);
445
446 /* Flags for for_each_*_object() below. */
447 enum for_each_object_flags {
448 /* Iterate only over local objects, not alternates. */
449 FOR_EACH_OBJECT_LOCAL_ONLY = (1<<0),
450
451 /* Only iterate over packs obtained from the promisor remote. */
452 FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
453
454 /*
455 * Visit objects within a pack in packfile order rather than .idx order
456 */
457 FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
458 };
459
460 /*
461 * Iterate over all accessible loose objects without respect to
462 * reachability. By default, this includes both local and alternate objects.
463 * The order in which objects are visited is unspecified.
464 *
465 * Any flags specific to packs are ignored.
466 */
467 int for_each_loose_object(each_loose_object_fn, void *,
468 enum for_each_object_flags flags);
469
470 /*
471 * Iterate over all accessible packed objects without respect to reachability.
472 * By default, this includes both local and alternate packs.
473 *
474 * Note that some objects may appear twice if they are found in multiple packs.
475 * Each pack is visited in an unspecified order. By default, objects within a
476 * pack are visited in pack-idx order (i.e., sorted by oid).
477 */
478 typedef int each_packed_object_fn(const struct object_id *oid,
479 struct packed_git *pack,
480 uint32_t pos,
481 void *data);
482 int for_each_object_in_pack(struct packed_git *p,
483 each_packed_object_fn, void *data,
484 enum for_each_object_flags flags);
485 int for_each_packed_object(each_packed_object_fn, void *,
486 enum for_each_object_flags flags);
487
488 #endif /* OBJECT_STORE_H */