csharp/2881099/csredis/src/CSRedisCore/Types.cs

Types.cs

using System;
using System.Runtime.Serialization;
namespace CSRedis
{
    /// 
    /// Sub-command used by Redis OBJECT command
    /// 
    public enum RedisObjectSubCommand
    {
        /// 
        /// Return the number of references of the value astociated with the specified key
        /// 
        RefCount,

        /// 
        /// Return the number of seconds since the object stored at the specified key is idle
        /// 
        IdleTime,
    };

    /// 
    /// Sort direction used by Redis SORT command
    /// 
    public enum RedisSortDir
    {
        /// 
        /// Sort ascending (a-z)
        /// 
        Asc,

        /// 
        /// Sort descending (z-a)
        /// 
        Desc,
    }

    /// 
    /// Insert position used by Redis LINSERT command
    /// 
    public enum RedisInsert
    {
        /// 
        /// Insert before pivot element
        /// 
        Before,

        /// 
        /// Insert after pivot element
        /// 
        After,
    }

    /// 
    /// Operation used by Redis BITOP command
    /// 
    public enum RedisBitOp
    {
        /// 
        /// Bitwise AND
        /// 
        And,

        /// 
        /// Bitwise OR
        /// 
        Or,

        /// 
        /// Bitwise EXCLUSIVE-OR
        /// 
        XOr,

        /// 
        /// Bitwise NOT
        /// 
        Not,
    }

    /// 
    /// Aggregation function used by Reids set operations
    /// 
    public enum RedisAggregate
    {
        /// 
        /// Aggregate SUM
        /// 
        Sum,

        /// 
        /// Aggregate MIN
        /// 
        Min,

        /// 
        /// Aggregate MAX
        /// 
        Max,
    }

    /// 
    /// Redis unified message prefix
    /// 
    public enum RedisMessage
    {
        /// 
        /// Error message
        /// 
        Error = '-',

        /// 
        /// Status message
        /// 
        Status = '+',

        /// 
        /// Bulk message
        /// 
        Bulk = '$',

        /// 
        /// Multi bulk message
        /// 
        MultiBulk = '*',

        /// 
        /// Int message
        /// 
        Int = ':',
    }

    /// 
    /// Redis subscription response type
    /// 
    public enum RedisSubscriptionResponseType
    {
        /// 
        /// Channel subscribed
        /// 
        Subscribe,

        /// 
        /// Message published
        /// 
        Message,

        /// 
        /// Channel unsubscribed
        /// 
        Unsubscribe,

        /// 
        /// Channel pattern subscribed
        /// 
        PSubscribe,

        /// 
        /// Message published to channel pattern
        /// 
        PMessage,

        /// 
        /// Channel pattern unsubsribed
        /// 
        PUnsubscribe,
    }

    /// 
    /// Redis existence specification for SET command
    /// 
    public enum RedisExistence
    {
        /// 
        /// Only set the key if it does not already exist
        /// 
        Nx,

        /// 
        /// Only set the key if it already exists
        /// 
        Xx,
    }

    /// 
    /// Base clast for Redis role information
    /// 
    public abstract clast RedisRole
    {
        readonly string _roleName;

        /// 
        /// Get the role type
        /// 
        public string RoleName { get { return _roleName; } }

        internal RedisRole(string roleName)
        {
            _roleName = roleName;
        }
    }

    /// 
    /// Represents information on the Redis master role
    /// 
    public clast RedisMasterRole : RedisRole
    {
        readonly long _replicationOffset;
        readonly Tuple[] _slaves;

        /// 
        /// Get the master replication offset
        /// 
        public long ReplicationOffset { get { return _replicationOffset; } }

        /// 
        /// Get the slaves astociated with the current master
        /// 
        public Tuple[] Slaves { get { return _slaves; } }

        internal RedisMasterRole(string role, long replicationOffset, Tuple[] slaves)
            : base(role)
        {
            _replicationOffset = replicationOffset;
            _slaves = slaves;
        }
    }

