Skip to content

Commit

Permalink
RavenDB-21649 : adders PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aviv86 committed Nov 20, 2023
1 parent f421410 commit 4857eb1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
47 changes: 29 additions & 18 deletions src/Raven.Server/Documents/TimeSeries/TimeSeriesReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 4857eb1

Please sign in to comment.