Ejemplo n.º 1
0
        /// <summary>
        /// 设置迭代器到指定键的位置。
        /// </summary>
        /// <param name="key">键。</param>
        public void Seek(BinaryValue key)
        {
            var keyData = key.ByteArray;

            LevelDBInterop.leveldb_iter_seek(this._handle, keyData, new IntPtr(keyData.LongLength));
            CheckLastError();
        }
Ejemplo n.º 2
0
        public override int GetHashCode()
        {
            int hashcode = 157;

            unchecked {
                if (__isset.stringValue)
                {
                    hashcode = (hashcode * 397) + StringValue.GetHashCode();
                }
                if (__isset.smallIntValue)
                {
                    hashcode = (hashcode * 397) + SmallIntValue.GetHashCode();
                }
                if (__isset.integerBitmask)
                {
                    hashcode = (hashcode * 397) + IntegerBitmask.GetHashCode();
                }
                if (__isset.integerFlag)
                {
                    hashcode = (hashcode * 397) + IntegerFlag.GetHashCode();
                }
                if (__isset.binaryValue)
                {
                    hashcode = (hashcode * 397) + BinaryValue.GetHashCode();
                }
                if (__isset.lenValue)
                {
                    hashcode = (hashcode * 397) + LenValue.GetHashCode();
                }
            }
            return(hashcode);
        }
        /// <summary>
        /// Find the child value with the specified index.
        /// </summary>
        /// <param name="nIndex">
        /// Index of the child value to find.
        /// </param>
        /// <param name="ofStart">
        /// Offset within the parent buffer to start search from.
        /// </param>
        /// <param name="iStart">
        /// Index of the child value to start search from.
        /// </param>
        /// <returns>
        /// The child value.
        /// </returns>
        protected override IPofValue FindChildInternal(int nIndex, int ofStart, int iStart)
        {
            BinaryMemoryStream buf = (BinaryMemoryStream)BinaryValue.GetStream();

            buf.Position = ofStart;
            DataReader reader = new DataReader(buf);

            // check array bounds
            if (nIndex < 0 || nIndex >= Length)
            {
                throw new IndexOutOfRangeException(
                          "Element index " + nIndex + " must be in the range [0 .. "
                          + Length + ").");
            }

            // skip children until we find the one we are looking for
            int iProp = iStart;

            while (iProp < nIndex)
            {
                SkipChild(reader);
                iProp++;
            }

            // child found. parse it and return it
            int of = (int)buf.Position;

            SkipChild(reader);
            int cb = (int)(buf.Position - of);

            return(ExtractChild(of, cb));
        }
Ejemplo n.º 4
0
        public void Xor(Command cmd)
        {
            var values = this.RequireTwoBinaryValues(cmd);
            var result = BinaryValue.Xor(values[0], values[1]);

            cmd.Commit(result);
        }
        public void SerializeDeserializeEmpty()
        {
            BinaryValue expected = new BinaryValue(new byte[0]);
            BinaryValue actual   = SerializeDeserialize(expected);

            Assert.AreEqual <BinaryValue>(expected, actual);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 删除指定键。
        /// </summary>
        /// <param name="key">键。</param>
        public WriteBatch Remove(BinaryValue key)
        {
            var keyData = key.ByteArray;

            LevelDBInterop.leveldb_writebatch_delete(_handle, keyData, (IntPtr)keyData.Length);
            return(this);
        }
        /// <summary>
        /// Find the child value with the specified index.
        /// </summary>
        /// <param name="nIndex">
        /// Index of the child value to find.
        /// </param>
        /// <param name="ofStart">
        /// Offset within the parent buffer to start search from.
        /// </param>
        /// <param name="iStart">
        /// Index of the child value to start search from.
        /// </param>
        /// <returns>
        /// The child value.
        /// </returns>
        protected override IPofValue FindChildInternal(int nIndex, int ofStart, int iStart)
        {
            BinaryMemoryStream buf = (BinaryMemoryStream)BinaryValue.GetStream();

            buf.Position = ofStart;
            DataReader reader = new DataReader(buf);

            // skip children until we either find the one we are looking for,
            // or reach the end of the sparse array (index == -1)
            int ofLastIndex = ofStart;
            int iProp       = reader.ReadPackedInt32();

            while (iProp < nIndex && iProp >= 0)
            {
                SkipChild(reader);
                ofLastIndex = (int)buf.Position;
                iProp       = reader.ReadPackedInt32();
            }

            // child found. extract it from the parent buffer
            if (iProp == nIndex)
            {
                int of = (int)buf.Position;
                SkipChild(reader);
                int cb = (int)(buf.Position - of);

                return(ExtractChild(of, cb));
            }

            // child not found
            return(InstantiateNullValue(ofLastIndex, nIndex));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 为有序集 <paramref name="key"/> 的成员 <paramref name="member"/> 的权重值加上增量 <paramref name="increment"/>。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="key">有序集的键名。</param>
        /// <param name="increment">递增量。可以通过传递一个负数值 <paramref name="increment"/> ,让权重减去相应的值。</param>
        /// <param name="member">有序集的成员。</param>
        /// <returns>递增 <paramref name="increment"/> 之后 <paramref name="key"/> 的 <paramref name="member"/> 的权重值。</returns>
        public static double ZIncrBy(this IRedisClient client, string key, double increment, BinaryValue member)
        {
            if(client == null) throw new ArgumentNullException(nameof(client));
            if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
            if(member == null) throw new ArgumentNullException(nameof(member));

            return client.Execute(new RedisFloat("ZINCRBY", key, increment, member));
        }
Ejemplo n.º 9
0
        public void Not(Command cmd)
        {
            this.RequireArgs(1);
            this.RequireType(0, RpnValueType.Binary);
            var value = (BinaryValue)cmd.UseTopValue();

            cmd.Commit(BinaryValue.Not(value, this.Calc));
        }
Ejemplo n.º 10
0
        public void WhenDecodeAndDefaultStrategy()
        {
            var req1 = System.Text.Encoding.UTF8.GetBytes("Hello world! :)");
            var val1 = new BinaryValue(req1, null);
            var res1 = val1.Decode().Value;

            res1.Should().Be("Hello world! :)");
        }
Ejemplo n.º 11
0
        public void WhenEncryptAndDefaultStrategy()
        {
            var req1 = System.Text.Encoding.UTF8.GetBytes("Hello world! :)");
            var val1 = new BinaryValue(req1, null);
            var res1 = val1.Encrypt().Value;

            res1.SequenceEqual(req1).Should().BeTrue();
        }
Ejemplo n.º 12
0
        public void WhenDecryptAndDefaultStrategy()
        {
            var req2 = System.Text.Encoding.UTF8.GetBytes("Hello world! :)");
            var val2 = new BinaryValue(req2, null);
            var res2 = val2.Decrypt().Value;

            res2.SequenceEqual(req2).Should().BeTrue();
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Extract child IPofValue from this value.
 /// </summary>
 /// <param name="of">
 /// Offset of the child within this value.
 /// </param>
 /// <param name="cb">
 /// Length of the child in bytes.
 /// </param>
 /// <returns>
 /// The child value.
 /// </returns>
 protected IPofValue ExtractChild(int of, int cb)
 {
     return(IsUniformCollection
            ? PofValueParser.ParseUniformValue(this, m_nElementType,
                                               BinaryValue.GetBinary(of, cb), PofContext, Offset + of)
            : PofValueParser.ParseValue(this,
                                        BinaryValue.GetBinary(of, cb), PofContext, Offset + of));
 }
Ejemplo n.º 14
0
        /// <summary>
        /// 设置指定键的值。
        /// </summary>
        /// <param name="key">键。</param>
        /// <param name="value">值。</param>
        public WriteBatch Set(BinaryValue key, BinaryValue value)
        {
            var keyData   = key.ByteArray;
            var valueData = value.ByteArray;

            LevelDBInterop.leveldb_writebatch_put(_handle, keyData, (IntPtr)keyData.LongLength, valueData, (IntPtr)valueData.LongLength);
            return(this);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// 将 <paramref name="value"/> 追加到 <paramref name="key"/> 原来的值的末尾。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="key">键名。</param>
        /// <param name="value">键值。</param>
        /// <returns>追加 <paramref name="value"/> 之后,<paramref name="key"/> 中的总字节长度。</returns>
        public static long Append(this IRedisClient client, string key, BinaryValue value)
        {
            if(client == null) throw new ArgumentNullException(nameof(client));
            if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
            if(value == null) throw new ArgumentNullException(nameof(value));

            return client.Execute(new RedisInteger("APPEND", key, value));
        }
Ejemplo n.º 16
0
        public void WhenConvertBytesToBase64String()
        {
            var req1 = System.Text.Encoding.UTF8.GetBytes("Hello world! :)");
            var val1 = new BinaryValue(req1, null);
            var res1 = val1.ToBase64().Value;

            res1.Should().Be(Convert.ToBase64String(req1));
        }
        public void SerializeDeserialize8BitLength()
        {
            BinaryValue expected = new BinaryValue(new[] { (byte)5 }, false);

            BinaryValue actual = SerializeDeserialize(expected);

            Assert.AreEqual <BinaryValue>(expected, actual);
        }
        public void SerializeDeserializeEmptyArray()
        {
            int[] expected = new int[0];
            BinaryValueConverter target = new BinaryValueConverter(new Uri("store://a"), new JsonReliableStateSerializerResolver());

            BinaryValue result = target.Serialize <int[]>(expected);

            int[] actual = target.Deserialize <int[]>(result);

            Assert.AreEqual(0, actual.Length);
        }
Ejemplo n.º 19
0
        private void RequireIntegerAndBinary(Command cmd, out int integerValue, out BinaryValue binaryValue)
        {
            this.RequireArgs(2);
            this.RequireType(0, RpnValueType.Integer, RpnValueType.Double);
            this.RequireType(1, RpnValueType.Binary);

            var values = cmd.UseTopValues(2);

            integerValue = (int)((NumericValue)values[0]).ToInteger();
            binaryValue  = (BinaryValue)values[1];
        }
Ejemplo n.º 20
0
 public void MyTestMethod()
 {
     var client = this.CreateClient();
     client.Set("a", "b");
     Assert.Equal("b", (string)client.Get("a"));
     var value1 = new BinaryValue(TestModels.RootContainer.CreateFakeRoot());
     client.Set("x", value1);
     var value2 = client.Get("x");
     Assert.Equal(value1.ByteArray, value2.ByteArray);
     Assert.IsType<TestModels.RootContainer>(value2.ToModel());
 }
        public void SerializeDeserializeString()
        {
            string expected             = "Mr. Burns";
            BinaryValueConverter target = new BinaryValueConverter(new Uri("store://a"), new JsonReliableStateSerializerResolver());

            BinaryValue result = target.Serialize <string>(expected);

            string actual = target.Deserialize <string>(result);

            Assert.AreEqual <string>(expected, actual);
        }
Ejemplo n.º 22
0
 public static BinaryValue GetBinaryValue(BinaryValue value, bool isExclusive, string nullValue)
 {
     if (!value.HasValue())
     {
         return(nullValue);
     }
     if (isExclusive)
     {
         return("(".ToBytes().Concat(value.ByteArray).ToArray());
     }
     return("[".ToBytes().Concat(value.ByteArray).ToArray());
 }
        public void SerializationSize255Bytes()
        {
            byte[] payload = new byte[255];
            for (int i = 0; i < 255; ++i)
            {
                payload[i] = (byte)7;
            }

            BinaryValue target = new BinaryValue(payload);

            Assert.AreEqual <long>(257, GetSerializdSize(target));
        }
        public void SerializationSize127Bytes()
        {
            byte[] payload = new byte[127];
            for (int i = 0; i < 127; ++i)
            {
                payload[i] = (byte)7;
            }

            BinaryValue target = new BinaryValue(payload);

            Assert.AreEqual <long>(128, GetSerializdSize(target));
        }
        private long GetSerializdSize(BinaryValue input)
        {
            BinaryValueStateSerializer target = new BinaryValueStateSerializer();

            using (MemoryStream stream = new MemoryStream())
            {
                BinaryWriter writer = new BinaryWriter(stream);
                target.Write(input, writer);

                return(stream.Length);
            }
        }
Ejemplo n.º 26
0
 private IItem ConvertToItem(Value value)
 {
     if (value is BinaryValue)
     {
         IItem       item        = null;
         BinaryValue binaryValue = (BinaryValue)value;
         if (binaryValue.Value != null)
         {
             using (MemoryStream stream = new MemoryStream(binaryValue.Value)) {
                 try {
                     item = XmlParser.Deserialize <IItem>(stream);
                 }
                 catch (Exception) { }
                 stream.Close();
             }
         }
         return(item != null ? item : new Data.StringValue(value.DataType.Name));
     }
     else if (value is BoolValue)
     {
         return(new Data.BoolValue(((BoolValue)value).Value));
     }
     else if (value is FloatValue)
     {
         return(new Data.DoubleValue(((FloatValue)value).Value));
     }
     else if (value is PercentValue)
     {
         return(new Data.PercentValue(((PercentValue)value).Value));
     }
     else if (value is DoubleValue)
     {
         return(new Data.DoubleValue(((DoubleValue)value).Value));
     }
     else if (value is IntValue)
     {
         return(new Data.IntValue((int)((IntValue)value).Value));
     }
     else if (value is LongValue)
     {
         return(new Data.IntValue((int)((LongValue)value).Value));
     }
     else if (value is StringValue)
     {
         return(new Data.StringValue(((StringValue)value).Value));
     }
     else if (value is TimeSpanValue)
     {
         return(new Data.TimeSpanValue(TimeSpan.FromSeconds((long)((TimeSpanValue)value).Value)));
     }
     return(null);
 }
Ejemplo n.º 27
0
        /// <summary>
        /// Parses template data for binary value from given XML
        /// </summary>
        /// <param name="element">XElement containing template data for binary value</param>
        /// <param name="partsList">current list of binary parts</param>
        private void ParseValue(XElement element, ObservableCollection <BinaryPart> partsList)
        {
            long   id                      = long.Parse(element.Attribute(Constants.TemplateXML_ID).Value);
            string name                    = element.Attribute(Constants.TemplateXML_Name).Value;
            string type                    = element.Attribute(Constants.TemplateXML_Type).Value.ToLower();
            string converterName           = element.Attribute(Constants.TemplateXML_Converter)?.Value ?? "";
            IntToStringConverter converter = !string.IsNullOrWhiteSpace(converterName) ? converters.Where(x => x.Name == converterName).FirstOrDefault() : null;

            BinaryValueType valueType   = (BinaryValueType)Enum.Parse(typeof(BinaryValueType), type, true);
            BinaryValue     binaryValue = new BinaryValue(id, name, valueType, converter);

            partsList.Add(binaryValue);
        }
        public void BinaryValue_SumInBinary_inputOne_11111111_inputTwo_00000000_should_return_valid_binary()
        {
            // Arrange
            var inputOne       = new BinaryValue("11111111");
            var inputTwo       = new BinaryValue("00000000");
            var expectedBinary = "11111111";

            // Act
            var actualBinary = BinaryValue.SumInBinary(inputOne, inputTwo);

            // Assert
            Assert.AreEqual(expectedBinary, actualBinary, "converting addition to binary: 11111111");
        }
        public void BinaryValue_SumInDecimalFromStrings_inputOne_11111111_inputTwo_00000000_should_return_valid_decimal()
        {
            // Arrange
            var inputOne        = "11111111";
            var inputTwo        = "00000000";
            var expectedDecimal = 255;

            // Act
            var actualDecimal = BinaryValue.SumInDecimalFromStrings(inputOne, inputTwo);

            // Assert
            Assert.AreEqual(expectedDecimal, actualDecimal, "converting addition to decimal: 255");
        }
        public void BinaryValue_DivisionInDecimalFromStrings_inputOne_10_inputTwo_10_should_return_valid_decimal()
        {
            // Arrange
            var inputOne        = "10";
            var inputTwo        = "10";
            var expectedDecimal = 1;

            // Act
            var actualDecimal = BinaryValue.DivisionInDecimalFromStrings(inputOne, inputTwo);

            // Assert
            Assert.AreEqual(expectedDecimal, actualDecimal, "converting division to decimal: 1");
        }
        public void TestComparableDefault()
        {
            BinaryValue defaultValue = default(BinaryValue);

            BinaryValue defaultValueSame = default(BinaryValue);

            BinaryValue greater = new BinaryValue(BitConverter.GetBytes(2));

            Assert.AreEqual <int>(defaultValue.GetHashCode(), defaultValueSame.GetHashCode());
            Assert.AreEqual <int>(-1, defaultValue.CompareTo(greater));
            Assert.AreEqual <int>(0, defaultValue.CompareTo(defaultValueSame));
            Assert.AreEqual <int>(1, greater.CompareTo(defaultValue));
        }
        public void BinaryValue_SumInDecimalFromStrings_inputOne_1111111111111111111111111111111_inputTwo_1_should_return_valid_binary()
        {
            // Arrange
            var inputOne        = "1111111111111111111111111111111";
            var inputTwo        = "1";
            var expectedDecimal = "2147483648";

            // Act
            var actualDecimal = BinaryValue.SumInDecimalFromStrings(inputOne, inputTwo);

            // Assert
            Assert.AreEqual(expectedDecimal, actualDecimal, "converting addition to binary: 2147483648");
        }
        public void TestComparable()
        {
            BinaryValue less = new BinaryValue(BitConverter.GetBytes(1));

            BinaryValue same = new BinaryValue(BitConverter.GetBytes(1));

            BinaryValue greater = new BinaryValue(BitConverter.GetBytes(2));

            Assert.AreEqual <int>(less.GetHashCode(), same.GetHashCode());
            Assert.AreEqual <int>(-1, less.CompareTo(greater));
            Assert.AreEqual <int>(0, less.CompareTo(same));
            Assert.AreEqual <int>(1, greater.CompareTo(less));
        }
Ejemplo n.º 34
0
        /// <summary>
        /// 设置键值。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="key">键名。</param>
        /// <param name="value">键值。</param>
        /// <param name="expiration">生存时间。</param>
        /// <param name="timeUnit">定位生存时间的单位。</param>
        /// <param name="behavior">定位键的行为。</param>
        /// <returns>设置成功返回 true,否则返回 false。</returns>
        public static bool Set(this IRedisClient client, string key, BinaryValue value
            , TimeSpan? expiration = null
            , RedisExpireTimeUnit timeUnit = RedisExpireTimeUnit.EX
            , RedisKeyBehavior behavior = RedisKeyBehavior.None)
        {
            long? expiration_64 = null;
            if(expiration.HasValue) expiration_64 = expiration.Value.GetExpiration(timeUnit);

            return Set(client, key, value, expiration_64, timeUnit, behavior);
        }
Ejemplo n.º 35
0
 /// <summary>
 /// 动态地调整 Redis 服务器的配置。
 /// </summary>
 /// <param name="client">Redis 客户端。</param>
 /// <param name="parameter">配置参数。</param>
 /// <param name="value">参数的值。</param>
 /// <returns>一个结果。</returns>
 public static Result ConfigSet(this IRedisClient client, string parameter, BinaryValue value)
 {
     if(client == null) throw new ArgumentNullException(nameof(client));
     if(string.IsNullOrWhiteSpace(parameter)) throw new ArgumentNullException(nameof(parameter));
     return client.Execute(new RedisStatus("CONFIG SET", parameter, value));
 }
Ejemplo n.º 36
0
 /// <summary>
 /// 初始化一个 <see cref="RedisFieldItem"/> 类的新实例。
 /// </summary>
 /// <param name="field">域名。</param>
 /// <param name="value">域值。</param>
 public RedisFieldItem(string field, BinaryValue value)
 {
     this._Field = field;
     this._Value = value;
 }
Ejemplo n.º 37
0
        /// <summary>
        /// 返回给定的有序集合键 <paramref name="key"/> 中,值介于 <paramref name="min"/> 和 <paramref name="max"/> 之间从低到高的顺序的成员。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="key">有序集的键名</param>
        /// <param name="min">最小成员值。可以为 null 值,表示负无限。</param>
        /// <param name="max">最大成员值。可以为 null 值,表示正无限。</param>
        /// <param name="exclusiveMin">指示最小是否为开区间(true 时表示不含最小值)。</param>
        /// <param name="exclusiveMax">指示最大值是否为开区间(true 时表示不含最大值)。</param>
        /// <param name="offset">返回结果的偏移量。</param>
        /// <param name="count">返回结果的数量。</param>
        /// <returns>一个从低到高的顺序列表,列表里面包含了有序集合在指定范围内的成员。</returns>
        public static BinaryValue[] ZRangeByLex(this IRedisClient client, string key
            , BinaryValue min, BinaryValue max
            , bool exclusiveMin = false, bool exclusiveMax = false
            , long? offset = null, long? count = null)
        {
            if(client == null) throw new ArgumentNullException(nameof(client));
            if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));

            IEnumerable<object> args = new object[] { key
                , RedisArgs.GetBinaryValue(min, exclusiveMin, "-")
                , RedisArgs.GetBinaryValue(max, exclusiveMax, "+") };
            if(offset.HasValue && count.HasValue)
            {
                args = RedisArgs.ConcatAll(args, new[] { "LIMIT", offset.Value.ToString(), count.Value.ToString() });
            }

            return client.Execute(RedisArray.Create(new RedisValue("ZRANGEBYLEX", args.ToArray())));
        }
Ejemplo n.º 38
0
        /// <summary>
        /// 返回有序集 <paramref name="key"/> 中,成员 <paramref name="member"/> 的权重值。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="key">有序集的键名。</param>
        /// <param name="member">有序集的成员。</param>
        /// <returns>如果 <paramref name="member"/> 是有序集 <paramref name="key"/> 的成员,返回 <paramref name="member"/> 的权重值。否则返回 null 值。</returns>
        public static double? ZScore(this IRedisClient client, string key, BinaryValue member)
        {
            if(client == null) throw new ArgumentNullException(nameof(client));
            if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
            if(member == null) throw new ArgumentNullException(nameof(member));

            return client.Execute(new RedisFloat.Nullable("ZSCORE", key, member));
        }
Ejemplo n.º 39
0
        /// <summary>
        /// 返回有序集 <paramref name="key"/> 中成员 <paramref name="member"/> 的排名。其中有序集成员按权重值递减(从大到小)顺序排列。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="key">有序集的键名。</param>
        /// <param name="member">有序集的成员。</param>
        /// <returns>如果 <paramref name="member"/> 是有序集 <paramref name="key"/> 的成员,返回 <paramref name="member"/> 的排名。否则返回 null 值。</returns>
        public static long? ZRevRank(this IRedisClient client, string key, BinaryValue member)
        {
            if(client == null) throw new ArgumentNullException(nameof(client));
            if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
            if(member == null) throw new ArgumentNullException(nameof(member));

            return client.Execute(new RedisInteger.Nullable("ZREVRANK", key, member));
        }
Ejemplo n.º 40
0
 /// <summary>
 /// 初始化一个 <see cref="RedisScoreItem"/> 类的新实例。
 /// </summary>
 /// <param name="score">权重值。</param>
 /// <param name="member">成员。</param>
 public RedisScoreItem(double score, BinaryValue member)
 {
     this._Score = score;
     this._Member = member;
 }
Ejemplo n.º 41
0
 void IRedisItem.Parse(RedisExecutor executor)
 {
     this._Member = executor.ReadBulk();
     this._Score = Aoite.Redis.Commands.RedisFloat.FromString(executor.ReadBulkString());
 }
Ejemplo n.º 42
0
 /// <summary>
 /// 将给定 <paramref name="key"/> 的值设为 <paramref name="value"/> ,并返回 key 的旧值。
 /// </summary>
 /// <param name="client">Redis 客户端。</param>
 /// <param name="key">键名。</param>
 /// <param name="value">键值。</param>
 /// <returns>当 <paramref name="key"/> 不存在时,返回 null ,否则返回 <paramref name="key"/> 的旧值。</returns>
 public static BinaryValue GetSet(this IRedisClient client, string key, BinaryValue value)
 {
     if(client == null) throw new ArgumentNullException(nameof(client));
     if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
     return client.Execute(new RedisValue("GETSET", key, value));
 }
Ejemplo n.º 43
0
        /// <summary>
        /// 判断 <paramref name="member"/> 是否集合 <paramref name="key"/> 的成员。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="key">集合的键名。</param>
        /// <param name="member">集合的成员。</param>
        /// <returns>存在返回 true,否则返回 false。</returns>
        public static bool SIsMember(this IRedisClient client, string key, BinaryValue member)
        {
            if(client == null) throw new ArgumentNullException(nameof(client));
            if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));

            return client.Execute(new RedisBoolean("SISMEMBER", key, member));
        }
