From 941dcdb833faa1f92f3000f36efb4bca3f6584cb Mon Sep 17 00:00:00 2001 From: Bron Gondwana Date: Wed, 14 Oct 2020 15:10:10 +1100 Subject: [PATCH 1/4] cyrusdb: add cyrusdb_lock API (not yet implemented in any backend) --- lib/cyrusdb.c | 11 +++++++++++ lib/cyrusdb.h | 4 ++++ lib/cyrusdb_flat.c | 1 + lib/cyrusdb_quotalegacy.c | 1 + lib/cyrusdb_skiplist.c | 1 + lib/cyrusdb_sql.c | 1 + lib/cyrusdb_twoskip.c | 1 + lib/cyrusdb_zeroskip.c | 1 + 8 files changed, 21 insertions(+) diff --git a/lib/cyrusdb.c b/lib/cyrusdb.c index a3c4ca538c..0795dafd19 100644 --- a/lib/cyrusdb.c +++ b/lib/cyrusdb.c @@ -335,6 +335,17 @@ EXPORTED int cyrusdb_delete(struct db *db, return db->backend->delete_(db->engine, key, keylen, tid, force); } +EXPORTED int cyrusdb_lock(struct db *db, struct txn **tid, int flags) +{ + if (!db->backend->lock) + return CYRUSDB_NOTIMPLEMENTED; +#ifdef DEBUGDB + syslog(LOG_NOTICE, "DEBUGDB lock(%llx, %d)\n", (long long unsigned)db->engine, flags); +#endif + return db->backend->lock(db->engine, tid, flags); + +} + EXPORTED int cyrusdb_commit(struct db *db, struct txn *tid) { if (!db->backend->commit) diff --git a/lib/cyrusdb.h b/lib/cyrusdb.h index cce0dd1e00..bf13f2c3a0 100644 --- a/lib/cyrusdb.h +++ b/lib/cyrusdb.h @@ -211,6 +211,9 @@ struct cyrusdb_backend { struct txn **tid, int force); /* 1 = ignore not found errors */ + /* start a transaction (shared if flags & CYRUSDB_SHARED) */ + int (*lock)(struct dbengine *db, struct txn **tid, int flags); + /* Commit the transaction. When commit() returns, the tid will no longer * be valid, regardless of if the commit succeeded or failed */ int (*commit)(struct dbengine *db, struct txn *tid); @@ -289,6 +292,7 @@ extern int cyrusdb_store(struct db *db, extern int cyrusdb_delete(struct db *db, const char *key, size_t keylen, struct txn **tid, int force); +extern int cyrusdb_lock(struct db *db, struct txn **tid, int flags); extern int cyrusdb_commit(struct db *db, struct txn *tid); extern int cyrusdb_abort(struct db *db, struct txn *tid); extern int cyrusdb_dump(struct db *db, int detail); diff --git a/lib/cyrusdb_flat.c b/lib/cyrusdb_flat.c index 5c714d5102..e480501157 100644 --- a/lib/cyrusdb_flat.c +++ b/lib/cyrusdb_flat.c @@ -882,6 +882,7 @@ EXPORTED struct cyrusdb_backend cyrusdb_flat = &store, &delete, + NULL, /* lock */ &commit_txn, &abort_txn, diff --git a/lib/cyrusdb_quotalegacy.c b/lib/cyrusdb_quotalegacy.c index 0a3fe480ec..4ab4618f1b 100644 --- a/lib/cyrusdb_quotalegacy.c +++ b/lib/cyrusdb_quotalegacy.c @@ -923,6 +923,7 @@ HIDDEN struct cyrusdb_backend cyrusdb_quotalegacy = &store, &delete, + NULL, /* lock */ &commit_txn, &abort_txn, diff --git a/lib/cyrusdb_skiplist.c b/lib/cyrusdb_skiplist.c index 6601a0dc1b..2b322560c8 100644 --- a/lib/cyrusdb_skiplist.c +++ b/lib/cyrusdb_skiplist.c @@ -2486,6 +2486,7 @@ EXPORTED struct cyrusdb_backend cyrusdb_skiplist = &store, &mydelete, + NULL, /* lock */ &mycommit, &myabort, diff --git a/lib/cyrusdb_sql.c b/lib/cyrusdb_sql.c index 5c00e32922..1d650de4f3 100644 --- a/lib/cyrusdb_sql.c +++ b/lib/cyrusdb_sql.c @@ -941,6 +941,7 @@ HIDDEN struct cyrusdb_backend cyrusdb_sql = &store, &delete, + NULL, /* lock */ &commit_txn, &abort_txn, diff --git a/lib/cyrusdb_twoskip.c b/lib/cyrusdb_twoskip.c index 4cc4825c3f..dd131a89b6 100644 --- a/lib/cyrusdb_twoskip.c +++ b/lib/cyrusdb_twoskip.c @@ -2616,6 +2616,7 @@ HIDDEN struct cyrusdb_backend cyrusdb_twoskip = &store, &delete, + NULL, /* lock */ &mycommit, &myabort, diff --git a/lib/cyrusdb_zeroskip.c b/lib/cyrusdb_zeroskip.c index 95bdef3df6..05bd524674 100644 --- a/lib/cyrusdb_zeroskip.c +++ b/lib/cyrusdb_zeroskip.c @@ -593,6 +593,7 @@ HIDDEN struct cyrusdb_backend cyrusdb_zeroskip = &cyrusdb_zeroskip_store, &cyrusdb_zeroskip_delete, + NULL, /* lock */ &cyrusdb_zeroskip_commit, &cyrusdb_zeroskip_abort, From 94854d83dd5892aec92645a9cef45f94d66ff40d Mon Sep 17 00:00:00 2001 From: Bron Gondwana Date: Wed, 14 Oct 2020 15:14:41 +1100 Subject: [PATCH 2/4] twoskip: implement cyrusdb_lock --- lib/cyrusdb_twoskip.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/cyrusdb_twoskip.c b/lib/cyrusdb_twoskip.c index dd131a89b6..2d97385a2e 100644 --- a/lib/cyrusdb_twoskip.c +++ b/lib/cyrusdb_twoskip.c @@ -1341,6 +1341,11 @@ static void dispose_db(struct dbengine *db) /************************************************************/ +static int mylock(struct dbengine *db, struct txn **mytid, int flags) +{ + return newtxn(db, flags & CYRUSDB_SHARED, mytid); +} + static int opendb(const char *fname, int flags, struct dbengine **ret, struct txn **mytid) { struct dbengine *db; @@ -1451,7 +1456,7 @@ static int opendb(const char *fname, int flags, struct dbengine **ret, struct tx unlock(db); if (mytid) { - r = newtxn(db, flags & CYRUSDB_SHARED, mytid); + r = mylock(db, mytid, flags); if (r) goto done; } @@ -2616,7 +2621,7 @@ HIDDEN struct cyrusdb_backend cyrusdb_twoskip = &store, &delete, - NULL, /* lock */ + &mylock, &mycommit, &myabort, From d3702e48b3dcf8627ea8951481ee68b6d593cb5f Mon Sep 17 00:00:00 2001 From: Bron Gondwana Date: Wed, 14 Oct 2020 15:17:21 +1100 Subject: [PATCH 3/4] skiplist: implement cyrusdb_lock NOTE: skiplist doesn't support shared locks --- lib/cyrusdb_skiplist.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/cyrusdb_skiplist.c b/lib/cyrusdb_skiplist.c index 2b322560c8..70538c6ca0 100644 --- a/lib/cyrusdb_skiplist.c +++ b/lib/cyrusdb_skiplist.c @@ -848,6 +848,11 @@ static int compare_signed(const char *s1, int l1, const char *s2, int l2) return 0; } +static int mylock(struct dbengine *db, struct txn **mytid, int flags __attribute__((unused))) +{ + return lock_or_refresh(db, mytid); +} + static int myopen(const char *fname, int flags, struct dbengine **ret, struct txn **mytid) { struct dbengine *db; @@ -1001,7 +1006,10 @@ static int myopen(const char *fname, int flags, struct dbengine **ret, struct tx list_ent->refcount = 1; open_db = list_ent; - return mytid ? lock_or_refresh(db, mytid) : 0; + if (mytid) + return mylock(db, mytid, /*flags*/0); + + return 0; } static int myclose(struct dbengine *db) @@ -2486,7 +2494,7 @@ EXPORTED struct cyrusdb_backend cyrusdb_skiplist = &store, &mydelete, - NULL, /* lock */ + &mylock, &mycommit, &myabort, From 6c5d84c4bab92e7d0f948bb1382a6c000970b820 Mon Sep 17 00:00:00 2001 From: Bron Gondwana Date: Wed, 14 Oct 2020 15:24:21 +1100 Subject: [PATCH 4/4] zeroskip: implement cyrusdb_lock API --- lib/cyrusdb_zeroskip.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/cyrusdb_zeroskip.c b/lib/cyrusdb_zeroskip.c index 05bd524674..9e4fa92a9c 100644 --- a/lib/cyrusdb_zeroskip.c +++ b/lib/cyrusdb_zeroskip.c @@ -148,6 +148,19 @@ HIDDEN int cyrusdb_zeroskip_unlink(const char *fname __attribute__((unused)), return CYRUSDB_OK; } +HIDDEN int cyrusdb_zeroskip_lock(struct dbengine *dbe, struct txn **mytid, + int flags __attribute__((unused))) +{ + struct txn *tid = xmalloc(sizeof(struct txn)); + int r = zsdb_transaction_begin(dbe->db, &tid->t); + if (r != ZS_OK) { + free(tid); + return CYRUSDB_INTERNAL; + } + *mytid = tid; + return 0; +} + static int cyrusdb_zeroskip_open(const char *fname, int flags, struct dbengine **ret, @@ -185,12 +198,8 @@ static int cyrusdb_zeroskip_open(const char *fname, *ret = dbe; if (mytid) { - *mytid = xmalloc(sizeof(struct txn)); - r = zsdb_transaction_begin(dbe->db, &(*mytid)->t); - if (r != ZS_OK) { - r = CYRUSDB_INTERNAL; - goto close_db; - } + r = cyrusdb_zeroskip_lock(dbe, mytid, flags); + if (r) goto close_db; } r = CYRUSDB_OK; @@ -593,7 +602,7 @@ HIDDEN struct cyrusdb_backend cyrusdb_zeroskip = &cyrusdb_zeroskip_store, &cyrusdb_zeroskip_delete, - NULL, /* lock */ + &cyrusdb_zeroskip_lock, &cyrusdb_zeroskip_commit, &cyrusdb_zeroskip_abort,