    /// 
    /// Represents information on the Redis slave role
    /// 
    public clast RedisSlaveRole : RedisRole
    {
        readonly string _masterIp;
        readonly int _masterPort;
        readonly string _replicationState;
        readonly long _dataReceived;

        /// 
        /// Get the IP address of the master node
        /// 
        public string MasterIp { get { return _masterIp; } }

        /// 
        /// Get the port of the master node
        /// 
        public int MasterPort { get { return _masterPort; } }

        /// 
        /// Get the replication state
        /// 
        public string ReplicationState { get { return _replicationState; } }

        /// 
        /// Get the number of bytes received
        /// 
        public long DataReceived { get { return _dataReceived; } }

        internal RedisSlaveRole(string role, string masterIp, int masterPort, string replicationState, long dataReceived)
            : base(role)
        {
            _masterIp = masterIp;
            _masterPort = masterPort;
            _replicationState = replicationState;
            _dataReceived = dataReceived;
        }
    }

    /// 
    /// Represents information on the Redis sentinel role
    /// 
    public clast RedisSentinelRole : RedisRole
    {
        readonly string[] _masters;

        /// 
        /// Get the masters known to the current Sentinel
        /// 
        public string[] Masters { get { return _masters; } }

        internal RedisSentinelRole(string role, string[] masters)
            : base(role)
        {
            _masters = masters;
        }
    }

    /// 
    /// Represents the result of a Redis SCAN or SSCAN operation
    /// 
    public clast RedisScan
    {
        readonly long _cursor;
        readonly T[] _items;

        /// 
        /// Updated cursor that should be used as the cursor argument in the next call
        /// 
        public long Cursor { get { return _cursor; } }

        /// 
        /// Collection of elements returned by the SCAN operation
        /// 
        public T[] Items { get { return _items; } }

        internal RedisScan(long cursor, T[] items)
        {
            _cursor = cursor;
            _items = items;
        }
    }

    /// 
    /// Represents a Redis subscription response
    /// 
    public clast RedisSubscriptionResponse
    {
        readonly string _channel;
        readonly string _pattern;
        readonly string _type;

        /// 
        /// Get the subscription channel name
        /// 
        public string Channel { get { return _channel; } }

        /// 
        /// Get the subscription pattern
        /// 
        public string Pattern { get { return _pattern; } }

        /// 
        /// Get the message type
        /// 
        public string Type { get { return _type; } }

        internal RedisSubscriptionResponse(string type, string channel, string pattern)
        {
            _type = type;
            _channel = channel;
            _pattern = pattern;
        }
    }

    /// 
    /// Represents a Redis subscription channel
    /// 
    public clast RedisSubscriptionChannel : RedisSubscriptionResponse
    {
        readonly long _count;

        /// 
        /// Get the count of active subscriptions
        /// 
        public long Count { get { return _count; } }

        internal RedisSubscriptionChannel(string type, string channel, string pattern, long count)
            : base(type, channel, pattern)
        {
            _count = count;
        }
    }

    /// 
    /// Represents a Redis subscription message
    /// 
    public clast RedisSubscriptionMessage : RedisSubscriptionResponse
    {
        readonly string _body;

        /// 
        /// Get the subscription message
        /// 
        public string Body { get { return _body; } }

        internal RedisSubscriptionMessage(string type, string channel, string body)
            : base(type, channel, null)
        {
            _body = body;
        }

        internal RedisSubscriptionMessage(string type, string pattern, string channel, string body)
            : base(type, channel, pattern)
        {
            _body = body;
        }
    }


