forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryCacheFixture.cs
319 lines (262 loc) · 8.62 KB
/
QueryCacheFixture.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using System;
using System.Collections;
using System.Threading;
using NHibernate.Stat;
using NUnit.Framework;
using NHibernate.Transform;
namespace NHibernate.Test.SecondLevelCacheTests
{
[TestFixture]
public class ScalarQueryFixture : TestCase
{
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}
protected override string[] Mappings
{
get { return new[] { "SecondLevelCacheTest.Item.hbm.xml" }; }
}
protected override void Configure(Cfg.Configuration configuration)
{
configuration.SetProperty(Cfg.Environment.GenerateStatistics, "true");
}
public void FillDb(int startId)
{
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
for (int i = startId; i < startId + 10; i++)
{
s.Save(new AnotherItem { Id = i, Name = (i / 2).ToString() });
}
tx.Commit();
}
}
public void CleanUp()
{
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.Delete("from AnotherItem");
tx.Commit();
}
}
[Test]
public void ShouldHitCacheUsingNamedQueryWithProjection()
{
FillDb(1);
Sfi.Statistics.Clear();
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.GetNamedQuery("Stat").List();
tx.Commit();
}
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1));
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1));
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(0));
Sfi.Statistics.Clear();
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.GetNamedQuery("Stat").List();
tx.Commit();
}
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(0));
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1));
Sfi.Statistics.LogSummary();
CleanUp();
}
[Test]
public void ShouldHitCacheUsingQueryWithProjection()
{
FillDb(1);
Sfi.Statistics.Clear();
int resultCount;
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
resultCount =
s.CreateQuery("select ai.Name, count(*) from AnotherItem ai group by ai.Name").SetCacheable(true).SetCacheRegion(
"Statistics").List().Count;
tx.Commit();
}
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1));
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1));
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(0));
Sfi.Statistics.Clear();
int secondResultCount;
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
secondResultCount = s.CreateQuery("select ai.Name, count(*) from AnotherItem ai group by ai.Name")
.SetCacheable(true).SetCacheRegion("Statistics").List().Count;
tx.Commit();
}
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(0));
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1));
Assert.That(secondResultCount, Is.EqualTo(resultCount));
Sfi.Statistics.LogSummary();
CleanUp();
}
[Test]
public void QueryCacheInvalidation()
{
Sfi.EvictQueries();
Sfi.Statistics.Clear();
const string queryString = "from Item i where i.Name='widget'";
object savedId = CreateItem(queryString);
QueryStatistics qs = Sfi.Statistics.GetQueryStatistics(queryString);
EntityStatistics es = Sfi.Statistics.GetEntityStatistics(typeof(Item).FullName);
Thread.Sleep(200);
IList result;
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
result = s.CreateQuery(queryString).SetCacheable(true).List();
Assert.That(result.Count, Is.EqualTo(1));
tx.Commit();
}
Assert.That(qs.CacheHitCount, Is.EqualTo(0));
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
result = s.CreateQuery(queryString).SetCacheable(true).List();
Assert.That(result.Count, Is.EqualTo(1));
tx.Commit();
}
Assert.That(qs.CacheHitCount, Is.EqualTo(1));
Assert.That(es.FetchCount, Is.EqualTo(0));
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
result = s.CreateQuery(queryString).SetCacheable(true).List();
Assert.That(result.Count, Is.EqualTo(1));
Assert.That(NHibernateUtil.IsInitialized(result[0]));
var i = (Item)result[0];
i.Name = "Widget";
tx.Commit();
}
Assert.That(qs.CacheHitCount, Is.EqualTo(2));
Assert.That(qs.CacheMissCount, Is.EqualTo(2));
Assert.That(es.FetchCount, Is.EqualTo(0));
Thread.Sleep(200);
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.CreateQuery(queryString).SetCacheable(true).List();
var i = s.Get<Item>(savedId);
Assert.That(i.Name, Is.EqualTo("Widget"));
s.Delete(i);
tx.Commit();
}
Assert.That(qs.CacheHitCount, Is.EqualTo(2));
Assert.That(qs.CacheMissCount, Is.EqualTo(3));
Assert.That(qs.CachePutCount, Is.EqualTo(3));
Assert.That(qs.ExecutionCount, Is.EqualTo(3));
Assert.That(es.FetchCount, Is.EqualTo(0)); //check that it was being cached
}
private object CreateItem(string queryString)
{
object savedId;
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.CreateQuery(queryString).SetCacheable(true).List();
var i = new Item { Name = "widget" };
savedId = s.Save(i);
tx.Commit();
}
return savedId;
}
[Test]
public void SimpleProjections()
{
var transformer = new CustomTransformer();
Sfi.EvictQueries();
Sfi.Statistics.Clear();
const string queryString = "select i.Name, i.Description from AnotherItem i where i.Name='widget'";
object savedId;
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.CreateQuery(queryString).SetCacheable(true).List();
var i = new AnotherItem { Name = "widget" };
savedId = s.Save(i);
tx.Commit();
}
QueryStatistics qs = Sfi.Statistics.GetQueryStatistics(queryString);
EntityStatistics es = Sfi.Statistics.GetEntityStatistics(typeof(AnotherItem).FullName);
Thread.Sleep(200);
IList result;
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.CreateQuery(queryString).SetCacheable(true).List();
tx.Commit();
}
Assert.That(qs.CacheHitCount, Is.EqualTo(0));
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.CreateQuery(queryString).SetCacheable(true).List();
tx.Commit();
}
Assert.That(qs.CacheHitCount, Is.EqualTo(1));
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.CreateQuery(queryString).SetCacheable(true).SetResultTransformer(transformer).List();
tx.Commit();
}
Assert.That(qs.CacheHitCount, Is.EqualTo(2), "hit count should go up since the cache contains the result before the possible application of a resulttransformer");
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.CreateQuery(queryString).SetCacheable(true).SetResultTransformer(transformer).List();
tx.Commit();
}
Assert.That(qs.CacheHitCount, Is.EqualTo(3), "hit count should go up since we are using the same resulttransformer");
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
result = s.CreateQuery(queryString).SetCacheable(true).List();
Assert.That(result.Count, Is.EqualTo(1));
var i = s.Get<AnotherItem>(savedId);
i.Name = "Widget";
tx.Commit();
}
Assert.That(qs.CacheHitCount, Is.EqualTo(4));
Assert.That(qs.CacheMissCount, Is.EqualTo(2));
Thread.Sleep(200);
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.CreateQuery(queryString).SetCacheable(true).List();
var i = s.Get<AnotherItem>(savedId);
Assert.That(i.Name, Is.EqualTo("Widget"));
s.Delete(i);
tx.Commit();
}
Assert.That(qs.CacheHitCount, Is.EqualTo(4));
Assert.That(qs.CacheMissCount, Is.EqualTo(3));
Assert.That(qs.CachePutCount, Is.EqualTo(3));
Assert.That(qs.ExecutionCount, Is.EqualTo(3));
Assert.That(es.FetchCount, Is.EqualTo(0)); //check that it was being cached
}
public class CustomTransformer : IResultTransformer
{
public object TransformTuple(object[] tuple, string[] aliases)
{
// Some db change the empty string to null, causing .ToString() to blow.
// https://stackoverflow.com/a/203536/1178314
return new AnotherItem { Name = tuple[0]?.ToString(), Description = tuple[1]?.ToString() };
}
public IList TransformList(IList collection)
{
return collection;
}
}
}
}