Skip to content

Commit

Permalink
Fixing things so indexes won't actually perform indexes on documents …
Browse files Browse the repository at this point in the history
…that they don't apply to
  • Loading branch information
ayende committed Dec 11, 2011
1 parent 906de64 commit 53614e0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
31 changes: 28 additions & 3 deletions Raven.Database/Indexing/IndexingExecuter.cs
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Raven.Tests/Bugs/CompiledIndexesNhsevidence.cs
Expand Up @@ -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;
Expand Down
60 changes: 59 additions & 1 deletion Raven.Tests/Bugs/Reindexing.cs
Expand Up @@ -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 }"
Expand All @@ -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.