@gmod/tabix 3.4.0 → 3.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/util.ts CHANGED
@@ -65,6 +65,7 @@ export function optimizeChunks(chunks: Chunk[], lowest?: VirtualOffset) {
65
65
  lastChunk.minv,
66
66
  chunk.maxv,
67
67
  lastChunk.bin,
68
+ chunk.endPosition,
68
69
  )
69
70
  lastMaxBlock = chunkMaxBlock
70
71
  }
@@ -78,6 +79,39 @@ export function optimizeChunks(chunks: Chunk[], lowest?: VirtualOffset) {
78
79
  return mergedChunks
79
80
  }
80
81
 
82
+ // Tighten each chunk's endPosition (default: a full max-size BGZF block past
83
+ // maxv) down to the next known block boundary. Every chunk min/maxv blockPosition
84
+ // and every extraBoundary (e.g. linear-index entries) is a real BGZF block start;
85
+ // since blocks don't overlap, the first boundary strictly greater than a chunk's
86
+ // maxv.blockPosition is an upper bound on where that final block ends — always at
87
+ // least the true block end, so the clamped fetch still contains the whole block.
88
+ // Shrinks both the byte estimate and the actual fetch with no extra I/O.
89
+ export function clampChunkEnds(chunks: Chunk[], extraBoundaries: number[] = []) {
90
+ const boundaries = [...extraBoundaries]
91
+ for (const c of chunks) {
92
+ boundaries.push(c.minv.blockPosition, c.maxv.blockPosition)
93
+ }
94
+ boundaries.sort((a, b) => a - b)
95
+
96
+ for (const c of chunks) {
97
+ const max = c.maxv.blockPosition
98
+ // first boundary strictly greater than max
99
+ let lo = 0
100
+ let hi = boundaries.length
101
+ while (lo < hi) {
102
+ const mid = (lo + hi) >>> 1
103
+ if (boundaries[mid]! > max) {
104
+ hi = mid
105
+ } else {
106
+ lo = mid + 1
107
+ }
108
+ }
109
+ if (lo < boundaries.length) {
110
+ c.endPosition = Math.min(c.endPosition, boundaries[lo]!)
111
+ }
112
+ }
113
+ }
114
+
81
115
  export function findFirstData(
82
116
  currentFdl: VirtualOffset | undefined,
83
117
  virtualOffset: VirtualOffset,