PageRenderTime 63ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/Raven.Tests/Storage/Voron/IndexingStorageActionsTests.cs

https://github.com/barryhagan/ravendb
C# | 488 lines | 412 code | 70 blank | 6 comment | 5 complexity | f9866862afa63b4730f1b63eaadfaeac MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, CC-BY-SA-3.0
  1. // -----------------------------------------------------------------------
  2. // <copyright file="IndexingStorageActionsTests.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using Raven.Tests.Common;
  7. namespace Raven.Tests.Storage.Voron
  8. {
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Threading;
  13. using Microsoft.Isam.Esent.Interop;
  14. using Raven.Abstractions;
  15. using Raven.Abstractions.Data;
  16. using Raven.Database.Indexing;
  17. using Xunit;
  18. using Xunit.Extensions;
  19. using global::Voron.Exceptions;
  20. [Trait("VoronTest", "StorageActionsTests")]
  21. public class IndexingStorageActionsTests : TransactionalStorageTestBase
  22. {
  23. [Theory]
  24. [PropertyData("Storages")]
  25. public void IndexCreation_In_DifferentBatches(string requestedStorage)
  26. {
  27. using (var storage = NewTransactionalStorage(requestedStorage))
  28. {
  29. storage.Batch(accessor => accessor.Indexing.AddIndex(101, false));
  30. //make sure that index already exists check works correctly
  31. Assert.DoesNotThrow(() => storage.Batch(accessor => accessor.Indexing.AddIndex(202, false)));
  32. }
  33. }
  34. [Theory]
  35. [PropertyData("Storages")]
  36. public void IndexCreation(string requestedStorage)
  37. {
  38. using (var storage = NewTransactionalStorage(requestedStorage))
  39. {
  40. storage.Batch(accessor =>
  41. {
  42. accessor.Indexing.AddIndex(101, false);
  43. accessor.Indexing.AddIndex(202, true);
  44. });
  45. storage.Batch(accessor =>
  46. {
  47. var stats = accessor.Indexing.GetIndexesStats().ToList();
  48. Assert.Equal(2, stats.Count);
  49. var stat1 = stats[0];
  50. Assert.Equal(101, stat1.Id);
  51. Assert.Equal(0, stat1.IndexingAttempts);
  52. Assert.Equal(0, stat1.IndexingSuccesses);
  53. Assert.Equal(0, stat1.IndexingErrors);
  54. Assert.Equal(IndexingPriority.Normal, stat1.Priority);
  55. Assert.Equal(0, stat1.TouchCount);
  56. Assert.Equal(UtcNow, stat1.CreatedTimestamp);
  57. Assert.Equal(DateTime.MinValue, stat1.LastIndexingTime);
  58. Assert.Null(stat1.ReduceIndexingAttempts);
  59. Assert.Null(stat1.ReduceIndexingSuccesses);
  60. Assert.Null(stat1.ReduceIndexingErrors);
  61. Assert.Null(stat1.LastReducedEtag);
  62. Assert.Null(stat1.LastReducedTimestamp);
  63. Assert.Equal(Etag.Empty, stat1.LastIndexedEtag);
  64. Assert.Equal(DateTime.MinValue, stat1.LastIndexedTimestamp);
  65. var stat2 = stats[1];
  66. Assert.Equal(202, stat2.Id);
  67. Assert.Equal(0, stat2.IndexingAttempts);
  68. Assert.Equal(0, stat2.IndexingSuccesses);
  69. Assert.Equal(0, stat2.IndexingErrors);
  70. Assert.Equal(IndexingPriority.Normal, stat2.Priority);
  71. Assert.Equal(0, stat2.TouchCount);
  72. Assert.Equal(UtcNow, stat2.CreatedTimestamp);
  73. Assert.Equal(DateTime.MinValue, stat2.LastIndexingTime);
  74. Assert.Equal(0, stat2.ReduceIndexingAttempts);
  75. Assert.Equal(0, stat2.ReduceIndexingSuccesses);
  76. Assert.Equal(0, stat2.ReduceIndexingErrors);
  77. Assert.Equal(Etag.Empty, stat2.LastReducedEtag);
  78. Assert.Equal(DateTime.MinValue, stat2.LastReducedTimestamp);
  79. Assert.Equal(Etag.Empty, stat2.LastIndexedEtag);
  80. Assert.Equal(DateTime.MinValue, stat2.LastIndexedTimestamp);
  81. });
  82. storage.Batch(accessor =>
  83. {
  84. accessor.Indexing.PrepareIndexForDeletion(101);
  85. accessor.Indexing.PrepareIndexForDeletion(202);
  86. accessor.Indexing.DeleteIndex(101, new CancellationToken());
  87. accessor.Indexing.DeleteIndex(202, new CancellationToken());
  88. });
  89. storage.Batch(accessor =>
  90. {
  91. var stats = accessor.Indexing.GetIndexesStats().ToList();
  92. Assert.Equal(0, stats.Count);
  93. });
  94. }
  95. }
  96. [Theory]
  97. [PropertyData("Storages")]
  98. public void CannotAddDuplicateIndex(string requestedStorage)
  99. {
  100. Type exception1Type = null;
  101. Type exception2Type = null;
  102. string exception1Message = null;
  103. string exception2Message = null;
  104. if (requestedStorage == "esent")
  105. {
  106. exception1Type = typeof(EsentKeyDuplicateException);
  107. exception1Message = "Illegal duplicate key";
  108. exception2Type = typeof(EsentKeyDuplicateException);
  109. exception2Message = "Illegal duplicate key";
  110. }
  111. else if (requestedStorage == "voron")
  112. {
  113. exception1Type = typeof(ArgumentException);
  114. exception1Message = "There is already an index with the name: '101'";
  115. exception2Type = typeof(ArgumentException);
  116. exception2Message = "There is already an index with the name: '202'";
  117. }
  118. using (var storage = NewTransactionalStorage(requestedStorage))
  119. {
  120. storage.Batch(accessor => accessor.Indexing.AddIndex(101, false));
  121. var e1 = Assert.Throws(exception1Type, () => storage.Batch(accessor => accessor.Indexing.AddIndex(101, false)));
  122. Assert.Equal(exception1Message, e1.Message);
  123. var e2 = Assert.Throws(exception2Type,
  124. () => storage.Batch(
  125. accessor =>
  126. {
  127. accessor.Indexing.AddIndex(202, false);
  128. accessor.Indexing.AddIndex(202, true);
  129. }));
  130. Assert.Equal(exception2Message, e2.Message);
  131. }
  132. }
  133. [Theory]
  134. [PropertyData("Storages")]
  135. public void IndexStats(string requestedStorage)
  136. {
  137. using (var storage = NewTransactionalStorage(requestedStorage))
  138. {
  139. storage.Batch(
  140. accessor =>
  141. {
  142. accessor.Indexing.AddIndex(101, false);
  143. accessor.Indexing.AddIndex(202, true);
  144. });
  145. storage.Batch(accessor =>
  146. {
  147. var stats = accessor.Indexing.GetIndexesStats().ToList();
  148. Assert.Equal(2, stats.Count);
  149. var stat1 = accessor.Indexing.GetIndexStats(101);
  150. Assert.Equal(101, stat1.Id);
  151. Assert.Equal(0, stat1.IndexingAttempts);
  152. Assert.Equal(0, stat1.IndexingSuccesses);
  153. Assert.Equal(0, stat1.IndexingErrors);
  154. Assert.Equal(IndexingPriority.Normal, stat1.Priority);
  155. Assert.Equal(0, stat1.TouchCount);
  156. Assert.Equal(UtcNow, stat1.CreatedTimestamp);
  157. Assert.Equal(DateTime.MinValue, stat1.LastIndexingTime);
  158. Assert.Null(stat1.ReduceIndexingAttempts);
  159. Assert.Null(stat1.ReduceIndexingSuccesses);
  160. Assert.Null(stat1.ReduceIndexingErrors);
  161. Assert.Null(stat1.LastReducedEtag);
  162. Assert.Null(stat1.LastReducedTimestamp);
  163. Assert.Equal(Etag.Empty, stat1.LastIndexedEtag);
  164. Assert.Equal(DateTime.MinValue, stat1.LastIndexedTimestamp);
  165. var stat2 = accessor.Indexing.GetIndexStats(202);
  166. Assert.Equal(202, stat2.Id);
  167. Assert.Equal(0, stat2.IndexingAttempts);
  168. Assert.Equal(0, stat2.IndexingSuccesses);
  169. Assert.Equal(0, stat2.IndexingErrors);
  170. Assert.Equal(IndexingPriority.Normal, stat2.Priority);
  171. Assert.Equal(0, stat2.TouchCount);
  172. Assert.Equal(UtcNow, stat2.CreatedTimestamp);
  173. Assert.Equal(DateTime.MinValue, stat2.LastIndexingTime);
  174. Assert.Equal(0, stat2.ReduceIndexingAttempts);
  175. Assert.Equal(0, stat2.ReduceIndexingSuccesses);
  176. Assert.Equal(0, stat2.ReduceIndexingErrors);
  177. Assert.Equal(Etag.Empty, stat2.LastReducedEtag);
  178. Assert.Equal(DateTime.MinValue, stat2.LastReducedTimestamp);
  179. Assert.Equal(Etag.Empty, stat2.LastIndexedEtag);
  180. Assert.Equal(DateTime.MinValue, stat2.LastIndexedTimestamp);
  181. });
  182. }
  183. }
  184. [Theory]
  185. [PropertyData("Storages")]
  186. public void IndexPriority(string requestedStorage)
  187. {
  188. using (var storage = NewTransactionalStorage(requestedStorage))
  189. {
  190. storage.Batch(accessor => accessor.Indexing.AddIndex(101, false));
  191. storage.Batch(
  192. accessor =>
  193. {
  194. var stats = accessor.Indexing.GetIndexStats(101);
  195. Assert.Equal(IndexingPriority.Normal, stats.Priority);
  196. accessor.Indexing.SetIndexPriority(101, IndexingPriority.Forced);
  197. });
  198. storage.Batch(
  199. accessor =>
  200. {
  201. var stats = accessor.Indexing.GetIndexStats(101);
  202. Assert.Equal(IndexingPriority.Forced, stats.Priority);
  203. });
  204. }
  205. }
  206. [Theory]
  207. [PropertyData("Storages")]
  208. public void FailureRate(string requestedStorage)
  209. {
  210. using (var storage = NewTransactionalStorage(requestedStorage))
  211. {
  212. storage.Batch(
  213. accessor =>
  214. {
  215. accessor.Indexing.AddIndex(101, false);
  216. accessor.Indexing.AddIndex(202, true);
  217. });
  218. storage.Batch(accessor =>
  219. {
  220. var rate1 = accessor.Indexing.GetFailureRate(101);
  221. Assert.NotNull(rate1);
  222. Assert.Equal(0, rate1.Attempts);
  223. Assert.Equal(0, rate1.Errors);
  224. Assert.Equal(0, rate1.Successes);
  225. Assert.Null(rate1.ReduceAttempts);
  226. Assert.Null(rate1.ReduceErrors);
  227. Assert.Null(rate1.ReduceSuccesses);
  228. var rate2 = accessor.Indexing.GetFailureRate(202);
  229. Assert.NotNull(rate2);
  230. Assert.Equal(0, rate2.Attempts);
  231. Assert.Equal(0, rate2.Errors);
  232. Assert.Equal(0, rate2.Successes);
  233. Assert.Equal(0, rate2.ReduceAttempts);
  234. Assert.Equal(0, rate2.ReduceErrors);
  235. Assert.Equal(0, rate2.ReduceSuccesses);
  236. });
  237. }
  238. }
  239. [Theory]
  240. [PropertyData("Storages")]
  241. public void UpdateLastIndexed(string requestedStorage)
  242. {
  243. using (var storage = NewTransactionalStorage(requestedStorage))
  244. {
  245. storage.Batch(accessor => accessor.Indexing.AddIndex(101, false));
  246. var etag = new Etag(Guid.NewGuid().ToString());
  247. var date = DateTime.Now.AddDays(1);
  248. storage.Batch(accessor => accessor.Indexing.UpdateLastIndexed(101, etag, date));
  249. storage.Batch(accessor =>
  250. {
  251. var stat1 = accessor.Indexing.GetIndexStats(101);
  252. Assert.Equal(101, stat1.Id);
  253. Assert.Equal(etag, stat1.LastIndexedEtag);
  254. Assert.Equal(date, stat1.LastIndexedTimestamp);
  255. });
  256. }
  257. }
  258. [Theory]
  259. [PropertyData("Storages")]
  260. public void UpdateLastReduced(string requestedStorage)
  261. {
  262. using (var storage = NewTransactionalStorage(requestedStorage))
  263. {
  264. storage.Batch(accessor => accessor.Indexing.AddIndex(101, true));
  265. var etag = new Etag(Guid.NewGuid().ToString());
  266. var date = DateTime.Now.AddDays(1);
  267. storage.Batch(accessor => accessor.Indexing.UpdateLastReduced(101, etag, date));
  268. storage.Batch(accessor =>
  269. {
  270. var stat1 = accessor.Indexing.GetIndexStats(101);
  271. Assert.Equal(101, stat1.Id);
  272. Assert.Equal(etag, stat1.LastReducedEtag);
  273. Assert.Equal(date, stat1.LastReducedTimestamp);
  274. });
  275. }
  276. }
  277. [Theory]
  278. [PropertyData("Storages")]
  279. public void TouchIndexEtag(string requestedStorage)
  280. {
  281. using (var storage = NewTransactionalStorage(requestedStorage))
  282. {
  283. storage.Batch(accessor => accessor.Indexing.AddIndex(101, true));
  284. storage.Batch(accessor =>
  285. {
  286. var stat1 = accessor.Indexing.GetIndexStats(101);
  287. Assert.Equal(101, stat1.Id);
  288. Assert.Equal(0, stat1.TouchCount);
  289. });
  290. storage.Batch(accessor => accessor.Indexing.TouchIndexEtag(101));
  291. storage.Batch(accessor =>
  292. {
  293. var stat1 = accessor.Indexing.GetIndexStats(101);
  294. Assert.Equal(101, stat1.Id);
  295. Assert.Equal(1, stat1.TouchCount);
  296. });
  297. storage.Batch(accessor => accessor.Indexing.TouchIndexEtag(101));
  298. storage.Batch(accessor =>
  299. {
  300. var stat1 = accessor.Indexing.GetIndexStats(101);
  301. Assert.Equal(101, stat1.Id);
  302. Assert.Equal(2, stat1.TouchCount);
  303. });
  304. }
  305. }
  306. [Theory]
  307. [PropertyData("Storages")]
  308. public void UpdateIndexingStats(string requestedStorage)
  309. {
  310. using (var storage = NewTransactionalStorage(requestedStorage))
  311. {
  312. storage.Batch(accessor => accessor.Indexing.AddIndex(101, true));
  313. storage.Batch(accessor => accessor.Indexing.UpdateIndexingStats(101, new IndexingWorkStats
  314. {
  315. IndexingAttempts = 11,
  316. IndexingErrors = 3,
  317. IndexingSuccesses = 2,
  318. Operation = IndexingWorkStats.Status.Reduce,
  319. ReduceAttempts = 6,
  320. ReduceErrors = 7,
  321. ReduceSuccesses = 9
  322. }));
  323. storage.Batch(accessor =>
  324. {
  325. var stat = accessor.Indexing.GetIndexStats(101);
  326. Assert.NotNull(stat);
  327. Assert.Equal(11, stat.IndexingAttempts);
  328. Assert.Equal(3, stat.IndexingErrors);
  329. Assert.Equal(2, stat.IndexingSuccesses);
  330. Assert.Equal(UtcNow, stat.LastIndexingTime);
  331. Assert.Equal(0, stat.ReduceIndexingAttempts);
  332. Assert.Equal(0, stat.ReduceIndexingErrors);
  333. Assert.Equal(0, stat.ReduceIndexingSuccesses);
  334. });
  335. }
  336. }
  337. [Theory]
  338. [PropertyData("Storages")]
  339. public void UpdateReduceStats(string requestedStorage)
  340. {
  341. using (var storage = NewTransactionalStorage(requestedStorage))
  342. {
  343. storage.Batch(accessor => accessor.Indexing.AddIndex(101, true));
  344. storage.Batch(accessor => accessor.Indexing.UpdateReduceStats(101, new IndexingWorkStats
  345. {
  346. IndexingAttempts = 11,
  347. IndexingErrors = 3,
  348. IndexingSuccesses = 2,
  349. Operation = IndexingWorkStats.Status.Reduce,
  350. ReduceAttempts = 6,
  351. ReduceErrors = 7,
  352. ReduceSuccesses = 9
  353. }));
  354. storage.Batch(accessor =>
  355. {
  356. var stat = accessor.Indexing.GetIndexStats(101);
  357. Assert.NotNull(stat);
  358. Assert.Equal(0, stat.IndexingAttempts);
  359. Assert.Equal(0, stat.IndexingErrors);
  360. Assert.Equal(0, stat.IndexingSuccesses);
  361. Assert.Equal(6, stat.ReduceIndexingAttempts);
  362. Assert.Equal(7, stat.ReduceIndexingErrors);
  363. Assert.Equal(9, stat.ReduceIndexingSuccesses);
  364. });
  365. }
  366. }
  367. [Theory]
  368. [PropertyData("Storages")]
  369. public void DocumentReferences1(string requestedStorage)
  370. {
  371. using (var storage = NewTransactionalStorage(requestedStorage))
  372. {
  373. storage.Batch(accessor => accessor.Indexing.AddIndex(101, true));
  374. storage.Batch(accessor => accessor.Indexing.UpdateDocumentReferences(303, "key1", new HashSet<string>()));
  375. storage.Batch(accessor =>
  376. {
  377. var references = accessor.Indexing.GetDocumentsReferencesFrom("key1").ToList();
  378. Assert.Equal(0, references.Count);
  379. });
  380. storage.Batch(accessor => accessor.Indexing.UpdateDocumentReferences(303, "key1", new HashSet<string> { "key2", "key3" }));
  381. storage.Batch(accessor =>
  382. {
  383. var references = accessor.Indexing.GetDocumentsReferencesFrom("key1").ToList();
  384. Assert.Equal(2, references.Count);
  385. Assert.True(references.Contains("key2"));
  386. Assert.True(references.Contains("key3"));
  387. });
  388. storage.Batch(accessor => accessor.Indexing.RemoveAllDocumentReferencesFrom("key1"));
  389. storage.Batch(accessor =>
  390. {
  391. var references = accessor.Indexing.GetDocumentsReferencesFrom("key1").ToList();
  392. Assert.Equal(0, references.Count);
  393. });
  394. }
  395. }
  396. [Theory]
  397. [PropertyData("Storages")]
  398. public void DocumentReferences2(string requestedStorage)
  399. {
  400. using (var storage = NewTransactionalStorage(requestedStorage))
  401. {
  402. storage.Batch(accessor => accessor.Indexing.AddIndex(101, true));
  403. storage.Batch(accessor => accessor.Indexing.UpdateDocumentReferences(303, "key1", new HashSet<string> { "key2", "key3" }));
  404. storage.Batch(accessor => accessor.Indexing.UpdateDocumentReferences(303, "key2", new HashSet<string> { "key1", "key3" }));
  405. storage.Batch(accessor =>
  406. {
  407. var documents = accessor.Indexing.GetDocumentsReferencing("key3").ToList();
  408. Assert.Equal(2, documents.Count);
  409. Assert.True(documents.Contains("key1"));
  410. Assert.True(documents.Contains("key2"));
  411. Assert.Equal(documents.Count, accessor.Indexing.GetCountOfDocumentsReferencing("key3"));
  412. Assert.Equal(1, accessor.Indexing.GetCountOfDocumentsReferencing("key1"));
  413. Assert.Equal(1, accessor.Indexing.GetCountOfDocumentsReferencing("key2"));
  414. });
  415. }
  416. }
  417. }
  418. }