MapDB |分批阅读

时间:2019-05-15 12:50:18

标签: kotlin mapdb

如何批量读取MapDB中的记录,而不是一次读取所有记录?

我列出了我通过MapDB保留在磁盘上的1000条记录。

private fun storeRecordsInFile(records : List<Record>): String {
    val fileName = "/some/location/temp_file.db"
    val db = DBMaker.fileDB(fileName).fileMmapEnable().closeOnJvmShutdown().make()
    val diskBasedRecordsList = db.indexTreeList("recordsList").create()
    records.forEach { diskBasedRecordsList.add(it) }

    db.commit()
    db.close()

    return fileName
}

我能够一次读取所有这些记录。

private fun readRecordsFromFile(fileName: String): List<Record> {
    val db = DBMaker.fileDB(fileName).fileMmapEnable().make()
    val diskBasedRecordsList = db.indexTreeList("recordsList").open()

    //read from disk based list
    val recordsList = mutableListOf<Record>()
    val size = diskBasedRecordsList.size
    for(i in 0 until size-1){
        recordsList.add(diskBasedRecordsList[i] as Record)
    }

    db.close()
    return recordsList
}

但是,我想一次只在内存中一次读取100个数据块(即不一次获取内存中的全部1000条记录)吗?

0 个答案:

没有答案