From 7011ab8ace89f6ab0f79c65468c1910248a53192 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 18 Feb 2024 21:19:59 +0700 Subject: [PATCH] Get rid of "intermediate" btreedb class --- gradle.properties | 2 +- .../ru/dbotthepony/kommons/io/BTreeDB.kt | 31 ------------------- .../ru/dbotthepony/kommons/io/BTreeDB6.kt | 18 ++++++----- 3 files changed, 11 insertions(+), 40 deletions(-) delete mode 100644 src/main/kotlin/ru/dbotthepony/kommons/io/BTreeDB.kt diff --git a/gradle.properties b/gradle.properties index 1e62ca3..e3ef8c9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ kotlin.code.style=official specifyKotlinAsDependency=false projectGroup=ru.dbotthepony.kommons -projectVersion=2.5.0 +projectVersion=2.6.0 guavaDepVersion=33.0.0 gsonDepVersion=2.8.9 diff --git a/src/main/kotlin/ru/dbotthepony/kommons/io/BTreeDB.kt b/src/main/kotlin/ru/dbotthepony/kommons/io/BTreeDB.kt deleted file mode 100644 index d3cf82d..0000000 --- a/src/main/kotlin/ru/dbotthepony/kommons/io/BTreeDB.kt +++ /dev/null @@ -1,31 +0,0 @@ -package ru.dbotthepony.kommons.io - -import ru.dbotthepony.kommons.util.KOptional -import java.io.Closeable -import java.io.File - -/** - * For actual implementation, see [BTreeDB6]. - */ -abstract class BTreeDB : Closeable { - abstract val file: File - abstract val blockSize: Int - - abstract operator fun contains(key: K): Boolean - - /** - * Reads data at specified [key] - */ - abstract fun read(key: K): KOptional - abstract fun findAllKeys(): List - - abstract fun write(key: K, value: V) -} - -abstract class ByteDataBTreeDB : BTreeDB() { - abstract fun write(key: K, value: ByteArray, offset: Int, length: Int) - - final override fun write(key: K, value: ByteArray) { - return write(key, value, 0, value.size) - } -} diff --git a/src/main/kotlin/ru/dbotthepony/kommons/io/BTreeDB6.kt b/src/main/kotlin/ru/dbotthepony/kommons/io/BTreeDB6.kt index c01edc0..c4a06f5 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/io/BTreeDB6.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/io/BTreeDB6.kt @@ -8,6 +8,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet import ru.dbotthepony.kommons.util.CarriedExecutor import ru.dbotthepony.kommons.util.KOptional +import java.io.Closeable import java.io.DataInputStream import java.io.DataOutputStream import java.io.File @@ -73,9 +74,10 @@ private fun readHeader(reader: RandomAccessFile, required: Char) { // TODO: Defragmenting // TODO: Add keys to DATA blocks, so entire tree can be reconstructed from scratch in event of both trees become unreadable // TODO: Changeset counter (to determine write-in-progress blocks/trees, to ignore them when reconstructing tree) -// TODO: Tree rotations (rebalancing) +// TODO: Tree rotations (rebalancing), tree height tracking, weighted subtree splitting // TODO: Removal of keys -class BTreeDB6 private constructor(override val file: File, private var reader: RandomAccessFile, private val sync: Boolean) : ByteDataBTreeDB() { +// TODO: Faster free bitmap scan +class BTreeDB6 private constructor(val file: File, private var reader: RandomAccessFile, private val sync: Boolean) : Closeable { constructor(file: File, sync: Boolean = true) : this(file, RandomAccessFile(file, "rw"), sync) init { @@ -97,8 +99,8 @@ class BTreeDB6 private constructor(override val file: File, private var reader: reader.close() } - override val blockSize = reader.readInt() - private val effectiveBlockSize = blockSize - 9 + val blockSize = reader.readInt() + val effectiveBlockSize = blockSize - 9 init { require(blockSize >= 64) { "Degenerate block size: $blockSize" } @@ -235,11 +237,11 @@ class BTreeDB6 private constructor(override val file: File, private var reader: } } - override fun contains(key: ByteKey): Boolean { + operator fun contains(key: ByteKey): Boolean { return searchForBlock(key) != null } - override fun read(key: ByteKey): KOptional { + fun read(key: ByteKey): KOptional { val data = searchForBlock(key) if (data != null) { @@ -274,7 +276,7 @@ class BTreeDB6 private constructor(override val file: File, private var reader: } } - override fun findAllKeys(): List { + fun findAllKeys(): List { if (rootBlockIndex == INVALID_BLOCK_INDEX) { return emptyList() } @@ -284,7 +286,7 @@ class BTreeDB6 private constructor(override val file: File, private var reader: return result } - override fun write(key: ByteKey, value: ByteArray, offset: Int, length: Int) { + fun write(key: ByteKey, value: ByteArray, offset: Int, length: Int) { if (rootBlockIndex == INVALID_BLOCK_INDEX) { // create LEAF node for root val block = allocBlock(BlockType.LEAF)