Files
git/odb/transaction.h
Patrick Steinhardt e837b812fe odb/streaming: consolidate read and write streams
The `struct odb_read_stream` and `struct odb_write_stream` both provide
the same functionality: they allow a caller to read object data from an
arbitrary source. Historically, the only difference was that the read
stream was used to read data out of the object database, whereas the
write stream was used to write data into the object database, but the
interfaces were mostly the same.

Over the preceding commits we have refactored the write stream to have
almost exactly the same interface as the read stream. With these
refactorings we can now easily merge those two streams into a single
interface that's used for both use cases.

While most of the changes are mechanical, there are two sites that need
special mention:

  - "builtin/unpack-objects.c" creates a write stream from compressed
    object data.

  - "odb/streaming.c" creates a write stream from a file descriptor.

Adapting these sites to yield the new stream type requires a couple more
changes. Most importantly, instead of embedding the pointer to the data
in `struct odb_write_stream`, we now allocate a structure that wraps the
new `struct odb_stream` base. Other than that though, the changes are
rather straight forward.

Some of the structures and functions are now somewhat misnamed. These
will be fixed in subsequent commits.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2026-08-05 09:42:50 -07:00

97 lines
3.4 KiB
C

#ifndef ODB_TRANSACTION_H
#define ODB_TRANSACTION_H
#include "gettext.h"
#include "odb.h"
/*
* A transaction may be started for an object database prior to writing new
* objects via odb_transaction_begin(). These objects are not committed until
* odb_transaction_commit() is invoked. Only a single transaction may be pending
* at a time.
*
* Each ODB source is expected to implement its own transaction handling.
*/
struct odb_transaction {
/* The ODB source the transaction is opened against. */
struct odb_source *source;
/*
* The ODB source specific callback invoked to commit a transaction.
* Returns 0 on success, a negative error code otherwise.
*/
int (*commit)(struct odb_transaction *transaction);
/*
* This callback is expected to write the given object stream into
* the ODB transaction.
*
* The resulting object ID shall be written into the out pointer. The
* callback is expected to return 0 on success, a negative error code
* otherwise.
*/
int (*write_object_stream)(struct odb_transaction *transaction,
struct odb_stream *stream,
struct object_id *oid);
/*
* This callback is expected to populate the provided strvec with the
* environment variables that a child process should inherit so that its
* object writes participate in the transaction. Returns 0 on success, a
* negative error code otherwise.
*/
int (*env)(struct odb_transaction *transaction, struct strvec *env);
};
/* Flags used to configure an ODB transaction. */
enum odb_transaction_flags {
/* Configures the transaction for use with git-receive-pack(1). */
ODB_TRANSACTION_RECEIVE = (1 << 0),
};
/*
* Starts an ODB transaction and returns it via `out`. Subsequent objects are
* written to the transaction and not committed until odb_transaction_commit()
* is invoked on the transaction. Returns 0 on success and a negative value on
* error. Note that it is considered an error to start a new transaction if the
* ODB already has an inflight transaction pending.
*/
int odb_transaction_begin(struct object_database *odb,
struct odb_transaction **out,
enum odb_transaction_flags flags);
static inline void odb_transaction_begin_or_die(struct object_database *odb,
struct odb_transaction **out,
enum odb_transaction_flags flags)
{
if (odb_transaction_begin(odb, out, flags))
die(_("failed to start ODB transaction"));
}
/*
* Commits an ODB transaction making the written objects visible. Returns 0 on
* success, a negative error code otherwise. Note that, if the specified
* transaction is NULL, the function is a no-op and no error is returned.
*/
int odb_transaction_commit(struct odb_transaction *transaction);
/*
* Writes the object in the provided stream into the transaction. The resulting
* object ID is written into the out pointer. Returns 0 on success, a negative
* error code otherwise.
*/
int odb_transaction_write_object_stream(struct odb_transaction *transaction,
struct odb_stream *stream,
struct object_id *oid);
/*
* Populates the provided strvec with the environment variables that a child
* process should inherit so that its object writes participate in the
* transaction, suitable for using via child_process.env. Returns 0 on success,
* a negative error code otherwise. Note that, if the specified transaction is
* NULL, the function is a no-op and no error is returned.
*/
int odb_transaction_env(struct odb_transaction *transaction, struct strvec *env);
#endif