Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 15e8e41

Browse files
committed
Add common GEO commands + tests
1 parent 5626a22 commit 15e8e41

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

lib/ServiceStack.Interfaces.dll

1 KB
Binary file not shown.

src/ServiceStack.Redis/RedisExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ public static List<string> ToStringList(this byte[][] multiDataList)
146146
return results;
147147
}
148148

149+
public static string[] ToStringArray(this byte[][] multiDataList)
150+
{
151+
if (multiDataList == null)
152+
return new string[0];
153+
154+
var to = new string[multiDataList.Length];
155+
for (int i = 0; i < multiDataList.Length; i++)
156+
{
157+
to[i] = multiDataList[i].FromUtf8Bytes();
158+
}
159+
return to;
160+
}
161+
149162
public static Dictionary<string, string> ToStringDictionary(this byte[][] multiDataList)
150163
{
151164
if (multiDataList == null)

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,19 +2213,32 @@ public long GeoAdd(string key, double longitude, double latitude, string member)
22132213
return SendExpectLong(Commands.GeoAdd, key.ToUtf8Bytes(), longitude.ToUtf8Bytes(), latitude.ToUtf8Bytes(), member.ToUtf8Bytes());
22142214
}
22152215

2216-
public long GeoAdd(string key, RedisGeo[] geoPoints)
2216+
public long GeoAdd(string key, params RedisGeo[] geoPoints)
22172217
{
2218-
throw new NotImplementedException();
2218+
var members = new byte[geoPoints.Length * 3][];
2219+
for (var i = 0; i < geoPoints.Length; i++)
2220+
{
2221+
var geoPoint = geoPoints[i];
2222+
members[i * 3 + 0] = geoPoint.Longitude.ToUtf8Bytes();
2223+
members[i * 3 + 1] = geoPoint.Latitude.ToUtf8Bytes();
2224+
members[i * 3 + 2] = geoPoint.Member.ToUtf8Bytes();
2225+
}
2226+
2227+
var cmdWithArgs = MergeCommandWithArgs(Commands.GeoAdd, key.ToUtf8Bytes(), members);
2228+
return SendExpectLong(cmdWithArgs);
22192229
}
22202230

22212231
public double GeoDist(string key, string fromMember, string toMember, string unit = null)
22222232
{
2223-
throw new NotImplementedException();
2233+
return unit == null
2234+
? SendExpectDouble(Commands.GeoDist, key.ToUtf8Bytes(), fromMember.ToUtf8Bytes(), toMember.ToUtf8Bytes())
2235+
: SendExpectDouble(Commands.GeoDist, key.ToUtf8Bytes(), fromMember.ToUtf8Bytes(), toMember.ToUtf8Bytes(), unit.ToUtf8Bytes());
22242236
}
22252237

2226-
public byte[][] GeoHash(string key, params string[] members)
2238+
public string[] GeoHash(string key, params string[] members)
22272239
{
2228-
throw new NotImplementedException();
2240+
var cmdWithArgs = MergeCommandWithArgs(Commands.GeoHash, key.ToUtf8Bytes(), members.Map(x => x.ToUtf8Bytes()).ToArray());
2241+
return SendExpectMultiData(cmdWithArgs).ToStringArray();
22292242
}
22302243

22312244
public List<RedisGeo> GeoPos(string key, params string[] members)

tests/ServiceStack.Redis.Tests/RedisGeoTests.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ServiceStack.Redis.Tests
55
[TestFixture]
66
public class RedisGeoTests
77
{
8-
private RedisNativeClient redis;
8+
private readonly RedisNativeClient redis;
99

1010
public RedisGeoTests()
1111
{
@@ -21,6 +21,7 @@ public void TestFixtureTearDown()
2121
[Test]
2222
public void Can_GeoAdd_and_GeoPos()
2323
{
24+
redis.FlushDb();
2425
redis.GeoAdd("Sicily", 13.361389, 38.115556, "Palermo");
2526
var results = redis.GeoPos("Sicily", "Palermo");
2627

@@ -29,5 +30,49 @@ public void Can_GeoAdd_and_GeoPos()
2930
Assert.That(results[0].Latitude, Is.EqualTo(38.115556).Within(.1));
3031
Assert.That(results[0].Member, Is.EqualTo("Palermo"));
3132
}
33+
34+
[Test]
35+
public void Can_GeoAdd_and_GeoPos_multiple()
36+
{
37+
redis.FlushDb();
38+
redis.GeoAdd("Sicily",
39+
new RedisGeo(13.361389, 38.115556, "Palermo"),
40+
new RedisGeo(15.087269, 37.502669, "Catania"));
41+
42+
var results = redis.GeoPos("Sicily", "Palermo", "Catania");
43+
44+
Assert.That(results.Count, Is.EqualTo(2));
45+
Assert.That(results[0].Longitude, Is.EqualTo(13.361389).Within(.1));
46+
Assert.That(results[0].Latitude, Is.EqualTo(38.115556).Within(.1));
47+
Assert.That(results[0].Member, Is.EqualTo("Palermo"));
48+
49+
Assert.That(results[1].Longitude, Is.EqualTo(15.087269).Within(.1));
50+
Assert.That(results[1].Latitude, Is.EqualTo(37.502669).Within(.1));
51+
Assert.That(results[1].Member, Is.EqualTo("Catania"));
52+
}
53+
54+
[Test]
55+
public void Can_GeoDist()
56+
{
57+
redis.FlushDb();
58+
redis.GeoAdd("Sicily",
59+
new RedisGeo(13.361389, 38.115556, "Palermo"),
60+
new RedisGeo(15.087269, 37.502669, "Catania"));
61+
62+
var distance = redis.GeoDist("Sicily", "Palermo", "Catania");
63+
Assert.That(distance, Is.EqualTo(166274.15156960039).Within(.1));
64+
}
65+
66+
[Test]
67+
public void Can_GeoHash()
68+
{
69+
redis.GeoAdd("Sicily",
70+
new RedisGeo(13.361389, 38.115556, "Palermo"),
71+
new RedisGeo(15.087269, 37.502669, "Catania"));
72+
73+
var hashes = redis.GeoHash("Sicily", "Palermo", "Catania");
74+
Assert.That(hashes[0], Is.EqualTo("sqc8b49rny0"));
75+
Assert.That(hashes[1], Is.EqualTo("sqdtr74hyu0"));
76+
}
3277
}
3378
}

0 commit comments

Comments
 (0)