예제 #1
0
        internal void PrepareRollups(DocumentsOperationContext context, DateTime currentTime, long take, long start, List <RollupState> states, out Stopwatch duration)
        {
            duration = Stopwatch.StartNew();

            var table = context.Transaction.InnerTransaction.OpenTable(RollupSchema, TimeSeriesRollupTable);

            if (table == null)
            {
                return;
            }

            var currentTicks = currentTime.Ticks;

            using (DocumentsStorage.GetEtagAsSlice(context, start, out var startSlice))
            {
                foreach (var item in table.SeekForwardFrom(RollupSchema.Indexes[NextRollupIndex], startSlice, 0))
                {
                    if (take <= 0)
                    {
                        return;
                    }

                    var rollUpTime = DocumentsStorage.TableValueToEtag((int)RollupColumns.NextRollup, ref item.Result.Reader);
                    if (rollUpTime > currentTicks)
                    {
                        return;
                    }

                    DocumentsStorage.TableValueToSlice(context, (int)RollupColumns.Key, ref item.Result.Reader, out var key);
                    SplitKey(key, out var docId, out var name);
                    name = context.DocumentDatabase.DocumentsStorage.TimeSeriesStorage.GetOriginalName(context, docId, name);

                    var state = new RollupState
                    {
                        Key          = key,
                        DocId        = docId,
                        Name         = name,
                        Collection   = DocumentsStorage.TableValueToId(context, (int)RollupColumns.Collection, ref item.Result.Reader),
                        NextRollup   = new DateTime(rollUpTime),
                        RollupPolicy = DocumentsStorage.TableValueToString(context, (int)RollupColumns.PolicyToApply, ref item.Result.Reader),
                        Etag         = DocumentsStorage.TableValueToLong((int)RollupColumns.Etag, ref item.Result.Reader),
                        ChangeVector = DocumentsStorage.TableValueToChangeVector(context, (int)RollupColumns.ChangeVector, ref item.Result.Reader)
                    };

                    if (_logger.IsInfoEnabled)
                    {
                        _logger.Info($"{state} is prepared.");
                    }

                    states.Add(state);
                    take--;
                }
            }
        }