From 834a8597c5d77b7b06ff6a3f34bc8058cfe52957 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 28 Sep 2020 16:11:59 +0300 Subject: [PATCH] [#53] object/util: Add seek range function to range traverser Add SetSeekRange method to RangeTraverser that switches traverser to work with provided object payload range. Signed-off-by: Leonard Lyubich --- pkg/services/object/util/chain.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pkg/services/object/util/chain.go b/pkg/services/object/util/chain.go index c7e8fe187..46d28dfd6 100644 --- a/pkg/services/object/util/chain.go +++ b/pkg/services/object/util/chain.go @@ -104,3 +104,33 @@ func (c *RangeTraverser) PushSuccessSize(sz uint64) { c.chain = c.chain.next } } + +// SetSeekRange moves the chain to the specified range. +// The range is expected to be within the filled chain. +func (c *RangeTraverser) SetSeekRange(r *objectSDK.Range) { + ln, off := r.GetLength(), r.GetOffset() + + for { + if off < c.chain.bounds.left { + if c.chain.prev == nil { + break + } + + c.chain = c.chain.prev + } else if off >= c.chain.bounds.left && off < c.chain.bounds.right { + break + } else if off >= c.chain.bounds.right { + if c.chain.next == nil { + break + } + + c.chain = c.chain.next + } + } + + if c.seekBounds == nil { + c.seekBounds = new(rangeBounds) + } + + c.seekBounds.left, c.seekBounds.right = off, off+ln +}