From 0fd5f718fa3d5a6838dfb4e503f2d5ae43acbc5c Mon Sep 17 00:00:00 2001 From: Henry Date: Mon, 24 May 2021 09:07:22 +0800 Subject: [PATCH] fix the problem --- .../BeetleX.Redis.XUnitTest.csproj | 7 +++++++ BeetleX.Redis.XUnitTest/DBTester.cs | 13 ++++--------- BeetleX.Redis.XUnitTest/HashTableTester.cs | 12 ++++++------ BeetleX.Redis.XUnitTest/SequenceTest.cs | 9 +++++---- BeetleX.Redis.XUnitTest/SetsTester.cs | 2 +- src/BeetleX.Redis.csproj | 6 +++--- src/Command.cs | 4 ++++ src/Commands/HSET.cs | 2 ++ src/Commands/SET.cs | 2 ++ src/Commands/ZUNIONSTORE.cs | 2 +- src/RedisDB.cs | 5 +++-- src/Sequence.cs | 10 +++++++--- 12 files changed, 45 insertions(+), 29 deletions(-) diff --git a/BeetleX.Redis.XUnitTest/BeetleX.Redis.XUnitTest.csproj b/BeetleX.Redis.XUnitTest/BeetleX.Redis.XUnitTest.csproj index 87a64be..f0e1c91 100644 --- a/BeetleX.Redis.XUnitTest/BeetleX.Redis.XUnitTest.csproj +++ b/BeetleX.Redis.XUnitTest/BeetleX.Redis.XUnitTest.csproj @@ -6,6 +6,13 @@ false + + + + + + + diff --git a/BeetleX.Redis.XUnitTest/DBTester.cs b/BeetleX.Redis.XUnitTest/DBTester.cs index 5f0dfea..d7b750c 100644 --- a/BeetleX.Redis.XUnitTest/DBTester.cs +++ b/BeetleX.Redis.XUnitTest/DBTester.cs @@ -16,14 +16,9 @@ public class DBTester public DBTester(ITestOutputHelper output) { this.Console = output; - //DB.Host.AddWriteHost("192.168.2.19", 6378, true); -<<<<<<< HEAD - DB.Host.AddWriteHost("192.168.2.19", 6379); -======= DB.Host.AddWriteHost("127.0.0.1"); - DB.KeyPrefix = "bbq"; ->>>>>>> KeyPrefix - + DB.KeyPrefix = "BeetleX"; + DB.AutoPing = false; } private RedisDB DB = new RedisDB(0); @@ -100,7 +95,7 @@ public async void Set() var emptyValue = await DB.Get("test"); Write(emptyValue); - Assert.Equal(emptyValue, ""); + Assert.Equal(emptyValue, null); var nullResult = await DB.Set("test", null); @@ -108,7 +103,7 @@ public async void Set() var nullValue = await DB.Get("test"); Write(nullValue); - Assert.Equal(nullValue, ""); + Assert.Equal(nullValue, null); } diff --git a/BeetleX.Redis.XUnitTest/HashTableTester.cs b/BeetleX.Redis.XUnitTest/HashTableTester.cs index aa61867..3a3070e 100644 --- a/BeetleX.Redis.XUnitTest/HashTableTester.cs +++ b/BeetleX.Redis.XUnitTest/HashTableTester.cs @@ -12,7 +12,7 @@ public HashTableTester(ITestOutputHelper output) { this.Console = output; DB.Host.AddWriteHost("localhost"); - DB.KeyPrefix = "KV"; + DB.KeyPrefix = "BeetleX"; } private RedisDB DB = new RedisDB(0); @@ -73,7 +73,7 @@ public async void HGet() var emptyValue = await table.Get("fieldEmpty"); Write(emptyValue); - Assert.Equal(emptyValue, ""); + Assert.Equal(emptyValue, null); var nullResult = await table.Set("fieldNull", null); @@ -81,7 +81,7 @@ public async void HGet() var nullValue = await table.Get("fieldNull"); Write(nullValue); - Assert.Equal(nullValue, ""); + Assert.Equal(nullValue, null); } @@ -134,9 +134,9 @@ public async void HLen() public async void HMGet() { await DB.Flushall(); - var table = DB.CreateHashTable("myhash"); - Write(await table.MSet(("field1", "hello"), ("field2", "world"))); - var values = await table.Get("field1", "field2", "nofield"); +var table = DB.CreateHashTable("myhash"); +Write(await table.MSet(("field1", "hello"), ("field2", "world"))); +var values = await table.Get("field1", "field2", "nofield"); Write(values.Item1); Write(values.Item2); Write(values.Item3); diff --git a/BeetleX.Redis.XUnitTest/SequenceTest.cs b/BeetleX.Redis.XUnitTest/SequenceTest.cs index 9bc6681..06e9084 100644 --- a/BeetleX.Redis.XUnitTest/SequenceTest.cs +++ b/BeetleX.Redis.XUnitTest/SequenceTest.cs @@ -14,6 +14,7 @@ public SequenceTest(ITestOutputHelper output) { this.Console = output; DB.Host.AddWriteHost("localhost"); + DB.KeyPrefix = "BeetleX"; } private RedisDB DB = new RedisDB(0); @@ -50,7 +51,7 @@ public async void ZSCORE() var sequeue = DB.CreateSequence("seq2"); var count = await sequeue.ZAdd((4.14, "bca")); var value = await sequeue.ZScore("bca"); - Assert.Equal(value, 4.14); + Assert.Equal(value.Value, 4.14); var unknownMemberValue = await sequeue.ZScore("unknownseq"); Assert.Equal(unknownMemberValue, null); @@ -64,10 +65,10 @@ public async void ZINCRBY() var sequeue = DB.CreateSequence("seq2"); var count = await sequeue.ZAdd((4.14, member)); var value = await sequeue.ZScore(member); - Assert.Equal(value, 4.14); + Assert.Equal(value.Value, 4.14); await sequeue.ZIncrby(5, member); value = await sequeue.ZScore(member); - Assert.Equal(value, 9.14); + Assert.Equal(value.Value, 9.14); } [Fact] public async void ZCARD() @@ -161,7 +162,7 @@ public async void ZRANK() var sequeue = DB.CreateSequence("seq2"); await sequeue.ZAdd((100, "A1"), (200, "A2"), (300, "A3"), (400, "A4")); var value = await sequeue.ZRank("A4"); - Assert.Equal(value, 3); + Assert.Equal(value.Value, 3); var unknownMemberValue = await sequeue.ZRank("unknownseq"); diff --git a/BeetleX.Redis.XUnitTest/SetsTester.cs b/BeetleX.Redis.XUnitTest/SetsTester.cs index c32bc94..93ba538 100644 --- a/BeetleX.Redis.XUnitTest/SetsTester.cs +++ b/BeetleX.Redis.XUnitTest/SetsTester.cs @@ -17,7 +17,7 @@ public SetsTester(ITestOutputHelper output) DefaultRedis.Instance.Host.AddWriteHost("localhost"); DefaultRedis.Instance.DataFormater = new JsonFormater(); DB = DefaultRedis.Instance.Cloneable(); - DB.KeyPrefix = "HENRY"; + DB.KeyPrefix = "BeetleX"; } private RedisDB DB; diff --git a/src/BeetleX.Redis.csproj b/src/BeetleX.Redis.csproj index 70b851c..7252f21 100644 --- a/src/BeetleX.Redis.csproj +++ b/src/BeetleX.Redis.csproj @@ -2,7 +2,7 @@ netcoreapp2.1;netcoreapp3.1 - 1.2.4 + 1.3.1 beetlex.io henryfan Copyright © beetlex.io 2019-2021 @@ -17,11 +17,11 @@ - + - + diff --git a/src/Command.cs b/src/Command.cs index dee2bac..9538111 100644 --- a/src/Command.cs +++ b/src/Command.cs @@ -29,6 +29,8 @@ static Command() public static byte[] GetMsgHeaderLengthData(int length) { + if (length <= 0) + return null; if (length > MAX_LENGTH_TABLE) return null; return mMsgHeaderLenData[length - 1]; @@ -38,6 +40,8 @@ public static byte[] GetMsgHeaderLengthData(int length) public static byte[] GetBodyHeaderLenData(int length) { + if (length <= 0) + return null; if (length > MAX_LENGTH_TABLE) return null; return mBodyHeaderLenData[length - 1]; diff --git a/src/Commands/HSET.cs b/src/Commands/HSET.cs index 9050add..62df066 100644 --- a/src/Commands/HSET.cs +++ b/src/Commands/HSET.cs @@ -29,6 +29,8 @@ public override void OnExecute() base.OnExecute(); OnWriteKey(Key); AddText(Field); + if (Value == null) + Value = string.Empty; AddData(Value); } diff --git a/src/Commands/SET.cs b/src/Commands/SET.cs index fb0aa80..b5dbff1 100644 --- a/src/Commands/SET.cs +++ b/src/Commands/SET.cs @@ -31,6 +31,8 @@ public override void OnExecute() { base.OnExecute(); OnWriteKey(Key); + if (Data == null) + Data = string.Empty; AddData(Data); if (TimeOut > 0) { diff --git a/src/Commands/ZUNIONSTORE.cs b/src/Commands/ZUNIONSTORE.cs index 69e5f7f..8189f93 100644 --- a/src/Commands/ZUNIONSTORE.cs +++ b/src/Commands/ZUNIONSTORE.cs @@ -37,7 +37,7 @@ public override void OnExecute() AddText(Items.Length); foreach (var item in Items) { - AddText(item.key); + OnWriteKey(item.key); } AddText("WEIGHTS"); foreach (var item in Items) diff --git a/src/RedisDB.cs b/src/RedisDB.cs index 276cf68..0ddf21e 100644 --- a/src/RedisDB.cs +++ b/src/RedisDB.cs @@ -90,6 +90,7 @@ public RedisStream GetStream(string name) public RedisDB Cloneable(IDataFormater dataFormater = null) { var result = new RedisDB(this.DB, dataFormater, this); + result.KeyPrefix = this.KeyPrefix; return result; } @@ -163,7 +164,7 @@ public async Task Execute(Command cmd, params Type[] types) return new Result() { ResultType = ResultType.NetError, Messge = "exceeding maximum number of connections" }; try { - var result = host.Connect(this,client); + var result = host.Connect(this, client); if (result.IsError) { return result; @@ -661,7 +662,7 @@ public async Task PFMerge(string key, params string[] items) throw new RedisException(result.Messge); } - + public async ValueTask Info(InfoSection? section = null) diff --git a/src/Sequence.cs b/src/Sequence.cs index 47472dc..0c53fff 100644 --- a/src/Sequence.cs +++ b/src/Sequence.cs @@ -28,12 +28,14 @@ public async ValueTask ZAdd(params (double, string)[] items) return (long)result.Value; } - public async ValueTask ZScore(string member) + public async ValueTask ZScore(string member) { ZSCORE cmd = new ZSCORE(Key, member); var result = await DB.Execute(cmd, typeof(string)); if (result.IsError) throw new RedisException(result.Messge); + if (result.Value == null) + return null; return System.Convert.ToDouble(result.Value); } @@ -114,12 +116,14 @@ public async ValueTask ZCount(double min, double max) return GetItems(result, withscores); } - public async ValueTask ZRank(string member) + public async ValueTask ZRank(string member) { ZRANK cmd = new ZRANK(Key, member); var result = await DB.Execute(cmd, typeof(string)); if (result.IsError) throw new RedisException(result.Messge); + if (result.Value == null) + return null; return System.Convert.ToInt64(result.Value); } @@ -211,7 +215,7 @@ public ValueTask ZUnionsStore(params string[] keys) return ZUnionsStore(items); } - public ValueTask ZUnionsStore(params (string key, double weight)[] items) + public ValueTask ZUnionsStore(params (string key, double weight)[] items) { return ZUnionsStore(items, ZUNIONSTORE.AggregateType.SUM); }