Esempio n. 1
0
        public static RedisResult <RedisGeoPosition[]> ToGeoPosition(RedisArray array)
        {
            if (array == null)
            {
                return(new RedisResult <RedisGeoPosition[]>(new RedisGeoPosition[0]));
            }

            var items = array.Value;

            if (items == null)
            {
                return(new RedisResult <RedisGeoPosition[]>(new RedisGeoPosition[0]));
            }

            var count = items.Count;

            var result = new RedisGeoPosition[count];

            if (count > 0)
            {
                for (var i = 0; i < count; i++)
                {
                    result[i] = ToGeoPositionItem((RedisArray)items[i]);
                }
            }

            return(new RedisResult <RedisGeoPosition[]>(result));
        }
Esempio n. 2
0
        public static RedisClusterSlotInfo[] Parse(RedisArray parts)
        {
            if (!ReferenceEquals(parts, null))
            {
                var items = parts.Value;
                if (items != null)
                {
                    var count = items.Count;
                    if (count > 0)
                    {
                        var result = new List <RedisClusterSlotInfo>(count);
                        for (var i = 0; i < count; i++)
                        {
                            var item = items[i];
                            if (!ReferenceEquals(item, null))
                            {
                                var info = item as RedisArray;
                                if (!ReferenceEquals(info, null))
                                {
                                    result.Add(new RedisClusterSlotInfo(info));
                                }
                            }
                        }

                        return(result.ToArray());
                    }
                }
            }
            return(null);
        }
Esempio n. 3
0
        private static string[] ParseCommandInfo(RedisArray array)
        {
            if (array != null)
            {
                var items = array.Value;
                if (items != null)
                {
                    var count = items.Count;
                    if (count > 0)
                    {
                        var result = new List <string>(count);

                        foreach (var item in items)
                        {
                            if (item != null &&
                                item.Type == RedisResultType.Bytes)
                            {
                                result.Add(((RedisBytes)item).Value.ToUTF8String());
                            }
                        }
                        return(result.ToArray());
                    }
                }
            }
            return(null);
        }
Esempio n. 4
0
        public static RedisResult <RedisGeoRadiusResult[]> ToGeoRadiusArray(RedisArray array)
        {
            if (array == null)
            {
                return(new RedisResult <RedisGeoRadiusResult[]>(new RedisGeoRadiusResult[0]));
            }

            var items = array.Value;

            if (items == null)
            {
                return(new RedisResult <RedisGeoRadiusResult[]>(new RedisGeoRadiusResult[0]));
            }

            var count = items.Count;

            if (count == 0)
            {
                return(new RedisResult <RedisGeoRadiusResult[]>(new RedisGeoRadiusResult[0]));
            }

            var list = new List <RedisGeoRadiusResult>(count);

            for (var i = 0; i < count; i++)
            {
                list.Add(ToGeoRadiusResult(items[i]));
            }

            return(new RedisResult <RedisGeoRadiusResult[]>(list.ToArray()));
        }
Esempio n. 5
0
        public static RedisMasterRoleSlaveInfo Parse(RedisArray array)
        {
            if (!ReferenceEquals(array, null))
            {
                var list = array.Value;
                if (list != null)
                {
                    var count = list.Count;
                    if (count > 0)
                    {
                        var result = new RedisMasterRoleSlaveInfo();

                        var item = list[0];
                        if (!ReferenceEquals(item, null) && item.Type == RedisResultType.Bytes)
                        {
                            result.IPAddress = ((RedisBytes)item).Value.ToUTF8String();
                        }

                        if (count > 1)
                        {
                            item = list[1];
                            if (!ReferenceEquals(item, null) && item.Type == RedisResultType.Bytes)
                            {
                                var port = ((RedisBytes)item).Value.ToUTF8String();
                                if (!port.IsEmpty())
                                {
                                    int i;
                                    if (port.TryParse(out i))
                                    {
                                        result.Port = i;
                                    }
                                }
                            }

                            if (count > 2)
                            {
                                item = list[2];
                                if (!ReferenceEquals(item, null) && item.Type == RedisResultType.Bytes)
                                {
                                    var offset = ((RedisBytes)item).Value.ToUTF8String();
                                    if (!offset.IsEmpty())
                                    {
                                        long l;
                                        if (offset.TryParse(out l))
                                        {
                                            result.ReplicationOffset = l;
                                        }
                                    }
                                }
                            }
                        }

                        return(result);
                    }
                }
            }
            return(null);
        }