    /// 
    /// Base clast for Redis server-info objects reported by Sentinel
    /// 
    public abstract clast RedisServerInfo : ISerializable
    {
        /// 
        /// Create new RedisServerInfo via deserialization
        /// 
        /// Serialization info
        /// Serialization context
        public RedisServerInfo(SerializationInfo info, StreamingContext context)
        {
            Name = info.GetString("name");
            Ip = info.GetString("ip");
            Port = info.GetInt32("port");
            RunId = info.GetString("runid");
            Flags = info.GetString("flags").Split(',');
            //PendingCommands = info.GetInt64("pending-commands");
            //LastOkPingReply = info.GetInt64("last-ok-ping-reply");
            //LastPingReply = info.GetInt64("last-ping-reply");
            //DownAfterMilliseconds = info.GetInt64("down-after-milliseconds");
        }

        /// 
        /// Get or set Redis server name
        /// 
        public string Name { get; set; }

        /// 
        /// Get or set Redis server IP
        /// 
        public string Ip { get; set; }

        /// 
        /// Get or set Redis server port
        /// 
        public int Port { get; set; }

        /// 
        /// Get or set Redis server run ID
        /// 
        public string RunId { get; set; }

        /// 
        /// Get or set Redis server flags
        /// 
        public string[] Flags { get; set; }

        /// 
        /// Get or set number of pending Redis server commands
        /// 
        public long PendingCommands { get; set; }

        /// 
        /// Get or set last ping sent
        /// 
        public long LastPingSent { get; set; }

        /// 
        /// Get or set milliseconds since last successful ping reply
        /// 
        public long LastOkPingReply { get; set; }

        /// 
        /// Get or set milliseconds since last ping reply
        /// 
        public long LastPingReply { get; set; }

        /// 
        /// Get or set down after milliseconds
        /// 
        public long DownAfterMilliseconds { get; set; }

        /// 
        /// Not implemented
        /// 
        /// info
        /// info
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            throw new NotImplementedException();
        }
    }

    /// 
    /// Base clast for Redis master/slave objects reported by Sentinel
    /// 
    public abstract clast RedisMasterSlaveInfo : RedisServerInfo
    {
        /// 
        /// Create new RedisMasterSlaveInfo via deserialization
        /// 
        /// Serialization info
        /// Serialization context
        public RedisMasterSlaveInfo(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            InfoRefresh = info.GetInt64("info-refresh");
            RoleReported = info.GetString("role-reported");
            RoleReportedTime = info.GetInt64("role-reported-time");
        }

        /// 
        /// Get or set info refresh
        /// 
        public long InfoRefresh { get; set; }

        /// 
        /// Get or set role reported
        /// 
        public string RoleReported { get; set; }

        /// 
        /// Get or set role reported time
        /// 
        public long RoleReportedTime { get; set; }
    }

    /// 
    /// Represents a Redis master node as reported by a Redis Sentinel
    /// 
    public clast RedisMasterInfo : RedisMasterSlaveInfo
    {
        /// 
        /// Create new RedisMasterInfo via deserialization
        /// 
        /// Serialization info
        /// Serialization context
        public RedisMasterInfo(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            ConfigEpoch = info.GetInt64("config-epoch");
            NumSlaves = info.GetInt64("num-slaves");
            NumOtherSentinels = info.GetInt64("num-other-sentinels");
            Quorum = info.GetInt64("quorum");
            FailoverTimeout = info.GetInt64("failover-timeout");
            ParallelSyncs = info.GetInt64("parallel-syncs");
        }

        /// 
        /// Get or set the config epoch
        /// 
        public long ConfigEpoch { get; set; }
        /// 
        /// Get or set number of slaves of the current master node
        /// 
        public long NumSlaves { get; set; }
        /// 
        /// Get or set number of other Sentinels
        /// 
        public long NumOtherSentinels { get; set; }
        /// 
        /// Get or set Sentinel quorum count
        /// 
        public long Quorum { get; set; }
        /// 
        /// Get or set the failover timeout
        /// 
        public long FailoverTimeout { get; set; }
        /// 
        /// Get or set the parallel syncs
        /// 
        public long ParallelSyncs { get; set; }
    }



    /// 
    /// Represents a Redis slave node as reported by a Redis Setinel
    /// 
    public clast RedisSlaveInfo : RedisMasterSlaveInfo
    {
        /// 
        /// Create new RedisSlaveInfo via deserialization
        /// 
        /// Serialization info
        /// Serialization context
        public RedisSlaveInfo(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            MasterLinkDownTime = info.GetInt64("master-link-down-time");
            MasterLinkStatus = info.GetString("master-link-status");
            MasterHost = info.GetString("master-host");
            MasterPort = info.GetInt32("master-port");
            SlavePriority = info.GetInt64("slave-priority");
            SlaveReplOffset = info.GetInt64("slave-repl-offset");
        }

        /// 
        /// Get or set the master link down time
        /// 
        public long MasterLinkDownTime { get; set; }

        /// 
        /// Get or set status of master link
        /// 
        public string MasterLinkStatus { get; set; }

        /// 
        /// Get or set the master host of the current Redis slave node
        /// 
        public string MasterHost { get; set; }

        /// 
        /// Get or set the master port of the current Redis slave node
        /// 
        public int MasterPort { get; set; }

        /// 
        /// Get or set the priority of the current Redis slave node
        /// 
        public long SlavePriority { get; set; }

        /// 
        /// Get or set the slave replication offset
        /// 
        public long SlaveReplOffset { get; set; }
    }

    /// 
    /// Represents a Redis Sentinel node as reported by a Redis Sentinel
    /// 
    public clast RedisSentinelInfo : RedisServerInfo
    {
        /// 
        /// Create new RedisSentinelInfo via deserialization
        /// 
        /// Serialization info
        /// Serialization context
        public RedisSentinelInfo(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            SDownTime = this.GetSerializationItemValue(info, "s-down-time");
            LastHelloMessage = this.GetSerializationItemValue(info, "last-hello-message");
            VotedLeader = this.GetSerializationItemValue(info, "voted-leader");
            VotedLeaderEpoch = this.GetSerializationItemValue(info, "voted-leader-epoch");

            if (SDownTime == 0) SDownTime = -1;
            if (LastHelloMessage == 0) LastHelloMessage = -1;
            if (VotedLeaderEpoch == 0) VotedLeaderEpoch = -1;
        }

        /// 
        /// Get a value from an instance of the SerializationInfo
        /// 
        /// 
        /// 
        /// 
        /// 
        private T GetSerializationItemValue(SerializationInfo info, string key)
        {
            foreach (SerializationEntry entry in info)
            {
                if (entry.Name == key)
                {
                    return (T)Convert.ChangeType(entry.Value, typeof(T), System.Globalization.CultureInfo.InvariantCulture);
                }
            }
            return default(T);
        }

        /// 
        /// Get or set the subjective down time
        /// 
        public long SDownTime { get; set; }

        /// 
        /// Get or set milliseconds(?) since last hello message from current Sentinel node
        /// 
        public long LastHelloMessage { get; set; }

        /// 
        /// Get or set the voted-leader value
        /// 
        public string VotedLeader { get; set; }
        /// 
        /// Get or set the voted-leader epoch
        /// 
        public long VotedLeaderEpoch { get; set; }
    }

    /// 
    /// Represents an entry from the Redis slow log
    /// 
    public clast RedisSlowLogEntry
    {
        readonly long _id;
        readonly DateTime _date;
        readonly TimeSpan _latency;
        readonly string[] _arguments;

        /// 
        /// Get the entry ID
        /// 
        public long Id { get { return _id; } }
        /// 
        /// Get the entry date
        /// 
        public DateTime Date { get { return _date; } }
        /// 
        /// Get the entry latency
        /// 
        public TimeSpan Latency { get { return _latency; } }
        /// 
        /// Get the entry arguments
        /// 
        public string[] Arguments { get { return _arguments; } }

        internal RedisSlowLogEntry(long id, DateTime date, TimeSpan latency, string[] arguments)
        {
            _id = id;
            _date = date;
            _latency = latency;
            _arguments = arguments;
        }
    }

    /// 
    /// Represents state as reported by Sentinel
    /// 
    public clast RedisMasterState
    {
        readonly long _downState;
        readonly string _leader;
        readonly long _voteEpoch;

        /// 
        /// Get the master down state
        /// 
        public long DownState { get { return _downState; } }
        /// 
        /// Get the leader
        /// 
        public string Leader { get { return _leader; } }
        /// 
        /// Get the vote epoch
        /// 
        public long VoteEpoch { get { return _voteEpoch; } }

        internal RedisMasterState(long downState, string leader, long voteEpoch)
        {
            _downState = downState;
            _leader = leader;
            _voteEpoch = voteEpoch;
        }
    }
}