| 1 | #ifndef ODB_TRANSACTION_H |
| 2 | #define ODB_TRANSACTION_H |
| 3 | |
| 4 | #include "gettext.h" |
| 5 | #include "odb.h" |
| 6 | |
| 7 | /* |
| 8 | * A transaction may be started for an object database prior to writing new |
| 9 | * objects via odb_transaction_begin(). These objects are not committed until |
| 10 | * odb_transaction_commit() is invoked. Only a single transaction may be pending |
| 11 | * at a time. |
| 12 | * |
| 13 | * Each ODB source is expected to implement its own transaction handling. |
| 14 | */ |
| 15 | struct odb_transaction { |
| 16 | /* The ODB source the transaction is opened against. */ |
| 17 | struct odb_source *source; |
| 18 | |
| 19 | /* |
| 20 | * The ODB source specific callback invoked to commit a transaction. |
| 21 | * Returns 0 on success, a negative error code otherwise. |
| 22 | */ |
| 23 | int (*commit)(struct odb_transaction *transaction); |
| 24 | |
| 25 | /* |
| 26 | * This callback is expected to write the given object stream into |
| 27 | * the ODB transaction. Note that for now, only blobs support streaming. |
| 28 | * |
| 29 | * The resulting object ID shall be written into the out pointer. The |
| 30 | * callback is expected to return 0 on success, a negative error code |
| 31 | * otherwise. |
| 32 | */ |
| 33 | int (*write_object_stream)(struct odb_transaction *transaction, |
| 34 | struct odb_write_stream *stream, size_t len, |
| 35 | struct object_id *oid); |
| 36 | |
| 37 | /* |
| 38 | * This callback is expected to populate the provided strvec with the |
| 39 | * environment variables that a child process should inherit so that its |
| 40 | * object writes participate in the transaction. Returns 0 on success, a |
| 41 | * negative error code otherwise. |
| 42 | */ |
| 43 | int (*env)(struct odb_transaction *transaction, struct strvec *env); |
| 44 | }; |
| 45 | |
| 46 | /* Flags used to configure an ODB transaction. */ |
| 47 | enum odb_transaction_flags { |
| 48 | /* Configures the transaction for use with git-receive-pack(1). */ |
| 49 | ODB_TRANSACTION_RECEIVE = (1 << 0), |
| 50 | }; |
| 51 | |
| 52 | /* |
| 53 | * Starts an ODB transaction and returns it via `out`. Subsequent objects are |
| 54 | * written to the transaction and not committed until odb_transaction_commit() |
| 55 | * is invoked on the transaction. Returns 0 on success and a negative value on |
| 56 | * error. Note that it is considered an error to start a new transaction if the |
| 57 | * ODB already has an inflight transaction pending. |
| 58 | */ |
| 59 | int odb_transaction_begin(struct object_database *odb, |
| 60 | struct odb_transaction **out, |
| 61 | enum odb_transaction_flags flags); |
| 62 | |
| 63 | static inline void odb_transaction_begin_or_die(struct object_database *odb, |
| 64 | struct odb_transaction **out, |
| 65 | enum odb_transaction_flags flags) |
| 66 | { |
| 67 | if (odb_transaction_begin(odb, out, flags)) |
| 68 | die(_("failed to start ODB transaction")); |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Commits an ODB transaction making the written objects visible. Returns 0 on |
| 73 | * success, a negative error code otherwise. Note that, if the specified |
| 74 | * transaction is NULL, the function is a no-op and no error is returned. |
| 75 | */ |
| 76 | int odb_transaction_commit(struct odb_transaction *transaction); |
| 77 | |
| 78 | /* |
| 79 | * Writes the object in the provided stream into the transaction. The resulting |
| 80 | * object ID is written into the out pointer. Returns 0 on success, a negative |
| 81 | * error code otherwise. |
| 82 | */ |
| 83 | int odb_transaction_write_object_stream(struct odb_transaction *transaction, |
| 84 | struct odb_write_stream *stream, |
| 85 | size_t len, struct object_id *oid); |
| 86 | |
| 87 | /* |
| 88 | * Populates the provided strvec with the environment variables that a child |
| 89 | * process should inherit so that its object writes participate in the |
| 90 | * transaction, suitable for using via child_process.env. Returns 0 on success, |
| 91 | * a negative error code otherwise. Note that, if the specified transaction is |
| 92 | * NULL, the function is a no-op and no error is returned. |
| 93 | */ |
| 94 | int odb_transaction_env(struct odb_transaction *transaction, struct strvec *env); |
| 95 | |
| 96 | #endif |