BTreeDB.hasKey

This commit is contained in:
DBotThePony 2024-02-15 14:13:38 +07:00
parent f99abb9fa9
commit e2ca74cd53
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 29 additions and 11 deletions

View File

@ -4,7 +4,7 @@ kotlin.code.style=official
specifyKotlinAsDependency=false specifyKotlinAsDependency=false
projectGroup=ru.dbotthepony.kommons projectGroup=ru.dbotthepony.kommons
projectVersion=2.3.1 projectVersion=2.3.2
guavaDepVersion=33.0.0 guavaDepVersion=33.0.0
gsonDepVersion=2.8.9 gsonDepVersion=2.8.9

View File

@ -14,6 +14,12 @@ abstract class BTreeDB<K, V> : Closeable {
abstract val file: File abstract val file: File
abstract val blockSize: Int abstract val blockSize: Int
open operator fun contains(key: K): Boolean {
return hasKey(key).get()
}
abstract fun hasKey(key: K): CompletableFuture<Boolean>
/** /**
* Reads data at specified [key] * Reads data at specified [key]
*/ */

View File

@ -127,9 +127,9 @@ class BTreeDB6 private constructor(override val file: File, private val reader:
reader.write(headerBuf) reader.write(headerBuf)
} }
private fun doRead(key: ByteKey): KOptional<ByteArray> { private fun searchForBlock(key: ByteKey): Block? {
if (rootBlockIndex == INVALID_BLOCK_INDEX) { if (rootBlockIndex == INVALID_BLOCK_INDEX) {
return KOptional.empty() return null
} }
var block = readBlock(rootBlockIndex) var block = readBlock(rootBlockIndex)
@ -141,23 +141,35 @@ class BTreeDB6 private constructor(override val file: File, private val reader:
if (block.type == BlockType.LEAF) { if (block.type == BlockType.LEAF) {
val leaf = LeafData(block) val leaf = LeafData(block)
val data = leaf.get(key) return leaf.get(key)
if (data != null) {
val stream = DataInputStream(BlockInputStream(data))
val output = ByteArray(stream.readInt())
stream.readFully(output)
return KOptional(output)
}
} else if (block.type == BlockType.DATA) { } else if (block.type == BlockType.DATA) {
throw IllegalStateException("Hit data block when scanning index") throw IllegalStateException("Hit data block when scanning index")
} else if (block.type == BlockType.FREE) { } else if (block.type == BlockType.FREE) {
throw IllegalStateException("Hit free block when scanning index") throw IllegalStateException("Hit free block when scanning index")
} }
return null
}
private fun doRead(key: ByteKey): KOptional<ByteArray> {
val data = searchForBlock(key)
if (data != null) {
val stream = DataInputStream(BlockInputStream(data))
val output = ByteArray(stream.readInt())
stream.readFully(output)
return KOptional(output)
}
return KOptional.empty() return KOptional.empty()
} }
override fun hasKey(key: ByteKey): CompletableFuture<Boolean> {
return CompletableFuture.supplyAsync(Supplier {
searchForBlock(key) != null
}, carrier)
}
override fun read(key: ByteKey): CompletableFuture<KOptional<ByteArray>> { override fun read(key: ByteKey): CompletableFuture<KOptional<ByteArray>> {
return CompletableFuture.supplyAsync(Supplier { return CompletableFuture.supplyAsync(Supplier {
doRead(key) doRead(key)