本文整理汇总了C#中ConcurrentQueue.Sum方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentQueue.Sum方法的具体用法?C# ConcurrentQueue.Sum怎么用?C# ConcurrentQueue.Sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentQueue
的用法示例。
在下文中一共展示了ConcurrentQueue.Sum方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SingleStepReduce
private void SingleStepReduce(IndexToWorkOn index, string[] keysToReduce, AbstractViewGenerator viewGenerator,
ConcurrentSet<object> itemsToDelete)
{
var needToMoveToSingleStepQueue = new ConcurrentQueue<HashSet<string>>();
Log.Debug(() => string.Format("Executing single step reducing for {0} keys [{1}]", keysToReduce.Length, string.Join(", ", keysToReduce)));
var batchTimeWatcher = Stopwatch.StartNew();
var count = 0;
var size = 0;
var state = new ConcurrentQueue<Tuple<HashSet<string>, List<MappedResultInfo>>>();
var reducingBatchThrottlerId = Guid.NewGuid();
try
{
BackgroundTaskExecuter.Instance.ExecuteAllBuffered(context, keysToReduce, enumerator =>
{
var localNeedToMoveToSingleStep = new HashSet<string>();
needToMoveToSingleStepQueue.Enqueue(localNeedToMoveToSingleStep);
var localKeys = new HashSet<string>();
while (enumerator.MoveNext())
{
localKeys.Add(enumerator.Current);
}
transactionalStorage.Batch(actions =>
{
var getItemsToReduceParams = new GetItemsToReduceParams(index: index.IndexName, reduceKeys: localKeys, level: 0,
loadData: false,
itemsToDelete: itemsToDelete)
{
Take = int.MaxValue// just get all, we do the rate limit when we load the number of keys to reduce, anyway
};
var scheduledItems = actions.MapReduce.GetItemsToReduce(getItemsToReduceParams).ToList();
autoTuner.CurrentlyUsedBatchSizes.GetOrAdd(reducingBatchThrottlerId, scheduledItems.Sum(x => x.Size));
if (scheduledItems.Count == 0)
{
if (Log.IsWarnEnabled)
{
Log.Warn("Found single reduce items ({0}) that didn't have any items to reduce. Deleting level 1 & level 2 items for those keys. (If you can reproduce this, please contact [email protected])",
string.Join(", ", keysToReduce));
}
// Here we have an interesting issue. We have scheduled reductions, because GetReduceTypesPerKeys() returned them
// and at the same time, we don't have any at level 0. That probably means that we have them at level 1 or 2.
// They shouldn't be here, and indeed, we remove them just a little down from here in this function.
// That said, they might bave smuggled in between versions, or something happened to cause them to be here.
// In order to avoid that, we forcibly delete those extra items from the scheduled reductions, and move on
foreach (var reduceKey in keysToReduce)
{
actions.MapReduce.DeleteScheduledReduction(index.IndexName, 1, reduceKey);
actions.MapReduce.DeleteScheduledReduction(index.IndexName, 2, reduceKey);
}
}
foreach (var reduceKey in localKeys)
{
var lastPerformedReduceType = actions.MapReduce.GetLastPerformedReduceType(index.IndexName, reduceKey);
if (lastPerformedReduceType != ReduceType.SingleStep)
localNeedToMoveToSingleStep.Add(reduceKey);
if (lastPerformedReduceType != ReduceType.MultiStep)
continue;
Log.Debug("Key {0} was moved from multi step to single step reduce, removing existing reduce results records",
reduceKey);
// now we are in single step but previously multi step reduce was performed for the given key
var mappedBuckets = actions.MapReduce.GetMappedBuckets(index.IndexName, reduceKey).ToList();
// add scheduled items too to be sure we will delete reduce results of already deleted documents
mappedBuckets.AddRange(scheduledItems.Select(x => x.Bucket));
foreach (var mappedBucket in mappedBuckets.Distinct())
{
actions.MapReduce.RemoveReduceResults(index.IndexName, 1, reduceKey, mappedBucket);
actions.MapReduce.RemoveReduceResults(index.IndexName, 2, reduceKey, mappedBucket / 1024);
}
}
var mappedResults = actions.MapReduce.GetMappedResults(
index.IndexName,
localKeys,
loadData: true
).ToList();
Interlocked.Add(ref count, mappedResults.Count);
Interlocked.Add(ref size, mappedResults.Sum(x => x.Size));
mappedResults.ApplyIfNotNull(x => x.Bucket = 0);
state.Enqueue(Tuple.Create(localKeys, mappedResults));
});
});
var reduceKeys = new HashSet<string>(state.SelectMany(x => x.Item1));
var results = state.SelectMany(x => x.Item2)
.Where(x => x.Data != null)
.GroupBy(x => x.Bucket, x => JsonToExpando.Convert(x.Data))
//.........这里部分代码省略.........
示例2: InitPackagePool
/// <summary>
/// 初始化红包池
/// </summary>
/// <param name="amount">红包总额</param>
/// <param name="packageCount">红包总数</param>
/// <param name="ceiling">浮动上限(0-100)</param>
/// <param name="floor">浮动下限(0-100)</param>
private static void InitPackagePool(decimal amount, int packageCount, decimal ceiling, decimal floor)
{
lock (lockObj)
{
if (packagePool != null) { return; }
if (amount <= 0 || packageCount <= 0 || ceiling < 0 || ceiling > 100 || floor < 0 || floor > 100)
{
return;
}
Console.WriteLine("Init---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
packagePool = new ConcurrentQueue<decimal>();
lock (packagePool)
{
#region 处理红包均分时的情况
//如果上下限中有一个为零 那么红包只能进行均分
if (ceiling * floor == 0) { floor = 0; ceiling = 0; }
//如果是均分红包及floor与ceiling都为0 则直接分配 如果有剩余则直接舍弃
if (floor == 0 && ceiling == 0)
{
//计算均分的红包大小,保证不会进行五入操作
var pkg = Math.Floor(Math.Floor(amount * 100) / packageCount) / 100;
for (int i = 0; i < packageCount; i++)
{
packagePool.Enqueue(pkg);
}
return;
}
#endregion
//乘以100,去掉小数点
amount = Math.Round(amount, 2) * 100;
//计算当前可随机分配的值 保留红包的最低金额
var randAmount = amount * floor / 100;
//分发时去除余数 减轻小数的影响
decimal amountMod = amount % packageCount;
randAmount -= amountMod;
//分发的红包可能出现0.01级别的误差
//这里对红包的界限进行进位处理
//均分红包
decimal avgPkg = Math.Ceiling((amount - amountMod) / packageCount);
//最小红包
decimal minPkg = Math.Ceiling(avgPkg * (100 - floor) / 100);
//最大红包
decimal maxPkg = Math.Ceiling(avgPkg * (100 + ceiling) / 100);
//两个红包间可能的最大差值
decimal diffValue = maxPkg - minPkg;
//随机分配一次红包 总数为packageCount
AssignRedPacket(diffValue, packageCount);
//第一次分配完之后 红包总数可能超过或者小于总金额的限定
//这里对剩余或者溢出的情况进行处理
while (true)
{
//计算剩余或者溢出的部分
var otherSection = randAmount - packagePool.Sum();
//如果剩余或者溢出的部分被均分后小于1(实际就是小于0.01,即最小精度),则舍弃剩余或者溢出的部分
//否则就会陷入死循环
if (otherSection / packageCount < 1)
{
break;
}
//处理剩余或者溢出的部分
if (otherSection > 0)
{
//剩余
ProcessRemainSection(diffValue, otherSection);
}
else if (otherSection < 0)
{
//溢出了
ProcessOverflowSection(diffValue, Math.Abs(otherSection));
}
else
{
//等于0则处理完毕
break;
}
}
//所以这里要将所有红包恢复到正常的值 将附加的状态都恢复正常
//.........这里部分代码省略.........