问题描述
我想使用服务结构可靠的字典来存储我们打算通过rest接口查询的数据.我想知道查询这些集合中保存的数据的最佳方法是什么.
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.
该文档似乎没有提供任何方法来支持查询IReliableDictionary
接口,而不仅仅是简单地枚举集合.
The documentation doesn't seem to provide any methods to support querying a IReliableDictionary
interface apart from simply enumerating over the collection.
从可靠字典中提取每个对象并弹出到List 集合中进行查询的唯一选择吗?
Is the only option to fetch every object out of the reliable dictionary and pop into a List<> collection for querying?
谢谢
推荐答案
正如瓦茨拉夫(Vaclav)在他的回答中提到的那样,您始终可以根据需要枚举整个字典并进行过滤.此外,请查看 IReliableDictionary ,特别是CreateEnumerableAsync(ITransaction, Func<TKey, Boolean>, EnumerationMode)
.这样一来,您就可以预先过滤希望包含在枚举中的键,这可以提高性能(例如,通过避免将不必要的值从磁盘分页回磁盘中).
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
}
}
您可以创建与基于键进行搜索的REST查询相对应的罐头或动态键过滤器-对于基于值的查询,您需要退回到完全枚举(例如,通过使用与上述相同的模式,但是省略键过滤器)或使用通知来构建自己的二级索引.
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.
这篇关于查询可靠字典集合的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!