Skip to content

Commit cc7112a

Browse files
author
rstam
committed
CSHARP-815: GeoNearPoint now *only* wraps alternate point representations for the GeoNear command. The implementation of a 2D point was moved to XYPoint. Replaced From methods in GeoNearPoint with implicit conversions.
1 parent 705d0c8 commit cc7112a

File tree

6 files changed

+140
-73
lines changed

6 files changed

+140
-73
lines changed

MongoDB.Driver/GeoHaystackSearchArgs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class GeoHaystackSearchArgs
3131
private int? _limit;
3232
private double? _maxDistance;
3333
private TimeSpan? _maxTime;
34-
private GeoNearPoint.Legacy _near;
34+
private XYPoint _near;
3535

3636
// public properties
3737
/// <summary>
@@ -100,7 +100,7 @@ public TimeSpan? MaxTime
100100
/// <value>
101101
/// The location near which to search.
102102
/// </value>
103-
public GeoNearPoint.Legacy Near
103+
public XYPoint Near
104104
{
105105
get { return _near; }
106106
set { _near = value; }

MongoDB.Driver/GeoNearPoint.cs

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,138 +21,142 @@
2121
namespace MongoDB.Driver
2222
{
2323
/// <summary>
24-
/// Represents a GeoNearLocation (either a legacy coordinate pair or a GeoJsonPoint).
24+
/// Represents a GeoNearPoint (wraps either an XYPoint or a GeoJsonPoint).
2525
/// </summary>
2626
public abstract class GeoNearPoint
2727
{
28-
// public static methods
28+
// implicit conversions
2929
/// <summary>
30-
/// Creates a GeoNearLocation.GeoJson{TCoordinates} from a GeoJsonPoint{TCoordinates}.
30+
/// Implicit conversion to wrap an XYPoint in a GeoNearPoint value.
3131
/// </summary>
32-
/// <typeparam name="TCoordinates">The type of the coordinates.</typeparam>
33-
/// <param name="point">The GeoJson point.</param>
34-
/// <returns>A GeoNearLocation.GeoJson{TCoordinates}.</returns>
35-
public static GeoNearPoint.GeoJson<TCoordinates> From<TCoordinates>(GeoJsonPoint<TCoordinates> point) where TCoordinates : GeoJsonCoordinates
32+
/// <param name="value">The value.</param>
33+
/// <returns>A GeoNearPoint value.</returns>
34+
public static implicit operator GeoNearPoint(XYPoint value)
3635
{
37-
return new GeoJson<TCoordinates>(point);
36+
return new Legacy(value);
3837
}
3938

4039
/// <summary>
41-
/// Creates a GeoNearPoint from legacy x and y coordinates.
40+
/// Implicit conversion to wrap a 2D GeoJson point in a GeoNearPoint value.
4241
/// </summary>
43-
/// <param name="x">The x value.</param>
44-
/// <param name="y">The y value.</param>
45-
/// <returns></returns>
46-
public static GeoNearPoint.Legacy From(double x, double y)
42+
/// <param name="value">The value.</param>
43+
/// <returns>A GeoNearPoint value.</returns>
44+
public static implicit operator GeoNearPoint(GeoJsonPoint<GeoJson2DCoordinates> value)
45+
{
46+
return new GeoJson<GeoJson2DCoordinates>(value);
47+
}
48+
49+
/// <summary>
50+
/// Implicit conversion to wrap a 2D GeoJson point in a GeoNearPoint value.
51+
/// </summary>
52+
/// <param name="value">The value.</param>
53+
/// <returns>A GeoNearPoint value.</returns>
54+
public static implicit operator GeoNearPoint(GeoJsonPoint<GeoJson2DGeographicCoordinates> value)
4755
{
48-
return new Legacy(x, y);
56+
return new GeoJson<GeoJson2DGeographicCoordinates>(value);
4957
}
5058

51-
// public methods
5259
/// <summary>
53-
/// Converts the GeoNearLocation into a BsonValue for the GeoNear command.
60+
/// Implicit conversion to wrap a 2D GeoJson point in a GeoNearPoint value.
61+
/// </summary>
62+
/// <param name="value">The value.</param>
63+
/// <returns>A GeoNearPoint value.</returns>
64+
public static implicit operator GeoNearPoint(GeoJsonPoint<GeoJson2DProjectedCoordinates> value)
65+
{
66+
return new GeoJson<GeoJson2DProjectedCoordinates>(value);
67+
}
68+
69+
// internal methods
70+
/// <summary>
71+
/// Converts the GeoNearPoint into a BsonValue for the GeoNear command.
5472
/// </summary>
5573
/// <returns></returns>
56-
public abstract BsonValue ToGeoNearCommandField();
74+
internal abstract BsonValue ToGeoNearCommandValue();
5775

5876
// nested classes
5977
/// <summary>
60-
/// Represents a GeoNearLocation expressed as a legacy coordinate pair.
78+
/// Represents a GeoNearPoint that wraps an XYPoint.
6179
/// </summary>
6280
public class Legacy : GeoNearPoint
6381
{
6482
// private fields
65-
private readonly double _x;
66-
private readonly double _y;
83+
private readonly XYPoint _value;
6784

6885
// constructors
6986
/// <summary>
7087
/// Initializes a new instance of the <see cref="Legacy"/> class.
7188
/// </summary>
72-
/// <param name="x">The x value.</param>
73-
/// <param name="y">The y value.</param>
74-
public Legacy(double x, double y)
89+
/// <param name="value">The value.</param>
90+
public Legacy(XYPoint value)
7591
{
76-
_x = x;
77-
_y = y;
92+
_value = value;
7893
}
7994

8095
// public properties
8196
/// <summary>
82-
/// Gets the X value.
83-
/// </summary>
84-
/// <value>
85-
/// The X value.
86-
/// </value>
87-
public double X
88-
{
89-
get { return _x; }
90-
}
91-
92-
/// <summary>
93-
/// Gets the Y value.
97+
/// Gets the value.
9498
/// </summary>
9599
/// <value>
96-
/// The Y value.
100+
/// The value.
97101
/// </value>
98-
public double Y
102+
public XYPoint Value
99103
{
100-
get { return _y; }
104+
get { return _value; }
101105
}
102106

103-
// public methods
107+
// internal methods
104108
/// <summary>
105-
/// Converts the GeoNearLocation into a BsonValue for the GeoNear command.
109+
/// Converts the GeoNearPoint into a BsonValue for the GeoNear command.
106110
/// </summary>
107-
/// <returns></returns>
108-
public override BsonValue ToGeoNearCommandField()
111+
/// <returns>A BsonValue.</returns>
112+
internal override BsonValue ToGeoNearCommandValue()
109113
{
110-
return new BsonArray { _x, _y };
114+
return new BsonArray { _value.X, _value.Y };
111115
}
112116
}
113117

114118
/// <summary>
115-
/// Represents a GeoNearLocation expressed as a GeoJsonPoint.
119+
/// Represents a GeoNearPoint that wraps a GeoJsonPoint.
116120
/// </summary>
117121
/// <typeparam name="TCoordinates">The type of the coordinates.</typeparam>
118122
public class GeoJson<TCoordinates> : GeoNearPoint where TCoordinates : GeoJsonCoordinates
119123
{
120124
// private fields
121-
private readonly GeoJsonPoint<TCoordinates> _point;
125+
private readonly GeoJsonPoint<TCoordinates> _value;
122126

123127
// constructors
124128
/// <summary>
125129
/// Initializes a new instance of the <see cref="GeoJson{TCoordinates}"/> class.
126130
/// </summary>
127-
/// <param name="point">The point.</param>
128-
public GeoJson(GeoJsonPoint<TCoordinates> point)
131+
/// <param name="value">The value.</param>
132+
public GeoJson(GeoJsonPoint<TCoordinates> value)
129133
{
130-
_point = point;
134+
_value = value;
131135
}
132136

133137
// public properties
134138
/// <summary>
135-
/// Gets the point.
139+
/// Gets the value.
136140
/// </summary>
137141
/// <value>
138-
/// The point.
142+
/// The value.
139143
/// </value>
140-
public GeoJsonPoint<TCoordinates> Point
144+
public GeoJsonPoint<TCoordinates> Value
141145
{
142-
get { return _point; }
146+
get { return _value; }
143147
}
144148

145-
// public methods
149+
// internal methods
146150
/// <summary>
147-
/// Converts the GeoNearLocation into a BsonValue for the GeoNear command.
151+
/// Converts the GeoNearPoint into a BsonValue for the GeoNear command.
148152
/// </summary>
149-
/// <returns></returns>
150-
public override BsonValue ToGeoNearCommandField()
153+
/// <returns>A BsonValue.</returns>
154+
internal override BsonValue ToGeoNearCommandValue()
151155
{
152156
var document = new BsonDocument();
153157
using (var writer = new BsonDocumentWriter(document, BsonDocumentWriterSettings.Defaults))
154158
{
155-
new GeoJsonPointSerializer<TCoordinates>().Serialize(writer, typeof(GeoJsonPoint<TCoordinates>), _point, null);
159+
new GeoJsonPointSerializer<TCoordinates>().Serialize(writer, typeof(GeoJsonPoint<TCoordinates>), _value, null);
156160
}
157161
return document;
158162
}

MongoDB.Driver/MongoCollection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ public virtual GeoHaystackSearchResult<TDocument> GeoHaystackSearchAs<TDocument>
877877
var optionsDocument = options.ToBsonDocument();
878878
var args = new GeoHaystackSearchArgs
879879
{
880-
Near = GeoNearPoint.From(x, y),
880+
Near = new XYPoint(x, y),
881881
MaxDistance = optionsDocument.Contains("maxDistance") ? (int?)optionsDocument["maxDistance"].ToInt32() : null,
882882
Limit = optionsDocument.Contains("limit") ? (int?)optionsDocument["limit"].ToInt32() : null
883883
};
@@ -966,7 +966,7 @@ public virtual GeoNearResult<TDocument> GeoNearAs<TDocument>(GeoNearArgs args)
966966
var command = new CommandDocument
967967
{
968968
{ "geoNear", _name },
969-
{ "near", args.Near.ToGeoNearCommandField() },
969+
{ "near", args.Near.ToGeoNearCommandValue() },
970970
{ "limit", () => args.Limit.Value, args.Limit.HasValue }, // optional
971971
{ "maxDistance", () => args.MaxDistance.Value, args.MaxDistance.HasValue }, // optional
972972
{ "query", () => BsonDocumentWrapper.Create(args.Query), args.Query != null }, // optional
@@ -1023,7 +1023,7 @@ public virtual GeoNearResult<TDocument> GeoNearAs<TDocument>(
10231023
var optionsDocument = options.ToBsonDocument();
10241024
var args = new GeoNearArgs
10251025
{
1026-
Near = GeoNearPoint.From(x, y),
1026+
Near = new XYPoint(x, y),
10271027
Limit = limit,
10281028
Query = query,
10291029
DistanceMultiplier = optionsDocument.Contains("distanceMultiplier") ? (double?)optionsDocument["distanceMultiplier"].ToDouble() : null,

MongoDB.Driver/MongoDB.Driver.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@
445445
<Compile Include="SafeMode.cs" />
446446
<Compile Include="UpdateFlags.cs" />
447447
<Compile Include="ExternalEvidence.cs" />
448+
<Compile Include="XYPoint.cs" />
448449
</ItemGroup>
449450
<ItemGroup>
450451
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">

MongoDB.Driver/XYPoint.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* Copyright 2010-2014 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
namespace MongoDB.Driver
17+
{
18+
/// <summary>
19+
/// Represents a 2D point represented using x, y coordinates.
20+
/// </summary>
21+
public class XYPoint
22+
{
23+
// private fields
24+
private readonly double _x;
25+
private readonly double _y;
26+
27+
// constructors
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="XYPoint"/> class.
30+
/// </summary>
31+
/// <param name="x">The x value.</param>
32+
/// <param name="y">The y value.</param>
33+
public XYPoint(double x, double y)
34+
{
35+
_x = x;
36+
_y = y;
37+
}
38+
39+
// public properties
40+
/// <summary>
41+
/// Gets the X value.
42+
/// </summary>
43+
/// <value>
44+
/// The X value.
45+
/// </value>
46+
public double X
47+
{
48+
get { return _x; }
49+
}
50+
51+
/// <summary>
52+
/// Gets the Y value.
53+
/// </summary>
54+
/// <value>
55+
/// The Y value.
56+
/// </value>
57+
public double Y
58+
{
59+
get { return _y; }
60+
}
61+
}
62+
}

MongoDB.DriverUnitTests/MongoCollectionTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ public void TestGeoHaystackSearch()
12701270

12711271
var args = new GeoHaystackSearchArgs
12721272
{
1273-
Near = GeoNearPoint.From(33, 33),
1273+
Near = new XYPoint(33, 33),
12741274
AdditionalFieldName = "Type",
12751275
AdditionalFieldValue = "restaurant",
12761276
Limit = 30,
@@ -1312,7 +1312,7 @@ public void TestGeoHaystackSearchWithMaxTime()
13121312
failpoint.SetAlwaysOn();
13131313
var args = new GeoHaystackSearchArgs
13141314
{
1315-
Near = GeoNearPoint.From(33, 33),
1315+
Near = new XYPoint(33, 33),
13161316
AdditionalFieldName = "Type",
13171317
AdditionalFieldValue = "restaurant",
13181318
Limit = 30,
@@ -1342,7 +1342,7 @@ public void TestGeoHaystackSearch_Typed()
13421342

13431343
var args = new GeoHaystackSearchArgs
13441344
{
1345-
Near = GeoNearPoint.From(33, 33),
1345+
Near = new XYPoint(33, 33),
13461346
Limit = 30,
13471347
MaxDistance = 6
13481348
}
@@ -1376,7 +1376,7 @@ public void TestGeoNear()
13761376

13771377
var args = new GeoNearArgs
13781378
{
1379-
Near = GeoNearPoint.From(0, 0),
1379+
Near = new XYPoint(0, 0),
13801380
Limit = 100,
13811381
DistanceMultiplier = 1,
13821382
MaxDistance = 100
@@ -1422,7 +1422,7 @@ public void TestGeoNearGeneric()
14221422

14231423
var args = new GeoNearArgs
14241424
{
1425-
Near = GeoNearPoint.From(0, 0),
1425+
Near = new XYPoint(0, 0),
14261426
Limit = 100,
14271427
DistanceMultiplier = 1,
14281428
MaxDistance = 100
@@ -1466,7 +1466,7 @@ public void TestGeoNearSphericalFalse()
14661466

14671467
var args = new GeoNearArgs
14681468
{
1469-
Near = GeoNearPoint.From(-74.0, 40.74),
1469+
Near = new XYPoint(-74.0, 40.74),
14701470
Limit = 100,
14711471
Spherical = false
14721472
};
@@ -1523,7 +1523,7 @@ public void TestGeoNearSphericalTrue()
15231523

15241524
var args = new GeoNearArgs
15251525
{
1526-
Near = GeoNearPoint.From(-74.0, 40.74),
1526+
Near = new XYPoint(-74.0, 40.74),
15271527
Limit = 100,
15281528
Spherical = true
15291529
};
@@ -1581,7 +1581,7 @@ public void TestGeoNearWithGeoJsonPoints()
15811581

15821582
var args = new GeoNearArgs
15831583
{
1584-
Near = GeoNearPoint.From(GeoJson.Point(GeoJson.Geographic(-74.0, 40.74))),
1584+
Near = GeoJson.Point(GeoJson.Geographic(-74.0, 40.74)),
15851585
Spherical = true
15861586
};
15871587
var result = _collection.GeoNearAs<PlaceGeoJson>(args);
@@ -1624,7 +1624,7 @@ public void TestGeoNearWithMaxTime()
16241624
failpoint.SetAlwaysOn();
16251625
var args = new GeoNearArgs
16261626
{
1627-
Near = GeoNearPoint.From(0, 0),
1627+
Near = new XYPoint(0, 0),
16281628
MaxTime = TimeSpan.FromMilliseconds(1)
16291629
};
16301630
Assert.Throws<ExecutionTimeoutException>(() => _collection.GeoNearAs<BsonDocument>(args));

0 commit comments

Comments
 (0)