-
Notifications
You must be signed in to change notification settings - Fork 9
Mv register #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Mv register #139
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,11 @@ import kotlin.reflect.KClass | |
| * - List<Ref> | ||
| */ | ||
|
|
||
| val scalarRange = 0..31 | ||
| val listRange = 32..63 | ||
| val pnCounterRange = 64..95 | ||
| val registerRange = 96..127 | ||
|
|
||
| @Suppress("UNCHECKED_CAST") | ||
| sealed class DataType<out T : Any> { | ||
|
|
||
|
|
@@ -31,22 +36,23 @@ sealed class DataType<out T : Any> { | |
| private val values: Array<DataType<*>> | ||
| get() = arrayOf(QBoolean, QByte, QInt, QLong, QString, QBytes, QGid, QRef) | ||
|
|
||
| fun ofCode(code: Byte): DataType<*>? = | ||
| if (code <= 19) { | ||
| values.firstOrNull { it.code == code } | ||
| } else { | ||
| values.map { it.list() }.firstOrNull { it.code == code } | ||
| } | ||
| fun ofCode(code: Byte): DataType<*>? = when(code) { | ||
| in scalarRange -> values.firstOrNull { it.code == code } | ||
| in listRange -> ofCode((code - listRange.first).toByte())?.list() | ||
| in pnCounterRange -> ofCode((code - pnCounterRange.first).toByte())?.counter() | ||
| in registerRange -> ofCode((code - registerRange.first).toByte())?.register() | ||
| else -> null | ||
| } | ||
|
|
||
| fun <T : Any> ofValue(value: T?): DataType<T>? = when (value) { | ||
| fun <T : Any> ofValue(value: T?): DataType<T>? = when (value) { // TODO REFACTOR | ||
| is Boolean -> QBoolean as DataType<T> | ||
| is Byte -> QByte as DataType<T> | ||
| is Int -> QInt as DataType<T> | ||
| is Long -> QLong as DataType<T> | ||
| is String -> QString as DataType<T> | ||
| is ByteArray -> QBytes as DataType<T> | ||
| is Gid -> QGid as DataType<T> | ||
| is List<*> -> value.firstOrNull()?.let { ofValue(it)?.list() } as DataType<T> | ||
| is List<*> -> value.firstOrNull()?.let { ofValue(it)?.list() } as DataType<T>? | ||
| else -> QRef as DataType<T> | ||
| } | ||
| } | ||
|
|
@@ -57,9 +63,25 @@ sealed class DataType<out T : Any> { | |
| return QList(this) | ||
| } | ||
|
|
||
| fun isList(): Boolean = (code.toInt().and(32)) > 0 | ||
| fun isList(): Boolean = code in listRange | ||
|
|
||
| fun ref(): Boolean = this == QRef || this is QList<*> && this.itemsType == QRef | ||
| fun counter(): QCounter<T> { | ||
| require(this is QByte || this is QInt || this is QLong) { "Only primitive number values are allowed in counters" } | ||
| return QCounter(this) | ||
| } | ||
|
|
||
| fun isCounter(): Boolean = code in pnCounterRange | ||
|
|
||
| fun register(): QRegister<T> { | ||
| require(!(this is QList<*> || this is QCounter || this is QRegister)) { "Nested wrappers is not allowed" } | ||
| return QRegister(this) | ||
| } | ||
|
|
||
| fun isRegister(): Boolean = code in registerRange | ||
|
|
||
| fun ref(): Boolean = this == QRef || | ||
| this is QList<*> && this.itemsType == QRef || | ||
| this is QRegister<*> && this.itemsType == QRef | ||
|
|
||
| fun value(): Boolean = !ref() | ||
|
|
||
|
|
@@ -73,15 +95,28 @@ sealed class DataType<out T : Any> { | |
| is QBytes -> ByteArray::class | ||
| is QGid -> Gid::class | ||
| is QList<*> -> this.itemsType.typeClass() | ||
| is QCounter<*> -> this.primitiveType.typeClass() | ||
| is QRegister<*> -> this.itemsType.typeClass() | ||
| QRef -> Any::class | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| data class QList<out I : Any>(val itemsType: DataType<I>) : DataType<List<I>>() { | ||
|
|
||
| override val code = (32 + itemsType.code).toByte() | ||
| override val code = (listRange.first + itemsType.code).toByte() | ||
|
|
||
| } | ||
|
|
||
| data class QCounter<out I : Any>(val primitiveType: DataType<I>) : DataType<I>() { | ||
|
|
||
| override val code = (pnCounterRange.first + primitiveType.code).toByte() | ||
|
|
||
| } | ||
|
|
||
| data class QRegister<out I : Any>(val itemsType: DataType<I>) : DataType<I>() { | ||
|
|
||
| override val code = (registerRange.first + itemsType.code).toByte() | ||
|
|
||
| } | ||
|
|
||
|
|
@@ -134,4 +169,4 @@ object QGid : DataType<Gid>() { | |
| } | ||
|
|
||
| fun isListOfVals(list: List<Any>?) = | ||
| list == null || list.isEmpty() || list.firstOrNull()?.let { DataType.ofValue(it)?.value() } ?: true | ||
| list == null || list.isEmpty() || list.firstOrNull()?.let { DataType.ofValue(it)?.value() } ?: true // TODO REFACTOR | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тот же вопрос |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package qbit.api.model | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| class Register<T>( | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Мне тут не нравится всё
|
||
| private var entries: List<T> | ||
| ) { | ||
| fun getValues(): List<T> = entries | ||
|
|
||
| fun setValue(t: T) { | ||
| entries = listOf(t) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,10 +14,7 @@ import kotlinx.serialization.modules.SerializersModule | |
| import kotlinx.serialization.modules.SerializersModuleCollector | ||
| import qbit.api.QBitException | ||
| import qbit.api.gid.Gid | ||
| import qbit.api.model.Attr | ||
| import qbit.api.model.Eav | ||
| import qbit.api.model.Entity | ||
| import qbit.api.model.Tombstone | ||
| import qbit.api.model.* | ||
| import qbit.api.tombstone | ||
| import qbit.collections.IdentityMap | ||
| import qbit.factoring.* | ||
|
|
@@ -99,6 +96,12 @@ internal class EntityEncoder( | |
| ValueKind.REF_LIST -> { | ||
| serializeRefList(value as Iterable<Any>) | ||
| } | ||
| ValueKind.VALUE_REGISTER -> { | ||
| (value as Register<Any>).getValues() | ||
| } | ||
| ValueKind.REF_REGISTER -> { | ||
| serializeRefList((value as Register<Any>).getValues()) | ||
| } | ||
| } | ||
|
|
||
| val fieldPointer = Pointer( | ||
|
|
@@ -185,7 +188,7 @@ internal class EntityEncoder( | |
|
|
||
| enum class ValueKind { | ||
|
|
||
| SCALAR_VALUE, SCALAR_REF, VALUE_LIST, REF_LIST; | ||
| SCALAR_VALUE, SCALAR_REF, VALUE_LIST, REF_LIST, VALUE_REGISTER, REF_REGISTER; | ||
|
|
||
| companion object { | ||
| fun of(descriptor: SerialDescriptor, index: Int, value: Any): ValueKind { | ||
|
|
@@ -194,7 +197,10 @@ enum class ValueKind { | |
| isScalarValue(value) -> { | ||
| SCALAR_VALUE | ||
| } | ||
| isScalarRef(elementDescriptor) -> { | ||
| isScalarRef( | ||
| elementDescriptor, | ||
| value | ||
| ) -> { | ||
| SCALAR_REF | ||
| } | ||
| isValueList( | ||
|
|
@@ -209,6 +215,18 @@ enum class ValueKind { | |
| ) -> { | ||
| REF_LIST | ||
| } | ||
| isValueRegister( | ||
| elementDescriptor, | ||
| value | ||
| ) -> { | ||
| VALUE_REGISTER | ||
| } | ||
| isRefRegister( | ||
| elementDescriptor, | ||
| value | ||
| ) -> { | ||
| REF_REGISTER | ||
| } | ||
| else -> { | ||
| throw AssertionError("Writing primitive via encodeSerializableElement") | ||
| } | ||
|
|
@@ -219,8 +237,8 @@ enum class ValueKind { | |
| // other primitive values are encoded directly via encodeXxxElement | ||
| value is Gid || value is ByteArray | ||
|
|
||
| private fun isScalarRef(elementDescriptor: SerialDescriptor) = | ||
| elementDescriptor.kind == StructureKind.CLASS | ||
| private fun isScalarRef(elementDescriptor: SerialDescriptor, value: Any) = | ||
| elementDescriptor.kind == StructureKind.CLASS && value !is Register<*> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| private fun isValueList(elementDescriptor: SerialDescriptor, value: Any) = | ||
| elementDescriptor.kind == StructureKind.LIST && | ||
|
|
@@ -231,6 +249,13 @@ enum class ValueKind { | |
| private fun isRefList(elementDescriptor: SerialDescriptor, value: Any) = | ||
| elementDescriptor.kind == StructureKind.LIST && value is List<*> | ||
|
|
||
| private fun isValueRegister(elementDescriptor: SerialDescriptor, value: Any) = | ||
| value is Register<*> && //TODO DEDUPLICATE | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. С чем? |
||
| (elementDescriptor.getElementDescriptor(0).getElementDescriptor(0).kind is PrimitiveKind || | ||
| elementDescriptor.getElementDescriptor(0).getElementDescriptor(0).kind == StructureKind.LIST) // ByteArray | ||
|
|
||
| private fun isRefRegister(elementDescriptor: SerialDescriptor, value: Any) = // TODO REFACTOR | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Как? |
||
| value is Register<*> && elementDescriptor.getElementDescriptor(0).getElementDescriptor(0).kind is StructureKind.CLASS | ||
| } | ||
|
|
||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Рефактор что, как, зачем?