RavenDB如何刷新? [英] RavenDB how to flush?

查看:84
本文介绍了RavenDB如何刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用嵌入式RavenDb.作为集成测试的一部分,我要检查对象是否持久.当我在对象上保存更改,然后对其进行检索时,除非我处理连接,否则将找不到该对象.

I am using RavenDb embedded. As a part of my intergration tests I want to check objects are persisted. When I SaveChanges on an object, then retrieve it, it cannot be found unless I dispose my connection.

这对我不起作用,因为没有文件返回

This does not work for me, as no files are returned

 using (var session = _dataDocumentStore.Instance.OpenSession())
 {
         session.Store(file);
         session.SaveChanges();
 }

....

using (var session = _dataDocumentStore.Instance.OpenSession() )
{
          return session.Query<File>().ToList();
}

我创建了一个Flush方法,该方法可以处理并重新创建一个可以正常工作的EmbeddableDocumentStore,但是由于这有点感觉,因此我可能会以错误的方式进行操作:

I created a Flush method which disposes and recreates a EmbeddableDocumentStore which works, but as this is somthing that feels fundamental I may be going about things the wrong way:

  public static IDocumentStore Initialize()
        {
            instance = new EmbeddableDocumentStore
            {
                DataDirectory = "App_Data/Database",
                UseEmbeddedHttpServer = true,


            };

            instance.Initialize();
            return instance;
        }

        public void Flush()
        {
            instance.Dispose();
            Initialize();

        }

如何在RavenDB中持久保存,然后检查它是否已经持久保存?关于此的任何建议都很好

How do you persist in RavenDB and then check it has been persisted? Any advice on this would be great

推荐答案

基本上,与保存和查询相比,EmbeddableDocumentStore花费更长的时间保存和索引该新数据.

Basically, the EmbeddableDocumentStore takes longer to save and Index that new data, than saving and querying.

所以当您的测试说:-

  1. 存储并保存更改.
  2. 加载.
  3. 此负载了吗?

加载完成的速度比索引创建所需的时间快得多.

因此,就像Daniel Lang所说的那样,您需要等待过时的结果.

So, Like Daniel Lang said, you need to wait for stale results.

但是,对于您要检查的每个查询,您都必须在代码中执行此操作.所以,让我们作弊(合法):)

But, you'll have to do that for every query you wish to check, in your code. So, let's cheat (legally) :)

如果有什么问题询问文档存储,您可以通过以下方法告诉文档存储始终等待过时的结果:

Here is how you can tell your document store to ALWAYS wait for stale results, if something queries the store:

// Initialise the Store.
var documentStore = new EmbeddableDocumentStore
                    {
                        RunInMemory = true
                    };
documentStore.Initialize();

// Force query's to wait for index's to catch up. Unit Testing only :P
documentStore.RegisterListener(new NoStaleQueriesListener());

....


#region Nested type: NoStaleQueriesListener

public class NoStaleQueriesListener : IDocumentQueryListener
{
    #region Implementation of IDocumentQueryListener

    public void BeforeQueryExecuted(IDocumentQueryCustomization queryCustomization)
    {
        queryCustomization.WaitForNonStaleResults();
    }

    #endregion
}

#endregion

现在要查看实际效果,请在Github上查看 RavenOverflow .该解决方案中的Tests项目拥有您可能想要的所有爱.

Now to see this in action, check out RavenOverflow @ Github. And the Tests project in that solution has all the love you might want.

这篇关于RavenDB如何刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