diff --git a/src/Raven.Server/Documents/Queries/Results/TimeSeries/TimeSeriesRetriever.cs b/src/Raven.Server/Documents/Queries/Results/TimeSeries/TimeSeriesRetriever.cs index 03beebc0be32..95c798a7cebf 100644 --- a/src/Raven.Server/Documents/Queries/Results/TimeSeries/TimeSeriesRetriever.cs +++ b/src/Raven.Server/Documents/Queries/Results/TimeSeries/TimeSeriesRetriever.cs @@ -771,16 +771,7 @@ string GetSourceAndId() private static RangeGroup InitializeRangeSpecs(string groupByTimePeriod, DateTime from, DateTime to, TimeSpan? offset) { - if (offset.HasValue) - { - var minWithOffset = from.Ticks + offset.Value.Ticks; - var maxWithOffset = to.Ticks + offset.Value.Ticks; - - if (minWithOffset >= 0 && minWithOffset <= DateTime.MaxValue.Ticks) - from = from.Add(offset.Value); - if (maxWithOffset >= 0 && maxWithOffset <= DateTime.MaxValue.Ticks) - to = to.Add(offset.Value); - } + TimeSeriesReader.AddOffsetIfNeeded(offset, ref from, ref to); if (groupByTimePeriod != null) return RangeGroup.ParseRangeFromString(groupByTimePeriod, from); diff --git a/src/Raven.Server/Documents/TimeSeries/TimeSeriesReader.cs b/src/Raven.Server/Documents/TimeSeries/TimeSeriesReader.cs index f631c2d25b01..addea3452f87 100644 --- a/src/Raven.Server/Documents/TimeSeries/TimeSeriesReader.cs +++ b/src/Raven.Server/Documents/TimeSeries/TimeSeriesReader.cs @@ -152,18 +152,16 @@ internal bool Init() if (_from > _to) return false; - var baseline = _from; - AddOffsetIfNeeded(); - - using (var holder = new TimeSeriesSliceHolder(_context, _documentId, _name).WithBaseline(baseline)) + using (var holder = new TimeSeriesSliceHolder(_context, _documentId, _name).WithBaseline(_from)) { - if (_table.SeekOneBackwardByPrimaryKeyPrefix(holder.TimeSeriesPrefixSlice, holder.TimeSeriesKeySlice, out _tvr) == false) - { - return _table.SeekOnePrimaryKeyWithPrefix(holder.TimeSeriesPrefixSlice, holder.TimeSeriesKeySlice, out _tvr); - } - - return true; + if (_table.SeekOneBackwardByPrimaryKeyPrefix(holder.TimeSeriesPrefixSlice, holder.TimeSeriesKeySlice, out _tvr) == false && + _table.SeekOnePrimaryKeyWithPrefix(holder.TimeSeriesPrefixSlice, holder.TimeSeriesKeySlice, out _tvr) == false) + return false; } + + AddOffsetIfNeeded(_offset, ref _from, ref _to); + + return true; } public SingleResult First() @@ -621,18 +619,31 @@ internal string GetCurrentSegmentChangeVector() return (etag, changeVector, baseline); } - private void AddOffsetIfNeeded() + internal static void AddOffsetIfNeeded(TimeSpan? offset, ref DateTime from, ref DateTime to) + { + if (offset.HasValue == false) + return; + + AddOffset(offset.Value, ref from); + AddOffset(offset.Value, ref to); + } + + private static void AddOffset(TimeSpan offset, ref DateTime date) { - if (_offset.HasValue == false) + var withOffset = date.Ticks + offset.Ticks; + if (withOffset < 0) + { + date = DateTime.MinValue; return; + } - var minWithOffset = _from.Ticks + _offset.Value.Ticks; - var maxWithOffset = _to.Ticks + _offset.Value.Ticks; + if (withOffset > DateTime.MaxValue.Ticks) + { + date = DateTime.MaxValue; + return; + } - if (minWithOffset >= 0 && minWithOffset <= DateTime.MaxValue.Ticks) - _from = _from.Add(_offset.Value); - if (maxWithOffset >= 0 && maxWithOffset <= DateTime.MaxValue.Ticks) - _to = _to.Add(_offset.Value); + date = date.Add(offset); } }