From 04fa15ca90d18723ad78325340ff7d06188e474e Mon Sep 17 00:00:00 2001 From: Tomasz Bekas Date: Wed, 14 Aug 2024 18:00:04 +0200 Subject: [PATCH] Skip unnecessary writes in default impl for modifyGet --- datastore/datastore.nim | 3 +++ datastore/defaultimpl.nim | 4 ++++ datastore/sql/sqliteds.nim | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/datastore/datastore.nim b/datastore/datastore.nim index 47f339b..7dfa992 100644 --- a/datastore/datastore.nim +++ b/datastore/datastore.nim @@ -71,6 +71,9 @@ method modifyGet*(self: Datastore, key: Key, fn: ModifyGet): Future[?!seq[byte]] ## | some(u) | none | delete u | ## | some(u) | some(v) | replace u with v (if u != v) | ## + ## If `fn` raises an error, the value associated with `key` remains unchanged and raised error is wrapped + ## into a failure and returned as a result. + ## ## Note that `fn` can be called multiple times (when concurrent modification was detected). In such case ## only the last auxillary value is returned. ## diff --git a/datastore/defaultimpl.nim b/datastore/defaultimpl.nim index be7178a..475d627 100644 --- a/datastore/defaultimpl.nim +++ b/datastore/defaultimpl.nim @@ -37,6 +37,10 @@ proc defaultModifyGetImpl*( except CatchableError as err: return failure(err) + if maybeCurrentData == maybeNewData: + # no need to change currently stored value (if any) + return aux.success + if newData =? maybeNewData: if err =? (await self.put(key, newData)).errorOption: return failure(err) diff --git a/datastore/sql/sqliteds.nim b/datastore/sql/sqliteds.nim index 8f16f07..70bc06f 100644 --- a/datastore/sql/sqliteds.nim +++ b/datastore/sql/sqliteds.nim @@ -66,7 +66,7 @@ method modifyGet*(self: SQLiteDatastore, key: Key, fn: ModifyGet): Future[?!seq[ return failure(err) if maybeCurrentData == maybeNewData: - # no need to change currently stored value + # no need to change currently stored value (if any) break if err =? self.db.beginStmt.exec().errorOption: