Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial spike for true custom sorting
- Loading branch information
Showing
15 changed files
with
250 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using Lucene.Net.Search; | ||
using Raven.Abstractions.Data; | ||
|
||
namespace Raven.Database.Indexing.Sorting.Custom | ||
{ | ||
public class CustomSortField : SortField | ||
{ | ||
private readonly IndexEntriesToComparablesGenerator generator; | ||
public CustomSortField(string typeName, IndexQuery indexQuery) | ||
: base(String.Empty, INT) | ||
{ | ||
var clrType = System.Type.GetType(typeName, throwOnError: true); | ||
generator = (IndexEntriesToComparablesGenerator)Activator.CreateInstance(clrType, new object[] {indexQuery}); | ||
} | ||
|
||
public override FieldComparator GetComparator(int numHits, int sortPos) | ||
{ | ||
return new CustomSortFieldCompartor(generator, numHits); | ||
} | ||
|
||
public override FieldComparatorSource ComparatorSource | ||
{ | ||
get | ||
{ | ||
return new CustomSortFieldComparatorSource(this); | ||
} | ||
} | ||
private class CustomSortFieldComparatorSource :FieldComparatorSource | ||
{ | ||
private readonly CustomSortField parent; | ||
|
||
public CustomSortFieldComparatorSource(CustomSortField parent) | ||
{ | ||
this.parent = parent; | ||
} | ||
|
||
public override FieldComparator NewComparator(string fieldname, int numHits, int sortPos, bool reversed) | ||
{ | ||
return new CustomSortFieldCompartor(parent.generator, numHits); | ||
} | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
Raven.Database/Indexing/Sorting/Custom/CustomSortFieldCompartor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="CustomSortFieldCompartor.cs" company="Hibernating Rhinos LTD"> | ||
// Copyright (c) Hibernating Rhinos LTD. All rights reserved. | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
using System; | ||
using Lucene.Net.Index; | ||
using Lucene.Net.Search; | ||
|
||
namespace Raven.Database.Indexing.Sorting.Custom | ||
{ | ||
public class CustomSortFieldCompartor : FieldComparator | ||
{ | ||
private readonly IndexEntriesToComparablesGenerator generator; | ||
private readonly IComparable[] values; | ||
private IComparable bottom; | ||
private IndexReader currentReader; | ||
|
||
public CustomSortFieldCompartor(IndexEntriesToComparablesGenerator generator, int numHits) | ||
{ | ||
this.generator = generator; | ||
values= new IComparable[numHits]; | ||
} | ||
|
||
public override int Compare(int slot1, int slot2) | ||
{ | ||
var a = values[slot1]; | ||
var b = values[slot2]; | ||
return a.CompareTo(b); | ||
} | ||
|
||
public override void SetBottom(int slot) | ||
{ | ||
bottom = values[slot]; | ||
} | ||
|
||
public override int CompareBottom(int doc) | ||
{ | ||
var current = generator.Generate(currentReader,doc); | ||
return bottom.CompareTo(current); | ||
} | ||
|
||
public override void Copy(int slot, int doc) | ||
{ | ||
values[slot] = generator.Generate(currentReader, doc); | ||
} | ||
|
||
public override void SetNextReader(IndexReader reader, int docBase) | ||
{ | ||
currentReader = reader; | ||
} | ||
|
||
public override IComparable this[int slot] | ||
{ | ||
get { return values[slot]; } | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Raven.Database/Indexing/Sorting/Custom/IndexEntriesToComparablesGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using Lucene.Net.Index; | ||
using Raven.Abstractions.Data; | ||
|
||
namespace Raven.Database.Indexing.Sorting.Custom | ||
{ | ||
public abstract class IndexEntriesToComparablesGenerator | ||
{ | ||
protected IndexQuery IndexQuery; | ||
|
||
protected IndexEntriesToComparablesGenerator(IndexQuery indexQuery) | ||
{ | ||
IndexQuery = indexQuery; | ||
} | ||
|
||
public abstract IComparable Generate(IndexReader reader, int doc); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="CustomSorting.cs" company="Hibernating Rhinos LTD"> | ||
// Copyright (c) Hibernating Rhinos LTD. All rights reserved. | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
using System.Linq; | ||
using Jint.Parser; | ||
using Raven.Abstractions.Indexing; | ||
using Raven.Client.Indexes; | ||
using Raven.Tests.Common; | ||
using Xunit; | ||
|
||
namespace Raven.Tests.Sorting | ||
{ | ||
public class CustomSorting : RavenTest | ||
{ | ||
public class User | ||
{ | ||
public string Name; | ||
} | ||
|
||
public class User_Search : AbstractIndexCreationTask<User> | ||
{ | ||
public User_Search() | ||
{ | ||
Map = users => | ||
from user in users | ||
select new { user.Name }; | ||
|
||
Store(x => x.Name, FieldStorage.Yes); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Normal() | ||
{ | ||
using (var store = NewDocumentStore()) | ||
{ | ||
using (var session = store.OpenSession()) | ||
{ | ||
session.Store(new User { Name = "Maxim" }); | ||
session.Store(new User { Name = "Oren" }); | ||
session.SaveChanges(); | ||
} | ||
|
||
new User_Search().Execute(store); | ||
WaitForIndexing(store); | ||
|
||
using (var session = store.OpenSession()) | ||
{ | ||
var users = session.Query<User, User_Search>() | ||
.Customize(x => x.CustomSortUsing(typeof (SortByNumberOfCharactersFromEnd).AssemblyQualifiedName)) | ||
.AddTransformerParameter("len", 1) | ||
.ToList(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="SortByNumberOfCharactersFromEnd.cs" company="Hibernating Rhinos LTD"> | ||
// Copyright (c) Hibernating Rhinos LTD. All rights reserved. | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
using System; | ||
using Lucene.Net.Index; | ||
using Raven.Abstractions.Data; | ||
using Raven.Database.Indexing.Sorting.Custom; | ||
|
||
namespace Raven.Tests.Sorting | ||
{ | ||
public class SortByNumberOfCharactersFromEnd : IndexEntriesToComparablesGenerator | ||
{ | ||
private readonly int len; | ||
|
||
public SortByNumberOfCharactersFromEnd(IndexQuery indexQuery) : base(indexQuery) | ||
{ | ||
len = IndexQuery.TransformerParameters["len"].Value<int>(); | ||
} | ||
|
||
public override IComparable Generate(IndexReader reader, int doc) | ||
{ | ||
var document = reader.Document(doc); | ||
var name = document.GetField("Name").StringValue; | ||
return name.Substring(name.Length - len, len); | ||
} | ||
} | ||
} |