From 86ad24ccdb42eb3f25b14afc4e8435663fc83812 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 17 Feb 2024 19:36:22 +0700 Subject: [PATCH] ByteKey UUID and wrap() construction --- .../ru/dbotthepony/kommons/io/BTreeDB6.kt | 1 + .../ru/dbotthepony/kommons/io/ByteKey.kt | 51 ++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kommons/io/BTreeDB6.kt b/src/main/kotlin/ru/dbotthepony/kommons/io/BTreeDB6.kt index c63ebbe..d4783d0 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/io/BTreeDB6.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/io/BTreeDB6.kt @@ -65,6 +65,7 @@ private fun readHeader(reader: RandomAccessFile, required: Char) { // 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: Removal of keys class BTreeDB6 private constructor(override val file: File, private var reader: RandomAccessFile, pool: Executor, private val sync: Boolean) : ByteDataBTreeDB() { constructor(file: File, pool: Executor, sync: Boolean = true) : this(file, RandomAccessFile(file, "rw"), pool, sync) diff --git a/src/main/kotlin/ru/dbotthepony/kommons/io/ByteKey.kt b/src/main/kotlin/ru/dbotthepony/kommons/io/ByteKey.kt index 538233a..09c768b 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/io/ByteKey.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/io/ByteKey.kt @@ -2,9 +2,36 @@ package ru.dbotthepony.kommons.io import java.io.InputStream import java.io.OutputStream +import java.util.UUID -class ByteKey(private vararg val bytes: Byte) : Comparable { - constructor(key: String) : this(*key.toByteArray()) +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 { + 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) @@ -52,4 +79,24 @@ class ByteKey(private vararg val bytes: Byte) : Comparable { 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) + } + } }