Esempio n. 6
0
        }                                                                                  // 0

        #endregion Properties

        #region Methods

        public static RedisSentinelNodeInfo[] ParseInfoResponse(RedisArray array)
        {
            if (!ReferenceEquals(array, null))
            {
                var items = array.Value;
                if (items != null)
                {
                    var count = items.Count;
                    if (count > 0)
                    {
                        var isInfoArray = true;
                        for (var i = 0; i < count; i++)
                        {
                            var item = items[i];
                            if (!ReferenceEquals(item, null) &&
                                item.Type != RedisResultType.Array)
                            {
                                isInfoArray = false;
                                break;
                            }
                        }

                        if (!isInfoArray)
                        {
                            var info = new RedisSentinelNodeInfo(array);
                            return(new[] { info });
                        }

                        var list = new List <RedisSentinelNodeInfo>(count);
                        for (var i = 0; i < count; i++)
                        {
                            var item = items[i];
                            if (!ReferenceEquals(item, null) && item.Type == RedisResultType.Array)
                            {
                                var info = new RedisSentinelNodeInfo((RedisArray)item);
                                list.Add(info);
                            }
                        }

                        if (list.Count > 0)
                        {
                            return(list.ToArray());
                        }
                    }
                }
            }
            return(null);
        }
        public RedisMultiInteger ScriptExists(RedisParam sha1, params RedisParam[] sha1s)
        {
            if (sha1.IsEmpty)
            {
                throw new ArgumentNullException("sha1");
            }

            RedisArray response = null;

            if (sha1s.Length == 0)
            {
                response = ExpectArray(new RedisCommand(DbIndex, RedisCommandList.Script, RedisCommandList.Exists, sha1));
            }
            else
            {
                var parameters = RedisCommandList.Exists
                                 .Join(sha1)
                                 .Join(sha1s);

                response = ExpectArray(new RedisCommand(DbIndex, RedisCommandList.Script, RedisCommandList.Exists, sha1));
            }

            var resultLength = sha1.Length + 1;
            var result       = new long[resultLength];

            if (response != null)
            {
                var items = response.Value;
                if (items != null)
                {
                    var responseLength = items.Count;

                    for (var i = 0; i < resultLength && i < responseLength; i++)
                    {
                        var item = items[i];
                        if (item != null &&
                            item.Type == RedisResultType.Integer)
                        {
                            result[i] = ((RedisInteger)item).Value;
                        }
                    }
                }
            }
            return(result);
        }
