-
Notifications
You must be signed in to change notification settings - Fork 9
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
Implement the state-based counter prototype #138
base: master
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -2,10 +2,9 @@ package qbit.resolving | |
|
||
import kotlinx.coroutines.flow.toList | ||
import qbit.api.Instances | ||
import qbit.api.QBitException | ||
import qbit.api.gid.Gid | ||
import qbit.api.model.Attr | ||
import qbit.api.model.Eav | ||
import qbit.api.model.Hash | ||
import qbit.api.model.* | ||
import qbit.index.RawEntity | ||
import qbit.serialization.* | ||
import qbit.trx.TrxLog | ||
|
@@ -72,6 +71,51 @@ internal fun lastWriterWinsResolve(resolveAttrName: (String) -> Attr<Any>?): (Li | |
} | ||
} | ||
|
||
internal fun crdtResolve( | ||
resolveEntity: (Gid) -> StoredEntity?, | ||
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. resolveBaseEntity И ваще лучше коммент к методу написать. Как писать хоршие комменты можешь почитать в Philosophy of Software Design и вроде том же Clean Code было |
||
resolveAttrName: (String) -> Attr<Any>? | ||
): (List<PersistedEav>, List<PersistedEav>) -> List<Eav> = { eavsFromA, eavsFromB -> | ||
require(eavsFromA.isNotEmpty()) { "eavsFromA should be not empty" } | ||
require(eavsFromB.isNotEmpty()) { "eavsFromB should be not empty" } | ||
|
||
val gid = eavsFromA[0].eav.gid | ||
val attr = resolveAttrName(eavsFromA[0].eav.attr) | ||
?: throw IllegalArgumentException("Cannot resolve ${eavsFromA[0].eav.attr}") | ||
|
||
when { | ||
// temporary dirty hack until crdt counter or custom resolution strategy support is implemented | ||
attr == Instances.nextEid -> listOf((eavsFromA + eavsFromB).maxByOrNull { it.eav.value as Int }!!.eav) | ||
attr.list -> (eavsFromA + eavsFromB).map { it.eav }.distinct() | ||
DataType.ofCode(attr.type)!!.isCounter() -> { | ||
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. Симетрия и постоянство уровней абстракции - очень важные свойства кода. |
||
val latestFromA = eavsFromA.maxByOrNull { it.timestamp }!!.eav.value | ||
val latestFromB = eavsFromB.maxByOrNull { it.timestamp }!!.eav.value | ||
val previous = resolveEntity(gid)?.tryGet(attr) | ||
|
||
listOf( | ||
if (previous != null) | ||
Eav( | ||
eavsFromA[0].eav.gid, | ||
eavsFromA[0].eav.attr, | ||
if (previous is Byte && latestFromA is Byte && latestFromB is Byte) latestFromA + latestFromB - previous | ||
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. Это тоже лучше утащить в operator fun Number.plus(another: Number) = when (this) {
is Int -> this + another.toInt()
// ...
} |
||
else if (previous is Int && latestFromA is Int && latestFromB is Int) latestFromA + latestFromB - previous | ||
else if (previous is Long && latestFromA is Long && latestFromB is Long) latestFromA + latestFromB - previous | ||
else throw QBitException("Unexpected counter value type for eav with gid=$gid, attr=$attr") | ||
) | ||
else | ||
Eav( | ||
eavsFromA[0].eav.gid, | ||
eavsFromA[0].eav.attr, | ||
if (latestFromA is Byte && latestFromB is Byte) latestFromA + latestFromB | ||
else if (latestFromA is Int && latestFromB is Int) latestFromA + latestFromB | ||
else if (latestFromA is Long && latestFromB is Long) latestFromA + latestFromB | ||
else throw QBitException("Unexpected counter value type for eav with gid=$gid, attr=$attr") | ||
) | ||
) | ||
} | ||
else -> listOf((eavsFromA + eavsFromB).maxByOrNull { it.timestamp }!!.eav) | ||
} | ||
} | ||
|
||
internal fun findBaseNode(node1: Node<Hash>, node2: Node<Hash>, nodesDepth: Map<Hash, Int>): Node<Hash> { | ||
return when { | ||
node1 == node2 -> node1 | ||
|
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.
Эммм, это очень странно.
Зачем дублировать логику lastWriterWinsResolve? А тесты не дублировать?