forked from ravendb/ravendb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathZNS2.cs
152 lines (137 loc) · 5.53 KB
/
ZNS2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// -----------------------------------------------------------------------
// <copyright file="ZNS2.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Embedded;
using Raven.Client.Linq;
using Raven.Tests.Common;
using Xunit;
namespace Raven.Tests.MailingList
{
public class ZNS2 : RavenTest
{
public class TestItem
{
public string Id { get; set; }
public string Name { get; set; }
public int Area { get; set; }
public EventDate[] Dates { get; set; }
public override int GetHashCode()
{
return this.Id.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj != null && obj is TestItem)
return ((TestItem)obj).Id == this.Id;
return false;
}
}
public class EventDate
{
public DateTime Date { get; set; }
public string Time { get; set; }
}
[Fact]
public void Can_SortAndPageMultipleDates()
{
using (var store = new EmbeddableDocumentStore { RunInMemory = true })
{
//Create an index
store.Initialize();
store.DatabaseCommands.PutIndex("TestItemsIndex", new Raven.Abstractions.Indexing.IndexDefinition
{
Name = "TestItemsIndex",
Map = @"from item in docs.TestItems
from d in item.Dates.Select((Func<dynamic,dynamic>)(x => x.Date)).Distinct().DefaultIfEmpty()
select new {Id = item.Id, Name = item.Name, EventDate = d, Area = item.Area};",
Stores = { { "EventDate", FieldStorage.No } }
}, true);
//Insert some events at random dates
var size = 50;
using (var session = store.OpenSession())
{
for (int i = 0; i < size; i++)
{
var r = new System.Random();
session.Store(new TestItem()
{
Id = "testitems/" + 1000 + i,
Name = "Event Number " + (1000 + i),
Area = r.Next(1, 3),
Dates = null
});
}
}
using (var session = store.OpenSession())
{
for (int i = 1; i <= size; i++)
{
var r = new System.Random(i);
var dates = new List<DateTime>();
for (var j = 0; j < 5; j++)
{
dates.Add(new DateTime(2012, r.Next(1, 12), r.Next(1, 28)));
}
session.Store(new TestItem()
{
Id = "testitems/" + i,
Name = "Event Number " + i,
Area = r.Next(1, 3),
Dates = dates.Select(x => new EventDate() { Date = x }).ToArray()
});
}
session.SaveChanges();
}
//Get all results
RavenQueryStatistics stats;
List<TestItem> result = null;
using (var session = store.OpenSession())
{
result = session.Advanced.DocumentQuery<TestItem>("TestItemsIndex")
.Statistics(out stats)
.WaitForNonStaleResults()
.WhereBetweenOrEqual("EventDate", DateTime.Parse("2012-02-01"), DateTime.Parse("2012-09-01"))
.AndAlso()
.WhereEquals("Area", 2)
.OrderBy("EventDate")
.Take(1000)
.ToList();
}
//Get all results, paged
List<TestItem> paged = new List<TestItem>();
RavenQueryStatistics stats2;
int skip = 0;
var take = 10;
int page = 0;
do
{
using (var session = store.OpenSession())
{
var r = session.Advanced.DocumentQuery<TestItem>("TestItemsIndex")
.Statistics(out stats2)
.WhereBetweenOrEqual("EventDate", DateTime.Parse("2012-02-01"), DateTime.Parse("2012-09-01"))
.AndAlso()
.WhereEquals("Area", 2)
.OrderBy("EventDate")
.Skip((page * take) + skip)
.Take(take)
.ToList();
skip += stats2.SkippedResults;
page++;
paged.AddRange(r);
}
} while (paged.Count < result.Count);
Assert.Equal(result.Count, paged.Count);
Assert.Equal(result.Select(x=>x.Id),paged.Select(x=>x.Id));
}
}
}
}