Skip unnecessary writes in default impl for modifyGet

This commit is contained in:
Tomasz Bekas 2024-08-14 18:00:04 +02:00
parent b3d4bd40fc
commit 04fa15ca90
No known key found for this signature in database
GPG Key ID: 4854E04C98824959
3 changed files with 8 additions and 1 deletions

View File

@ -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.
##

View File

@ -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)

View File

@ -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: