|
2 | 2 | using Enyim.Caching.Memcached.Results.Extensions; |
3 | 3 | using Enyim.Caching.Memcached.Results.Helpers; |
4 | 4 | using Enyim.Caching.Memcached.Results; |
| 5 | +using System.Buffers.Binary; |
5 | 6 |
|
6 | 7 | namespace Enyim.Caching.Memcached.Protocol.Binary |
7 | 8 | { |
8 | | - public class MutatorOperation : BinarySingleItemOperation, IMutatorOperation |
9 | | - { |
10 | | - private ulong defaultValue; |
11 | | - private ulong delta; |
12 | | - private uint expires; |
13 | | - private MutationMode mode; |
14 | | - private ulong result; |
15 | | - |
16 | | - public MutatorOperation(MutationMode mode, string key, ulong defaultValue, ulong delta, uint expires) |
17 | | - : base(key) |
18 | | - { |
19 | | - if (delta < 0) throw new ArgumentOutOfRangeException("delta", "delta must be >= 0"); |
20 | | - |
21 | | - this.defaultValue = defaultValue; |
22 | | - this.delta = delta; |
23 | | - this.expires = expires; |
24 | | - this.mode = mode; |
25 | | - } |
26 | | - |
27 | | - protected unsafe void UpdateExtra(BinaryRequest request) |
28 | | - { |
29 | | - byte[] extra = new byte[20]; |
30 | | - |
31 | | - fixed (byte* buffer = extra) |
32 | | - { |
33 | | - BinaryConverter.EncodeUInt64(this.delta, buffer, 0); |
34 | | - |
35 | | - BinaryConverter.EncodeUInt64(this.defaultValue, buffer, 8); |
36 | | - BinaryConverter.EncodeUInt32(this.expires, buffer, 16); |
37 | | - } |
38 | | - |
39 | | - request.Extra = new ArraySegment<byte>(extra); |
40 | | - } |
41 | | - |
42 | | - protected override BinaryRequest Build() |
43 | | - { |
44 | | - var request = new BinaryRequest((OpCode)this.mode) |
45 | | - { |
46 | | - Key = this.Key, |
47 | | - Cas = this.Cas |
48 | | - }; |
49 | | - |
50 | | - this.UpdateExtra(request); |
51 | | - |
52 | | - return request; |
53 | | - } |
54 | | - |
55 | | - protected override IOperationResult ProcessResponse(BinaryResponse response) |
56 | | - { |
57 | | - var result = new BinaryOperationResult(); |
58 | | - var status = response.StatusCode; |
59 | | - this.StatusCode = status; |
60 | | - |
61 | | - if (status == 0) |
62 | | - { |
63 | | - var data = response.Data; |
64 | | - if (data.Count != 8) |
65 | | - return result.Fail("Result must be 8 bytes long, received: " + data.Count, new InvalidOperationException()); |
66 | | - |
67 | | - this.result = BinaryConverter.DecodeUInt64(data.Array, data.Offset); |
68 | | - |
69 | | - return result.Pass(); |
70 | | - } |
71 | | - |
72 | | - var message = ResultHelper.ProcessResponseData(response.Data); |
73 | | - return result.Fail(message); |
74 | | - } |
75 | | - |
76 | | - MutationMode IMutatorOperation.Mode |
77 | | - { |
78 | | - get { return this.mode; } |
79 | | - } |
80 | | - |
81 | | - ulong IMutatorOperation.Result |
82 | | - { |
83 | | - get { return this.result; } |
84 | | - } |
85 | | - } |
| 9 | + public class MutatorOperation : BinarySingleItemOperation, IMutatorOperation |
| 10 | + { |
| 11 | + private readonly ulong defaultValue; |
| 12 | + private readonly ulong delta; |
| 13 | + private readonly uint expires; |
| 14 | + private readonly MutationMode mode; |
| 15 | + private ulong result; |
| 16 | + |
| 17 | + public MutatorOperation(MutationMode mode, string key, ulong defaultValue, ulong delta, uint expires) |
| 18 | + : base(key) |
| 19 | + { |
| 20 | + if (delta < 0) throw new ArgumentOutOfRangeException("delta", "delta must be >= 0"); |
| 21 | + |
| 22 | + this.defaultValue = defaultValue; |
| 23 | + this.delta = delta; |
| 24 | + this.expires = expires; |
| 25 | + this.mode = mode; |
| 26 | + } |
| 27 | + |
| 28 | + protected unsafe void UpdateExtra(BinaryRequest request) |
| 29 | + { |
| 30 | + if (mode == MutationMode.Touch) |
| 31 | + { |
| 32 | + Span<byte> extra = stackalloc byte[4]; |
| 33 | + BinaryPrimitives.WriteUInt32BigEndian(extra, this.expires); |
| 34 | + request.Extra = new ArraySegment<byte>(extra.ToArray()); |
| 35 | + } |
| 36 | + else |
| 37 | + { |
| 38 | + byte[] extra = new byte[20]; |
| 39 | + |
| 40 | + fixed (byte* buffer = extra) |
| 41 | + { |
| 42 | + BinaryConverter.EncodeUInt64(this.delta, buffer, 0); |
| 43 | + |
| 44 | + BinaryConverter.EncodeUInt64(this.defaultValue, buffer, 8); |
| 45 | + BinaryConverter.EncodeUInt32(this.expires, buffer, 16); |
| 46 | + } |
| 47 | + |
| 48 | + request.Extra = new ArraySegment<byte>(extra); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + protected override BinaryRequest Build() |
| 53 | + { |
| 54 | + var request = new BinaryRequest((OpCode)this.mode) |
| 55 | + { |
| 56 | + Key = this.Key, |
| 57 | + Cas = this.Cas |
| 58 | + }; |
| 59 | + |
| 60 | + this.UpdateExtra(request); |
| 61 | + |
| 62 | + return request; |
| 63 | + } |
| 64 | + |
| 65 | + protected override IOperationResult ProcessResponse(BinaryResponse response) |
| 66 | + { |
| 67 | + var result = new BinaryOperationResult(); |
| 68 | + var status = response.StatusCode; |
| 69 | + this.StatusCode = status; |
| 70 | + |
| 71 | + if (status == 0) |
| 72 | + { |
| 73 | + if (mode != MutationMode.Touch) |
| 74 | + { |
| 75 | + var data = response.Data; |
| 76 | + if (data.Count != 8) |
| 77 | + return result.Fail("Result must be 8 bytes long, received: " + data.Count, new InvalidOperationException()); |
| 78 | + |
| 79 | + this.result = BinaryConverter.DecodeUInt64(data.Array, data.Offset); |
| 80 | + } |
| 81 | + |
| 82 | + return result.Pass(); |
| 83 | + } |
| 84 | + |
| 85 | + var message = ResultHelper.ProcessResponseData(response.Data); |
| 86 | + return result.Fail(message); |
| 87 | + } |
| 88 | + |
| 89 | + MutationMode IMutatorOperation.Mode |
| 90 | + { |
| 91 | + get { return this.mode; } |
| 92 | + } |
| 93 | + |
| 94 | + ulong IMutatorOperation.Result |
| 95 | + { |
| 96 | + get { return this.result; } |
| 97 | + } |
| 98 | + } |
86 | 99 | } |
87 | 100 |
|
88 | 101 | #region [ License information ] |
89 | 102 | /* ************************************************************ |
90 | 103 | * |
91 | | - * Copyright (c) 2010 Attila Kiskó, enyim.com |
| 104 | + * Copyright (c) 2010 Attila Kisk? enyim.com |
92 | 105 | * |
93 | 106 | * Licensed under the Apache License, Version 2.0 (the "License"); |
94 | 107 | * you may not use this file except in compliance with the License. |
|
0 commit comments