protected override INodeStatsOperationResult CreateResult(BinaryResponse response)
        {
            if (stats == null)
            {
                stats = new Dictionary <string, string>();
            }

            var data = response.Data;

            // if empty key (last response packet response)
            // or if error
            // return the response object to break the loop and let the node process the next op.
            if (response.KeyLength == 0 || !response.Success)
            {
                return(new NodeStatsOperationResult {
                    Value = stats
                }.WithResponse(response));
            }

            // decode stat key/value
            var key   = NetworkOrderConverter.DecodeKey(data.Array, data.Offset, response.KeyLength);
            var value = NetworkOrderConverter.DecodeKey(data.Array, data.Offset + response.KeyLength, data.Count - response.KeyLength);

            stats[key] = value;

            return(null);            // we expect more response packets
        }
Esempio n. 2
0
        /// <summary>
        /// Returns true if further IO is pending. (ie. body must be read)
        /// </summary>
        /// <param name="header"></param>
        /// <param name="bodyLength"></param>
        /// <param name="extraLength"></param>
        /// <returns></returns>
        private bool ProcessHeader(byte[] header, out int bodyLength)
        {
#if DEBUG
            if (header[Protocol.HEADER_INDEX_MAGIC] != Protocol.ResponseMagic)
            {
                throw new InvalidOperationException("Expected magic value " + Protocol.ResponseMagic + ", received: " + header[Protocol.HEADER_INDEX_MAGIC]);
            }
#endif

            // TODO test if unsafe array gives a perf boost
            OpCode        = header[Protocol.HEADER_INDEX_OPCODE];
            KeyLength     = NetworkOrderConverter.DecodeUInt16(header, Protocol.HEADER_INDEX_KEY);
            DataType      = header[Protocol.HEADER_INDEX_DATATYPE];
            StatusCode    = NetworkOrderConverter.DecodeUInt16(header, Protocol.HEADER_INDEX_STATUS);
            CorrelationId = unchecked ((uint)NetworkOrderConverter.DecodeInt32(header, Protocol.HEADER_INDEX_OPAQUE));
            CAS           = NetworkOrderConverter.DecodeUInt64(header, Protocol.HEADER_INDEX_CAS);

            bodyLength = NetworkOrderConverter.DecodeInt32(header, Protocol.HEADER_INDEX_BODY);

            if (bodyLength > 0)
            {
                var extraLength = header[Protocol.HEADER_INDEX_EXTRA];

                data = allocator.Take(bodyLength);

                Extra = new ArraySegment <byte>(data, 0, extraLength);
                Data  = new ArraySegment <byte>(data, extraLength, bodyLength - extraLength);

                return(true);
            }

            return(false);
        }
        protected override BinaryRequest CreateRequest()
        {
            var request = new BinaryRequest(Allocator, OpCode.Stat);

            if (!String.IsNullOrEmpty(type))
            {
                request.Key = NetworkOrderConverter.EncodeKey(Allocator, type);
            }

            return(request);
        }
            public StoreRequest(IBufferAllocator allocator, OpCode operation, CacheItem value, uint expires)
                : base(allocator, operation, ExtraLength)
            {
                var extra  = Extra.Array;
                var offset = Extra.Offset;

                // store the extra values
                NetworkOrderConverter.EncodeUInt32(value.Flags, extra, offset);
                NetworkOrderConverter.EncodeUInt32(expires, extra, offset + 4);

                Data = value.Segment.AsArraySegment();
            }
        protected override BinaryRequest CreateRequest()
        {
            var request = new BinaryRequest(Allocator, OpCode.Touch, ExtraLength)
            {
                Key = Key,
                Cas = Cas
            };

            // store expiration in Extra
            var extra = request.Extra;

            NetworkOrderConverter.EncodeUInt32(Expires, extra.Array, extra.Offset);

            return(request);
        }
Esempio n. 6
0
        protected override BinaryRequest CreateRequest()
        {
            var request = new BinaryRequest(Allocator, operations[(int)Mode], ExtraLength)
            {
                Key = Key, Cas = Cas
            };

            // store the extra values
            var extra  = request.Extra.Array;
            var offset = request.Extra.Offset;

            NetworkOrderConverter.EncodeUInt64(Delta, extra, offset);
            NetworkOrderConverter.EncodeUInt64(DefaultValue, extra, offset + 8);
            NetworkOrderConverter.EncodeUInt32(Expires, extra, offset + 16);

            return(request);
        }
