Skip to content

Commit

Permalink
Merge pull request #3256 from cyrusimap/cyrusdb-lock
Browse files Browse the repository at this point in the history
Cyrusdb lock
  • Loading branch information
elliefm authored Sep 16, 2024
2 parents 61acd6c + 6c5d84c commit 54fc311
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 8 deletions.
11 changes: 11 additions & 0 deletions lib/cyrusdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions lib/cyrusdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions lib/cyrusdb_flat.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ EXPORTED struct cyrusdb_backend cyrusdb_flat =
&store,
&delete,

NULL, /* lock */
&commit_txn,
&abort_txn,

Expand Down
1 change: 1 addition & 0 deletions lib/cyrusdb_quotalegacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ HIDDEN struct cyrusdb_backend cyrusdb_quotalegacy =
&store,
&delete,

NULL, /* lock */
&commit_txn,
&abort_txn,

Expand Down
11 changes: 10 additions & 1 deletion lib/cyrusdb_skiplist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -2486,6 +2494,7 @@ EXPORTED struct cyrusdb_backend cyrusdb_skiplist =
&store,
&mydelete,

&mylock,
&mycommit,
&myabort,

Expand Down
1 change: 1 addition & 0 deletions lib/cyrusdb_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ HIDDEN struct cyrusdb_backend cyrusdb_sql =
&store,
&delete,

NULL, /* lock */
&commit_txn,
&abort_txn,

Expand Down
8 changes: 7 additions & 1 deletion lib/cyrusdb_twoskip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -2616,6 +2621,7 @@ HIDDEN struct cyrusdb_backend cyrusdb_twoskip =
&store,
&delete,

&mylock,
&mycommit,
&myabort,

Expand Down
22 changes: 16 additions & 6 deletions lib/cyrusdb_zeroskip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -593,6 +602,7 @@ HIDDEN struct cyrusdb_backend cyrusdb_zeroskip =
&cyrusdb_zeroskip_store,
&cyrusdb_zeroskip_delete,

&cyrusdb_zeroskip_lock,
&cyrusdb_zeroskip_commit,
&cyrusdb_zeroskip_abort,

Expand Down

0 comments on commit 54fc311

Please sign in to comment.