Permalink
Browse files

Updated ReadResponse method of IOperation to return an IOperationResult.

  • Loading branch information...
1 parent 2cfc500 commit 2162ebd7a4f2cf89855a334456783f5b4d8a3b29 @johnzablocki johnzablocki committed Apr 23, 2012
View
2 Enyim.Caching/Enyim.Caching.csproj
@@ -241,6 +241,7 @@
</Compile>
<Compile Include="Memcached\Protocol\Text\TextSocketHelper.cs">
</Compile>
+ <Compile Include="Memcached\Results\BinaryOperationResult.cs" />
<Compile Include="Memcached\Results\ConcatOperationResult.cs" />
<Compile Include="Memcached\Results\Extensions\OperationResultExtensions.cs" />
<Compile Include="Memcached\Results\Factories\IGetOperationResultFactory`1.cs" />
@@ -272,6 +273,7 @@
<Compile Include="Memcached\Results\MutateOperationResult.cs" />
<Compile Include="Memcached\Results\OperationResultBase.cs" />
<Compile Include="Memcached\Results\StoreOperationResult.cs" />
+ <Compile Include="Memcached\Results\TextOperationResult.cs" />
<Compile Include="Memcached\ServerStats.cs">
</Compile>
<Compile Include="Memcached\SlidingBuffer.cs" />
View
11 Enyim.Caching/Memcached/MemcachedNode.cs
@@ -522,7 +522,16 @@ protected virtual IPooledSocketResult ExecuteOperation(IOperation op)
socket.Write(b);
- result.Success = op.ReadResponse(socket);
+ var readResult = op.ReadResponse(socket);
+ if (readResult.Success)
+ {
+ result.Pass();
+ }
+ else
+ {
+ result.InnerResult = readResult;
+ result.Fail("Failed to read response, see inner result for details");
+ }
return result;
}
catch (IOException e)
View
3 Enyim.Caching/Memcached/OperationInterfaces.cs
@@ -2,13 +2,14 @@
using System.Net;
using System.Collections.Generic;
using Enyim.Caching.Memcached.Protocol;
+using Enyim.Caching.Memcached.Results;
namespace Enyim.Caching.Memcached
{
public interface IOperation
{
IList<ArraySegment<byte>> GetBuffer();
- bool ReadResponse(PooledSocket socket);
+ IOperationResult ReadResponse(PooledSocket socket);
int StatusCode { get; }
View
2 Enyim.Caching/Memcached/Protocol/Binary/BinaryNode.cs
@@ -52,7 +52,7 @@ private bool Auth(PooledSocket socket)
socket.Write(currentStep.GetBuffer());
- while (!currentStep.ReadResponse(socket))
+ while (!currentStep.ReadResponse(socket).Success)
{
// challenge-response authentication
if (currentStep.StatusCode == 0x21)
View
20 Enyim.Caching/Memcached/Protocol/Binary/BinarySingleItemOperation.cs
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Extensions;
namespace Enyim.Caching.Memcached.Protocol.Binary
{
@@ -16,15 +18,27 @@ protected internal override IList<ArraySegment<byte>> GetBuffer()
protected abstract bool ProcessResponse(BinaryResponse response);
- protected internal override bool ReadResponse(PooledSocket socket)
+ protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
var response = new BinaryResponse();
var retval = response.Read(socket);
-
+
this.Cas = response.CAS;
this.StatusCode = response.StatusCode;
- return retval & this.ProcessResponse(response);
+ var result = new BinaryOperationResult()
+ {
+ Success = retval,
+ Cas = this.Cas,
+ StatusCode = this.StatusCode
+ };
+
+ if (! this.ProcessResponse(response))
+ {
+ result.Fail("Failed to process response, see StatusCode or InnerResult for details");
+ }
+
+ return result;
}
protected internal override bool ReadResponseAsync(PooledSocket socket, Action<bool> next)
View
12 Enyim.Caching/Memcached/Protocol/Binary/FlushOperation.cs
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Extensions;
namespace Enyim.Caching.Memcached.Protocol.Binary
{
@@ -14,14 +16,20 @@ protected override BinaryRequest Build()
return request;
}
- protected internal override bool ReadResponse(PooledSocket socket)
+ protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
var response = new BinaryResponse();
var retval = response.Read(socket);
this.StatusCode = StatusCode;
+ var result = new BinaryOperationResult()
+ {
+ Success = retval,
+ StatusCode = this.StatusCode
+ };
- return retval;
+ result.PassOrFail(retval, "Failed to read response");
+ return result;
}
}
}
View
9 Enyim.Caching/Memcached/Protocol/Binary/MultiGetOperation.cs
@@ -3,6 +3,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Extensions;
namespace Enyim.Caching.Memcached.Protocol.Binary
{
@@ -145,10 +147,11 @@ private void StoreResult(BinaryResponse reader)
}
}
- protected internal override bool ReadResponse(PooledSocket socket)
+ protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
this.result = new Dictionary<string, CacheItem>();
this.Cas = new Dictionary<string, ulong>();
+ var result = new TextOperationResult();
var response = new BinaryResponse();
@@ -158,7 +161,7 @@ protected internal override bool ReadResponse(PooledSocket socket)
// found the noop, quit
if (response.CorrelationId == this.noopId)
- return true;
+ return result.Pass();
string key;
@@ -180,7 +183,7 @@ protected internal override bool ReadResponse(PooledSocket socket)
}
// finished reading but we did not find the NOOP
- return false;
+ return result.Fail("Found response with CorrelationId {0}, but no key is matching it.");
}
public Dictionary<string, CacheItem> Result
View
12 Enyim.Caching/Memcached/Protocol/Binary/SaslStep.cs
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Extensions;
namespace Enyim.Caching.Memcached.Protocol.Binary
{
@@ -13,7 +15,7 @@ protected SaslStep(ISaslAuthenticationProvider provider)
protected ISaslAuthenticationProvider Provider { get; private set; }
- protected internal override bool ReadResponse(PooledSocket socket)
+ protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
var response = new BinaryResponse();
@@ -22,7 +24,13 @@ protected internal override bool ReadResponse(PooledSocket socket)
this.StatusCode = response.StatusCode;
this.Data = response.Data.Array;
- return retval;
+ var result = new BinaryOperationResult
+ {
+ StatusCode = this.StatusCode
+ };
+
+ result.PassOrFail(retval, "Failed to read response");
+ return result;
}
public byte[] Data { get; private set; }
View
12 Enyim.Caching/Memcached/Protocol/Binary/StatsOperation.cs
@@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Text;
using System.Net;
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Extensions;
namespace Enyim.Caching.Memcached.Protocol.Binary
{
@@ -26,7 +28,7 @@ protected override BinaryRequest Build()
return request;
}
- protected internal override bool ReadResponse(PooledSocket socket)
+ protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
var response = new BinaryResponse();
var serverData = new Dictionary<string, string>();
@@ -46,7 +48,13 @@ protected internal override bool ReadResponse(PooledSocket socket)
this.result = serverData;
this.StatusCode = response.StatusCode;
- return retval;
+ var result = new BinaryOperationResult()
+ {
+ StatusCode = StatusCode
+ };
+
+ result.PassOrFail(retval, "Failed to read response");
+ return result;
}
Dictionary<string, string> IStatsOperation.Result
View
5 Enyim.Caching/Memcached/Protocol/Operation.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using Enyim.Caching.Memcached.Results;
namespace Enyim.Caching.Memcached.Protocol
{
@@ -13,15 +14,15 @@ public abstract class Operation : IOperation
protected Operation() { }
internal protected abstract IList<ArraySegment<byte>> GetBuffer();
- internal protected abstract bool ReadResponse(PooledSocket socket);
+ internal protected abstract IOperationResult ReadResponse(PooledSocket socket);
internal protected abstract bool ReadResponseAsync(PooledSocket socket, Action<bool> next);
IList<ArraySegment<byte>> IOperation.GetBuffer()
{
return this.GetBuffer();
}
- bool IOperation.ReadResponse(PooledSocket socket)
+ IOperationResult IOperation.ReadResponse(PooledSocket socket)
{
return this.ReadResponse(socket);
}
View
9 Enyim.Caching/Memcached/Protocol/Text/DeleteOperation.cs
@@ -1,4 +1,6 @@
using System;
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Extensions;
namespace Enyim.Caching.Memcached.Protocol.Text
{
@@ -13,9 +15,12 @@ protected internal override System.Collections.Generic.IList<ArraySegment<byte>>
return TextSocketHelper.GetCommandBuffer(command);
}
- protected internal override bool ReadResponse(PooledSocket socket)
+ protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
- return String.Compare(TextSocketHelper.ReadResponse(socket), "DELETED", StringComparison.Ordinal) == 0;
+ return new TextOperationResult
+ {
+ Success = String.Compare(TextSocketHelper.ReadResponse(socket), "DELETED", StringComparison.Ordinal) == 0
+ };
}
protected internal override bool ReadResponseAsync(PooledSocket socket, System.Action<bool> next)
View
7 Enyim.Caching/Memcached/Protocol/Text/FlushOperation.cs
@@ -1,4 +1,6 @@
using System.Collections.Generic;
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Extensions;
namespace Enyim.Caching.Memcached.Protocol.Text
{
@@ -11,11 +13,10 @@ protected internal override IList<System.ArraySegment<byte>> GetBuffer()
return TextSocketHelper.GetCommandBuffer("flush_all" + TextSocketHelper.CommandTerminator);
}
- protected internal override bool ReadResponse(PooledSocket socket)
+ protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
TextSocketHelper.ReadResponse(socket);
-
- return true;
+ return new TextOperationResult().Pass();
}
protected internal override bool ReadResponseAsync(PooledSocket socket, System.Action<bool> next)
View
9 Enyim.Caching/Memcached/Protocol/Text/GetOperation.cs
@@ -1,3 +1,5 @@
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Extensions;
namespace Enyim.Caching.Memcached.Protocol.Text
{
@@ -14,18 +16,19 @@ protected internal override System.Collections.Generic.IList<System.ArraySegment
return TextSocketHelper.GetCommandBuffer(command);
}
- protected internal override bool ReadResponse(PooledSocket socket)
+ protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
GetResponse r = GetHelper.ReadItem(socket);
+ var result = new TextOperationResult();
- if (r == null) return false;
+ if (r == null) return result.Fail("Failed to read response");
this.result = r.Item;
this.Cas = r.CasValue;
GetHelper.FinishCurrent(socket);
- return true;
+ return result.Pass();
}
CacheItem IGetOperation.Result
View
6 Enyim.Caching/Memcached/Protocol/Text/MultiGetOperation.cs
@@ -2,6 +2,8 @@
using System.Linq;
using System.Collections.Generic;
using System.Text;
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Extensions;
namespace Enyim.Caching.Memcached.Protocol.Text
{
@@ -22,7 +24,7 @@ protected internal override IList<ArraySegment<byte>> GetBuffer()
return TextSocketHelper.GetCommandBuffer(command);
}
- protected internal override bool ReadResponse(PooledSocket socket)
+ protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
var retval = new Dictionary<string, CacheItem>();
var cas = new Dictionary<string, ulong>();
@@ -51,7 +53,7 @@ protected internal override bool ReadResponse(PooledSocket socket)
this.result = retval;
this.Cas = cas;
- return true;
+ return new TextOperationResult().Pass();
}
Dictionary<string, CacheItem> IMultiGetOperation.Result
View
11 Enyim.Caching/Memcached/Protocol/Text/MutatorOperation.cs
@@ -2,6 +2,8 @@
using System.Globalization;
using System.Text;
using System.Collections.Generic;
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Extensions;
namespace Enyim.Caching.Memcached.Protocol.Text
{
@@ -34,15 +36,18 @@ protected internal override IList<ArraySegment<byte>> GetBuffer()
return TextSocketHelper.GetCommandBuffer(command);
}
- protected internal override bool ReadResponse(PooledSocket socket)
+ protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
string response = TextSocketHelper.ReadResponse(socket);
+ var result = new TextOperationResult();
//maybe we should throw an exception when the item is not found?
if (String.Compare(response, "NOT_FOUND", StringComparison.Ordinal) == 0)
- return false;
+ return result.Fail("Failed to read response. Item not found");
- return UInt64.TryParse(response, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, CultureInfo.InvariantCulture, out this.result);
+ result.Success =
+ UInt64.TryParse(response, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, CultureInfo.InvariantCulture, out this.result);
+ return result;
}
MutationMode IMutatorOperation.Mode
View
6 Enyim.Caching/Memcached/Protocol/Text/StatsOperation.cs
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Net;
+using Enyim.Caching.Memcached.Results;
+using Enyim.Caching.Memcached.Results.Extensions;
namespace Enyim.Caching.Memcached.Protocol.Text
{
@@ -25,7 +27,7 @@ protected internal override IList<ArraySegment<byte>> GetBuffer()
return TextSocketHelper.GetCommandBuffer(command);
}
- protected internal override bool ReadResponse(PooledSocket socket)
+ protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
var serverData = new Dictionary<string, string>();
@@ -62,7 +64,7 @@ protected internal override bool ReadResponse(PooledSocket socket)
this.result = serverData;
- return true;
+ return new TextOperationResult().Pass();
}
Dictionary<string, string> IStatsOperation.Result
View
8 Enyim.Caching/Memcached/Protocol/Text/StoreOperationBase.cs
@@ -2,6 +2,7 @@
using System.Globalization;
using System.Text;
using System.Collections.Generic;
+using Enyim.Caching.Memcached.Results;
namespace Enyim.Caching.Memcached.Protocol.Text
{
@@ -64,9 +65,12 @@ protected internal override System.Collections.Generic.IList<ArraySegment<byte>>
return buffers;
}
- protected internal override bool ReadResponse(PooledSocket socket)
+ protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
- return String.Compare(TextSocketHelper.ReadResponse(socket), "STORED", StringComparison.Ordinal) == 0;
+ return new TextOperationResult
+ {
+ Success = String.Compare(TextSocketHelper.ReadResponse(socket), "STORED", StringComparison.Ordinal) == 0
+ };
}
protected internal override bool ReadResponseAsync(PooledSocket socket, System.Action<bool> next)
View
34 Enyim.Caching/Memcached/Results/BinaryOperationResult.cs
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Enyim.Caching.Memcached.Results
+{
+ public class BinaryOperationResult : OperationResultBase, ICasOperationResult
+ {
+ public ulong Cas { get; set; }
+ }
+}
+
+#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
View
17 Enyim.Caching/Memcached/Results/Extensions/OperationResultExtensions.cs
@@ -47,6 +47,23 @@ public static void Copy(this IOperationResult source, IOperationResult target)
target.Exception = source.Exception;
target.StatusCode = source.StatusCode;
}
+
+ /// <summary>
+ /// Copy properties from one IOperationResult to another. Does not use reflection.
+ /// Ony LCD properties are copied
+ /// </summary>
+ /// <param name="target"></param>
+ public static void PassOrFail(this IOperationResult source, bool success, string message = "", Exception ex = null)
+ {
+ if (success)
+ {
+ Pass(source);
+ }
+ else
+ {
+ Fail(source, message, ex);
+ }
+ }
}
}
View
33 Enyim.Caching/Memcached/Results/TextOperationResult.cs
@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Enyim.Caching.Memcached.Results
+{
+ public class TextOperationResult : OperationResultBase
+ {
+ }
+}
+
+#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 2162ebd

Please sign in to comment.