2012-01-30 57 views
10

我正在使用内存模式下的RavenDB进行单元测试。我的查询由静态索引支持。我没有使用WaitForNonStaleResults() API(我也不想)。测试期间应该如何处理陈旧的索引?

用于试验的典型工作流程是:

  1. 初始化RavenDB在内存中的模式
  2. 集成使用IndexCreation.CreateIndexes(Assembly, IDocumentStore)
  3. 插入测试数据的索引(用于验证查询行为)
  4. 运行查询
  5. 验证查询输出

我已经注意到步骤1-3发生得如此之快,静态索引没有时间在步骤4之前更新 - 因此索引是陈旧的。

我已经为此创建了一个快速解决方法。执行第3步后,我执行:

while (documentStore.DocumentDatabase.Statistics.StaleIndexes.Length != 0) 
    Thread.Sleep(10); 

这样感觉很麻烦。我想知道的是:

  • 这是正常的,在内存模式下运行时RavenDB索引过时?
  • 有没有更好的方法来避免测试过程中的陈旧索引?

回答

14

交叉邮寄到RavenDB usergroup并有一个工作解决方案。

在In-Memory 模式下运行RavenDB时,索引是否过时是否正常?

是的。索引是一个索引。

有没有更好的办法测试期间避免旧索引?

是的。初始化文件存储时配置的全球性公约:

var store = new EmbeddableDocumentStore(); 
store.RunInMemory = true; 
store.Conventions = new DocumentConvention 
{ 
    DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites 
}; 

store.Initialize(); 

注:ConsistencyOptions.QueryYourWrites不与地图工作/减少索引,即指数与Reduce => ...部分。对于这些,你必须使用Customize(x => x.WaitForNonStale...())查询

更新时:another approach,这可能会更好(没有亲自尝试过呢)。您可以实现IDocumentQueryListener以强制所有查询返回非陈旧结果:

var store = new EmbeddableDocumentStore { RunInMemory = true }; 
store.Initialize(); 

store.RegisterListener(new ForceNonStaleQueryListener()); 

public class ForceNonStaleQueryListener : IDocumentQueryListener 
{ 
    public void BeforeQueryExecuted(IDocumentQueryCustomization customization) 
    { 
     queryCustomization.WaitForNonStaleResults(); 
    } 
} 
+0

@MattWarren谢谢,马特。有用的知道。 – 2012-01-31 14:27:48

+0

Listener方法不再适用于当前的RavenDB构建。 – nathanchere 2012-04-02 03:13:48

+0

@FerretallicA我会再次检查最新版本。 – 2012-04-02 08:20:38

相关问题