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 /*
269 * Allocate and initialize a new source for the given object database located
270 * at `path`. `local` indicates whether or not the source is the local and thus
271 * primary object source of the object database.
272 */
273 struct odb_source *odb_source_new(struct object_database *odb,
274 const char *path,
275 bool local);
276
277 /*
278 * Initialize the source for the given object database located at `path`.
279 * `local` indicates whether or not the source is the local and thus primary
280 * object source of the object database.
281 *
282 * This function is only supposed to be called by specific object source
283 * implementations.
284 */
285 void odb_source_init(struct odb_source *source,
286 struct object_database *odb,
287 enum odb_source_type type,
288 const char *path,
289 bool local);
290
291 /*
292 * Free the object database source, releasing all associated resources and
293 * freeing the structure itself.
294 */
295 void odb_source_free(struct odb_source *source);
296
297 /*
298 * Release the object database source, releasing all associated resources.
299 *
300 * This function is only supposed to be called by specific object source
301 * implementations.
302 */
303 void odb_source_release(struct odb_source *source);
304
305 /*
306 * Close the object database source without releasing he underlying data. The
307 * source can still be used going forward, but it first needs to be reopened.
308 * This can be useful to reduce resource usage.
309 */
310 static inline void odb_source_close(struct odb_source *source)
311 {
312 source->close(source);
313 }
314
315 /*
316 * Prepare the object database source and clear any caches. Depending on the
317 * backend used this may have the effect that concurrently-written objects
318 * become visible.
319 */
320 static inline void odb_source_prepare(struct odb_source *source,
321 enum odb_prepare_flags flags)
322 {
323 source->prepare(source, flags);
324 }
325
326 /*
327 * Read an object from the object database source identified by its object ID.
328 * Returns 0 on success, a negative error code otherwise.
329 */
330 static inline int odb_source_read_object_info(struct odb_source *source,
331 const struct object_id *oid,
332 struct object_info *oi,
333 enum object_info_flags flags)
334 {
335 return source->read_object_info(source, oid, oi, flags);
336 }
337
338 /*
339 * Create a new read stream for the given object ID. Returns 0 on success, a
340 * negative error code otherwise.
341 */
342 static inline int odb_source_read_object_stream(struct odb_read_stream **out,
343 struct odb_source *source,
344 const struct object_id *oid)
345 {
346 return source->read_object_stream(out, source, oid);
347 }
348
349 /*
350 * Iterate through all objects contained in the given source and invoke the
351 * callback function for each of them. Returning a non-zero code from the
352 * callback function aborts iteration. There is no guarantee that objects
353 * are only iterated over once.
354 *
355 * The optional `request` structure serves as a template for retrieving the
356 * object info for each individual iterated object and will be populated as if
357 * `odb_source_read_object_info()` was called on the object. It will not be
358 * modified, the callback will instead be invoked with a separate `struct
359 * object_info` for every object. Object info will not be read when passing a
360 * `NULL` pointer.
361 *
362 * The flags is a bitfield of `ODB_FOR_EACH_OBJECT_*` flags. Not all flags may
363 * apply to a specific backend, so whether or not they are honored is defined
364 * by the implementation.
365 *
366 * Returns 0 when all objects have been iterated over, a negative error code in
367 * case iteration has failed, or a non-zero value returned from the callback.
368 */
369 static inline int odb_source_for_each_object(struct odb_source *source,
370 const struct object_info *request,
371 odb_for_each_object_cb cb,
372 void *cb_data,
373 const struct odb_for_each_object_options *opts)
374 {
375 return source->for_each_object(source, request, cb, cb_data, opts);
376 }
377
378 /*
379 * Count the number of objects in the given object database source.
380 *
381 * Returns 0 on success, a negative error code otherwise.
382 */
383 static inline int odb_source_count_objects(struct odb_source *source,
384 enum odb_count_objects_flags flags,
385 unsigned long *out)
386 {
387 return source->count_objects(source, flags, out);
388 }
389
390 /*
391 * Determine the minimum required length to make the given object ID unique in
392 * the given source. Returns 0 on success, a negative error code otherwise.
393 */
394 static inline int odb_source_find_abbrev_len(struct odb_source *source,
395 const struct object_id *oid,
396 unsigned min_len,
397 unsigned *out)
398 {
399 return source->find_abbrev_len(source, oid, min_len, out);
400 }
401
402 /*
403 * Freshen an object in the object database by updating its timestamp.
404 * Returns 1 in case the object has been freshened, 0 in case the object does
405 * not exist.
406 */
407 static inline int odb_source_freshen_object(struct odb_source *source,
408 const struct object_id *oid,
409 const time_t *mtime)
410 {
411 return source->freshen_object(source, oid, mtime);
412 }
413
414 /*
415 * Write an object into the object database source. Returns 0 on success, a
416 * negative error code otherwise. Populates the given out pointers for the
417 * object ID and the compatibility object ID, if non-NULL.
418 */
419 static inline int odb_source_write_object(struct odb_source *source,
420 const void *buf, unsigned long len,
421 enum object_type type,
422 const struct object_id *oid,
423 const struct object_id *compat_oid,
424 const time_t *mtime,
425 enum odb_write_object_flags flags)
426 {
427 return source->write_object(source, buf, len, type, oid,
428 compat_oid, mtime, flags);
429 }
430
431 /*
432 * Write an object into the object database source via a stream. The overall
433 * length of the object must be known in advance.
434 *
435 * Return 0 on success, a negative error code otherwise. Populates the given
436 * out pointer for the object ID.
437 */
438 static inline int odb_source_write_object_stream(struct odb_source *source,
439 struct odb_write_stream *stream,
440 size_t len,
441 struct object_id *oid)
442 {
443 return source->write_object_stream(source, stream, len, oid);
444 }
445
446 /*
447 * Read the list of alternative object database sources from the given backend
448 * and populate the `strvec` with them. The listing is not recursive -- that
449 * is, if any of the yielded alternate sources has alternates itself, those
450 * will not be yielded as part of this function call.
451 *
452 * Return 0 on success, a negative error code otherwise.
453 */
454 static inline int odb_source_read_alternates(struct odb_source *source,
455 struct strvec *out)
456 {
457 return source->read_alternates(source, out);
458 }
459
460 /*
461 * Write and persist a new alternate object database source for the given
462 * source. Any preexisting alternates are expected to stay valid, and the new
463 * alternate shall be appended to the end of the list.
464 *
465 * Returns 0 on success, a negative error code otherwise.
466 */
467 static inline int odb_source_write_alternate(struct odb_source *source,
468 const char *alternate)
469 {
470 return source->write_alternate(source, alternate);
471 }
472
473 /*
474 * Create a new transaction that can be used to write objects into a temporary
475 * staging area. The objects will only be persisted when the transaction is
476 * committed.
477 *
478 * Returns 0 on success, a negative error code otherwise.
479 */
480 static inline int odb_source_begin_transaction(struct odb_source *source,
481 struct odb_transaction **out,
482 enum odb_transaction_flags flags)
483 {
484 return source->begin_transaction(source, out, flags);
485 }
486
487 #endif