Skip to content

Commit

Permalink
Supporting the ability to define sort order on the client if it is no…
Browse files Browse the repository at this point in the history
…t specified on the server
  • Loading branch information
ayende committed Jun 2, 2012
1 parent 270dbf6 commit 665b950
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
21 changes: 14 additions & 7 deletions Raven.Database/Extensions/IndexingExtensions.cs
Expand Up @@ -13,6 +13,7 @@
using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
using Raven.Database.Indexing.Sorting;
using Raven.Database.Server;
using Constants = Raven.Abstractions.Data.Constants;

namespace Raven.Database.Extensions
Expand Down Expand Up @@ -117,16 +118,22 @@ public static Sort GetSort(this IndexQuery self, IndexDefinition indexDefinition
public static SortOptions? GetSortOption(this IndexDefinition self, string name)
{
SortOptions value;
if (!self.SortOptions.TryGetValue(name, out value))
if (self.SortOptions.TryGetValue(name, out value))
return value;

if (name.EndsWith("_Range"))
{
if (!name.EndsWith("_Range"))
{
return null;
}
string nameWithoutRange = name.Substring(0, name.Length - "_Range".Length);
if (!self.SortOptions.TryGetValue(nameWithoutRange, out value))
return null;
if (self.SortOptions.TryGetValue(nameWithoutRange, out value))
return value;
}
if (CurrentOperationContext.Headers.Value == null)
return value;

var hint = CurrentOperationContext.Headers.Value["SortHint-" + name];
if (string.IsNullOrEmpty(hint))
return value;
Enum.TryParse(hint, true, out value);
return value;
}
}
Expand Down
45 changes: 44 additions & 1 deletion Raven.Tests/MailingList/DynamicFieldSorting.cs
Expand Up @@ -47,6 +47,12 @@ public class ProjectionItem
{
public string SongId { get; set; }
public NumericAttribute[] NumericAttributes { get; set; }

public override string ToString()
{
return string.Format("SongId: {0}, N1: {1}", SongId,
NumericAttributes.First(x => x.Name == "N1").Value);
}
}

public WithDynamicIndex()
Expand All @@ -72,7 +78,7 @@ select new
[Fact]
public void CanSortDynamicaly()
{
using(var store = NewDocumentStore())
using (var store = NewDocumentStore())
{
new WithDynamicIndex().Execute(store);
using (var session = store.OpenSession())
Expand Down Expand Up @@ -108,6 +114,43 @@ public void CanSortDynamicaly()
}
}

[Fact]
public void CanSortDynamicaly_Desc()
{
using (var Store = NewDocumentStore())
{
new WithDynamicIndex().Execute(Store);
using (var session = Store.OpenSession())
{
session.Store(new DataSet
{
Items = Enumerable.Range(1, 50).Select(x =>
new Item
{
SongId = "songs/" + x,
NumericAttributes = new[]
{
new NumericAttribute("N1",x*0.99d),
new NumericAttribute("N4",x*0.01d),
}
}).ToList()
});
session.SaveChanges();
}

using (var s = Store.OpenSession())
{
var items = s.Advanced.LuceneQuery<WithDynamicIndex.ProjectionItem, WithDynamicIndex>()
.WaitForNonStaleResults()
.AddOrder("N1_Range", true, typeof(double))
.SelectFields<WithDynamicIndex.ProjectionItem>("SongId", "NumericAttributes")
.ToList();
Assert.Equal(50, items.Count);
Assert.Equal("songs/50", items.First().SongId);
}
}
}

}

}

0 comments on commit 665b950

Please sign in to comment.