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

Commit 3310e30

Browse files
committed
Add RedisClient descriptive GEO API's with tests
1 parent b04225a commit 3310e30

File tree

6 files changed

+488
-54
lines changed

6 files changed

+488
-54
lines changed

lib/ServiceStack.Interfaces.dll

1 KB
Binary file not shown.

src/ServiceStack.Redis/RedisClient_Set.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,64 @@ public List<string> GetSortedEntryValues(string setId, int startingFrom, int end
6868
return multiDataList.ToStringList();
6969
}
7070

71+
public long AddGeoMember(string key, double longitude, double latitude, string member)
72+
{
73+
return base.GeoAdd(key, longitude, latitude, member);
74+
}
75+
76+
public long AddGeoMembers(string key, params RedisGeo[] geoPoints)
77+
{
78+
return base.GeoAdd(key, geoPoints);
79+
}
80+
81+
public double CalculateDistanceBetweenGeoMembers(string key, string fromMember, string toMember, string unit = null)
82+
{
83+
return base.GeoDist(key, fromMember, toMember, unit);
84+
}
85+
86+
public string[] GetGeohashes(string key, params string[] members)
87+
{
88+
return base.GeoHash(key, members);
89+
}
90+
91+
public List<RedisGeo> GetGeoCoordinates(string key, params string[] members)
92+
{
93+
return base.GeoPos(key, members);
94+
}
95+
96+
public string[] FindGeoMembersInRadius(string key, double longitude, double latitude, double radius, string unit)
97+
{
98+
var results = base.GeoRadius(key, longitude, latitude, radius, unit);
99+
var to = new string[results.Count];
100+
for (var i = 0; i < results.Count; i++)
101+
{
102+
to[i] = results[i].Member;
103+
}
104+
return to;
105+
}
106+
107+
public List<RedisGeoResult> FindGeoResultsInRadius(string key, double longitude, double latitude, double radius, string unit,
108+
int? count = null, bool? sortByNearest = null)
109+
{
110+
return base.GeoRadius(key, longitude, latitude, radius, unit, withCoords:true, withDist:true, withHash:true, count:count, asc: sortByNearest);
111+
}
112+
113+
public string[] FindGeoMembersInRadius(string key, string member, double radius, string unit)
114+
{
115+
var results = base.GeoRadiusByMember(key, member, radius, unit);
116+
var to = new string[results.Count];
117+
for (var i = 0; i < results.Count; i++)
118+
{
119+
to[i] = results[i].Member;
120+
}
121+
return to;
122+
}
123+
124+
public List<RedisGeoResult> FindGeoResultsInRadius(string key, string member, double radius, string unit, int? count = null, bool? sortByNearest = null)
125+
{
126+
return base.GeoRadiusByMember(key, member, radius, unit, withCoords: true, withDist: true, withHash: true, count: count, asc: sortByNearest);
127+
}
128+
71129
public HashSet<string> GetAllItemsFromSet(string setId)
72130
{
73131
var multiDataList = SMembers(setId);

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,7 +2261,13 @@ public List<RedisGeo> GeoPos(string key, params string[] members)
22612261

22622262
for (var i = 0; i < members.Length; i++)
22632263
{
2264+
if (data.Children.Count <= i)
2265+
break;
2266+
22642267
var entry = data.Children[i];
2268+
if (entry.Children.Count == 0)
2269+
continue;
2270+
22652271
to.Add(new RedisGeo
22662272
{
22672273
Longitude = double.Parse(entry.Children[0].Data.FromUtf8Bytes()),
@@ -2345,10 +2351,75 @@ public List<RedisGeoResult> GeoRadius(string key, double longitude, double latit
23452351
return to;
23462352
}
23472353

2348-
public List<RedisGeoResult> GeoRadiusByMember(string key, double longitude, double latitude, double radius,
2349-
string unit = null, bool withCoords = false, bool withHash = false, int count = 0, bool? asc = null)
2354+
public List<RedisGeoResult> GeoRadiusByMember(string key, string member, double radius, string unit,
2355+
bool withCoords = false, bool withDist = false, bool withHash = false, int? count = null, bool? asc = null)
23502356
{
2351-
throw new NotImplementedException();
2357+
if (key == null)
2358+
throw new ArgumentNullException("key");
2359+
2360+
var args = new List<byte[]>
2361+
{
2362+
member.ToUtf8Bytes(),
2363+
radius.ToUtf8Bytes(),
2364+
Commands.GetUnit(unit),
2365+
};
2366+
2367+
if (withCoords)
2368+
args.Add(Commands.WithCoord);
2369+
if (withDist)
2370+
args.Add(Commands.WithDist);
2371+
if (withHash)
2372+
args.Add(Commands.WithHash);
2373+
2374+
if (count != null)
2375+
{
2376+
args.Add(Commands.Count);
2377+
args.Add(count.Value.ToUtf8Bytes());
2378+
}
2379+
2380+
if (asc == true)
2381+
args.Add(Commands.Asc);
2382+
else if (asc == false)
2383+
args.Add(Commands.Desc);
2384+
2385+
var cmdWithArgs = MergeCommandWithArgs(Commands.GeoRadiusByMember, key.ToUtf8Bytes(), args.ToArray());
2386+
2387+
var to = new List<RedisGeoResult>();
2388+
2389+
if (!(withCoords || withDist || withHash))
2390+
{
2391+
var members = SendExpectMultiData(cmdWithArgs).ToStringArray();
2392+
foreach (var x in members)
2393+
{
2394+
to.Add(new RedisGeoResult { Member = x });
2395+
}
2396+
}
2397+
else
2398+
{
2399+
var data = SendExpectComplexResponse(cmdWithArgs);
2400+
2401+
foreach (var child in data.Children)
2402+
{
2403+
var i = 0;
2404+
var result = new RedisGeoResult { Unit = unit, Member = child.Children[i++].Data.FromUtf8Bytes() };
2405+
2406+
if (withDist)
2407+
result.Distance = double.Parse(child.Children[i++].Data.FromUtf8Bytes());
2408+
2409+
if (withHash)
2410+
result.Hash = long.Parse(child.Children[i++].Data.FromUtf8Bytes());
2411+
2412+
if (withCoords)
2413+
{
2414+
result.Longitude = double.Parse(child.Children[i].Children[0].Data.FromUtf8Bytes());
2415+
result.Latitude = double.Parse(child.Children[i].Children[1].Data.FromUtf8Bytes());
2416+
}
2417+
2418+
to.Add(result);
2419+
}
2420+
}
2421+
2422+
return to;
23522423
}
23532424

23542425
#endregion

0 commit comments

Comments
 (0)