forked from ravendb/ravendb
/
CompiledIndexesNhsevidence.cs
228 lines (197 loc) · 4.5 KB
/
CompiledIndexesNhsevidence.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using Newtonsoft.Json;
using NLog;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Connection;
using Raven.Client.Document;
using Raven.Client.Embedded;
using Raven.Database.Linq;
using Raven.Database.Util;
using Raven.Json.Linq;
using Xunit;
namespace Raven.Tests.Bugs
{
public class CompiledIndexesNhsevidence : RavenTest
{
[Fact]
public void CanGetCorrectResults()
{
for (int x = 0; x < 50; x++)
{
using (var store = CreateStore())
{
for (int i = 0; i < 12; i++)
{
AddRecord(store, 1);
}
ReadRecords(store, 60);
}
}
}
private static void ReadRecords(IDocumentStore store, int shouldBe)
{
using(var session = store.OpenSession())
{
for (int i = 0; i < 6; i++)
{
int count = session.Advanced.LuceneQuery<object>("view" + (i+1)).WaitForNonStaleResults().QueryResult.TotalResults;
Assert.Equal(count, shouldBe);
}
}
}
private static void AddRecord(IDocumentStore store,int records)
{
using(var session = store.OpenSession())
{
for (int i = 0; i < records; i++)
{
var item = new TestClass {Items = new List<Item>()};
for (int j = 0; j < 5; j++)
{
item.Items.Add(new Item()
{
Id = j + 1,
Email = string.Format("rob{0}@text.com", i + 1).PadLeft(200, (char) i),
Name = string.Format("rob{0}", i + 1).PadLeft(300, (char) i)
});
}
session.Store(item);
}
session.SaveChanges();
}
}
private static EmbeddableDocumentStore CreateStore()
{
var store = new EmbeddableDocumentStore
{
Conventions = Conventions.Document,
Configuration =
{
RunInMemory = true,
MaxNumberOfParallelIndexTasks = 1
},
};
store.Configuration.Catalog.Catalogs.Add(new TypeCatalog(
typeof(View1),
typeof(View2),
typeof(View3),
typeof(View4),
typeof(View5),
typeof(View6)
));
store.Initialize();
return store;
}
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
[JsonObject(IsReference = true)]
public class TestClass
{
public string Id { get; set; }
public List<Item> Items { get; set; }
}
public class TestClassView : AbstractViewGenerator
{
public TestClassView()
{
ForEntityNames.Add("TestClasses");
MapDefinitions.Add(MapToPaths);
ReduceDefinition = Reduce;
GroupByExtraction = doc => doc.UserId;
AddField("UserId");
AddField("Name");
AddField("Email");
Indexes.Add("UserId", FieldIndexing.NotAnalyzed);
}
private IEnumerable<dynamic> Reduce(IEnumerable<dynamic> source)
{
foreach (var o in source)
{
//Console.WriteLine("{0},{1}",o.__document_id, o.UserId);
yield return new
{
o.__document_id,
o.UserId,
o.Name,
o.Email
};
}
}
private IEnumerable<dynamic> MapToPaths(IEnumerable<dynamic> source)
{
foreach (var o in source)
{
if(o["@metadata"]["Raven-Entity-Name"] != "TestClasses")
continue;
var testClass = FromRaven(o);
foreach (var item in testClass.Items)
{
yield return new
{
__document_id = o.Id,
UserId = item.Id,
item.Name,
item.Email
};
}
}
yield break;
}
TestClass FromRaven(dynamic o)
{
var jobject = (RavenJObject)o.Inner;
var item = ((TestClass)jobject.Deserialize(typeof(TestClass), Conventions.Document));
if (item == null)
throw new ApplicationException("Deserialisation failed");
return item;
}
}
public static class Conventions
{
public static readonly DocumentConvention Document = new DocumentConvention
{
FindTypeTagName = t => t.GetType() == typeof(TestClass) ? "testclass" : null,
MaxNumberOfRequestsPerSession = 3000,
DocumentKeyGenerator = doc =>
{
if (doc is TestClass)
return ((TestClass)doc).Id;
return null;
}
};
}
[DisplayName("view1")]
public class View1 : TestClassView
{
}
[DisplayName("view2")]
public class View2 : TestClassView
{
}
[DisplayName("view3")]
public class View3 : TestClassView
{
}
[DisplayName("view4")]
public class View4 : TestClassView
{
}
[DisplayName("view5")]
public class View5 : TestClassView
{
}
[DisplayName("view6")]
public class View6 : TestClassView
{
}
}
}