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