Ejemplo n.º 44
0
        /// <summary>
        /// 将 <paramref name="member"/> 从 <paramref name="source"/> 集合移动到 <paramref name="destination"/> 集合。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="source">源集合。</param>
        /// <param name="destination">目标集合。</param>
        /// <param name="member">移动的成员。</param>
        /// <returns>一个值,表示移动是否成功。</returns>
        public static bool SMove(this IRedisClient client, string source, string destination, BinaryValue member)
        {
            if(client == null) throw new ArgumentNullException(nameof(client));
            if(string.IsNullOrWhiteSpace(source)) throw new ArgumentNullException(nameof(source));
            if(string.IsNullOrWhiteSpace(destination)) throw new ArgumentNullException(nameof(destination));

            return client.Execute(new RedisBoolean("SMOVE", source, destination, member));
        }
Ejemplo n.º 45
0
        /// <summary>
        /// 移除给定的有序集合键 <paramref name="key"/> 中,值介于 <paramref name="min"/> 和 <paramref name="max"/> 之间的成员。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="key">有序集的键名</param>
        /// <param name="min">最小成员值。可以为 null 值,表示负无限。</param>
        /// <param name="max">最大成员值。可以为 null 值,表示正无限。</param>
        /// <param name="exclusiveMin">指示最小是否为开区间(true 时表示不含最小值)。</param>
        /// <param name="exclusiveMax">指示最大值是否为开区间(true 时表示不含最大值)。</param>
        /// <returns>被移除的成员数量。</returns>
        public static long ZRemRangeByLex(this IRedisClient client, string key
            , BinaryValue min, BinaryValue max
            , bool exclusiveMin = false, bool exclusiveMax = false)
        {
            if(client == null) throw new ArgumentNullException(nameof(client));
            if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));

            return client.Execute(new RedisInteger("ZREMRANGEBYLEX", key
                , RedisArgs.GetBinaryValue(min, exclusiveMin, "-")
                , RedisArgs.GetBinaryValue(max, exclusiveMax, "+")));
        }
