ByteKey UUID and wrap() construction
This commit is contained in:
parent
7a059821f8
commit
86ad24ccdb
@ -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: 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: Changeset counter (to determine write-in-progress blocks/trees, to ignore them when reconstructing tree)
|
||||||
// TODO: Tree rotations (rebalancing)
|
// 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<ByteKey>() {
|
class BTreeDB6 private constructor(override val file: File, private var reader: RandomAccessFile, pool: Executor, private val sync: Boolean) : ByteDataBTreeDB<ByteKey>() {
|
||||||
constructor(file: File, pool: Executor, sync: Boolean = true) : this(file, RandomAccessFile(file, "rw"), pool, sync)
|
constructor(file: File, pool: Executor, sync: Boolean = true) : this(file, RandomAccessFile(file, "rw"), pool, sync)
|
||||||
|
|
||||||
|
@ -2,9 +2,36 @@ package ru.dbotthepony.kommons.io
|
|||||||
|
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
class ByteKey(private vararg val bytes: Byte) : Comparable<ByteKey> {
|
private fun toBytes(key: UUID): ByteArray {
|
||||||
constructor(key: String) : this(*key.toByteArray())
|
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 {
|
override fun equals(other: Any?): Boolean {
|
||||||
return this === other || other is ByteKey && other.bytes.contentEquals(bytes)
|
return this === other || other is ByteKey && other.bytes.contentEquals(bytes)
|
||||||
@ -52,4 +79,24 @@ class ByteKey(private vararg val bytes: Byte) : Comparable<ByteKey> {
|
|||||||
|
|
||||||
return 0
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user