Skip to content

Commit

Permalink
Cache readIntervals()
Browse files Browse the repository at this point in the history
  • Loading branch information
danvk committed Apr 23, 2015
1 parent cb110b3 commit eb31118
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/bai.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class ImmediateBaiFile {
remoteFile: RemoteFile;
indexChunks: Object;
indexCache: Q.Promise<Object>[]; // ref ID -> parsed BaiIndex
intervalsCache: Array<?VirtualOffset[]>; // ref ID -> linear index

constructor(buffer: ?ArrayBuffer, remoteFile: RemoteFile, indexChunks?: Object) {
this.buffer = buffer;
Expand All @@ -138,6 +139,7 @@ class ImmediateBaiFile {
}
}
this.indexCache = new Array(this.indexChunks.chunks.length);
this.intervalsCache = new Array(this.indexChunks.chunks.length);
}

getChunksForInterval(range: ContigInterval<number>): Q.Promise<Chunk[]> {
Expand All @@ -154,7 +156,7 @@ class ImmediateBaiFile {
.flatten()
.value();

var linearIndex = readIntervals(contigIndex.intervals);
var linearIndex = this.getIntervals(contigIndex.intervals, range.contig);
var startIdx = Math.max(0, Math.floor(range.start() / 16384));
var minimumOffset = linearIndex[startIdx];

Expand Down Expand Up @@ -186,6 +188,17 @@ class ImmediateBaiFile {
return this.remoteFile.getBytes(start, stop - start + 1);
}
}

// Cached wrapper around readIntervals()
getIntervals(blob: Uint8Array, refId: number): VirtualOffset[] {
var linearIndex = this.intervalsCache[refId];
if (linearIndex) {
return linearIndex;
}
linearIndex = readIntervals(blob);
this.intervalsCache[refId] = linearIndex;
return linearIndex;
}
}


Expand Down

0 comments on commit eb31118

Please sign in to comment.