Esempio n. 7
0
        protected override IGetOperationResult CreateResult(BinaryResponse response)
        {
            var retval = new GetOperationResult();

            if (response == null)
            {
                return(retval.NotFound(this));
            }

            if (response.StatusCode == 0)
            {
                var flags = NetworkOrderConverter.DecodeUInt32(response.Extra.Array, 0);

                retval.Value = new CacheItem((uint)flags, response.Data.Clone());
            }

            return(retval.WithResponse(response));
        }
        protected override IGetOperationResult CreateResult(BinaryResponse response)
        {
            var retval = new GetOperationResult();

            if (response == null)
            {
                return(retval.NotFound(this));
            }

            if (response.StatusCode == 0)
            {
                var flags = NetworkOrderConverter.DecodeInt32(response.Extra.Array, 0);
                // HACK
                var copy = new PooledSegment(Allocator, response.Data.Count);
                Buffer.BlockCopy(response.Data.Array, response.Data.Offset, copy.Array, 0, copy.Count);
                retval.Value = new CacheItem((uint)flags, copy);
            }

            return(retval.WithResponse(response));
        }
        protected override BinaryRequest CreateRequest()
        {
            var request = new BinaryRequest(Allocator, GetOpCode(Mode), ExtraLength)
            {
                Key  = Key,
                Cas  = Cas,
                Data = Value.Data.AsArraySegment()
            };

            var extra       = request.Extra;
            var extraArray  = extra.Array;
            var extraOffset = extra.Offset;

            Debug.Assert(extraArray != null);

            // store the extra values
            NetworkOrderConverter.EncodeUInt32(Value.Flags, extraArray, extraOffset);
            NetworkOrderConverter.EncodeUInt32(Expires, extraArray, extraOffset + 4);

            return(request);
        }
        protected override IMutateOperationResult CreateResult(BinaryResponse response)
        {
            var retval = new MutateOperationResult();

            if (response == null)
            {
                return(retval.NotFound(this));
            }

            if (response.Success)
            {
                var data = response.Data;
                if (data.Count != ResultLength)
                {
                    return(retval.Failed(this, new InvalidOperationException("Result must be " + ResultLength + " bytes long, received: " + data.Count)));
                }

                retval.Value = NetworkOrderConverter.DecodeUInt64(data.Array, data.Offset);
            }

            return(retval.WithResponse(response));
        }
Esempio n. 11
0
        /// <summary>
        /// Returns true if further IO is pending. (ie. body must be read)
        /// </summary>
        /// <param name="header"></param>
        /// <param name="bodyLength"></param>
        /// <param name="extraLength"></param>
        /// <returns></returns>
        private bool ProcessHeader(byte[] header)
        {
            if (header[Protocol.HEADER_INDEX_MAGIC] != Protocol.ResponseMagic)
            {
                throw new InvalidOperationException($"Expected magic value {Protocol.ResponseMagic}, received: {header[Protocol.HEADER_INDEX_MAGIC]}");
            }

            // TODO test if unsafe array gives a perf boost
            OpCode        = header[Protocol.HEADER_INDEX_OPCODE];
            KeyLength     = NetworkOrderConverter.DecodeUInt16(header, Protocol.HEADER_INDEX_KEY);
            DataType      = header[Protocol.HEADER_INDEX_DATATYPE];
            StatusCode    = NetworkOrderConverter.DecodeUInt16(header, Protocol.HEADER_INDEX_STATUS);
            CorrelationId = NetworkOrderConverter.DecodeUInt32(header, Protocol.HEADER_INDEX_OPAQUE);
            CAS           = NetworkOrderConverter.DecodeUInt64(header, Protocol.HEADER_INDEX_CAS);

            var bodyLength = (int)NetworkOrderConverter.DecodeUInt32(header, Protocol.HEADER_INDEX_BODY_LENGTH);

            if (bodyLength > 0)
            {
                var extraLength = header[Protocol.HEADER_INDEX_EXTRA];
                if (extraLength > 0)
                {
                    Extra = ByteBuffer.Allocate(allocator, extraLength);
                }

                var dataLength = bodyLength - extraLength;
                if (dataLength > 0)
                {
                    Data = ByteBuffer.Allocate(allocator, dataLength);
                }

                return(true);
            }

            return(false);
        }