Usenet streaming with a WebDAV server and a SABnzbd-compatible API
C#
Implement better interpolation search for finding the seek position of a stream.
nzbdav-dev9ee24e3a390f
diff --git a/backend/Streams/NzbFileStream.cs b/backend/Streams/NzbFileStream.csindex 404c39d3..53ee5157 100644--- a/backend/Streams/NzbFileStream.cs+++ b/backend/Streams/NzbFileStream.cs@@ -96,28 +96,27 @@ public class NzbFileStream(}- private async Task<(int segmentIndex, YencHeader header)> SeekSegment(long byteOffset,- CancellationToken cancellationToken)+ private async Task<InterpolationSearch.Result> SeekSegment(long byteOffset, CancellationToken ct){- YencHeader? header = null;- var segmentIndex = await InterpolationSearch.Find(0, fileSegmentIds.Length,- async guess =>+ return await InterpolationSearch.Find(+ byteOffset,+ new InterpolationSearch.Range(0, fileSegmentIds.Length),+ new InterpolationSearch.Range(0, fileSize),+ async (guess) =>{- header = await client.GetSegmentYencHeaderAsync(fileSegmentIds[guess], cancellationToken);- if (header.PartOffset <= byteOffset && byteOffset < header.PartOffset + header.PartSize)- return null;- return byteOffset / (header.PartOffset + header.PartSize / 2.0d);- }+ var header = await client.GetSegmentYencHeaderAsync(fileSegmentIds[guess], ct);+ return new InterpolationSearch.Range(header.PartOffset, header.PartOffset + header.PartSize);+ },+ ct);- return (segmentIndex, header!);}private async Task<CombinedStream> GetFileStream(long rangeStart, CancellationToken cancellationToken){if (rangeStart == 0) return GetCombinedStream(0, cancellationToken);- var (firstSegmentIndex, header) = await SeekSegment(rangeStart, cancellationToken);- var stream = GetCombinedStream(firstSegmentIndex, cancellationToken);- await stream.DiscardBytesAsync(rangeStart - header.PartOffset);+ var foundSegment = await SeekSegment(rangeStart, cancellationToken);+ var stream = GetCombinedStream(foundSegment.FoundIndex, cancellationToken);+ await stream.DiscardBytesAsync(rangeStart - foundSegment.FoundByteRange.StartInclusive);return stream;}diff --git a/backend/Utils/InterpolationSearch.cs b/backend/Utils/InterpolationSearch.csindex 134d4bc6..3caa5208 100644--- a/backend/Utils/InterpolationSearch.cs+++ b/backend/Utils/InterpolationSearch.cs@@ -2,38 +2,67 @@public static class InterpolationSearch{- public static async Task<int> Find+ public static async Task<Result> Find(- int startInclusive,- int endExclusive,- Func<int, Task<double?>> getGuessResult+ long searchByte,+ Range indexRangeToSearch,+ Range byteRangeToSearch,+ Func<int, Task<Range>> getByteRangeOfGuessedIndex,+ CancellationToken cancellationToken){- var guess = (startInclusive + endExclusive) / 2;- return await Find(guess, startInclusive, endExclusive, getGuessResult);+ while (true)+ {+ cancellationToken.ThrowIfCancellationRequested();++ // make sure our search is even possible.+ if (!byteRangeToSearch.Contains(searchByte) || indexRangeToSearch.Count <= 0)+ throw new Exception($"Corrupt file. Cannot find byte position {searchByte}.");++ // make a guess+ var searchByteFromStart = searchByte - byteRangeToSearch.StartInclusive;+ var bytesPerIndex = (double)byteRangeToSearch.Count / indexRangeToSearch.Count;+ var guessFromStart = (long)Math.Floor(searchByteFromStart / bytesPerIndex);+ var guessedIndex = (int)(indexRangeToSearch.StartInclusive + guessFromStart);+ var byteRangeOfGuessedIndex = await getByteRangeOfGuessedIndex(guessedIndex);++ // make sure the result is within the range of our search space+ if (!byteRangeOfGuessedIndex.IsContainedWithin(byteRangeToSearch))+ throw new Exception($"Corrupt file. Cannot find byte position {searchByte}.");++ // if we guessed too low, adjust our lower bounds in order to search higher next time+ if (byteRangeOfGuessedIndex.EndExclusive <= searchByte)+ {+ indexRangeToSearch = indexRangeToSearch with { StartInclusive = guessedIndex + 1 };+ byteRangeToSearch = byteRangeToSearch with { StartInclusive = byteRangeOfGuessedIndex.EndExclusive };+ }++ // if we guessed too high, adjust our upper bounds in order to search lower next time+ else if (byteRangeOfGuessedIndex.StartInclusive > searchByte)+ {+ indexRangeToSearch = indexRangeToSearch with { EndExclusive = guessedIndex };+ byteRangeToSearch = byteRangeToSearch with { EndExclusive = byteRangeOfGuessedIndex.StartInclusive };+ }++ // if we guessed correctly, we're done+ else if (byteRangeOfGuessedIndex.Contains(searchByte))+ return new Result(guessedIndex, byteRangeOfGuessedIndex);+ }}- private static async Task<int> Find- (- int guess,- int startInclusive,- int endExclusive,- Func<int, Task<double?>> guessResult- )+ public record Range(long StartInclusive, long EndExclusive){- var result = await guessResult(guess);- if (result == null) return guess;- var newGuess = (int)((guess - startInclusive) * result.Value) + startInclusive;- if (newGuess >= endExclusive) newGuess = endExclusive - 1;- if (result < 1 && newGuess >= guess) newGuess--;- if (result > 1 && newGuess <= guess) newGuess++;- if (newGuess < 0 || newGuess >= endExclusive)- throw new Exception("Could not find through interpolation search.");-- // Add termination condition to prevent infinite recursion- if (newGuess == guess || Math.Abs(newGuess - guess) <= 1)- return guess;-- return await Find(newGuess, startInclusive, endExclusive, guessResult);+ public long Count => EndExclusive - StartInclusive;++ public bool Contains(long value) =>+ value >= StartInclusive && value < EndExclusive;++ public bool Contains(Range range) =>+ range.StartInclusive >= StartInclusive && range.EndExclusive <= EndExclusive;++ public bool IsContainedWithin(Range range) =>+ range.Contains(this);}++ public record Result(int FoundIndex, Range FoundByteRange);}\ No newline at end of file