While BTreeDB6 was a fun little project, it is inferior to SQLite in all ways
This commit is contained in:
parent
373b7ce682
commit
1f6eb023ed
@ -4,7 +4,7 @@ kotlin.code.style=official
|
|||||||
specifyKotlinAsDependency=false
|
specifyKotlinAsDependency=false
|
||||||
|
|
||||||
projectGroup=ru.dbotthepony.kommons
|
projectGroup=ru.dbotthepony.kommons
|
||||||
projectVersion=2.18.1
|
projectVersion=3.0.0
|
||||||
|
|
||||||
guavaDepVersion=33.0.0
|
guavaDepVersion=33.0.0
|
||||||
gsonDepVersion=2.8.9
|
gsonDepVersion=2.8.9
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,102 +0,0 @@
|
|||||||
package ru.dbotthepony.kommons.io
|
|
||||||
|
|
||||||
import java.io.InputStream
|
|
||||||
import java.io.OutputStream
|
|
||||||
import java.util.UUID
|
|
||||||
|
|
||||||
private fun toBytes(key: UUID): ByteArray {
|
|
||||||
val value = ByteArray(16)
|
|
||||||
|
|
||||||
value[0] = ((key.mostSignificantBits ushr 56) and 0xFFL).toByte()
|
|
||||||
value[1] = ((key.mostSignificantBits ushr 48) and 0xFFL).toByte()
|
|
||||||
value[2] = ((key.mostSignificantBits ushr 40) and 0xFFL).toByte()
|
|
||||||
value[3] = ((key.mostSignificantBits ushr 32) and 0xFFL).toByte()
|
|
||||||
value[4] = ((key.mostSignificantBits ushr 24) and 0xFFL).toByte()
|
|
||||||
value[5] = ((key.mostSignificantBits ushr 16) and 0xFFL).toByte()
|
|
||||||
value[6] = ((key.mostSignificantBits ushr 8) and 0xFFL).toByte()
|
|
||||||
value[7] = ((key.mostSignificantBits ushr 0) and 0xFFL).toByte()
|
|
||||||
|
|
||||||
value[8 + 0] = ((key.leastSignificantBits ushr 56) and 0xFFL).toByte()
|
|
||||||
value[8 + 1] = ((key.leastSignificantBits ushr 48) and 0xFFL).toByte()
|
|
||||||
value[8 + 2] = ((key.leastSignificantBits ushr 40) and 0xFFL).toByte()
|
|
||||||
value[8 + 3] = ((key.leastSignificantBits ushr 32) and 0xFFL).toByte()
|
|
||||||
value[8 + 4] = ((key.leastSignificantBits ushr 24) and 0xFFL).toByte()
|
|
||||||
value[8 + 5] = ((key.leastSignificantBits ushr 16) and 0xFFL).toByte()
|
|
||||||
value[8 + 6] = ((key.leastSignificantBits ushr 8) and 0xFFL).toByte()
|
|
||||||
value[8 + 7] = ((key.leastSignificantBits ushr 0) and 0xFFL).toByte()
|
|
||||||
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
class ByteKey private constructor(private val bytes: ByteArray, mark: Nothing?) : Comparable<ByteKey> {
|
|
||||||
constructor(vararg bytes: Byte) : this(bytes, null)
|
|
||||||
constructor(key: String) : this(key.toByteArray(), null)
|
|
||||||
constructor(key: UUID) : this(toBytes(key), null)
|
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
|
||||||
return this === other || other is ByteKey && other.bytes.contentEquals(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
val size: Int
|
|
||||||
get() = bytes.size
|
|
||||||
|
|
||||||
operator fun get(index: Int): Byte {
|
|
||||||
return bytes[index]
|
|
||||||
}
|
|
||||||
|
|
||||||
fun write(stream: OutputStream) {
|
|
||||||
stream.writeVarInt(bytes.size)
|
|
||||||
stream.write(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun writeRaw(stream: OutputStream) {
|
|
||||||
stream.write(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun toByteArray(): ByteArray {
|
|
||||||
return bytes.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
|
||||||
return bytes.contentHashCode()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
return "ByteKey[${bytes.map { it.toInt() and 0xFF }.joinToString(", ")}]"
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun compareTo(other: ByteKey): Int {
|
|
||||||
val cmp = size.compareTo(other.size)
|
|
||||||
if (cmp != 0) return cmp
|
|
||||||
|
|
||||||
for (i in bytes.indices) {
|
|
||||||
if (bytes[i].toInt() and 0xFF > other.bytes[i].toInt() and 0xFF) {
|
|
||||||
return 1
|
|
||||||
} else if (bytes[i].toInt() and 0xFF < other.bytes[i].toInt() and 0xFF) {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
/**
|
|
||||||
* Constructs [ByteKey] without any copying of provided array
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun wrap(bytes: ByteArray): ByteKey {
|
|
||||||
return ByteKey(bytes, null)
|
|
||||||
}
|
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
fun read(stream: InputStream): ByteKey {
|
|
||||||
return stream.readByteKey()
|
|
||||||
}
|
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
fun readRaw(stream: InputStream, size: Int): ByteKey {
|
|
||||||
return stream.readByteKeyRaw(size)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -43,7 +43,7 @@ import kotlin.concurrent.withLock
|
|||||||
* to avoid structure corruption a lock is employed.
|
* to avoid structure corruption a lock is employed.
|
||||||
*
|
*
|
||||||
* Attached delegates can be safely mutated by multiple threads concurrently
|
* Attached delegates can be safely mutated by multiple threads concurrently
|
||||||
* (attached [ListenableDelegate] can call [Listenable.addListener] callback concurrently).
|
* (attached [ListenableDelegate] can call [Listenable.addListener] callback from different threads).
|
||||||
*/
|
*/
|
||||||
@Suppress("UNCHECKED_CAST", "UNUSED")
|
@Suppress("UNCHECKED_CAST", "UNUSED")
|
||||||
class DelegateSyncher : Observer {
|
class DelegateSyncher : Observer {
|
||||||
|
@ -327,11 +327,3 @@ fun InputStream.readByteArray(maxRead: Int = Int.MAX_VALUE): ByteArray {
|
|||||||
throw IllegalArgumentException("Trying to read $size bytes when $maxRead is the max allowed")
|
throw IllegalArgumentException("Trying to read $size bytes when $maxRead is the max allowed")
|
||||||
return readOrError(ByteArray(size))
|
return readOrError(ByteArray(size))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun InputStream.readByteKey(): ByteKey {
|
|
||||||
return ByteKey(*ByteArray(readVarInt()).also { read(it) })
|
|
||||||
}
|
|
||||||
|
|
||||||
fun InputStream.readByteKeyRaw(size: Int): ByteKey {
|
|
||||||
return ByteKey(*ByteArray(size).also { read(it) })
|
|
||||||
}
|
|
||||||
|
@ -327,11 +327,3 @@ fun OutputStream.writeByteArray(value: ByteArray, offset: Int, length: Int) {
|
|||||||
writeVarInt(length)
|
writeVarInt(length)
|
||||||
write(value, offset, length)
|
write(value, offset, length)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun OutputStream.writeByteKey(key: ByteKey) {
|
|
||||||
key.write(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun OutputStream.writeRawByteKey(key: ByteKey) {
|
|
||||||
key.writeRaw(this)
|
|
||||||
}
|
|
||||||
|
@ -1,52 +0,0 @@
|
|||||||
package ru.dbotthepony.kommons.test
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.DisplayName
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
import ru.dbotthepony.kommons.io.BTreeDB6
|
|
||||||
import ru.dbotthepony.kommons.io.ByteKey
|
|
||||||
import java.io.File
|
|
||||||
import java.util.concurrent.Executor
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
|
|
||||||
object BTreeDB6Tests {
|
|
||||||
@Test
|
|
||||||
@DisplayName("BTreeDB6 Tests")
|
|
||||||
fun test() {
|
|
||||||
val file = File("dbtest.bdb")
|
|
||||||
if (file.exists()) file.delete()
|
|
||||||
val create = BTreeDB6.create(file, 4096, sync = false)
|
|
||||||
|
|
||||||
for (i in 0 .. 800) {
|
|
||||||
val s = "This is key $i"
|
|
||||||
val k = ByteKey("This is key $i")
|
|
||||||
create.write(k, s.toByteArray())
|
|
||||||
assertEquals(s, String(create.read(k).get()))
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i in 0 .. 800) {
|
|
||||||
val s = "This is key $i"
|
|
||||||
val k = ByteKey("This is key $i")
|
|
||||||
assertEquals(s, String(create.read(k).get()))
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i in 0 .. 800) {
|
|
||||||
val s = "This is key $i"
|
|
||||||
val k = ByteKey("This is key $i")
|
|
||||||
create.write(k, s.toByteArray())
|
|
||||||
assertEquals(s, String(create.read(k).get()))
|
|
||||||
}
|
|
||||||
|
|
||||||
create.close()
|
|
||||||
|
|
||||||
val create2 = BTreeDB6(file)
|
|
||||||
|
|
||||||
for (i in 0 .. 800) {
|
|
||||||
val s = "This is key $i"
|
|
||||||
val k = ByteKey("This is key $i")
|
|
||||||
assertEquals(s, String(create2.read(k).get()))
|
|
||||||
}
|
|
||||||
|
|
||||||
create2.checkFreeBitmap()
|
|
||||||
create2.close()
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user