Ejemplo n.º 46
0
        /// <summary>
        /// 将列表 <paramref name="key"/> 下标为 <paramref name="index"/> 的元素的值设置为 <paramref name="value"/> 。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="key">列表的键名。</param>
        /// <param name="index">设置的索引。
        /// <para>当 <paramref name="index"/> 参数超出范围,或对一个空列表( <paramref name="key"/> 不存在)进行 LSET 时,返回一个错误</para>
        /// </param>
        /// <param name="value">设置的元素值。</param>
        /// <returns>一个结果。</returns>
        public static Result LSet(this IRedisClient client, string key, long index, BinaryValue value)
        {
            if(client == null) throw new ArgumentNullException(nameof(client));
            if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));

            return client.Execute(new RedisStatus("LSET", key, index, value));
        }
Ejemplo n.º 47
0
 /// <summary>
 /// 将值 <paramref name="value"/> 插入到列表 <paramref name="key"/> 当中,位于值 <paramref name="pivot"/> 之前或之后。
 /// </summary>
 /// <param name="client">Redis 客户端。</param>
 /// <param name="key">列表的键名。</param>
 /// <param name="position">插入的位置。</param>
 /// <param name="pivot">定位的支点键名。</param>
 /// <param name="value">插入的值。</param>
 /// <returns>插入的结果。</returns>
 public static long LInsert(this IRedisClient client, string key, RedisInsertPosition position, string pivot, BinaryValue value)
 {
     if(client == null) throw new ArgumentNullException(nameof(client));
     if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
     if(string.IsNullOrWhiteSpace(pivot)) throw new ArgumentNullException(nameof(pivot));
     return client.Execute(new RedisInteger("LINSERT", key
         , position == RedisInsertPosition.Before ? "BEFORE" : "AFTER"
         , pivot, value));
 }
