From de968ee2dc17d06af9aa4bcb6dd444ceab656de9 Mon Sep 17 00:00:00 2001 From: Alexander Volgunin Date: Wed, 9 Oct 2024 10:49:08 -0600 Subject: [PATCH 1/2] Optionally do not throw exception from Touch command when record does not exist --- AerospikeClient/Command/TouchCommand.cs | 9 +++++++++ AerospikeClient/Policy/WritePolicy.cs | 20 ++++++++++++++++---- AerospikeTest/Sync/Basic/TestTouch.cs | 14 +++++++++++++- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/AerospikeClient/Command/TouchCommand.cs b/AerospikeClient/Command/TouchCommand.cs index ecad9438..ebfef228 100644 --- a/AerospikeClient/Command/TouchCommand.cs +++ b/AerospikeClient/Command/TouchCommand.cs @@ -65,6 +65,15 @@ protected internal override void ParseResult(IConnection conn) return; } + if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR) + { + if (writePolicy.failOnTouchRecordDoesNotExist) + { + throw new AerospikeException(resultCode); + } + return; + } + if (resultCode == ResultCode.FILTERED_OUT) { if (writePolicy.failOnFilteredOut) diff --git a/AerospikeClient/Policy/WritePolicy.cs b/AerospikeClient/Policy/WritePolicy.cs index 3fd1247e..1ae1c60f 100644 --- a/AerospikeClient/Policy/WritePolicy.cs +++ b/AerospikeClient/Policy/WritePolicy.cs @@ -99,11 +99,22 @@ public sealed class WritePolicy : Policy /// Default: false (do not tombstone deleted records). /// public bool durableDelete; - + /// - /// Copy write policy from another write policy. + /// Qualify how to handle touches when the record does not exist. + /// If true, throw + /// with result code ; + /// otherwise, do nothing. + /// + /// Default: true + /// /// - public WritePolicy(WritePolicy other) + public bool failOnTouchRecordDoesNotExist = true; + + /// + /// Copy write policy from another write policy. + /// + public WritePolicy(WritePolicy other) : base(other) { this.recordExistsAction = other.recordExistsAction; @@ -113,7 +124,8 @@ public WritePolicy(WritePolicy other) this.expiration = other.expiration; this.respondAllOps = other.respondAllOps; this.durableDelete = other.durableDelete; - } + this.failOnTouchRecordDoesNotExist = other.failOnTouchRecordDoesNotExist; + } /// /// Copy write policy from another policy. diff --git a/AerospikeTest/Sync/Basic/TestTouch.cs b/AerospikeTest/Sync/Basic/TestTouch.cs index bb9ebf59..fcb81dff 100644 --- a/AerospikeTest/Sync/Basic/TestTouch.cs +++ b/AerospikeTest/Sync/Basic/TestTouch.cs @@ -56,6 +56,18 @@ record = client.Get(null, key, bin.name); record = client.Get(null, key, bin.name); Assert.IsNull(record); - } + + try + { + client.Touch(writePolicy, key); + Assert.Fail("Must throw ResultCode.KEY_NOT_FOUND_ERROR exception."); + } + catch (AerospikeException ex) when (ex.Result == ResultCode.KEY_NOT_FOUND_ERROR) + { + } + + writePolicy.failOnTouchRecordDoesNotExist = false; + client.Touch(writePolicy, key); + } } } From 92eb312f8fcb116bf8acee403b4c312afb55c617 Mon Sep 17 00:00:00 2001 From: Alexander Volgunin Date: Wed, 9 Oct 2024 10:52:40 -0600 Subject: [PATCH 2/2] fix formatting --- AerospikeClient/Policy/WritePolicy.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/AerospikeClient/Policy/WritePolicy.cs b/AerospikeClient/Policy/WritePolicy.cs index 1ae1c60f..6dd7da8b 100644 --- a/AerospikeClient/Policy/WritePolicy.cs +++ b/AerospikeClient/Policy/WritePolicy.cs @@ -111,10 +111,10 @@ public sealed class WritePolicy : Policy /// public bool failOnTouchRecordDoesNotExist = true; - /// - /// Copy write policy from another write policy. - /// - public WritePolicy(WritePolicy other) + /// + /// Copy write policy from another write policy. + /// + public WritePolicy(WritePolicy other) : base(other) { this.recordExistsAction = other.recordExistsAction;