| 1 | #ifndef ODB_TRANSACTION_H |
| 2 | #define ODB_TRANSACTION_H |
| 3 | |
| 4 | #include "odb.h" |
| 5 | #include "odb/source.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 | /* The ODB source specific callback invoked to commit a transaction. */ |
| 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); |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * Starts an ODB transaction. Subsequent objects are written to the transaction |
| 37 | * and not committed until odb_transaction_commit() is invoked on the |
| 38 | * transaction. If the ODB already has a pending transaction, NULL is returned. |
| 39 | */ |
| 40 | struct odb_transaction *odb_transaction_begin(struct object_database *odb); |
| 41 | |
| 42 | /* |
| 43 | * Commits an ODB transaction making the written objects visible. If the |
| 44 | * specified transaction is NULL, the function is a no-op. |
| 45 | */ |
| 46 | void odb_transaction_commit(struct odb_transaction *transaction); |
| 47 | |
| 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 | |
| 57 | #endif |