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