Permalink
Browse files

Fixing things so indexes won't actually perform indexes on documents …

…that they don't apply to
  • Loading branch information...
1 parent 906de64 commit 53614e04a2e4193cf774a03cd43a38eef784dddf @ayende ayende committed Dec 11, 2011
@@ -90,6 +90,7 @@ private void ExecuteIndexingInternal(IList<IndexToWorkOn> indexesToWorkOn, Actio
if (jsonDocs.Length > 0)
{
var result = FilterIndexes(indexesToWorkOn, jsonDocs).ToList();
+ indexesToWorkOn = result.Select(x => x.Item1).ToList();
indexingOp(result);
}
}
@@ -137,15 +138,39 @@ private void MarkIndexes(IndexToWorkOn indexToWorkOn, ComparableByteArray lastIn
var lastModified = last.LastModified.Value;
var lastIndexedEtag = new ComparableByteArray(lastEtag.ToByteArray());
-
+ Action<IStorageActionsAccessor> action = null;
foreach (var indexToWorkOn in indexesToWorkOn)
{
var indexLastInedexEtag = new ComparableByteArray(indexToWorkOn.LastIndexedEtag.ToByteArray());
if (indexLastInedexEtag.CompareTo(lastIndexedEtag) >= 0)
continue;
- yield return Tuple.Create(indexToWorkOn, jsonDocs.Where(doc => indexLastInedexEtag.CompareTo(new ComparableByteArray(doc.Etag.Value.ToByteArray())) < 0));
- //actions.Indexing.UpdateLastIndexed(indexToWorkOn.IndexName, lastEtag, lastModified);
+ var filteredDocs = jsonDocs.Where(doc => indexLastInedexEtag.CompareTo(new ComparableByteArray(doc.Etag.Value.ToByteArray())) < 0);
+
+ var indexName = indexToWorkOn.IndexName;
+ var viewGenerator = context.IndexDefinitionStorage.GetViewGenerator(indexName);
+ if(viewGenerator == null)
+ continue; // probably deleted
+
+ if (viewGenerator.ForEntityNames.Count != 0) // limit for the items that it care for
+ {
+ filteredDocs = filteredDocs.Where(x => viewGenerator.ForEntityNames.Contains(x.Metadata.Value<string>(Constants.RavenEntityName)));
+ }
+
+ List<JsonDocument> jsonDocuments = filteredDocs.ToList();
+
+ if(jsonDocuments.Count == 0)
+ {
+ // we use it this way to batch all the updates together
+ action += accessor => accessor.Indexing.UpdateLastIndexed(indexName, lastEtag, lastModified);
+ }
+
+ yield return Tuple.Create<IndexToWorkOn, IEnumerable<JsonDocument>>(indexToWorkOn, jsonDocuments);
+ }
+
+ if (action != null)
+ {
+ transactionalStorage.Batch(action);
}
}
@@ -113,7 +113,7 @@ public class TestClassView : AbstractViewGenerator
public TestClassView()
{
- ForEntityNames.Add("TestClass");
+ ForEntityNames.Add("TestClasses");
MapDefinitions.Add(MapToPaths);
ReduceDefinition = Reduce;
GroupByExtraction = doc => doc.UserId;
@@ -36,7 +36,6 @@ public void ShouldNotReindexAlreadyIndexedDocs()
store.DocumentDatabase.StopBackgroundWokers();
-
store.DatabaseCommands.PutIndex("test1", new IndexDefinition
{
Map = "from doc in docs select new { doc.Name }"
@@ -58,5 +57,64 @@ public void ShouldNotReindexAlreadyIndexedDocs()
Assert.Equal(2, store.DocumentDatabase.Statistics.Indexes.First(x => x.Name == "test").IndexingAttempts);
}
}
+
+
+ [Fact]
+ public void IndexOnUsersShouldNotIndexPosts()
+ {
+ using (var store = NewDocumentStore())
+ {
+ store.DatabaseCommands.PutIndex("test", new IndexDefinition
+ {
+ Map = "from doc in docs.Users select new { doc.Name }"
+ });
+
+ using (var session = store.OpenSession())
+ {
+ session.Store(new User() { Name = "oren" });
+ session.SaveChanges();
+ }
+
+ using (var session = store.OpenSession())
+ {
+ session.Query<object>("test").Customize(x => x.WaitForNonStaleResults()).ToList();
+ }
+
+ Assert.Equal(1, store.DocumentDatabase.Statistics.Indexes.First(x => x.Name == "test").IndexingAttempts);
+
+ using (var session = store.OpenSession())
+ {
+ session.Store(new Patching.Post { });
+ session.SaveChanges();
+ }
+
+ store.DatabaseCommands.PutIndex("test2", new IndexDefinition
+ {
+ Map = "from doc in docs.Users select new { doc.Name }"
+ });
+
+ using (var session = store.OpenSession())
+ {
+ session.Query<object>("test2").Customize(x => x.WaitForNonStaleResults()).ToList();
+ }
+
+ Assert.Equal(1, store.DocumentDatabase.Statistics.Indexes.First(x => x.Name == "test2").IndexingAttempts);
+
+
+ using (var session = store.OpenSession())
+ {
+ session.Query<object>("test").Customize(x => x.WaitForNonStaleResults()).ToList();
+ }
+
+ Assert.Equal(1, store.DocumentDatabase.Statistics.Indexes.First(x => x.Name == "test").IndexingAttempts);
+
+ using (var session = store.OpenSession())
+ {
+ session.Query<object>("test2").Customize(x => x.WaitForNonStaleResults()).ToList();
+ }
+
+ Assert.Equal(1, store.DocumentDatabase.Statistics.Indexes.First(x => x.Name == "test2").IndexingAttempts);
+ }
+ }
}
}

0 comments on commit 53614e0

Please sign in to comment.