Skip to content

Commit 4dbf9fe

Browse files
committed
odb/transaction: make write_object_stream() pluggable
How an ODB transaction handles writing objects is expected to vary between implementations. Introduce a new `write_object_stream()` callback in `struct odb_transaction` to make this function pluggable. Wire up `index_blob_packfile_transaction()` for use with `struct odb_transaction_files` accordingly. Signed-off-by: Justin Tobler <jltobler@gmail.com>
1 parent d2e77b1 commit 4dbf9fe

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

object-file.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,9 +1575,10 @@ static void flush_packfile_transaction(struct odb_transaction_files *transaction
15751575
* binary blobs, they generally do not want to get any conversion, and
15761576
* callers should avoid this code path when filters are requested.
15771577
*/
1578-
static int index_blob_packfile_transaction(struct odb_transaction *base,
1579-
struct odb_write_stream *stream,
1580-
size_t size, struct object_id *result_oid)
1578+
static int odb_transaction_files_write_object_stream(struct odb_transaction *base,
1579+
struct odb_write_stream *stream,
1580+
size_t size,
1581+
struct object_id *result_oid)
15811582
{
15821583
struct odb_transaction_files *transaction = container_of(base,
15831584
struct odb_transaction_files,
@@ -1661,10 +1662,10 @@ int index_fd(struct index_state *istate, struct object_id *oid,
16611662
struct object_database *odb = the_repository->objects;
16621663
struct odb_transaction *transaction = odb_transaction_begin(odb);
16631664

1664-
ret = index_blob_packfile_transaction(odb->transaction,
1665-
&stream,
1666-
xsize_t(st->st_size),
1667-
oid);
1665+
ret = odb_transaction_write_object_stream(odb->transaction,
1666+
&stream,
1667+
xsize_t(st->st_size),
1668+
oid);
16681669
odb_transaction_commit(transaction);
16691670
} else {
16701671
ret = hash_blob_stream(&stream,
@@ -2129,6 +2130,7 @@ struct odb_transaction *odb_transaction_files_begin(struct odb_source *source)
21292130
transaction = xcalloc(1, sizeof(*transaction));
21302131
transaction->base.source = source;
21312132
transaction->base.commit = odb_transaction_files_commit;
2133+
transaction->base.write_object_stream = odb_transaction_files_write_object_stream;
21322134

21332135
return &transaction->base;
21342136
}

odb/transaction.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ void odb_transaction_commit(struct odb_transaction *transaction)
2626
transaction->source->odb->transaction = NULL;
2727
free(transaction);
2828
}
29+
30+
int odb_transaction_write_object_stream(struct odb_transaction *transaction,
31+
struct odb_write_stream *stream,
32+
size_t len, struct object_id *oid)
33+
{
34+
return transaction->write_object_stream(transaction, stream, len, oid);
35+
}

odb/transaction.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,24 @@
1212
*
1313
* Each ODB source is expected to implement its own transaction handling.
1414
*/
15-
struct odb_transaction;
16-
typedef void (*odb_transaction_commit_fn)(struct odb_transaction *transaction);
1715
struct odb_transaction {
1816
/* The ODB source the transaction is opened against. */
1917
struct odb_source *source;
2018

2119
/* The ODB source specific callback invoked to commit a transaction. */
22-
odb_transaction_commit_fn commit;
20+
void (*commit)(struct odb_transaction *transaction);
21+
22+
/*
23+
* This callback is expected to write the given object stream into
24+
* the ODB transaction. Note that for now, only blobs support streaming.
25+
*
26+
* The resulting object ID shall be written into the out pointer. The
27+
* callback is expected to return 0 on success, a negative error code
28+
* otherwise.
29+
*/
30+
int (*write_object_stream)(struct odb_transaction *transaction,
31+
struct odb_write_stream *stream, size_t len,
32+
struct object_id *oid);
2333
};
2434

2535
/*
@@ -35,4 +45,13 @@ struct odb_transaction *odb_transaction_begin(struct object_database *odb);
3545
*/
3646
void odb_transaction_commit(struct odb_transaction *transaction);
3747

48+
/*
49+
* Writes the object in the provided stream into the transaction. The resulting
50+
* object ID is written into the out pointer. Returns 0 on success, a negative
51+
* error code otherwise.
52+
*/
53+
int odb_transaction_write_object_stream(struct odb_transaction *transaction,
54+
struct odb_write_stream *stream,
55+
size_t len, struct object_id *oid);
56+
3857
#endif

0 commit comments

Comments
 (0)