Commit c4e9fd05 authored by mgravell's avatar mgravell

Initial tidy ups

parent 4e4bcae6
......@@ -11,12 +11,14 @@ public void GeoAddEveryWay()
{
using (var conn = Create())
{
var key = "Sicily";
var db = conn.GetDatabase(3);
var added1 = db.GeoAdd("Sicily", 14.361389, 39.115556, "PalermoPlusOne");
db.KeyDelete(key);
var added1 = db.GeoAdd(key, 14.361389, 39.115556, "PalermoPlusOne");
var geo1 = new GeoEntry(13.361389, 38.115556, "Palermo");
var geo2 = new GeoEntry(15.087269, 37.502669, "Catania");
var added2 = db.GeoAdd("Sicily",new GeoEntry[] {geo1,geo2});
Assert.IsTrue(added1 & (added2==2));
var added2 = db.GeoAdd(key, new GeoEntry[] { geo1, geo2 });
Assert.IsTrue(added1 & (added2 == 2));
}
}
......@@ -26,11 +28,13 @@ public void GetGeoDist()
using (var conn = Create())
{
var db = conn.GetDatabase(3);
var key = "Sicily";
db.KeyDelete(key);
var geo1 = new GeoEntry(13.361389, 38.115556, "Palermo");
var geo2 = new GeoEntry(15.087269, 37.502669, "Catania");
var added2 = db.GeoAdd("Sicily", new GeoEntry[] { geo1, geo2 });
var val = db.GeoDistance("Sicily", "Palermo", "Catania",GeoUnit.Meters);
Assert.Equals(166274.15156960039, (double) val);
var added2 = db.GeoAdd(key, new GeoEntry[] { geo1, geo2 });
var val = db.GeoDistance(key, "Palermo", "Catania", GeoUnit.Meters);
Assert.Equals(166274.15156960039, (double)val);
}
}
}
......
......@@ -48,28 +48,28 @@ public struct GeoRadiusResult
public double? DistanceFromCenter { get; }
/// <summary>
/// The Geo Hash of the matched member as an integer. (The key in the sorted set)
/// The raw geohash-encoded sorted set score of the item, in the form of a 52 bit unsigned integer. This is only useful for low level hacks or debugging and is otherwise of little interest for the general user.
/// </summary>
public long? GeoHash { get; }
public long? ScoreHash { get; }
/// <summary>
/// The coordinates of the matched member.
/// </summary>
public GeoPosition? GeoPosition { get; }
public GeoPosition? Location { get; }
/// <summary>
/// Returns a new GeoRadiusResult
/// Creates a new GeoRadiusResult
/// </summary>
public GeoRadiusResult(RedisValue member,GeoRadius command,double? distanceFromCenter,long? geoHash,GeoPosition? geoPosition)
internal GeoRadiusResult(RedisValue member, GeoRadius command, double? distanceFromCenter, long? scoreHash, GeoPosition? location)
{
Member = member;
Command = command;
DistanceFromCenter = distanceFromCenter;
GeoHash = geoHash;
GeoPosition = geoPosition;
ScoreHash = scoreHash;
Location = location;
}
}
......@@ -86,7 +86,7 @@ public class GeoRadius
/// <summary>
/// The center point to base the search.
/// </summary>
public GeoPosition GeoPosition { get; }
public GeoPosition Position { get; }
/// <summary>
/// The key to use.
......@@ -101,7 +101,7 @@ public class GeoRadius
/// <summary>
/// The possible options for the GeoRadius command
/// </summary>
public GeoRadiusOptions GeoRadiusOptions { get; }
public GeoRadiusOptions Options { get; }
/// <summary>
/// The maximum number of results to return.
......@@ -112,19 +112,22 @@ public class GeoRadius
/// <summary>
/// Creates a new GeoRadius
/// </summary>
public GeoRadius(RedisKey key,GeoPosition geoPosition,double radius,int maxReturnCount =-1,GeoUnit unit = GeoUnit.Meters,GeoRadiusOptions geoRadiusOptions = (GeoRadiusOptions.WithCoordinates | GeoRadiusOptions.WithDistance | GeoRadiusOptions.WithGeoHash))
public GeoRadius(RedisKey key, GeoPosition geoPosition, double radius, int maxReturnCount = -1, GeoUnit unit = GeoUnit.Meters, GeoRadiusOptions geoRadiusOptions = (GeoRadiusOptions.WithCoordinates | GeoRadiusOptions.WithDistance | GeoRadiusOptions.WithGeoHash))
{
Key = key;
GeoPosition = geoPosition;
Position = geoPosition;
Radius = radius;
Unit = unit;
GeoRadiusOptions = geoRadiusOptions;
Options = geoRadiusOptions;
MaxReturnCount = maxReturnCount;
}
/// <summary>
/// Indicates if the specified flag is set
/// </summary>
public bool HasFlag(GeoRadiusOptions flag)
{
return (GeoRadiusOptions & flag) != 0;
return (Options & flag) != 0;
}
}
......@@ -146,9 +149,7 @@ public struct GeoPosition : IEquatable<GeoPosition>
/// <summary>
/// Creates a new GeoPosition
/// </summary>
/// <param name="longitude"></param>
/// <param name="latitude"></param>
public GeoPosition(double longitude,double latitude)
public GeoPosition(double longitude, double latitude)
{
Longitude = longitude;
Latitude = latitude;
......@@ -196,7 +197,7 @@ public bool Equals(GeoPosition value)
/// </summary>
public static bool operator !=(GeoPosition x, GeoPosition y)
{
return x.Longitude!= y.Longitude || x.Latitude != y.Latitude;
return x.Longitude != y.Longitude || x.Latitude != y.Latitude;
}
}
......@@ -209,39 +210,39 @@ public struct GeoEntry : IEquatable<GeoEntry>
/// <summary>
/// The name of the geo entry
/// </summary>
public string Member { get; }
public RedisValue Member { get; }
/// <summary>
/// Describes the longitude and latitude of a GeoEntry
/// </summary>
public GeoPosition Point { get; }
public GeoPosition Location { get; }
/// <summary>
/// Initializes a GeoEntry value
/// </summary>
public GeoEntry(double longitude,double latitude,RedisValue member)
public GeoEntry(double longitude, double latitude, RedisValue member)
{
Member = member;
Point = new GeoPosition(longitude, latitude);
Location = new GeoPosition(longitude, latitude);
}
/// <summary>
/// The longitude of the geo entry
/// </summary>
public double Longitude => Point.Longitude;
public double Longitude => Location.Longitude;
/// <summary>
/// The latitude of the geo entry
/// </summary>
public double Latitude => Point.Latitude;
public double Latitude => Location.Latitude;
/// <summary>
/// See Object.ToString()
/// </summary>
public override string ToString()
{
{
return $"({Longitude},{Latitude})={Member}";
}
/// <summary>
......@@ -249,7 +250,7 @@ public override string ToString()
/// </summary>
public override int GetHashCode()
{
return Point.GetHashCode() ^ Member.GetHashCode();
return Location.GetHashCode() ^ Member.GetHashCode();
}
/// <summary>
/// Compares two values for equality
......@@ -270,14 +271,14 @@ public bool Equals(GeoEntry value)
/// </summary>
public static bool operator ==(GeoEntry x, GeoEntry y)
{
return Equals(x.Point, y.Point) && x.Member == y.Member;
return Equals(x.Location, y.Location) && x.Member == y.Member;
}
/// <summary>
/// Compares two values for non-equality
/// </summary>
public static bool operator !=(GeoEntry x, GeoEntry y)
{
return !Equals(x.Point, y.Point) || x.Member != y.Member;
return !Equals(x.Location, y.Location) || x.Member != y.Member;
}
}
}
\ No newline at end of file
......@@ -86,14 +86,14 @@ public interface IDatabase : IRedis, IDatabaseAsync
/// </summary>
/// <returns>The command returns an array where each element is the Geohash corresponding to each member name passed as argument to the command.</returns>
/// <remarks>http://redis.io/commands/geohash</remarks>
string[] GeoHash(RedisKey key, string[] members, CommandFlags flags = CommandFlags.None);
string[] GeoHash(RedisKey key, RedisValue[] members, CommandFlags flags = CommandFlags.None);
/// <summary>
/// Return valid Geohash strings representing the position of one or more elements in a sorted set value representing a geospatial index (where elements were added using GEOADD).
/// </summary>
/// <returns>The command returns an array where each element is the Geohash corresponding to each member name passed as argument to the command.</returns>
/// <remarks>http://redis.io/commands/geohash</remarks>
string[] GeoHash(RedisKey key, string member, CommandFlags flags = CommandFlags.None);
string GeoHash(RedisKey key, RedisValue member, CommandFlags flags = CommandFlags.None);
/// <summary>
......@@ -101,14 +101,14 @@ public interface IDatabase : IRedis, IDatabaseAsync
/// </summary>
/// <returns>The command returns an array where each element is a two elements array representing longitude and latitude (x,y) of each member name passed as argument to the command.Non existing elements are reported as NULL elements of the array.</returns>
/// <remarks>http://redis.io/commands/geopos</remarks>
GeoPosition?[] GeoPos(RedisKey key, string[] members, CommandFlags flags = CommandFlags.None);
GeoPosition?[] GeoPos(RedisKey key, RedisValue[] members, CommandFlags flags = CommandFlags.None);
/// <summary>
/// Return the positions (longitude,latitude) of all the specified members of the geospatial index represented by the sorted set at key.
/// </summary>
/// <returns>The command returns an array where each element is a two elements array representing longitude and latitude (x,y) of each member name passed as argument to the command.Non existing elements are reported as NULL elements of the array.</returns>
/// <remarks>http://redis.io/commands/geopos</remarks>
GeoPosition? GeoPos(RedisKey key, string member, CommandFlags flags = CommandFlags.None);
GeoPosition? GeoPos(RedisKey key, RedisValue member, CommandFlags flags = CommandFlags.None);
/// <summary>
/// Return the members of a sorted set populated with geospatial information using GEOADD, which are within the borders of the area specified with the center location and the maximum distance from the center (the radius).
......
......@@ -51,22 +51,22 @@ public double GeoDistance(RedisKey key, RedisValue value0, RedisValue value1, Ge
return Inner.GeoDistance(ToInner(key), value0, value1, geoUnit, flags);
}
public string[] GeoHash(RedisKey key, string[] members, CommandFlags flags = CommandFlags.None)
public string[] GeoHash(RedisKey key, RedisValue[] members, CommandFlags flags = CommandFlags.None)
{
return Inner.GeoHash(key, members, flags);
}
public string[] GeoHash(RedisKey key, string member, CommandFlags flags = CommandFlags.None)
public string GeoHash(RedisKey key, RedisValue member, CommandFlags flags = CommandFlags.None)
{
return Inner.GeoHash(key, member, flags);
}
public GeoPosition?[] GeoPos(RedisKey key, string[] members, CommandFlags flags = CommandFlags.None)
public GeoPosition?[] GeoPos(RedisKey key, RedisValue[] members, CommandFlags flags = CommandFlags.None)
{
return Inner.GeoPos(key, members, flags);
}
public GeoPosition? GeoPos(RedisKey key, string member, CommandFlags flags = CommandFlags.None)
public GeoPosition? GeoPos(RedisKey key, RedisValue member, CommandFlags flags = CommandFlags.None)
{
return Inner.GeoPos(key, member, flags);
}
......
......@@ -355,8 +355,8 @@ public static Message Create(int db, CommandFlags flags, RedisCommand command, R
var redisValues = new List<RedisValue>();
foreach (var value in values)
{
redisValues.Add(value.Point.Longitude);
redisValues.Add(value.Point.Latitude);
redisValues.Add(value.Location.Longitude);
redisValues.Add(value.Location.Latitude);
redisValues.Add(value.Member);
}
return new CommandKeyValuesMessage(db,flags,command,key,redisValues.ToArray());
......
......@@ -76,12 +76,10 @@ public double GeoDistance(RedisKey key, RedisValue value0, RedisValue value1, Ge
return (double)ExecuteSync(msg, ResultProcessor.RedisValue);
}
public string[] GeoHash(RedisKey key, string[] members, CommandFlags flags = CommandFlags.None)
public string[] GeoHash(RedisKey key, RedisValue[] members, CommandFlags flags = CommandFlags.None)
{
if(members == null)throw new ArgumentNullException(nameof(members));
var redisValues = new RedisValue[members.Length];
for (var i = 0; i < members.Length; i++) redisValues[i] = members[i];
var msg = Message.Create(Database, flags, RedisCommand.GEOHASH, key, redisValues);
var msg = Message.Create(Database, flags, RedisCommand.GEOHASH, key, members);
var results = ExecuteSync(msg, ResultProcessor.RedisValueArray);
var retArray = new string[results.Length];
for (int i = 0; i < results.Length; i++)
......@@ -91,30 +89,28 @@ public string[] GeoHash(RedisKey key, string[] members, CommandFlags flags = Com
return retArray;
}
public string[] GeoHash(RedisKey key, string member, CommandFlags flags = CommandFlags.None)
public string GeoHash(RedisKey key, RedisValue member, CommandFlags flags = CommandFlags.None)
{
return GeoHash(key, new[] {member}, flags);
return GeoHash(key, new[] { member }, flags).FirstOrDefault();
}
public GeoPosition?[] GeoPos(RedisKey key, string[] members, CommandFlags flags = CommandFlags.None)
public GeoPosition?[] GeoPos(RedisKey key, RedisValue[] members, CommandFlags flags = CommandFlags.None)
{
if (members == null) throw new ArgumentNullException(nameof(members));
var redisValues = new RedisValue[members.Length];
for (var i = 0; i < members.Length; i++) redisValues[i] = members[i];
var msg = Message.Create(Database, flags, RedisCommand.GEOPOS, key, redisValues);
var msg = Message.Create(Database, flags, RedisCommand.GEOPOS, key, members);
return ExecuteSync(msg, ResultProcessor.RedisGeoPosition);
}
public GeoPosition? GeoPos(RedisKey key, string member, CommandFlags flags = CommandFlags.None)
public GeoPosition? GeoPos(RedisKey key, RedisValue member, CommandFlags flags = CommandFlags.None)
{
return GeoPos(key, new string[] {member}, flags).FirstOrDefault();
return GeoPos(key, new RedisValue[] {member}, flags).FirstOrDefault();
}
public GeoRadiusResult[] GeoRadius(RedisKey key, GeoRadius geoRadius,CommandFlags flags = CommandFlags.None)
{
var redisValues = new List<RedisValue>();
redisValues.Add(geoRadius.GeoPosition.Longitude);
redisValues.Add(geoRadius.GeoPosition.Latitude);
redisValues.Add(geoRadius.Position.Longitude);
redisValues.Add(geoRadius.Position.Latitude);
redisValues.Add(geoRadius.Radius);
redisValues.Add(_redisUnits[(int)geoRadius.Unit]);
if(geoRadius.HasFlag(GeoRadiusOptions.WithCoordinates))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment