Skip to content

Commit d6fc6fe

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 eb9635d commit d6fc6fe

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

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
@@ -53,6 +53,7 @@ enum object_info_flags {
5353
struct object_id;
5454
struct object_info;
5555
struct odb_read_stream;
56+
struct odb_transaction;
5657
struct odb_write_stream;
5758
struct strvec;
5859

@@ -233,6 +234,19 @@ struct odb_source {
233234
struct odb_write_stream *stream, size_t len,
234235
struct object_id *oid);
235236

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

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

0 commit comments

Comments
 (0)