本文整理汇总了C#中IFdbTransaction.AtomicAdd方法的典型用法代码示例。如果您正苦于以下问题:C# IFdbTransaction.AtomicAdd方法的具体用法?C# IFdbTransaction.AtomicAdd怎么用?C# IFdbTransaction.AtomicAdd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IFdbTransaction
的用法示例。
在下文中一共展示了IFdbTransaction.AtomicAdd方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Scenario6
private async Task Scenario6(IFdbTransaction tr)
{
var location = FdbSubspace.Create(Slice.FromAscii("TEST"));
tr.AtomicAdd(location.Pack("ATOMIC"), Slice.FromFixed32(0x55555555));
var x = await tr.GetAsync(location.Pack("ATOMIC"));
Console.WriteLine(x.ToInt32().ToString("x"));
}
示例2: Scenario3
private Task Scenario3(IFdbTransaction tr)
{
var location = FdbSubspace.Create(Slice.FromAscii("TEST"));
tr.Set(location.Key + (byte)'a', Slice.FromAscii("A"));
tr.AtomicAdd(location.Key + (byte)'k', Slice.FromFixed32(1));
tr.Set(location.Key + (byte)'z', Slice.FromAscii("C"));
tr.ClearRange(location.Key + (byte)'a', location.Key + (byte)'k');
tr.ClearRange(location.Key + (byte)'k', location.Key + (byte)'z');
return Task.FromResult<object>(null);
}
示例3: AllocateAsync
/// <summary>Returns a 64-bit integer that
/// 1) has never and will never be returned by another call to this
/// method on the same subspace
/// 2) is nearly as short as possible given the above
/// </summary>
public async Task<long> AllocateAsync(IFdbTransaction trans)
{
// find the current window size, by reading the last entry in the 'counters' subspace
long start = 0, count = 0;
var kv = await trans
.Snapshot
.GetRange(this.Counters.ToRange())
.LastOrDefaultAsync();
if (kv.Key.IsPresent)
{
start = this.Counters.UnpackSingle<long>(kv.Key);
count = kv.Value.ToInt64();
}
// check if the window is full
int window = GetWindowSize(start);
if ((count + 1) * 2 >= window)
{ // advance the window
trans.ClearRange(this.Counters.Key, this.Counters.Pack(start) + FdbKey.MinValue);
start += window;
trans.ClearRange(this.Recent.Key, this.Recent.Pack(start));
}
// Increment the allocation count for the current window
trans.AtomicAdd(this.Counters.Pack(start), Slice.FromFixed64(1));
// As of the snapshot being read from, the window is less than half
// full, so this should be expected to take 2 tries. Under high
// contention (and when the window advances), there is an additional
// subsequent risk of conflict for this transaction.
while (true)
{
// Find a random free slot in the current window...
long candidate;
lock (m_rnd)
{
candidate = start + m_rnd.Next(window);
}
// test if the key is used
var key = this.Recent.Pack(candidate);
var value = await trans.GetAsync(key).ConfigureAwait(false);
if (value.IsNull)
{ // free slot
// mark as used
trans.Set(key, Slice.Empty);
return candidate;
}
// no luck this time, try again...
}
}