/ToMigrate/Raven.Tests/IndexMerging/SimpleIndexMerging.cs

https://github.com/fitzchak/ravendb · C# · 274 lines · 204 code · 64 blank · 6 comment · 6 complexity · 72170e4f64018434d1c18ebbccd8eebf MD5 · raw file

  1. // -----------------------------------------------------------------------
  2. // <copyright file="SimpleIndexMerging.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using Raven.Abstractions.Indexing;
  9. using System.Text.RegularExpressions;
  10. using Raven.Tests.Common;
  11. using Xunit;
  12. namespace Raven.Tests.IndexMerging
  13. {
  14. public class SimpleIndexMerging : RavenTest
  15. {
  16. [Fact]
  17. public void WillSuggestMergeTwoSimpleIndexesForSameCollection()
  18. {
  19. using (var store = NewDocumentStore())
  20. {
  21. store.DatabaseCommands.PutIndex("test1", new IndexDefinition
  22. {
  23. Map = "from o in docs.Orders select new { o.Customer }"
  24. });
  25. store.DatabaseCommands.PutIndex("test2", new IndexDefinition
  26. {
  27. Map = "from o in docs.Orders select new { o.Email }"
  28. });
  29. var suggestions = store.DatabaseCommands.GetIndexMergeSuggestions();
  30. Assert.Equal(1, suggestions.Suggestions.Count);
  31. var mergeSuggestion = suggestions.Suggestions[0];
  32. Assert.Equal(new[] { "test1", "test2" }, mergeSuggestion.CanMerge);
  33. var suggestedIndexMap = RemoveSpaces(mergeSuggestion.MergedIndex.Map);
  34. var expectedIndexMap = "from doc in docs.Orders select new { Customer = doc.Customer, Email = doc.Email }";
  35. expectedIndexMap = RemoveSpaces(expectedIndexMap);
  36. Assert.Equal(expectedIndexMap, suggestedIndexMap);
  37. }
  38. }
  39. [Fact]
  40. public void WillSuggestMergeTwoSimpleIndexesForSameCollectionWithAdditionalProperties()
  41. {
  42. using (var store = NewDocumentStore())
  43. {
  44. store.DatabaseCommands.PutIndex("test1", new IndexDefinition
  45. {
  46. Map = "from o in docs.Orders select new { o.Customer }",
  47. Indexes = { { "Name", FieldIndexing.Analyzed } }
  48. });
  49. store.DatabaseCommands.PutIndex("test2", new IndexDefinition
  50. {
  51. Map = "from o in docs.Orders select new { o.Email }",
  52. Indexes = { { "Email", FieldIndexing.Analyzed } }
  53. });
  54. var suggestions = store.DatabaseCommands.GetIndexMergeSuggestions();
  55. Assert.Equal(1, suggestions.Suggestions.Count);
  56. var mergeSuggestion = suggestions.Suggestions[0];
  57. Assert.Equal(new[] { "test1", "test2" }, mergeSuggestion.CanMerge);
  58. var suggestedIndexMap = RemoveSpaces(mergeSuggestion.MergedIndex.Map);
  59. var expectedIndexMap = "from doc in docs.Orders select new { Customer = doc.Customer, Email = doc.Email }";
  60. expectedIndexMap = RemoveSpaces(expectedIndexMap);
  61. Assert.Equal(expectedIndexMap, suggestedIndexMap);
  62. Assert.Equal(FieldIndexing.Analyzed, mergeSuggestion.MergedIndex.Indexes["Name"]);
  63. Assert.Equal(FieldIndexing.Analyzed, mergeSuggestion.MergedIndex.Indexes["Email"]);
  64. }
  65. }
  66. [Fact]
  67. public void WillSuggestMergeTwoIndexesWithPropertiesForSameCollection()
  68. {
  69. using (var store = NewDocumentStore())
  70. {
  71. store.DatabaseCommands.PutIndex("test1", new IndexDefinition
  72. {
  73. Map = "from o in docs.Orders select new { o.Customer }",
  74. SortOptions = new Dictionary<string, SortOptions> { { "Customer", SortOptions.String } }
  75. });
  76. store.DatabaseCommands.PutIndex("test2", new IndexDefinition
  77. {
  78. Map = "from o in docs.Orders select new { o.Email }",
  79. Stores = new Dictionary<string, FieldStorage> { { "Email", FieldStorage.Yes } },
  80. SortOptions = new Dictionary<string, SortOptions> { { "Email", SortOptions.String } }
  81. });
  82. var suggestions = store.DatabaseCommands.GetIndexMergeSuggestions();
  83. Assert.Equal(1, suggestions.Suggestions.Count);
  84. var mergeSuggestion = suggestions.Suggestions[0];
  85. Assert.Equal(new[] { "test1", "test2" }, mergeSuggestion.CanMerge);
  86. var suggestedIndexMap = RemoveSpaces(mergeSuggestion.MergedIndex.Map);
  87. var expectedIndexMap = "from doc in docs.Orders select new { Customer = doc.Customer, Email = doc.Email }";
  88. expectedIndexMap = RemoveSpaces(expectedIndexMap);
  89. Assert.Equal(expectedIndexMap,
  90. suggestedIndexMap);
  91. var suggestedStoresDict = mergeSuggestion.MergedIndex.Stores.Keys.ToDictionary(key => key, key => mergeSuggestion.MergedIndex.Stores[key]);
  92. var suggestedSortDict = mergeSuggestion.MergedIndex.SortOptions.Keys.ToDictionary(key => key, key => mergeSuggestion.MergedIndex.SortOptions[key]);
  93. var expectedStoresDict = new Dictionary<string, FieldStorage> { { "Email", FieldStorage.Yes } };
  94. var expectedSortDict = new Dictionary<string, SortOptions> { { "Customer", SortOptions.String }, { "Email", SortOptions.String } };
  95. var result = DataDictionaryCompare(suggestedStoresDict, expectedStoresDict);
  96. Assert.Equal(true, result);
  97. result = DataDictionaryCompare(suggestedSortDict, expectedSortDict);
  98. Assert.Equal(true, result);
  99. }
  100. }
  101. [Fact]
  102. public void WillSuggestNoMergeTwoIndexesWithPropertiesForSameCollection()
  103. {
  104. using (var store = NewDocumentStore())
  105. {
  106. store.DatabaseCommands.PutIndex("test1", new IndexDefinition
  107. {
  108. Map = "from o in docs.Orders select new { o.Customer,o.Email }",
  109. SortOptions = new Dictionary<string, SortOptions> { { "Customer", SortOptions.String }, { "Email", SortOptions.String } },
  110. });
  111. store.DatabaseCommands.PutIndex("test2", new IndexDefinition
  112. {
  113. Map = "from o in docs.Orders select new { o.Email,o.Address }",
  114. SortOptions = new Dictionary<string, SortOptions> { { "Email", SortOptions.String } }
  115. });
  116. store.DatabaseCommands.PutIndex("test3", new IndexDefinition
  117. {
  118. Map = "from o in docs.Orders select new { o.Email,o.Tel }",
  119. SortOptions = new Dictionary<string, SortOptions> { { "Email", SortOptions.String } }
  120. });
  121. var suggestions = store.DatabaseCommands.GetIndexMergeSuggestions();
  122. Assert.Equal(1, suggestions.Suggestions.Count);
  123. var mergeSuggestion = suggestions.Suggestions[0];
  124. Assert.Equal(new[] { "test1", "test2", "test3" }, mergeSuggestion.CanMerge);
  125. var suggestedIndexMap = RemoveSpaces(mergeSuggestion.MergedIndex.Map);
  126. var expectedIndexMap = "from doc in docs.Orders select new { Address = doc.Address, Customer = doc.Customer, Email = doc.Email, Tel = doc.Tel }";
  127. expectedIndexMap = RemoveSpaces(expectedIndexMap);
  128. Assert.Equal(expectedIndexMap, suggestedIndexMap);
  129. var suggestedSortDict = mergeSuggestion.MergedIndex.SortOptions.Keys.ToDictionary(key => key, key => mergeSuggestion.MergedIndex.SortOptions[key]);
  130. var expectedSortDict = new Dictionary<string, SortOptions> { { "Customer", SortOptions.String }, { "Email", SortOptions.String } };
  131. bool result = DataDictionaryCompare(suggestedSortDict, expectedSortDict);
  132. Assert.Equal(true, result);
  133. store.DatabaseCommands.PutIndex("test4", new IndexDefinition
  134. {
  135. Map = "from o in docs.Orders select new { o.Email,o.Fax }",
  136. Stores = new Dictionary<string, FieldStorage> { { "Email", FieldStorage.Yes } },
  137. });
  138. var newSuggestions = store.DatabaseCommands.GetIndexMergeSuggestions();
  139. Assert.Equal(1, newSuggestions.Suggestions.Count);
  140. var newMergeSuggestion = suggestions.Suggestions[0];
  141. Assert.Equal(new[] { "test1", "test2", "test3" }, newMergeSuggestion.CanMerge);
  142. var expectedUnmergebleDict = new Dictionary<string, string>
  143. {
  144. { "Raven/DocumentsByEntityName", "Can't find any other index to merge this with" }
  145. };
  146. bool res = DataDictionaryCompare(suggestions.Unmergables, expectedUnmergebleDict);
  147. Assert.Equal(true, res);
  148. }
  149. }
  150. [Fact]
  151. public void UnMergeblesTest()
  152. {
  153. //Important : indexes with group bym orderby don't pass syntax check
  154. using (var store = NewDocumentStore())
  155. {
  156. store.DatabaseCommands.PutIndex("test1", new IndexDefinition
  157. {
  158. Map = @"docs.Datas.Where(doc => doc.Info.Contains(""2"")).Select(doc => new { InfoAndRefernce = doc.Info + ""_"" + ((object) doc.Reference)})",
  159. });
  160. store.DatabaseCommands.PutIndex("test6", new IndexDefinition
  161. {
  162. Map = "from article in docs.Articles select new { CategoryName = article.CategoryName }",
  163. Reduce = "from result in results group result by result.CategoryName into g select new { CategoryName = g.Key } "
  164. });
  165. store.DatabaseCommands.PutIndex("test7", new IndexDefinition
  166. {
  167. Map = @"from doc in docs let Tag = doc[""metadata""][""Raven-Entity-Name""] select new { Tag, LastModified = (DateTime)doc[""@metadata""][""Modified""] }"
  168. });
  169. store.DatabaseCommands.PutIndex("test3", new IndexDefinition
  170. {
  171. Map = "from o in docs.Orders select new { o.Email,o.Tel }",
  172. SortOptions = new Dictionary<string, SortOptions> { { "Email", SortOptions.String } }
  173. });
  174. var suggestions = store.DatabaseCommands.GetIndexMergeSuggestions();
  175. Assert.Equal(5, suggestions.Unmergables.Count);
  176. var unmergebleExpectedDict = new Dictionary<string, string>
  177. {
  178. {"test1", "Cannot merge indexes that have a where clause"},
  179. {"test6", "Cannot merge map/reduce indexes"},
  180. {"test7", "Cannot merge indexes that are using a let clause"},
  181. {"test3", "Can't find any other index to merge this with"},
  182. {"Raven/DocumentsByEntityName", "Can't find any other index to merge this with"}
  183. };
  184. bool result = DataDictionaryCompare(suggestions.Unmergables, unmergebleExpectedDict);
  185. Assert.Equal(true, result);
  186. }
  187. }
  188. private string RemoveSpaces(string inputString)
  189. {
  190. var resultStr = inputString.Replace("\r\n\t", " ");
  191. resultStr = resultStr.Replace("\t", " ").Trim();
  192. resultStr = Regex.Replace(resultStr, @"\s+", " ");
  193. return resultStr;
  194. }
  195. private bool DataDictionaryCompare<T>(IDictionary<string, T> dataDict1, IDictionary<string, T> dataDict2)
  196. {
  197. foreach (var kvp in dataDict1.Keys)
  198. {
  199. T v1, v2;
  200. var found1 = dataDict1.TryGetValue(kvp, out v1);
  201. var found2 = dataDict2.TryGetValue(kvp, out v2);
  202. if (found1 && found2 && Equals(v1, v2) == false)
  203. return false;
  204. if (found1 != found2)
  205. return false;
  206. }
  207. return true;
  208. }
  209. }
  210. }