What is the most optimal method of querying a reliable dictionary collection

老子叫甜甜 提交于 2019-12-10 09:53:51

问题


I would like to use service fabric reliable dictionaries to store data that we intend to query via a rest interface. I was wondering what was the most optimal approach to querying data held in those collections.

The documentation doesn't seem to provide any methods to support querying a IReliableDictionary interface apart from simply enumerating over the collection.

Is the only option to fetch every object out of the reliable dictionary and pop into a List<> collection for querying?

Thanks


回答1:


As Vaclav mentions in his answer, you can always enumerate the entire dictionary and filter as you wish. Additionally, take a look at the docs for IReliableDictionary, specifically CreateEnumerableAsync(ITransaction, Func<TKey, Boolean>, EnumerationMode). This allows you to filter the keys you wish to include in the enumeration up-front, which may improve perf (e.g. by avoiding paging unnecessary values back in from disk).

Simple Example (exception handling/retry/etc. omitted for brevity):

var myDictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<int, string>>(new Uri("fabric:/myDictionary"));

using (var tx = this.StateManager.CreateTransaction())
{
    // Fetch all key-value pairs where the key an integer less than 10
    var enumerable = await myDictionary.CreateEnumerableAsync(tx, key => key < 10, EnumerationMode.Ordered); 
    var asyncEnumerator = enumerable.GetAsyncEnumerator();

    while (await asyncEnumerator.MoveNextAsync(cancellationToken))
    {
        // Process asyncEnumerator.Current.Key and asyncEnumerator.Current.Value as you wish
    }
}

You could create canned or dynamic key filters corresponding to your REST queries which search based on key - for value-based queries you will need to fall back to full enumeration (e.g. by using the same pattern as above, but omitting the key filter) or use notifications in order to build your own secondary index.




回答2:


IReliableDictionary implements IEnumerable, which means you can query it with LINQ expressions, same way you would with an IDictionary.

Note that an enumeration over IReliableDictionary uses snapshot isolation, so it is lock free. Basically this means when you start the enumeration you are enumerating over the dictionary as it is at the time you started. If items are added or removed while you're enumerating, those changes won't appear in that enumeration. More info about this and Reliable Collections in general here: https://azure.microsoft.com/en-us/documentation/articles/service-fabric-reliable-services-reliable-collections/




回答3:


Look here What would be the best way to search an IReliableDictionary?

There is two way to do it

  1. Eli Arbel has async extension methods on Gist that provide a bridge to Ix-Async and also async implementations of Select, SelectMany, and Where.
  2. You can wrap IAsyncEnumerable in a regular IEnumerable that simply wraps the asynchronous


来源:https://stackoverflow.com/questions/33174145/what-is-the-most-optimal-method-of-querying-a-reliable-dictionary-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!