Permalink
Browse files

Updated ProcessResponse of BinarySingleItemOperation to return IOpera…

…tionResult.
  • Loading branch information...
1 parent 5f1adc5 commit ed9bdd4d8f8e980cbf5741065af372c2a87aa50e @johnzablocki johnzablocki committed Apr 23, 2012
@@ -256,6 +256,7 @@
<Compile Include="Memcached\Results\Factories\IMutateOperationResultFactory.cs" />
<Compile Include="Memcached\Results\Factories\IRemoveOperationResultFactory.cs" />
<Compile Include="Memcached\Results\GetOperationResult`1.cs" />
+ <Compile Include="Memcached\Results\Helpers\ResultHelper.cs" />
<Compile Include="Memcached\Results\IGetOperationResult`1.cs" />
<Compile Include="Memcached\Results\IPooledSocketResult.cs" />
<Compile Include="Memcached\Results\IRemoveOperationResult.cs" />
@@ -16,7 +16,7 @@ protected internal override IList<ArraySegment<byte>> GetBuffer()
return this.Build().CreateBuffer();
}
- protected abstract bool ProcessResponse(BinaryResponse response);
+ protected abstract IOperationResult ProcessResponse(BinaryResponse response);
protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
@@ -33,8 +33,10 @@ protected internal override IOperationResult ReadResponse(PooledSocket socket)
StatusCode = this.StatusCode
};
- if (! this.ProcessResponse(response))
+ IOperationResult responseResult;
+ if (! (responseResult = this.ProcessResponse(response)).Success)
{
+ result.InnerResult = responseResult;
result.Fail("Failed to process response, see StatusCode or InnerResult for details");
}
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
+using Enyim.Caching.Memcached.Results;
namespace Enyim.Caching.Memcached.Protocol.Binary
{
@@ -31,9 +32,9 @@ protected override BinaryRequest Build()
return request;
}
- protected override bool ProcessResponse(BinaryResponse response)
+ protected override IOperationResult ProcessResponse(BinaryResponse response)
{
- return true;
+ return new BinaryOperationResult() { Success = true };
}
ConcatenationMode IConcatOperation.Mode
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System.Text;
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Extensions;
+using Enyim.Caching.Memcached.Results.Helpers;
namespace Enyim.Caching.Memcached.Protocol.Binary
{
@@ -19,17 +22,25 @@ protected override BinaryRequest Build()
return request;
}
- protected override bool ProcessResponse(BinaryResponse response)
+ protected override IOperationResult ProcessResponse(BinaryResponse response)
{
+ var result = new BinaryOperationResult();
#if EVEN_MORE_LOGGING
if (log.IsDebugEnabled)
if (response.StatusCode == 0)
log.DebugFormat("Delete succeeded for key '{0}'.", this.Key);
else
log.DebugFormat("Delete failed for key '{0}'. Reason: {1}", this.Key, Encoding.ASCII.GetString(response.Data.Array, response.Data.Offset, response.Data.Count));
#endif
-
- return true;
+ if (response.StatusCode == 0)
+ {
+ return result.Pass();
+ }
+ else
+ {
+ var message = ResultHelper.ProcessResponseData("Delete failed for key " + Key, response.Data);
+ return result.Fail(message);
+ }
}
}
}
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System.Text;
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Extensions;
+using Enyim.Caching.Memcached.Results.Helpers;
namespace Enyim.Caching.Memcached.Protocol.Binary
{
@@ -21,9 +24,10 @@ protected override BinaryRequest Build()
return request;
}
- protected override bool ProcessResponse(BinaryResponse response)
+ protected override IOperationResult ProcessResponse(BinaryResponse response)
{
var status = response.StatusCode;
+ var result = new BinaryOperationResult();
this.StatusCode = status;
@@ -36,9 +40,9 @@ protected override bool ProcessResponse(BinaryResponse response)
#if EVEN_MORE_LOGGING
if (log.IsDebugEnabled)
log.DebugFormat("Get succeeded for key '{0}'.", this.Key);
-#endif
+#endif
- return true;
+ return result.Pass();
}
this.Cas = 0;
@@ -48,7 +52,8 @@ protected override bool ProcessResponse(BinaryResponse response)
log.DebugFormat("Get failed for key '{0}'. Reason: {1}", this.Key, Encoding.ASCII.GetString(response.Data.Array, response.Data.Offset, response.Data.Count));
#endif
- return false;
+ var message = ResultHelper.ProcessResponseData("Get failed for key " + Key, response.Data);
+ return result.Fail(message);
}
CacheItem IGetOperation.Result
@@ -1,4 +1,7 @@
using System;
+using Enyim.Caching.Memcached.Results.Extensions;
+using Enyim.Caching.Memcached.Results.Helpers;
+using Enyim.Caching.Memcached.Results;
namespace Enyim.Caching.Memcached.Protocol.Binary
{
@@ -49,23 +52,25 @@ protected override BinaryRequest Build()
return request;
}
- protected override bool ProcessResponse(BinaryResponse response)
+ protected override IOperationResult ProcessResponse(BinaryResponse response)
{
+ var result = new BinaryOperationResult();
var status = response.StatusCode;
this.StatusCode = status;
if (status == 0)
{
var data = response.Data;
if (data.Count != 8)
- throw new InvalidOperationException("result must be 8 bytes long, received: " + data.Count);
+ return result.Fail("Result must be 8 bytes long, received: " + data.Count, new InvalidOperationException());
this.result = BinaryConverter.DecodeUInt64(data.Array, data.Offset);
- return true;
+ return result.Pass();
}
- return false;
+ var message = ResultHelper.ProcessResponseData("Mutate failed for key " + Key, response.Data);
+ return result.Fail(message);
}
MutationMode IMutatorOperation.Mode
@@ -1,5 +1,8 @@
using System;
using System.Text;
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Helpers;
+using Enyim.Caching.Memcached.Results.Extensions;
namespace Enyim.Caching.Memcached.Protocol.Binary
{
@@ -46,8 +49,10 @@ protected override BinaryRequest Build()
return request;
}
- protected override bool ProcessResponse(BinaryResponse response)
+ protected override IOperationResult ProcessResponse(BinaryResponse response)
{
+ var result = new BinaryOperationResult();
+
#if EVEN_MORE_LOGGING
if (log.IsDebugEnabled)
if (response.StatusCode == 0)
@@ -57,10 +62,16 @@ protected override bool ProcessResponse(BinaryResponse response)
log.DebugFormat("Store failed for key '{0}'. Reason: {1}", this.Key, Encoding.ASCII.GetString(response.Data.Array, response.Data.Offset, response.Data.Count));
}
#endif
-
this.StatusCode = response.StatusCode;
-
- return true;
+ if (response.StatusCode == 0)
+ {
+ return result.Pass();
+ }
+ else
+ {
+ var message = ResultHelper.ProcessResponseData("Store failed for key " + Key, response.Data);
+ return result.Fail(message);
+ }
}
StoreMode IStoreOperation.Mode
@@ -0,0 +1,53 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Enyim.Caching.Memcached.Results.Factories;
+
+namespace Enyim.Caching.Memcached.Results.Helpers
+{
+
+ public static class ResultHelper
+ {
+
+ public static string ProcessResponseData(string message, ArraySegment<byte> data)
+ {
+
+ if (data != null && data.Count > 0)
+ {
+ try
+ {
+ return message + ": " + Encoding.ASCII.GetString(data.Array, data.Offset, data.Count);
+ }
+ catch (Exception ex)
+ {
+ return ex.GetBaseException().Message;
+ }
+ }
+
+ return string.Empty;
+ }
+ }
+}
+
+#region [ License information ]
+/* ************************************************************
+ *
+ * @author Couchbase <info@couchbase.com>
+ * @copyright 2012 Couchbase, Inc.
+ * @copyright 2012 Attila Kiskó, enyim.com
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ************************************************************/
+#endregion

0 comments on commit ed9bdd4

Please sign in to comment.