2012-01-30 24 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