Ejemplo n.º 48
0
 void IRedisItem.Parse(RedisExecutor executor)
 {
     this._Field = executor.ReadBulkString();
     this._Value = executor.ReadBulk();
 }
Ejemplo n.º 49
0
 /// <summary>
 /// 设置键值。
 /// </summary>
 /// <param name="client">Redis 客户端。</param>
 /// <param name="key">键名。</param>
 /// <param name="value">键值。</param>
 /// <returns>设置成功返回 true,否则返回 false。</returns>
 public static bool Set(this IRedisClient client, string key, BinaryValue value)
 {
     return Set(client, key, value, (long?)null);
 }
Ejemplo n.º 50
0
 /// <summary>
 /// 根据参数 <paramref name="count"/> 的值,移除列表中与参数 <paramref name="value"/> 相等的元素。
 /// </summary>
 /// <param name="client">Redis 客户端。</param>
 /// <param name="key">列表的键名。</param>
 /// <param name="count">移除的数量。
 /// <para>1、<paramref name="count"/> &gt; 0 : 从表头开始向表尾搜索,移除与 <paramref name="value"/> 相等的元素,数量为 <paramref name="count"/> 。</para>
 /// <para>2、<paramref name="count"/> &lt; 0 : 从表尾开始向表头搜索,移除与 <paramref name="value"/> 相等的元素,数量为 <paramref name="count"/> 的绝对值。</para>
 /// <para>3、<paramref name="count"/> = 0 : 移除表中所有与 <paramref name="value"/> 相等的值。</para>
 /// </param>
 /// <param name="value">匹配的元素值。</param>
 /// <returns>被移除元素的数量。</returns>
 public static long LRem(this IRedisClient client, string key, long count, BinaryValue value)
 {
     if(client == null) throw new ArgumentNullException(nameof(client));
     if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
     return client.Execute(new RedisInteger("LREM", key, count, value));
 }
