Raw
1 #ifndef ODB_SOURCE_H
2 #define ODB_SOURCE_H
3
4 #include "object.h"
5 #include "odb.h"
6 #include "odb/transaction.h"
7
8 enum odb_source_type {
9 /*
10 * The "unknown" type, which should never be in use. This type mostly
11 * exists to catch cases where the type field remains zeroed out.
12 */
13 ODB_SOURCE_UNKNOWN,
14
15 /* The "files" backend that uses loose objects and packfiles. */
16 ODB_SOURCE_FILES,
17
18 /* The "loose" backend that uses loose objects, only. */
19 ODB_SOURCE_LOOSE,
20
21 /* The "packed" backend that uses packfiles. */
22 ODB_SOURCE_PACKED,
23
24 /* The "in-memory" backend that stores objects in memory. */
25 ODB_SOURCE_INMEMORY,
26 };
27
28 /*
29 * Convert between the enum and its name. Returns the equivalent of "unknown"
30 * for unknown types.
31 */
32 const char *odb_source_type_to_name(enum odb_source_type type);
33
34 struct object_id;
35 struct odb_read_stream;
36 struct strvec;
37
38 /*
39 * The source is the part of the object database that stores the actual
40 * objects. It thus encapsulates the logic to read and write the specific
41 * on-disk format. An object database can have multiple sources:
42 *
43 * - The primary source, which is typically located in "$GIT_DIR/objects".
44 * This is where new objects are usually written to.
45 *
46 * - Alternate sources, which are configured via "objects/info/alternates" or
47 * via the GIT_ALTERNATE_OBJECT_DIRECTORIES environment variable. These
48 * alternate sources are only used to read objects.
49 */
50 struct odb_source {
51 struct odb_source *next;
52
53 /* Object database that owns this object source. */
54 struct object_database *odb;
55
56 /* The type used by this source. */
57 enum odb_source_type type;
58
59 /*
60 * Figure out whether this is the local source of the owning
61 * repository, which would typically be its ".git/objects" directory.
62 * This local object directory is usually where objects would be
63 * written to.
64 */
65 bool local;
66
67 /*
68 * This object store is ephemeral, so there is no need to fsync.
69 */
70 int will_destroy;
71
72 /*
73 * Path to the source. If this is a relative path, it is relative to
74 * the current working directory.
75 */
76 char *path;
77
78 /*
79 * This callback is expected to free the underlying object database source and
80 * all associated resources. The function will never be called with a NULL pointer.
81 */
82 void (*free)(struct odb_source *source);
83
84 /*
85 * This callback is expected to close any open resources, like for
86 * example file descriptors or connections. The source is expected to
87 * still be usable after it has been closed. Closed resources may need
88 * to be reopened in that case.
89 */
90 void (*close)(struct odb_source *source);
91
92 /*
93 * This callback is expected to create on-disk data structures that are
94 * required for this source to operate.
95 *
96 * The callback is expected to return 0 on success, a negative error
97 * code otherwise.
98 *
99 * This callback may be NULL in case the source does not need any
100 * on-disk setup.
101 */
102 int (*create_on_disk)(struct odb_source *source);
103
104 /*
105 * This callback is expected to prepare the source so that it becomes
106 * ready for use. It optionally clears underlying caches of the object
107 * database source.
108 */
109 void (*prepare)(struct odb_source *source,
110 enum odb_prepare_flags flags);
111
112 /*
113 * This callback is expected to read object information from the object
114 * database source. The object info will be partially populated with
115 * pointers for each bit of information that was requested by the
116 * caller.
117 *
118 * The flags field is a combination of `OBJECT_INFO` flags. Only the
119 * following fields need to be handled by the backend:
120 *
121 * - `OBJECT_INFO_QUICK` indicates it is fine to use caches without
122 * re-verifying the data.
123 *
124 * - `OBJECT_INFO_SECOND_READ` indicates that the initial object
125 * lookup has failed and that the object sources should check
126 * whether any of its on-disk state has changed that may have
127 * caused the object to appear. Sources are free to ignore the
128 * second read in case they know that the first read would have
129 * already surfaced the object without reloading any on-disk state.
130 *
131 * The callback is expected to return a negative error code in case
132 * reading the object has failed, 0 otherwise.
133 */
134 int (*read_object_info)(struct odb_source *source,
135 const struct object_id *oid,
136 struct object_info *oi,
137 enum object_info_flags flags);
138
139 /*
140 * This callback is expected to create a new read stream that can be
141 * used to stream the object identified by the given ID.
142 *
143 * The callback is expected to return a negative error code in case
144 * creating the object stream has failed, 0 otherwise.
145 */
146 int (*read_object_stream)(struct odb_read_stream **out,
147 struct odb_source *source,
148 const struct object_id *oid);
149
150 /*
151 * This callback is expected to iterate over all objects stored in this
152 * source and invoke the callback function for each of them. It is
153 * valid to yield the same object multiple time. A non-zero exit code
154 * from the object callback shall abort iteration.
155 *
156 * The optional `request` structure should serve as a template for
157 * looking up object info for every individual iterated object. It
158 * should not be modified directly and should instead be copied into a
159 * separate `struct object_info` that gets passed to the callback. If
160 * the caller passes a `NULL` pointer then the object itself shall not
161 * be read.
162 *
163 * The callback is expected to return a negative error code in case the
164 * iteration has failed to read all objects, 0 otherwise. When the
165 * callback function returns a non-zero error code then that error code
166 * should be returned.
167 */
168 int (*for_each_object)(struct odb_source *source,
169 const struct object_info *request,
170 odb_for_each_object_cb cb,
171 void *cb_data,
172 const struct odb_for_each_object_options *opts);
173
174 /*
175 * This callback is expected to count objects in the given object
176 * database source. The callback function does not have to guarantee
177 * that only unique objects are counted. The result shall be assigned
178 * to the `out` pointer.
179 *
180 * Accepts `enum odb_count_objects_flag` flags to alter the behaviour.
181 *
182 * The callback is expected to return 0 on success, or a negative error
183 * code otherwise.
184 */
185 int (*count_objects)(struct odb_source *source,
186 enum odb_count_objects_flags flags,
187 unsigned long *out);
188
189 /*
190 * This callback is expected to find the minimum required length to
191 * make the given object ID unique.
192 *
193 * The callback is expected to return a negative error code in case it
194 * failed, 0 otherwise.
195 */
196 int (*find_abbrev_len)(struct odb_source *source,
197 const struct object_id *oid,
198 unsigned min_length,
199 unsigned *out);
200
201 /*
202 * This callback is expected to freshen the given object so that its
203 * last access time is set to the current time. This is used to ensure
204 * that objects that are recent will not get garbage collected even if
205 * they were unreachable.
206 *
207 * Returns 0 in case the object does not exist, 1 in case the object
208 * has been freshened.
209 */
210 int (*freshen_object)(struct odb_source *source,
211 const struct object_id *oid,
212 const time_t *mtime);
213
214 /*
215 * This callback is expected to persist the given object into the
216 * object source. In case the object already exists it shall be
217 * freshened.
218 *
219 * The flags field is a combination of `WRITE_OBJECT` flags.
220 *
221 * The resulting object ID (and optionally the compatibility object ID)
222 * shall be written into the out pointers. The callback is expected to
223 * return 0 on success, a negative error code otherwise.
224 */
225 int (*write_object)(struct odb_source *source,
226 const void *buf, size_t len,
227 enum object_type type,
228 const struct object_id *oid,
229 const struct object_id *compat_oid,
230 const time_t *mtime,
231 enum odb_write_object_flags flags);
232
233 /*
234 * This callback is expected to persist the given object stream into
235 * the object source.
236 *
237 * The resulting object ID shall be written into the out pointer. The
238 * callback is expected to return 0 on success, a negative error code
239 * otherwise.
240 */
241 int (*write_object_stream)(struct odb_source *source,
242 struct odb_write_stream *stream, size_t len,
243 struct object_id *oid);
244
245 /*
246 * This callback is expected to create a new transaction that can be
247 * used to write objects to. The objects shall only be persisted into
248 * the object database when the transcation's commit function is
249 * called. Otherwise, the objects shall be discarded.
250 *
251 * Returns 0 on success, in which case the `*out` pointer will have
252 * been populated with the object database transaction. Returns a
253 * negative error code otherwise.
254 */
255 int (*begin_transaction)(struct odb_source *source,
256 struct odb_transaction **out,
257 enum odb_transaction_flags flags);
258
259 /*
260 * This callback is expected to read the list of alternate object
261 * database sources connected to it and write them into the `strvec`.
262 *
263 * The result is expected to be paths to the alternates. All paths must
264 * be resolved to absolute paths.
265 *
266 * The callback is expected to return 0 on success, a negative error
267 * code otherwise.
268 */
269 int (*read_alternates)(struct odb_source *source,
270 struct strvec *out);
271
272 /*
273 * This callback is expected to persist the singular alternate passed
274 * to it into its list of alternates. Any pre-existing alternates are
275 * expected to remain active. Subsequent calls to `read_alternates` are
276 * thus expected to yield the pre-existing list of alternates plus the
277 * newly added alternate appended to its end.
278 *
279 * The callback is expected to return 0 on success, a negative error
280 * code otherwise.
281 */
282 int (*write_alternate)(struct odb_source *source,
283 const char *alternate);
284
285 /*
286 * This callback is expected to optimize the object database source.
287 * Returns 0 on success, a negative error code otherwise.
288 */
289 int (*optimize)(struct odb_source *source,
290 const struct odb_optimize_options *opts);
291
292 /*
293 * This callback is expected to check whether optimization of the
294 * object database source is required given the provided options.
295 * Returns true if optimization should be performed, false otherwise.
296 */
297 bool (*optimize_required)(struct odb_source *source,
298 const struct odb_optimize_options *opts);
299 };
300
301 /*
302 * Allocate and initialize a new source for the given object database located
303 * at `path`. `local` indicates whether or not the source is the local and thus
304 * primary object source of the object database.
305 */
306 struct odb_source *odb_source_new(struct object_database *odb,
307 const char *path,
308 bool local);
309
310 /*
311 * Initialize the source for the given object database located at `path`.
312 * `local` indicates whether or not the source is the local and thus primary
313 * object source of the object database.
314 *
315 * This function is only supposed to be called by specific object source
316 * implementations.
317 */
318 void odb_source_init(struct odb_source *source,
319 struct object_database *odb,
320 enum odb_source_type type,
321 const char *path,
322 bool local);
323
324 /*
325 * Free the object database source, releasing all associated resources and
326 * freeing the structure itself.
327 */
328 void odb_source_free(struct odb_source *source);
329
330 /*
331 * Release the object database source, releasing all associated resources.
332 *
333 * This function is only supposed to be called by specific object source
334 * implementations.
335 */
336 void odb_source_release(struct odb_source *source);
337
338 /*
339 * Close the object database source without releasing he underlying data. The
340 * source can still be used going forward, but it first needs to be reopened.
341 * This can be useful to reduce resource usage.
342 */
343 static inline void odb_source_close(struct odb_source *source)
344 {
345 source->close(source);
346 }
347
348 /*
349 * Create on-disk data structures that are required for this source to operate
350 * correctly. Returns 0 on success, a negative error code otherwise.
351 */
352 static inline int odb_source_create_on_disk(struct odb_source *source)
353 {
354 if (!source->create_on_disk)
355 return 0;
356 return source->create_on_disk(source);
357 }
358
359 /*
360 * Prepare the object database source and clear any caches. Depending on the
361 * backend used this may have the effect that concurrently-written objects
362 * become visible.
363 */
364 static inline void odb_source_prepare(struct odb_source *source,
365 enum odb_prepare_flags flags)
366 {
367 source->prepare(source, flags);
368 }
369
370 /*
371 * Read an object from the object database source identified by its object ID.
372 * Returns 0 on success, a negative error code otherwise.
373 */
374 static inline int odb_source_read_object_info(struct odb_source *source,
375 const struct object_id *oid,
376 struct object_info *oi,
377 enum object_info_flags flags)
378 {
379 return source->read_object_info(source, oid, oi, flags);
380 }
381
382 /*
383 * Create a new read stream for the given object ID. Returns 0 on success, a
384 * negative error code otherwise.
385 */
386 static inline int odb_source_read_object_stream(struct odb_read_stream **out,
387 struct odb_source *source,
388 const struct object_id *oid)
389 {
390 return source->read_object_stream(out, source, oid);
391 }
392
393 /*
394 * Iterate through all objects contained in the given source and invoke the
395 * callback function for each of them. Returning a non-zero code from the
396 * callback function aborts iteration. There is no guarantee that objects
397 * are only iterated over once.
398 *
399 * The optional `request` structure serves as a template for retrieving the
400 * object info for each individual iterated object and will be populated as if
401 * `odb_source_read_object_info()` was called on the object. It will not be
402 * modified, the callback will instead be invoked with a separate `struct
403 * object_info` for every object. Object info will not be read when passing a
404 * `NULL` pointer.
405 *
406 * The flags is a bitfield of `ODB_FOR_EACH_OBJECT_*` flags. Not all flags may
407 * apply to a specific backend, so whether or not they are honored is defined
408 * by the implementation.
409 *
410 * Returns 0 when all objects have been iterated over, a negative error code in
411 * case iteration has failed, or a non-zero value returned from the callback.
412 */
413 static inline int odb_source_for_each_object(struct odb_source *source,
414 const struct object_info *request,
415 odb_for_each_object_cb cb,
416 void *cb_data,
417 const struct odb_for_each_object_options *opts)
418 {
419 return source->for_each_object(source, request, cb, cb_data, opts);
420 }
421
422 /*
423 * Count the number of objects in the given object database source.
424 *
425 * Returns 0 on success, a negative error code otherwise.
426 */
427 static inline int odb_source_count_objects(struct odb_source *source,
428 enum odb_count_objects_flags flags,
429 unsigned long *out)
430 {
431 return source->count_objects(source, flags, out);
432 }
433
434 /*
435 * Determine the minimum required length to make the given object ID unique in
436 * the given source. Returns 0 on success, a negative error code otherwise.
437 */
438 static inline int odb_source_find_abbrev_len(struct odb_source *source,
439 const struct object_id *oid,
440 unsigned min_len,
441 unsigned *out)
442 {
443 return source->find_abbrev_len(source, oid, min_len, out);
444 }
445
446 /*
447 * Freshen an object in the object database by updating its timestamp.
448 * Returns 1 in case the object has been freshened, 0 in case the object does
449 * not exist.
450 */
451 static inline int odb_source_freshen_object(struct odb_source *source,
452 const struct object_id *oid,
453 const time_t *mtime)
454 {
455 return source->freshen_object(source, oid, mtime);
456 }
457
458 /*
459 * Write an object into the object database source. Returns 0 on success, a
460 * negative error code otherwise. Populates the given out pointers for the
461 * object ID and the compatibility object ID, if non-NULL.
462 */
463 static inline int odb_source_write_object(struct odb_source *source,
464 const void *buf, unsigned long len,
465 enum object_type type,
466 const struct object_id *oid,
467 const struct object_id *compat_oid,
468 const time_t *mtime,
469 enum odb_write_object_flags flags)
470 {
471 return source->write_object(source, buf, len, type, oid,
472 compat_oid, mtime, flags);
473 }
474
475 /*
476 * Write an object into the object database source via a stream. The overall
477 * length of the object must be known in advance.
478 *
479 * Return 0 on success, a negative error code otherwise. Populates the given
480 * out pointer for the object ID.
481 */
482 static inline int odb_source_write_object_stream(struct odb_source *source,
483 struct odb_write_stream *stream,
484 size_t len,
485 struct object_id *oid)
486 {
487 return source->write_object_stream(source, stream, len, oid);
488 }
489
490 /*
491 * Read the list of alternative object database sources from the given backend
492 * and populate the `strvec` with them. The listing is not recursive -- that
493 * is, if any of the yielded alternate sources has alternates itself, those
494 * will not be yielded as part of this function call.
495 *
496 * Return 0 on success, a negative error code otherwise.
497 */
498 static inline int odb_source_read_alternates(struct odb_source *source,
499 struct strvec *out)
500 {
501 return source->read_alternates(source, out);
502 }
503
504 /*
505 * Write and persist a new alternate object database source for the given
506 * source. Any preexisting alternates are expected to stay valid, and the new
507 * alternate shall be appended to the end of the list.
508 *
509 * Returns 0 on success, a negative error code otherwise.
510 */
511 static inline int odb_source_write_alternate(struct odb_source *source,
512 const char *alternate)
513 {
514 return source->write_alternate(source, alternate);
515 }
516
517 /*
518 * Create a new transaction that can be used to write objects into a temporary
519 * staging area. The objects will only be persisted when the transaction is
520 * committed.
521 *
522 * Returns 0 on success, a negative error code otherwise.
523 */
524 static inline int odb_source_begin_transaction(struct odb_source *source,
525 struct odb_transaction **out,
526 enum odb_transaction_flags flags)
527 {
528 return source->begin_transaction(source, out, flags);
529 }
530
531 /*
532 * Optimize the object database source. Returns 0 on success, a negative error
533 * code otherwise.
534 */
535 static inline int odb_source_optimize(struct odb_source *source,
536 const struct odb_optimize_options *opts)
537 {
538 return source->optimize(source, opts);
539 }
540
541 /*
542 * Check whether optimization of the object database source is required given
543 * the provided options. Returns true if optimization should be performed,
544 * false otherwise.
545 */
546 static inline bool odb_source_optimize_required(struct odb_source *source,
547 const struct odb_optimize_options *opts)
548 {
549 return source->optimize_required(source, opts);
550 }
551
552 #endif