Permalink
Browse files

* Fixed bug with spatial queries in which they weren't setting the qu…

…ery stats properly.

* Fixed bug with spatial queries in which they wouldn't support projections properly
  • Loading branch information...
1 parent c0410bd commit 0e064b7bf01793fd2295394dcd6850f8fbc0f333 @ayende ayende committed Oct 11, 2011
@@ -132,7 +132,7 @@ public abstract class AbstractDocumentQuery<T, TSelf> : IDocumentQueryCustomizat
/// <summary>
/// Holds the query stats
/// </summary>
- protected readonly RavenQueryStatistics queryStats = new RavenQueryStatistics();
+ protected RavenQueryStatistics queryStats = new RavenQueryStatistics();
/// <summary>
/// Get the name of the index being queried
@@ -279,6 +279,9 @@ protected AbstractDocumentQuery(AbstractDocumentQuery<T, TSelf> other)
theWaitForNonStaleResults = other.theWaitForNonStaleResults;
includes = other.includes;
queryListeners = other.queryListeners;
+ queryStats = other.queryStats;
+
+ AfterQueryExecuted(queryStats.UpdateQueryStats);
}
#region TSelf Members
@@ -49,6 +49,7 @@ public class DocumentQuery<T> : AbstractDocumentQuery<T, DocumentQuery<T>>, IDoc
public DocumentQuery(DocumentQuery<T> other)
: base(other)
{
+
}
@@ -57,30 +58,33 @@ public DocumentQuery(DocumentQuery<T> other)
/// </summary>
/// <typeparam name="TProjection">The type of the projection.</typeparam>
/// <param name="fields">The fields.</param>
- public IDocumentQuery<TProjection> SelectFields<TProjection>(string[] fields)
+ public virtual IDocumentQuery<TProjection> SelectFields<TProjection>(string[] fields)
{
- return new DocumentQuery<TProjection>(theSession,
+ var documentQuery = new DocumentQuery<TProjection>(theSession,
#if !SILVERLIGHT
- theDatabaseCommands,
+ theDatabaseCommands,
#endif
#if !NET_3_5
- theAsyncDatabaseCommands,
+ theAsyncDatabaseCommands,
#endif
- indexName, fields,
- queryListeners)
+ indexName, fields,
+ queryListeners)
{
pageSize = pageSize,
theQueryText = new StringBuilder(theQueryText.ToString()),
start = start,
timeout = timeout,
cutoff = cutoff,
+ queryStats = queryStats,
theWaitForNonStaleResults = theWaitForNonStaleResults,
sortByHints = sortByHints,
orderByFields = orderByFields,
groupByFields = groupByFields,
aggregationOp = aggregationOp,
includes = new HashSet<string>(includes)
};
+ documentQuery.AfterQueryExecuted(queryStats.UpdateQueryStats);
+ return documentQuery;
}
/// <summary>
@@ -33,6 +33,11 @@ public SpatialDocumentQuery(DocumentQuery<T> documentQuery, double radius, doubl
lng = longitude;
}
+ public override IDocumentQuery<TProjection> SelectFields<TProjection>(string[] fields)
+ {
+ return new SpatialDocumentQuery<TProjection>((DocumentQuery<TProjection>)base.SelectFields<TProjection>(fields), radius, lat, lng);
+ }
+
/// <summary>
/// Initializes a new instance of the <see cref="SpatialDocumentQuery&lt;T&gt;"/> class.
/// </summary>
View
@@ -0,0 +1,149 @@
+using System;
+using System.Linq;
+using Raven.Abstractions.Indexing;
+using Raven.Client;
+using Raven.Client.Indexes;
+using Raven.Client.Linq;
+using Xunit;
+
+namespace Raven.Tests.Bugs
+{
+ public class SpatialTest : RavenTest
+ {
+ public class MyDocumentItem
+ {
+ public DateTime Date { get; set; }
+ public double? Latitude { get; set; }
+ public double? Longitude { get; set; }
+ }
+
+ public class MyDocument
+ {
+ public string Id { get; set; }
+ public MyDocumentItem[] Items { get; set; }
+ }
+
+ public class MyProjection
+ {
+ public string Id { get; set; }
+ public DateTime Date { get; set; }
+ public double Latitude { get; set; }
+ public double Longitude { get; set; }
+ public object _ { get; set; }
+ }
+
+ public class MyIndex : AbstractIndexCreationTask<MyDocument, MyProjection>
+ {
+ public MyIndex()
+ {
+ Map = docs =>
+ from doc in docs
+ from item in doc.Items
+ let lat = item.Latitude ?? 0
+ let lng = item.Longitude ?? 0
+ select new
+ {
+ Id = doc.Id,
+ Date = item.Date,
+
+ Latitude = lat,
+ Longitude = lng,
+ _ = SpatialIndex.Generate(lat, lng)
+ };
+
+ Store(x => x.Id, FieldStorage.Yes);
+ Store(x => x.Date, FieldStorage.Yes);
+
+ Store(x => x.Latitude, FieldStorage.Yes);
+ Store(x => x.Longitude, FieldStorage.Yes);
+ }
+ }
+
+ [Fact]
+ public void WeirdSpatialResults()
+ {
+ using (IDocumentStore store = NewDocumentStore())
+ {
+ using (IDocumentSession session = store.OpenSession())
+ {
+ session.Store(new MyDocument
+ {
+ Id = "First",
+ Items = new[]
+ {
+ new MyDocumentItem
+ {
+ Date = new DateTime(2011, 1, 1),
+ Latitude = 10,
+ Longitude = 10
+ }
+ }
+ });
+ session.SaveChanges();
+
+ }
+
+ new MyIndex().Execute(store);
+ using (IDocumentSession session = store.OpenSession())
+ {
+ RavenQueryStatistics stats;
+ var result = session.Advanced
+ .LuceneQuery<MyDocument, MyIndex>()
+ .WaitForNonStaleResults()
+ .WithinRadiusOf(0, 12.3456789f, 12.3456789f)
+ .Statistics(out stats)
+ .SelectFields<MyProjection>("Id", "Latitude", "Longitude")
+ .Take(50)
+ .ToArray();
+
+ Assert.Equal(0, stats.TotalResults);
+ Assert.Equal(0, result.Length); // Assert.AreEqual failed. Expected:<0>. Actual:<50>.
+ }
+ }
+ }
+
+
+ [Fact]
+ public void MatchSpatialResults()
+ {
+ using (IDocumentStore store = NewDocumentStore())
+ {
+ using (IDocumentSession session = store.OpenSession())
+ {
+ session.Store(new MyDocument
+ {
+ Id = "First",
+ Items = new[]
+ {
+ new MyDocumentItem
+ {
+ Date = new DateTime(2011, 1, 1),
+ Latitude = 10,
+ Longitude = 10
+ }
+ }
+ });
+ session.SaveChanges();
+
+ }
+
+ new MyIndex().Execute(store);
+ using (IDocumentSession session = store.OpenSession())
+ {
+ RavenQueryStatistics stats;
+ var result = session.Advanced
+ .LuceneQuery<MyDocument, MyIndex>()
+ .WaitForNonStaleResults()
+ .WithinRadiusOf(1,10, 10)
+ .Statistics(out stats)
+ .SelectFields<MyProjection>("Id", "Latitude", "Longitude")
+ .Take(50)
+ .ToArray();
+
+ Assert.Equal(1, stats.TotalResults);
+ Assert.Equal(1, result.Length); // Assert.AreEqual failed. Expected:<0>. Actual:<50>.
+ }
+ }
+ }
+ }
+}
@@ -435,6 +435,7 @@
<Compile Include="Bugs\SimpleJson.cs" />
<Compile Include="Bugs\SinglePropertyDocument.cs" />
<Compile Include="Bugs\MapRedue\VersionedDocument.cs" />
+ <Compile Include="Bugs\Spatial.cs" />
<Compile Include="Bugs\spokeypokey.cs" />
<Compile Include="Bugs\Stacey\InServerDatabase.cs" />
<Compile Include="Bugs\Stacey\Models.cs" />

0 comments on commit 0e064b7

Please sign in to comment.