Esempio n. 8
0
        public static RedisSlowLogInfo[] ToSlowLogInfo(RedisArray array)
        {
            if (array != null)
            {
                var children = array.Value;
                if (children != null && children.Count > 0)
                {
                    var result = new List <RedisSlowLogInfo>(children.Count);

                    foreach (var child in children)
                    {
                        if (child != null &&
                            child.Type == RedisResultType.Array)
                        {
                            var items = ((RedisArray)child).Value;
                            if (items != null && items.Count >= 4)
                            {
                                var id       = ((RedisInteger)items[0]).Value;
                                var time     = ((RedisInteger)items[0]).Value.FromUnixTimeStamp();
                                var duration = TimeSpan.FromMilliseconds(((RedisInteger)items[0]).Value);

                                var commandInfo = ParseCommandInfo((RedisArray)items[3]);

                                string clientInfo = null;
                                string clientName = null;

                                if (items.Count > 4)
                                {
                                    clientInfo = ((RedisBytes)items[4]).Value.ToUTF8String();
                                    if (items.Count > 5)
                                    {
                                        clientName = ((RedisBytes)items[5]).Value.ToUTF8String();
                                    }
                                }

                                result.Add(new RedisSlowLogInfo(id, time, duration, clientInfo, clientName, commandInfo));
                            }
                        }
                    }

                    return(result.ToArray());
                }
            }
            return(null);
        }
        protected override void ParseInfo(RedisArray array)
        {
            if (!ReferenceEquals(array, null))
            {
                var list = array.Value;
                if (list != null)
                {
                    var count = list.Count;
                    if (count > 1)
                    {
                        var item = list[1];
                        if (!ReferenceEquals(item, null) && item.Type == RedisResultType.Array)
                        {
                            var items = ((RedisArray)item).Value;
                            if (items != null)
                            {
                                var itemCount = items.Count;
                                if (itemCount > 0)
                                {
                                    var masterNames = new List <string>(itemCount);
                                    for (var i = 0; i < itemCount; i++)
                                    {
                                        item = items[i];
                                        if (!ReferenceEquals(item, null) && item.Type == RedisResultType.Bytes)
                                        {
                                            var masterName = ((RedisBytes)item).Value.ToUTF8String();
                                            if (!masterName.IsEmpty())
                                            {
                                                masterNames.Add(masterName);
                                            }
                                        }
                                    }

                                    if (masterNames.Count > 0)
                                    {
                                        MasterNames = masterNames.ToArray();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 10
0
        public static RedisRoleInfo Parse(RedisArray array)
        {
            if (!ReferenceEquals(array, null))
            {
                var list = array.Value;
                if (list != null)
                {
                    var count = list.Count;
                    if (count > 0)
                    {
                        var item = list[0];
                        if (!ReferenceEquals(item, null) && item.Type == RedisResultType.Bytes)
                        {
                            var roleStr = ((RedisBytes)item).Value.ToUTF8String();
                            if (!roleStr.IsEmpty())
                            {
                                roleStr = roleStr.ToLowerInvariant();
                                var role = roleStr.ToRedisRole();

                                switch (role)
                                {
                                case RedisRole.Master:
                                case RedisRole.Slave:
                                case RedisRole.Sentinel:
                                {
                                    var result = new RedisSentinelRoleInfo(roleStr);
                                    result.ParseInfo(array);
                                    return(result);
                                }

                                default:
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            return(null);
        }
Esempio n. 11
0
        protected override void ParseInfo(RedisArray array)
        {
            if (!ReferenceEquals(array, null))
            {
                var list = array.Value;
                if (list != null)
                {
                    var count = list.Count;
                    if (count > 1)
                    {
                        var item = list[1];
                        if (!ReferenceEquals(item, null) && item.Type == RedisResultType.Integer)
                        {
                            ReplicationOffset = ((RedisInteger)item).Value;
                        }

                        if (count > 2)
                        {
                            var slaves = new List <RedisMasterRoleSlaveInfo>(count - 2);
                            for (var i = 2; i < count; i++)
                            {
                                var slaveInfo = RedisMasterRoleSlaveInfo.Parse((RedisArray)list[i]);
                                if (slaveInfo != null)
                                {
                                    slaves.Add(slaveInfo);
                                }
                            }

                            if (slaves.Count > 0)
                            {
                                Slaves = slaves.ToArray();
                            }
                        }
                    }
                }
            }
        }
        }                                                                                            // 30000

        #endregion Properties

        #region Methods

        protected virtual void Parse(RedisArray array)
        {
            if (!ReferenceEquals(array, null))
            {
                var items = array.Value;
                if (items != null)
                {
                    var count = items.Count;
                    if (count > 0)
                    {
                        for (var i = 0; i < count; i += 2)
                        {
                            var item = items[i];
                            if (!ReferenceEquals(item, null) && item.Type == RedisResultType.Bytes)
                            {
                                var key = ((RedisBytes)item).Value.ToUTF8String();
                                if (!key.IsEmpty())
                                {
                                    var value = (string)null;
                                    if (i < count - 1)
                                    {
                                        item = items[i + 1];
                                        if (!ReferenceEquals(item, null) && item.Type == RedisResultType.Bytes)
                                        {
                                            value = ((RedisBytes)item).Value.ToUTF8String();
                                        }
                                    }

                                    base[key] = value;
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 13
0
 private static RedisGeoPosition ToGeoPositionItem(RedisArray array)
 {
     if (array != null)
     {
         var items = array.Value;
         if (items != null && items.Count >= 2)
         {
             var item = items[0] as RedisBytes;
             if (!ReferenceEquals(item, null))
             {
                 var data = item.Value;
                 if (!data.IsEmpty())
                 {
                     double longitude;
                     if (double.TryParse(data.ToUTF8String(), out longitude))
                     {
                         item = items[1] as RedisBytes;
                         if (!ReferenceEquals(item, null))
                         {
                             data = item.Value;
                             if (!data.IsEmpty())
                             {
                                 double latitude;
                                 if (double.TryParse(data.ToUTF8String(), out latitude))
                                 {
                                     return(new RedisGeoPosition(longitude, latitude));
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return(RedisGeoPosition.Empty);
 }
Esempio n. 14
0
        public void TryParse(RedisBufferContext context)
        {
            var buffer = context.Buffer;

            if (buffer != null)
            {
                var bufferLen = buffer.Length;

                var contextLen    = context.Length;
                var contextOffset = context.Offset;

                if (bufferLen == 0 || contextLen <= 0 ||
                    contextOffset < 0 || contextOffset >= bufferLen)
                {
                    return;
                }

                context.Length = Math.Min(contextLen, bufferLen - contextOffset);

                var sign = buffer[contextOffset];
                switch (sign)
                {
                case BulkStringSign:
                {
                    context.Offset++;
                    context.Length--;

                    context.ResultType = RedisRawObjectType.BulkString;

                    byte[] bytes;
                    if (!TryParseLine(context, out bytes))
                    {
                        context.Offset--;
                        context.Length++;
                    }
                    else
                    {
                        long dataSize;
                        if (!bytes.TryParse(out dataSize))
                        {
                            throw new RedisException("Invalid bulk string size", RedisErrorCode.CorruptResponse);
                        }

                        if (dataSize > RedisConstants.MinusOne)
                        {
                            var iSize = checked ((int)dataSize);

                            if (context.Length >= iSize + CRLFLength)
                            {
                                var crPos = context.Offset + iSize;
                                if (!(buffer[crPos] == CR && buffer[crPos + 1] == LF))
                                {
                                    throw new RedisException("Invalid bulk string data termination", RedisErrorCode.CorruptResponse);
                                }

                                context.Completed = true;
                                if (iSize == 0)
                                {
                                    context.Result = new RedisBytes(new byte[0]);
                                }
                                else
                                {
                                    var result = new byte[iSize];
                                    Array.Copy(buffer, context.Offset, result, 0, iSize);

                                    context.Result = new RedisBytes(result);
                                }

                                context.Offset += iSize + CRLFLength;
                                context.Length -= iSize + CRLFLength;
                            }
                        }
                        else
                        {
                            if (dataSize < RedisConstants.MinusOne)
                            {
                                throw new RedisException("Invalid bulk string size", RedisErrorCode.CorruptResponse);
                            }

                            context.Completed = true;
                            context.Result    = new RedisBytes(null);
                        }
                    }
                }
                break;

                case ArraySign:
                {
                    context.Offset++;
                    context.Length--;

                    context.ResultType = RedisRawObjectType.Array;

                    byte[] bytes;
                    if (!TryParseLine(context, out bytes))
                    {
                        context.Offset--;
                        context.Length++;
                    }
                    else
                    {
                        long itemCount;
                        if (!bytes.TryParse(out itemCount))
                        {
                            throw new RedisException("Invalid bulk string size", RedisErrorCode.CorruptResponse);
                        }

                        if (itemCount > RedisConstants.Zero)
                        {
                            var iCount = checked ((int)itemCount);

                            var array = new RedisArray(iCount);
                            var items = array.RawData as IList <RedisResult>;

                            for (var i = 0; i < iCount; i++)
                            {
                                var innerContext =
                                    new RedisBufferContext
                                {
                                    Buffer = buffer,
                                    Length = context.Length,
                                    Offset = context.Offset,
                                };

                                TryParse(innerContext);
                                if (!innerContext.Completed)
                                {
                                    return;
                                }

                                context.Offset = innerContext.Offset;
                                context.Length = innerContext.Length;

                                items.Add(innerContext.Result);
                            }

                            array.TrySetCompleted();

                            context.Result    = array;
                            context.Completed = true;
                        }
                        else if (itemCount == RedisConstants.Zero)
                        {
                            context.Result    = new RedisArray(new List <RedisResult>(), 0);
                            context.Completed = true;
                        }
                        else
                        {
                            context.Result    = new RedisArray(null, -1);
                            context.Completed = true;
                        }
                    }
                }
                break;

                case SimpleStringSign:
                {
                    context.Offset++;
                    context.Length--;

                    context.ResultType = RedisRawObjectType.SimpleString;

                    byte[] bytes;
                    if (!TryParseLine(context, out bytes))
                    {
                        context.Offset--;
                        context.Length++;
                    }
                    else
                    {
                        context.Result    = new RedisString((bytes == null) ? null : RedisCommon.ToUTF8String(bytes));
                        context.Completed = true;
                    }
                }
                break;

                case ErrorSign:
                {
                    context.Offset++;
                    context.Length--;

                    context.ResultType = RedisRawObjectType.Error;

                    byte[] bytes;
                    if (!TryParseLine(context, out bytes))
                    {
                        context.Offset--;
                        context.Length++;
                    }
                    else
                    {
                        context.Result    = new RedisError((bytes == null) ? null : RedisCommon.ToUTF8String(bytes));
                        context.Completed = true;
                    }
                }
                break;

                case NumberSign:
                {
                    context.Offset++;
                    context.Length--;

                    context.ResultType = RedisRawObjectType.Integer;

                    byte[] bytes;
                    if (!TryParseLine(context, out bytes))
                    {
                        context.Offset--;
                        context.Length++;
                    }
                    else
                    {
                        long number;
                        if (!bytes.TryParse(out number))
                        {
                            throw new RedisException("Invalid integer value", RedisErrorCode.CorruptResponse);
                        }

                        context.Result    = new RedisInteger(number);
                        context.Completed = true;
                    }
                }
                break;

                default:
                    throw new RedisException("Undefined redis response type", RedisErrorCode.CorruptResponse);
                }
            }
        }
 internal RedisSentinelInfoBase(RedisArray array)
 {
     Parse(array);
 }
Esempio n. 16
0
        protected static RedisPubSubMessage ToPubSubMessage(RedisArray response)
        {
            if (!ReferenceEquals(response, null))
            {
                var items = response.Value;
                if (items != null && items.Count >= 3)
                {
                    var index    = 0;
                    var typeItem = items[index++] as RedisBytes;

                    if (!ReferenceEquals(typeItem, null))
                    {
                        var data = typeItem.Value;
                        if (data != null)
                        {
                            var typeStr = data.ToUTF8String();
                            if (typeStr != null)
                            {
                                var type = ToPubSubMessageType(typeStr);
                                if (type != RedisPubSubMessageType.Undefined)
                                {
                                    if (type == RedisPubSubMessageType.PMessage && items.Count < 4)
                                    {
                                        return(RedisPubSubMessage.Empty);
                                    }

                                    var channelItem = items[index++] as RedisBytes;
                                    if (!ReferenceEquals(channelItem, null))
                                    {
                                        data = channelItem.Value;
                                        if (data != null)
                                        {
                                            var channel = data.ToUTF8String();
                                            if (!channel.IsEmpty())
                                            {
                                                var pattern = String.Empty;
                                                switch (type)
                                                {
                                                case RedisPubSubMessageType.PMessage:
                                                {
                                                    var patternItem = items[index++] as RedisBytes;
                                                    if (!ReferenceEquals(patternItem, null))
                                                    {
                                                        data = patternItem.Value;
                                                        if (data != null)
                                                        {
                                                            pattern = data.ToUTF8String();
                                                        }
                                                    }

                                                    var tmp = channel;
                                                    channel = pattern;
                                                    pattern = tmp;
                                                }
                                                break;

                                                case RedisPubSubMessageType.PSubscribe:
                                                case RedisPubSubMessageType.PUnsubscribe:
                                                {
                                                    pattern = channel;
                                                    channel = String.Empty;
                                                }
                                                break;

                                                default:
                                                    break;
                                                }

                                                var dataItem = items[index++];
                                                if (dataItem != null)
                                                {
                                                    switch (dataItem.Type)
                                                    {
                                                    case RedisResultType.Bytes:
                                                    case RedisResultType.Integer:
                                                        return(new RedisPubSubMessage(type, typeStr, channel, pattern, dataItem));

                                                    default:
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(RedisPubSubMessage.Empty);
        }
Esempio n. 17
0
        private void ParseInternal(RedisArray info)
        {
            if (!ReferenceEquals(info, null))
            {
                var items = info.Value;
                if (items != null)
                {
                    var count = items.Count;
                    if (count > 0)
                    {
                        var rangeStart = items[0] as RedisInteger;
                        if (!ReferenceEquals(rangeStart, null))
                        {
                            base["start_slot_range"] = rangeStart.Value.ToString();

                            if (count > 1)
                            {
                                var rangeEnd = items[1] as RedisInteger;
                                if (!ReferenceEquals(rangeEnd, null))
                                {
                                    base["end_slot_range"] = rangeEnd.Value.ToString();

                                    if (count > 2)
                                    {
                                        var endPoints = new List <RedisEndPoint>(count - 2);

                                        for (var i = 2; i < count; i++)
                                        {
                                            var ep = items[i] as RedisArray;
                                            if (!ReferenceEquals(ep, null))
                                            {
                                                var epList = ep.Value;
                                                if (epList != null)
                                                {
                                                    var length = epList.Count;
                                                    if (length > 1)
                                                    {
                                                        var ip = epList[0] as RedisString;
                                                        if (!ReferenceEquals(ip, null))
                                                        {
                                                            var ipValue = ip.Value;
                                                            if (!ipValue.IsEmpty())
                                                            {
                                                                var port = epList[1] as RedisInteger;
                                                                if (!ReferenceEquals(port, null))
                                                                {
                                                                    endPoints.Add(new RedisEndPoint(ipValue, (int)port.Value));
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                        m_Masters = endPoints.ToArray();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 18
0
 internal RedisSentinelNodeInfo(RedisArray array)
     : base(array)
 {
 }
Esempio n. 19
0
 private RedisClusterSlotInfo(RedisArray info)
 {
     ParseInternal(info);
 }
Esempio n. 20
0
        public static RedisScanBytes ToScanBytes(RedisArray array)
        {
            if (array == null)
            {
                return(new RedisScanBytes(null));
            }

            var items = array.Value;

            if (items == null)
            {
                return(new RedisScanBytes(null));
            }

            var count = items.Count;

            if (count == 0)
            {
                return(new RedisScanBytes(null));
            }

            var item = items[0];

            if (item == null)
            {
                return(new RedisScanBytes(null));
            }

            if (item.Type != RedisResultType.Bytes)
            {
                throw new RedisException("Invalid scan cursor type");
            }

            var str = ((RedisBytes)item).Value.ToUTF8String();

            if (str.IsEmpty())
            {
                throw new RedisException("Invalid scan cursor type");
            }

            var cursor = ulong.Parse(str);

            var result = (RedisByteArray[])null;

            if (items.Count > 1)
            {
                item = items[1];
                if (item != null)
                {
                    if (item.Type != RedisResultType.Array)
                    {
                        throw new RedisException("Invalid scan result type");
                    }

                    var subItems = ((RedisArray)item).Value;
                    if (subItems != null)
                    {
                        var subCount = subItems.Count;
                        if (subCount > 0)
                        {
                            var list = new List <RedisByteArray>(subCount);
                            for (var i = 0; i < count; i++)
                            {
                                var subItem = subItems[i];
                                if (subItem == null || subItem.Type != RedisResultType.Bytes)
                                {
                                    throw new RedisException("Invalid scan result item type");
                                }

                                list.Add(((RedisBytes)subItem).Value);
                            }

                            result = list.ToArray();
                        }
                    }
                }
            }

            return(new RedisScanBytes(new RedisScanBytesData(cursor, result)));
        }
Esempio n. 21
0
 protected virtual void ParseInfo(RedisArray array)
 {
 }