Ejemplo n.º 51
0
 /// <summary>
 /// 初始化一个 <see cref="RedisKeyItem"/> 类的新实例。
 /// </summary>
 /// <param name="key">键名。</param>
 /// <param name="value">键值。</param>
 public RedisKeyItem(string key, BinaryValue value)
 {
     this._Key = key;
     this._Value = value;
 }
Ejemplo n.º 52
0
 /// <summary>
 /// 用 <paramref name="value"/> 参数覆写(overwrite)给定 <paramref name="key"/> 所储存的值,从偏移量 <paramref name="offset"/> 开始。
 /// <para>不存在的 <paramref name="key"/> 当作空值处理。</para>
 /// </summary>
 /// <param name="client">Redis 客户端。</param>
 /// <param name="key">键名。</param>
 /// <param name="offset">偏移量。</param>
 /// <param name="value">覆盖的键值。</param>
 /// <returns>修改之后的字节总长度。</returns>
 public static long SetRange(this IRedisClient client, string key, long offset, BinaryValue value)
 {
     if(client == null) throw new ArgumentNullException(nameof(client));
     if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
     if(offset < 0) throw new ArgumentOutOfRangeException(nameof(offset));
     return client.Execute(new RedisInteger("SETRANGE", key, offset, value));
 }
Ejemplo n.º 53
0
        /// <summary>
        /// 设置键值。
        /// </summary>
        /// <param name="client">Redis 客户端。</param>
        /// <param name="key">键名。</param>
        /// <param name="value">键值。</param>
        /// <param name="expiration">生存时间。</param>
        /// <param name="timeUnit">定位生存时间的单位。</param>
        /// <param name="behavior">定位键的行为。</param>
        /// <returns>设置成功返回 true,否则返回 false。</returns>
        public static bool Set(this IRedisClient client, string key, BinaryValue value
            , long? expiration = null
            , RedisExpireTimeUnit timeUnit = RedisExpireTimeUnit.EX
            , RedisKeyBehavior behavior = RedisKeyBehavior.None)
        {
            if(client == null) throw new ArgumentNullException(nameof(client));
            if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));

            List<object> args = new List<object>(5);
            args.Add(key);
            args.Add(value);
            if(expiration.HasValue && expiration.Value > 0)
            {
                args.Add(timeUnit == RedisExpireTimeUnit.EX ? "EX" : "PX");
                args.Add(expiration.Value);
            }

            if(behavior != RedisKeyBehavior.None) args.Add(behavior == RedisKeyBehavior.NX ? "NX" : "XX");

            return client.Execute(new RedisStatus.MultiBulk("SET", args.ToArray()));
        }