Skip to content

Commit

Permalink
Introduced consts for prefetch levels
Browse files Browse the repository at this point in the history
  • Loading branch information
iromeo committed Apr 27, 2018
1 parent 762ad9f commit 6862698
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 24 deletions.
8 changes: 5 additions & 3 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ Changed
- Use `RomBufferFactory` to detect file endianness
- Changed `prefetch` option to int value in `BigFile.read()`,
`BigWigFile.read()`, `BigBedFile.read()`. Supported values are:
* `0` : do not prefetch
* `1` : prefetch zoom level indexes tree, chromosomes list
* `2` : prefetch w/o zoom level data tree indexes up to chromosomes level
* `BigFile.PREFETCH_LEVEL_OFF` : do not prefetch
* `BigFile.PREFETCH_LEVEL_FAST` : prefetch zoom level indexes tree,
chromosomes list
* `BigFile.PREFETCH_LEVEL_DETAILED` : prefetch w/o zoom level data tree
indexes up to chromosomes level
Larger prefetch values increase big file opening time and memory
consumption but provides faster data access with fewer i/o operations

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/jetbrains/bio/big/BigBed.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class BigBedFile private constructor(

@Throws(IOException::class)
@JvmStatic
fun read(src: String, prefetch: Int = 2,
fun read(src: String, prefetch: Int = BigFile.PREFETCH_LEVEL_DETAILED,
cancelledChecker: (() -> Unit)? = null,
factoryProvider: RomBufferFactoryProvider = defaultFactory()
): BigBedFile {
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/org/jetbrains/bio/big/BigFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ abstract class BigFile<out T> internal constructor(
bPlusTree = BPlusTree.read(input, header.chromTreeOffset)

// if do prefetch input and file not empty:
if (prefetch > 0) {
if (prefetch >= BigFile.PREFETCH_LEVEL_FAST) {
// stored in the beginning of file near header
cancelledChecker?.invoke()
prefetechedTotalSummary = BigSummary.read(input, header.totalSummaryOffset)
Expand Down Expand Up @@ -132,7 +132,7 @@ abstract class BigFile<out T> internal constructor(
// in buffered stream
cancelledChecker?.invoke()
rTree = RTreeIndex.read(input, header.unzoomedIndexOffset)
if (prefetch > 1) {
if (prefetch >= BigFile.PREFETCH_LEVEL_DETAILED) {
rTree.prefetchBlocksIndex(
input, false, true, header.uncompressBufSize, cancelledChecker
)
Expand Down Expand Up @@ -467,6 +467,9 @@ abstract class BigFile<out T> internal constructor(

companion object {
private val LOG = LogManager.getLogger(BigFile::class.java)
const val PREFETCH_LEVEL_OFF = 0
const val PREFETCH_LEVEL_FAST = 1
const val PREFETCH_LEVEL_DETAILED = 2

private val lastCachedBlockInfo: ThreadLocal<Pair<RomBufferState, RomBuffer?>> = ThreadLocal.withInitial {
RomBufferState(null, 0L, 0L, "") to null
Expand Down Expand Up @@ -522,7 +525,7 @@ abstract class BigFile<out T> internal constructor(

@Throws(IOException::class)
@JvmStatic
fun read(src: String, prefetch: Int = 2,
fun read(src: String, prefetch: Int = BigFile.PREFETCH_LEVEL_DETAILED,
cancelledChecker: (() -> Unit)? = null,
factoryProvider: RomBufferFactoryProvider = defaultFactory()
): BigFile<Comparable<*>> = factoryProvider(src, ByteOrder.LITTLE_ENDIAN).use { factory ->
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/jetbrains/bio/big/BigWig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class BigWigFile private constructor(

@Throws(IOException::class)
@JvmStatic
fun read(src: String, prefetch: Int = 2,
fun read(src: String, prefetch: Int = BigFile.PREFETCH_LEVEL_DETAILED,
cancelledChecker: (() -> Unit)? = null,
factoryProvider: RomBufferFactoryProvider = defaultFactory()
): BigWigFile {
Expand Down
6 changes: 3 additions & 3 deletions src/test/kotlin/org/jetbrains/bio/TestSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fun threadSafeRomFactoryProvidersAndPrefetchParams(): List<Array<Any>> {
})
}
return providers.flatMap { fp ->
intArrayOf(0, 1, 2).map { prefetch ->
intArrayOf(BigFile.PREFETCH_LEVEL_OFF, BigFile.PREFETCH_LEVEL_FAST, BigFile.PREFETCH_LEVEL_DETAILED).map { prefetch ->
arrayOf(fp, prefetch)
}
}
Expand All @@ -98,7 +98,7 @@ fun threadSafeRomFactoryProvidersAndPrefetchParams(): List<Array<Any>> {
fun romFactoryProviderParams(): List<Array<Any>> = romFactoryProviders().map { arrayOf<Any>(it) }

fun romFactoryProviderAndPrefetchParams(): List<Array<Any>> = romFactoryProviders().flatMap { fp ->
intArrayOf(0, 1, 2).map { prefetch ->
intArrayOf(BigFile.PREFETCH_LEVEL_OFF, BigFile.PREFETCH_LEVEL_FAST, BigFile.PREFETCH_LEVEL_DETAILED).map { prefetch ->
arrayOf(fp, prefetch)
}
}
Expand Down Expand Up @@ -132,7 +132,7 @@ fun romFactoryByteOrderCompressionParamsSets(): Iterable<Array<Any>> {
}

fun allBigFileParams(): List<Array<Any>> = romFactoryByteOrderCompressionParamsSets().flatMap {
intArrayOf(0, 1, 2).map { prefetch ->
intArrayOf(BigFile.PREFETCH_LEVEL_OFF, BigFile.PREFETCH_LEVEL_FAST, BigFile.PREFETCH_LEVEL_DETAILED).map { prefetch ->
it + prefetch
}
}
10 changes: 5 additions & 5 deletions src/test/kotlin/org/jetbrains/bio/big/BigBedFileTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class BigBedFileTest(
BigBedFile.read(Examples["example1.bb"], bfProvider, prefetch).use { bf ->
assertEquals(5, bf.zoomLevels.size)
assertEquals(39110000, bf.zoomLevels[4].reduction)
assertEquals(prefetch > 0, bf.prefetchedLevel2RTreeIndex != null)
if (prefetch > 0) {
assertEquals(prefetch == BigFile.PREFETCH_LEVEL_OFF, bf.prefetchedLevel2RTreeIndex == null)
if (prefetch >= BigFile.PREFETCH_LEVEL_FAST) {
assertEquals(5, bf.prefetchedLevel2RTreeIndex!!.size)

val zRTree0 = bf.prefetchedLevel2RTreeIndex!![bf.zoomLevels[0]]!!
Expand All @@ -41,8 +41,8 @@ class BigBedFileTest(
(zRTree4.rootNode!! as RTReeNodeLeaf).leaves[0].interval.toString())
}

assertEquals(prefetch > 0, bf.prefetchedChr2Leaf != null)
if (prefetch > 0) {
assertEquals(prefetch == BigFile.PREFETCH_LEVEL_OFF, bf.prefetchedChr2Leaf == null)
if (prefetch >= BigFile.PREFETCH_LEVEL_FAST) {
assertEquals(1, bf.prefetchedChr2Leaf!!.size)
assertEquals("chr21", bf.prefetchedChr2Leaf!!["chr21"]!!.key)
assertEquals(0, bf.prefetchedChr2Leaf!!["chr21"]!!.id)
Expand All @@ -58,7 +58,7 @@ class BigBedFileTest(

assertEquals(192819, bf.rTree.header.rootOffset)
assertEquals(14810, bf.rTree.header.itemCount)
if (prefetch > 1) {
if (prefetch >= BigFile.PREFETCH_LEVEL_DETAILED) {
assertNotNull(bf.rTree.rootNode)
assertTrue(bf.rTree.rootNode!! is RTReeNodeLeaf)
assertEquals(232, (bf.rTree.rootNode!! as RTReeNodeLeaf).leaves.size)
Expand Down
10 changes: 5 additions & 5 deletions src/test/kotlin/org/jetbrains/bio/big/BigWigFileTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class BigWigFileTest(
BigWigFile.read(Examples["example2.bw"], bfProvider, prefetch).use { bf ->
assertEquals(10, bf.zoomLevels.size)
assertEquals(25600, bf.zoomLevels[4].reduction)
assertEquals(prefetch > 0, bf.prefetchedLevel2RTreeIndex != null)
if (prefetch > 0) {
assertEquals(prefetch == BigFile.PREFETCH_LEVEL_OFF, bf.prefetchedLevel2RTreeIndex == null)
if (prefetch >= BigFile.PREFETCH_LEVEL_FAST) {
assertEquals(10, bf.prefetchedLevel2RTreeIndex!!.size)

val zRTree0 = bf.prefetchedLevel2RTreeIndex!![bf.zoomLevels[0]]!!
Expand All @@ -42,8 +42,8 @@ class BigWigFileTest(
(zRTree4.rootNode!! as RTReeNodeLeaf).leaves[1].interval)
}

assertEquals(prefetch > 0, bf.prefetchedChr2Leaf != null)
if (prefetch > 0) {
assertEquals(prefetch == BigFile.PREFETCH_LEVEL_OFF, bf.prefetchedChr2Leaf == null)
if (prefetch >= BigFile.PREFETCH_LEVEL_FAST) {
assertEquals(1, bf.prefetchedChr2Leaf!!.size)
assertEquals("chr21", bf.prefetchedChr2Leaf!!["chr21"]!!.key)
assertEquals(0, bf.prefetchedChr2Leaf!!["chr21"]!!.id)
Expand All @@ -59,7 +59,7 @@ class BigWigFileTest(

assertEquals(15751097, bf.rTree.header.rootOffset)
assertEquals(6857, bf.rTree.header.itemCount)
if (prefetch > 1) {
if (prefetch >= BigFile.PREFETCH_LEVEL_DETAILED) {
assertNotNull(bf.rTree.rootNode)
assertTrue(bf.rTree.rootNode!! is RTReeNodeIntermediate)
assertEquals(27, (bf.rTree.rootNode!! as RTReeNodeIntermediate).children.size)
Expand Down
6 changes: 3 additions & 3 deletions src/test/kotlin/org/jetbrains/bio/big/RTreeIndexTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RTreeIndexTest(
assertEquals(192771L, rti.header.endDataOffset)
assertEquals(64, rti.header.itemsPerSlot)
assertEquals(192819L, rti.header.rootOffset)
if (prefetch > 1) {
if (prefetch >= BigFile.PREFETCH_LEVEL_DETAILED) {
assertNotNull(bbf.rTree.rootNode)
assertTrue(bbf.rTree.rootNode is RTReeNodeLeaf)
assertEquals(232, (bbf.rTree.rootNode as RTReeNodeLeaf).leaves.size)
Expand All @@ -49,7 +49,7 @@ class RTreeIndexTest(
f.create().use { input ->

val rti = RTreeIndex.read(input, 0L)
if (prefetch > 0) {
if (prefetch >= BigFile.PREFETCH_LEVEL_FAST) {
rti.prefetchBlocksIndex(input, true, false, 0, null)
}

Expand Down Expand Up @@ -108,7 +108,7 @@ class RTreeIndexTest(
bfProvider(path.toString(), ByteOrder.nativeOrder()).use { f ->
f.create().use { input ->
val rti = RTreeIndex.read(input, 0)
if (prefetch > 0) {
if (prefetch >= BigFile.PREFETCH_LEVEL_FAST) {
rti.prefetchBlocksIndex(input, true, false, 0, null)
}
for (leaf in leaves) {
Expand Down

0 comments on commit 6862698

Please sign in to comment.