Service Fabric 可靠集合 - 关键过滤器是否按确定顺序应用于 CreateEnumerableAsync?
Service Fabric Reliable Collections - Are key filters applied in deterministic order for CreateEnumerableAsync?
我正在尝试为可靠的字典实现基于游标的分页。我知道 IReliableDictionary
键必须实现 IComparable
,并且 IReliableDictionary
这个用于枚举字典条目的方法:
IAsyncEnumerable<KeyValuePair<TKey,TValue>>>
CreateEnumerableAsync (
ITransaction txn,
Func<TKey,bool> filter,
EnumerationMode enumerationMode);
当使用EnumerationMode.Ordered
时,我假设我们根据键的IComparable
实现枚举键值对。
我们是否也可以假设 filter
参数按照键的 IComparable
实现顺序应用于每个键?也许另一种问法 - 内存中的键 and/or 是按照 IComparable
实现的顺序枚举的吗?如果是这样,是否记录了此行为,或者是否应将其视为可能更改的实现细节?
我使用 Voting Web 示例进行了一项实验,并且键似乎确实按照其 IComparable
实现的顺序进行了过滤,但有一些注意事项:
- 过滤器似乎可以 运行 多次使用同一个键
- 过滤器可以应用于最近删除的项目
VotingData.Controllers.VoteDataController
有以下获取和放置操作来列出和添加投票类别:
[HttpPut("{name}")]
public async Task<IActionResult> Put(string name)
{
IReliableDictionary<string, int> votesDictionary = await this.stateManager.GetOrAddAsync<IReliableDictionary<string, int>>("counts");
using (ITransaction tx = this.stateManager.CreateTransaction())
{
await votesDictionary.AddOrUpdateAsync(tx, name, 1, (key, oldvalue) => oldvalue + 1);
await tx.CommitAsync();
}
return new OkResult();
}
我修改了 Get
以按顺序枚举 votesDictionary
,并应用了一个过滤器,该过滤器构建了过滤器看到的键列表:
[HttpGet]
public async Task<IActionResult> Get()
{
CancellationToken ct = new CancellationToken();
IReliableDictionary<string, int> votesDictionary = await this.stateManager.GetOrAddAsync<IReliableDictionary<string, int>>("counts");
var filteredKeys = new List<string>();
using (ITransaction tx = this.stateManager.CreateTransaction())
{
IAsyncEnumerable<KeyValuePair<string, int>> list = await votesDictionary.CreateEnumerableAsync(tx, key =>
{
lock (this.locker)
{
filteredKeys.Add(key);
return true;
}
},
EnumerationMode.Ordered);
IAsyncEnumerator<KeyValuePair<string, int>> enumerator = list.GetAsyncEnumerator();
List<KeyValuePair<string, int>> result = new List<KeyValuePair<string, int>>();
while (await enumerator.MoveNextAsync(ct))
{
result.Add(enumerator.Current);
}
return this.Json(result);
}
}
我按随机字母顺序向字典中添加了关键字,并刷新页面以 运行 Get
查询。每次刷新时,filteredKeys
集合包含我按字母顺序排序的条目。如上所述,该集合有时包含某些字符串的重复条目。当我从集合中删除项目并刷新页面时,我发现删除的键仍然添加到 filteredKeys
,尽管这些元素没有返回到结果枚举中。
我正在尝试为可靠的字典实现基于游标的分页。我知道 IReliableDictionary
键必须实现 IComparable
,并且 IReliableDictionary
这个用于枚举字典条目的方法:
IAsyncEnumerable<KeyValuePair<TKey,TValue>>>
CreateEnumerableAsync (
ITransaction txn,
Func<TKey,bool> filter,
EnumerationMode enumerationMode);
当使用EnumerationMode.Ordered
时,我假设我们根据键的IComparable
实现枚举键值对。
我们是否也可以假设 filter
参数按照键的 IComparable
实现顺序应用于每个键?也许另一种问法 - 内存中的键 and/or 是按照 IComparable
实现的顺序枚举的吗?如果是这样,是否记录了此行为,或者是否应将其视为可能更改的实现细节?
我使用 Voting Web 示例进行了一项实验,并且键似乎确实按照其 IComparable
实现的顺序进行了过滤,但有一些注意事项:
- 过滤器似乎可以 运行 多次使用同一个键
- 过滤器可以应用于最近删除的项目
VotingData.Controllers.VoteDataController
有以下获取和放置操作来列出和添加投票类别:
[HttpPut("{name}")]
public async Task<IActionResult> Put(string name)
{
IReliableDictionary<string, int> votesDictionary = await this.stateManager.GetOrAddAsync<IReliableDictionary<string, int>>("counts");
using (ITransaction tx = this.stateManager.CreateTransaction())
{
await votesDictionary.AddOrUpdateAsync(tx, name, 1, (key, oldvalue) => oldvalue + 1);
await tx.CommitAsync();
}
return new OkResult();
}
我修改了 Get
以按顺序枚举 votesDictionary
,并应用了一个过滤器,该过滤器构建了过滤器看到的键列表:
[HttpGet]
public async Task<IActionResult> Get()
{
CancellationToken ct = new CancellationToken();
IReliableDictionary<string, int> votesDictionary = await this.stateManager.GetOrAddAsync<IReliableDictionary<string, int>>("counts");
var filteredKeys = new List<string>();
using (ITransaction tx = this.stateManager.CreateTransaction())
{
IAsyncEnumerable<KeyValuePair<string, int>> list = await votesDictionary.CreateEnumerableAsync(tx, key =>
{
lock (this.locker)
{
filteredKeys.Add(key);
return true;
}
},
EnumerationMode.Ordered);
IAsyncEnumerator<KeyValuePair<string, int>> enumerator = list.GetAsyncEnumerator();
List<KeyValuePair<string, int>> result = new List<KeyValuePair<string, int>>();
while (await enumerator.MoveNextAsync(ct))
{
result.Add(enumerator.Current);
}
return this.Json(result);
}
}
我按随机字母顺序向字典中添加了关键字,并刷新页面以 运行 Get
查询。每次刷新时,filteredKeys
集合包含我按字母顺序排序的条目。如上所述,该集合有时包含某些字符串的重复条目。当我从集合中删除项目并刷新页面时,我发现删除的键仍然添加到 filteredKeys
,尽管这些元素没有返回到结果枚举中。