aboutsummaryrefslogtreecommitdiffstats
path: root/src/db.c
diff options
context:
space:
mode:
authorJose Bollo <jose.bollo@iot.bzh>2019-05-08 15:12:16 +0200
committerJose Bollo <jose.bollo@iot.bzh>2019-05-09 15:12:37 +0200
commited91352e50be1efcfa70e04f8d7e0761690837b9 (patch)
treeb4b41837d74f3336c089b06f3bd3973049932099 /src/db.c
parent97e91b39fceed0309b6863f508f92db2bcc3b114 (diff)
Rework transaction
Diffstat (limited to 'src/db.c')
-rw-r--r--src/db.c38
1 files changed, 26 insertions, 12 deletions
diff --git a/src/db.c b/src/db.c
index 98c96e7..5d28246 100644
--- a/src/db.c
+++ b/src/db.c
@@ -30,6 +30,7 @@
#include "anydb.h"
#include "fdb.h"
#include "memdb.h"
+#include "db.h"
static anydb_t *memdb;
@@ -75,27 +76,40 @@ db_is_empty(
return fdb_is_empty();
}
-/** synchronize db on files */
+/** enter atomic mode */
int
-db_sync(
+db_transaction_begin(
) {
- return fdb_sync();
-}
+ int rc1, rc2;
-/** make a backup of the database */
-int
-db_backup(
-) {
- return fdb_backup();
+ rc1 = fdb_backup();
+ rc2 = anydb_transaction(memdb, Anydb_Transaction_Start);
+
+ return rc1 ?: rc2;
}
-/** recover the database from latest backup */
+/** leave atomic mode */
int
-db_recover(
+db_transaction_end(
+ bool commit
) {
- return fdb_recover();
+ int rc1, rc2, rc3, rc4;
+
+ if (commit) {
+ rc1 = 0;
+ rc2 = anydb_transaction(memdb, Anydb_Transaction_Commit);
+ rc3 = db_cleanup();
+ } else {
+ rc1 = fdb_recover();
+ rc2 = anydb_transaction(memdb, Anydb_Transaction_Cancel);
+ rc3 = 0;
+ }
+ rc4 = fdb_sync();
+
+ return rc1 ?: rc2 ?: rc3 ?: rc4;
}
+
/** enumerate */
void
db_for_all(