1fecbf950Sjeffrymorrisusing System;
22502d6d7SBrant Burnettusing Couchbase.Core.IO.Converters;
3fecbf950Sjeffrymorris
4e308481bSJeff Morrisnamespace Couchbase.Core.IO.Operations.EnhancedDurability
5fecbf950Sjeffrymorris{
6fecbf950Sjeffrymorris    internal class ObserveSeqno : OperationBase<ObserveSeqnoResponse>
7fecbf950Sjeffrymorris    {
8fecbf950Sjeffrymorris        /// <summary>
943cf091cSMike Goldsmith        /// Gets the operation code for <see cref="OpCode"/>
10fecbf950Sjeffrymorris        /// </summary>
11fecbf950Sjeffrymorris        /// <value>
12fecbf950Sjeffrymorris        /// The operation code.
13fecbf950Sjeffrymorris        /// </value>
1443cf091cSMike Goldsmith        public override OpCode OpCode => OpCode.ObserveSeqNo;
15fecbf950Sjeffrymorris
161a28374eSBrant Burnett        protected override void WriteExtras(OperationBuilder builder)
17fecbf950Sjeffrymorris        {
1821843b11SBrant Burnett        }
19fecbf950Sjeffrymorris
201a28374eSBrant Burnett        protected override void WriteFramingExtras(OperationBuilder builder)
2121843b11SBrant Burnett        {
2221843b11SBrant Burnett        }
23cd4ac9ffSBrant Burnett
241a28374eSBrant Burnett        protected override void WriteKey(OperationBuilder builder)
2521843b11SBrant Burnett        {
26fecbf950Sjeffrymorris        }
27fecbf950Sjeffrymorris        /// <summary>
28fecbf950Sjeffrymorris        /// Gets the value of the memecached response packet and converts it to a <see cref="ObserveSeqnoResponse"/> instance.
29fecbf950Sjeffrymorris        /// </summary>
30fecbf950Sjeffrymorris        /// <returns></returns>
31fecbf950Sjeffrymorris        public override ObserveSeqnoResponse GetValue()
32fecbf950Sjeffrymorris        {
33fecbf950Sjeffrymorris            var result = default(ObserveSeqnoResponse);
342d18b01eSjeffrymorris            if (Data.Length > 0)
35fecbf950Sjeffrymorris            {
36fecbf950Sjeffrymorris                try
37fecbf950Sjeffrymorris                {
3808fea4a2SBrant Burnett                    var buffer = Data.Span.Slice(Header.BodyOffset);
39fecbf950Sjeffrymorris
404e4f482dSBrant Burnett                    var isHardFailover = buffer[0] == 1;
41fecbf950Sjeffrymorris                    if (isHardFailover)
42fecbf950Sjeffrymorris                    {
43fecbf950Sjeffrymorris                        result = new ObserveSeqnoResponse
44fecbf950Sjeffrymorris                        {
4543cf091cSMike Goldsmith                            IsHardFailover = true,
469be6fa4eSBrant Burnett                            VBucketId = ByteConverter.ToInt16(buffer.Slice(1)),
479be6fa4eSBrant Burnett                            VBucketUuid = ByteConverter.ToInt64(buffer.Slice(3)),
489be6fa4eSBrant Burnett                            LastPersistedSeqno = ByteConverter.ToInt64(buffer.Slice(11)),
499be6fa4eSBrant Burnett                            CurrentSeqno = ByteConverter.ToInt64(buffer.Slice(19)),
509be6fa4eSBrant Burnett                            OldVBucketUuid = ByteConverter.ToInt64(buffer.Slice(27)),
519be6fa4eSBrant Burnett                            LastSeqnoReceived = ByteConverter.ToInt64(buffer.Slice(35))
52fecbf950Sjeffrymorris                        };
53fecbf950Sjeffrymorris                    }
54fecbf950Sjeffrymorris                    else
55fecbf950Sjeffrymorris                    {
56fecbf950Sjeffrymorris                        result = new ObserveSeqnoResponse
57fecbf950Sjeffrymorris                        {
5843cf091cSMike Goldsmith                            IsHardFailover = false,
599be6fa4eSBrant Burnett                            VBucketId = ByteConverter.ToInt16(buffer.Slice(1)),
609be6fa4eSBrant Burnett                            VBucketUuid = ByteConverter.ToInt64(buffer.Slice(3)),
619be6fa4eSBrant Burnett                            LastPersistedSeqno = ByteConverter.ToInt64(buffer.Slice(11)),
629be6fa4eSBrant Burnett                            CurrentSeqno = ByteConverter.ToInt64(buffer.Slice(19)),
63fecbf950Sjeffrymorris                        };
64fecbf950Sjeffrymorris                    }
65fecbf950Sjeffrymorris                }
66fecbf950Sjeffrymorris                catch (Exception e)
67fecbf950Sjeffrymorris                {
68fecbf950Sjeffrymorris                    Exception = e;
69fecbf950Sjeffrymorris                    HandleClientError(e.Message, ResponseStatus.ClientFailure);
70fecbf950Sjeffrymorris                }
71fecbf950Sjeffrymorris            }
72fecbf950Sjeffrymorris            return result;
73fecbf950Sjeffrymorris        }
74fecbf950Sjeffrymorris    }
75fecbf950Sjeffrymorris}
76fecbf950Sjeffrymorris
77fecbf950Sjeffrymorris#region [ License information          ]
78fecbf950Sjeffrymorris
79fecbf950Sjeffrymorris/* ************************************************************
80fecbf950Sjeffrymorris *
81fecbf950Sjeffrymorris *    @author Couchbase <info@couchbase.com>
82fecbf950Sjeffrymorris *    @copyright 2015 Couchbase, Inc.
83fecbf950Sjeffrymorris *
84fecbf950Sjeffrymorris *    Licensed under the Apache License, Version 2.0 (the "License");
85fecbf950Sjeffrymorris *    you may not use this file except in compliance with the License.
86fecbf950Sjeffrymorris *    You may obtain a copy of the License at
87fecbf950Sjeffrymorris *
88fecbf950Sjeffrymorris *        http://www.apache.org/licenses/LICENSE-2.0
89fecbf950Sjeffrymorris *
90fecbf950Sjeffrymorris *    Unless required by applicable law or agreed to in writing, software
91fecbf950Sjeffrymorris *    distributed under the License is distributed on an "AS IS" BASIS,
92fecbf950Sjeffrymorris *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
93fecbf950Sjeffrymorris *    See the License for the specific language governing permissions and
94fecbf950Sjeffrymorris *    limitations under the License.
95fecbf950Sjeffrymorris *
96fecbf950Sjeffrymorris * ************************************************************/
97fecbf950Sjeffrymorris
98fecbf950Sjeffrymorris#endregion
99