Skip to content

Commit

Permalink
Rm stat
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Dec 18, 2024
1 parent c4ac029 commit 2d4712e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 36 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default tseslint.config(
'esm/*',
'example/*',
'eslint.config.mjs',
'coverage',
],
},
{
Expand Down
34 changes: 5 additions & 29 deletions src/bgzFilehandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,9 @@ export default class BgzFilehandle {
})
}

async stat() {
const compressedStat = await this.filehandle.stat()
return Object.assign(compressedStat, {
size: await this.getUncompressedFileSize(),
blocks: undefined,
blksize: undefined,
})
}

async getUncompressedFileSize() {
// read the last block's ISIZE (see gzip RFC),
// and add it to its uncompressedPosition
const [, uncompressedPosition] = await this.gzi.getLastBlock()

const { size } = await this.filehandle.stat()

// note: there should be a 28-byte EOF marker (an empty block) at
// the end of the file, so we skip backward past that
const buf = await this.filehandle.read(4, size - 28 - 4)
const dataView = new DataView(buf.buffer)
const lastBlockUncompressedSize = dataView.getUint32(0, true)
return uncompressedPosition + lastBlockUncompressedSize
}

async _readAndUncompressBlock(
[compressedPosition]: [number],
[nextCompressedPosition]: [number],
compressedPosition: number,
nextCompressedPosition: number,
) {
let next = nextCompressedPosition
if (!next) {
Expand Down Expand Up @@ -95,10 +71,10 @@ export default class BgzFilehandle {
blockNum += 1
) {
const uncompressedBuffer = await this._readAndUncompressBlock(
blockPositions[blockNum],
blockPositions[blockNum + 1],
blockPositions[blockNum]![0],
blockPositions[blockNum + 1]![0],
)
const [, uncompressedPosition] = blockPositions[blockNum]
const [, uncompressedPosition] = blockPositions[blockNum]!
const sourceOffset =
uncompressedPosition >= position ? 0 : position - uncompressedPosition
const sourceEnd =
Expand Down
9 changes: 6 additions & 3 deletions src/gziIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ export default class GziIndex {
return entries.at(-1)
}

async getRelevantBlocksForRead(length: number, position: number) {
async getRelevantBlocksForRead(
length: number,
position: number,
): Promise<([number, number] | never[])[]> {
const endPosition = position + length
if (length === 0) {
return []
Expand Down Expand Up @@ -129,10 +132,10 @@ export default class GziIndex {
}

// here's where we read forward
relevant.push(entries[searchPosition])
relevant.push(entries[searchPosition]!)
let i = searchPosition + 1
for (; i < entries.length; i += 1) {
relevant.push(entries[i])
relevant.push(entries[i]!)
if (entries[i]![UNCOMPRESSED_POSITION] >= endPosition) {
break
}
Expand Down
4 changes: 0 additions & 4 deletions test/indexedfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ async function testRead(basename: string, length: number, position: number) {
fs.readSync(fd, buf2, 0, length, position)
expect(buf.length).toEqual(buf2.length)
expect(Array.from(buf)).toEqual(Array.from(buf2))

const directStat = fs.fstatSync(fd)
const myStat = await f.stat()
expect(myStat.size).toEqual(directStat.size)
}

test('can read gff3_with_syncs.gff3.gz 1', async () => {
Expand Down

0 comments on commit 2d4712e

Please sign in to comment.