Skip to content
This repository has been archived by the owner on Jul 7, 2019. It is now read-only.

Commit

Permalink
Updated ProcessResponse of BinarySingleItemOperation to return IOpera…
Browse files Browse the repository at this point in the history
…tionResult.
  • Loading branch information
jzablocki committed Apr 23, 2012
1 parent 5f1adc5 commit ed9bdd4
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 19 deletions.
1 change: 1 addition & 0 deletions Enyim.Caching/Enyim.Caching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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");
}

Expand Down
5 changes: 3 additions & 2 deletions Enyim.Caching/Memcached/Protocol/Binary/ConcatOperation.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using Enyim.Caching.Memcached.Results;

namespace Enyim.Caching.Memcached.Protocol.Binary
{
Expand Down Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions Enyim.Caching/Memcached/Protocol/Binary/DeleteOperation.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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);
}
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions Enyim.Caching/Memcached/Protocol/Binary/GetOperation.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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
Expand Down
13 changes: 9 additions & 4 deletions Enyim.Caching/Memcached/Protocol/Binary/MutatorOperation.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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
Expand Down
19 changes: 15 additions & 4 deletions Enyim.Caching/Memcached/Protocol/Binary/StoreOperation.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
53 changes: 53 additions & 0 deletions Enyim.Caching/Memcached/Results/Helpers/ResultHelper.cs
Original file line number Diff line number Diff line change
@@ -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.