private void PrioritizeAndCompressLocalQueue() { WorkItem front = localQueue.Peek(); if (prioritizeReadRequests) { if (front.IsReadRequest) { return; } else { WorkItem readReq = localQueue.FirstOrDefault(x => x.IsReadRequest); if (readReq != null) { WorkItem[] other = localQueue.Where(x => x != readReq).ToArray(); localQueue.Clear(); localQueue.Enqueue(readReq); foreach (WorkItem it in other) { localQueue.Enqueue(it); } return; } } } if (front is WI_BatchAppend && localQueue.Count > 1) { WI_BatchAppend[] appends = localQueue.TakeWhile(wi => wi is WI_BatchAppend).Cast <WI_BatchAppend>().ToArray(); if (appends.Length > 1) { StoreValue[] values = appends.SelectMany(app => app.Values).ToArray(); WI_BatchAppend frontAppend = new WI_BatchAppend() { Values = values }; WorkItem[] other = localQueue.Skip(appends.Length).ToArray(); localQueue.Clear(); localQueue.Enqueue(frontAppend); foreach (WorkItem it in other) { localQueue.Enqueue(it); } } return; } }
private void Append(WI_BatchAppend append) { var nonExisting = new List <StoreValue>(); var set = new Dictionary <VariableRef, VarHistoyChange>(); foreach (StoreValue sv in append.Values) { VariableRef vr = sv.Value.Variable; Timestamp newT = sv.Value.Value.T; if (!set.ContainsKey(vr)) { set[vr] = new VarHistoyChange() { Var = vr, Start = newT, End = newT }; if (!ExistsChannel(vr)) { nonExisting.Add(sv); } } else { VarHistoyChange change = set[vr]; if (newT < change.Start) { change.Start = newT; } if (newT > change.End) { change.End = newT; } } } var db = GetDbOrThrow(); if (nonExisting.Count > 0) { int count = nonExisting.Count; logger.Debug("Creating {0} not yet existing channels.", count); ChannelInfo[] newChannels = nonExisting.Select(v => new ChannelInfo(v.Value.Variable.Object.LocalObjectID, v.Value.Variable.Name, v.Type)).ToArray(); var swCreate = Stopwatch.StartNew(); Channel[] channels = db.CreateChannels(newChannels); logger.Debug("Created {0} channels completed in {1} ms", count, swCreate.ElapsedMilliseconds); for (int i = 0; i < nonExisting.Count; ++i) { mapChannels[nonExisting[i].Value.Variable] = channels[i]; } } var swBatch = Stopwatch.StartNew(); Func <PrepareContext, string?>[] appendActions = append.Values.Select(v => { Channel ch = GetChannelOrThrow(v.Value.Variable); Func <PrepareContext, string?> f = ch.PrepareAppend(v.Value.Value); return(f); }).ToArray(); string[] errors = db.BatchExecute(appendActions); logger.Debug("db.BatchExecute completed for {0} appends in {1} ms", appendActions.Length, swBatch.ElapsedMilliseconds); if (errors.Length > 0) { logger.Error("Batch append actions failed ({0} of {1}): \n\t{2}", errors.Length, appendActions.Length, string.Join("\n\t", errors)); } notifyAppend(set.Values); }