Skip to content

Commit 7d96b06

Browse files
pks-tgitster
authored andcommitted
odb/source: make begin_transaction() function pluggable
Introduce a new callback function in `struct odb_source` to make the function pluggable. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7ac02d9 commit 7d96b06

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

odb/source-files.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ static int odb_source_files_write_object_stream(struct odb_source *source,
122122
return odb_source_loose_write_stream(source, stream, len, oid);
123123
}
124124

125+
static int odb_source_files_begin_transaction(struct odb_source *source,
126+
struct odb_transaction **out)
127+
{
128+
struct odb_transaction *tx = odb_transaction_files_begin(source);
129+
if (!tx)
130+
return -1;
131+
*out = tx;
132+
return 0;
133+
}
134+
125135
static int odb_source_files_read_alternates(struct odb_source *source,
126136
struct strvec *out)
127137
{
@@ -213,6 +223,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
213223
files->base.freshen_object = odb_source_files_freshen_object;
214224
files->base.write_object = odb_source_files_write_object;
215225
files->base.write_object_stream = odb_source_files_write_object_stream;
226+
files->base.begin_transaction = odb_source_files_begin_transaction;
216227
files->base.read_alternates = odb_source_files_read_alternates;
217228
files->base.write_alternate = odb_source_files_write_alternate;
218229

odb/source.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ enum object_info_flags {
5454
struct object_id;
5555
struct object_info;
5656
struct odb_read_stream;
57+
struct odb_transaction;
5758
struct odb_write_stream;
5859
struct strvec;
5960

@@ -231,6 +232,19 @@ struct odb_source {
231232
struct odb_write_stream *stream, size_t len,
232233
struct object_id *oid);
233234

235+
/*
236+
* This callback is expected to create a new transaction that can be
237+
* used to write objects to. The objects shall only be persisted into
238+
* the object database when the transcation's commit function is
239+
* called. Otherwise, the objects shall be discarded.
240+
*
241+
* Returns 0 on success, in which case the `*out` pointer will have
242+
* been populated with the object database transaction. Returns a
243+
* negative error code otherwise.
244+
*/
245+
int (*begin_transaction)(struct odb_source *source,
246+
struct odb_transaction **out);
247+
234248
/*
235249
* This callback is expected to read the list of alternate object
236250
* database sources connected to it and write them into the `strvec`.
@@ -434,4 +448,17 @@ static inline int odb_source_write_alternate(struct odb_source *source,
434448
return source->write_alternate(source, alternate);
435449
}
436450

451+
/*
452+
* Create a new transaction that can be used to write objects into a temporary
453+
* staging area. The objects will only be persisted when the transaction is
454+
* committed.
455+
*
456+
* Returns 0 on success, a negative error code otherwise.
457+
*/
458+
static inline int odb_source_begin_transaction(struct odb_source *source,
459+
struct odb_transaction **out)
460+
{
461+
return source->begin_transaction(source, out);
462+
}
463+
437464
#endif

0 commit comments

Comments
 (0)