-
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
op-based counter #141
base: crdt-wip
Are you sure you want to change the base?
op-based counter #141
Changes from 4 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ import kotlinx.coroutines.flow.toList | |
import qbit.api.Instances | ||
import qbit.api.gid.Gid | ||
import qbit.api.model.Attr | ||
import qbit.api.model.DataType | ||
import qbit.api.model.Eav | ||
import qbit.api.model.Hash | ||
import qbit.index.RawEntity | ||
|
@@ -41,7 +42,9 @@ data class LogsDiff( | |
resolve(writesFromA[it]!!, writesFromB[it]!!) | ||
} | ||
} | ||
return resolvingEavsByGid.values.map { RawEntity(it.first().gid, it) } | ||
return resolvingEavsByGid | ||
.filter { !it.value.isEmpty() } | ||
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. Во-первых тут идея ругается, что надо it.value.isNotEmpty() Во-вторых чёт мне эта фильтрация в целом не очень нравится, давай хотя бы коммент напишем откуда там пустые значения В-третьих, чёт меня малёха смущает, что это приведёт к утере атрибута сущности, а тут вроде везде речь о сущностях целиком |
||
.values.map { RawEntity(it.first().gid, it) } | ||
} | ||
|
||
fun logAEntities(): List<RawEntity> { | ||
|
@@ -68,6 +71,7 @@ internal fun lastWriterWinsResolve(resolveAttrName: (String) -> Attr<Any>?): (Li | |
// 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) | ||
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. Это надо выкосить? |
||
attr.list -> (eavsFromA + eavsFromB).map { it.eav }.distinct() | ||
DataType.ofCode(attr.type)!!.isCounter() -> ArrayList() | ||
else -> listOf((eavsFromA + eavsFromB).maxByOrNull { it.timestamp }!!.eav) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package qbit.trx | ||
|
||
import qbit.api.QBitException | ||
import qbit.api.model.DataType | ||
import qbit.api.model.Eav | ||
import qbit.index.InternalDb | ||
|
||
fun operationalize(db: InternalDb, facts: List<Eav>): List<Eav> { | ||
return facts.map { operationalizeCounter(db, it) } | ||
} | ||
|
||
private fun operationalizeCounter(db: InternalDb, fact: Eav): Eav { | ||
val attr = db.attr(fact.attr)!! | ||
val dataType = DataType.ofCode(attr.type)!! | ||
return if (dataType.isCounter()) { | ||
val previous = db.pullEntity(fact.gid)?.tryGet(attr) | ||
if (previous != null) { | ||
Eav( | ||
fact.gid, | ||
fact.attr, | ||
if (previous is Byte && fact.value is Byte) fact.value - previous | ||
else if (previous is Int && fact.value is Int) fact.value - previous | ||
else if (previous is Long && fact.value is Long) fact.value - previous | ||
else throw QBitException("Unexpected counter value type for $fact") | ||
) | ||
} else fact | ||
} else fact | ||
} | ||
|
||
fun deoperationalize(db: InternalDb, facts: List<Eav>): List<Eav> { | ||
return facts.map { deoperationalizeCounter(db, it) } | ||
} | ||
|
||
private fun deoperationalizeCounter(db: InternalDb, fact: Eav): Eav { | ||
val attr = db.attr(fact.attr) | ||
return if (attr != null && DataType.ofCode(attr.type)!!.isCounter()) { | ||
val previous = db.pullEntity(fact.gid)?.tryGet(attr) | ||
if (previous != null) { | ||
Eav( | ||
fact.gid, | ||
fact.attr, | ||
if (fact.value is Byte) (previous as Number).toByte() + fact.value | ||
else if (fact.value is Int) (previous as Number).toInt() + fact.value | ||
else if (fact.value is Long) (previous as Number).toLong() + fact.value | ||
else throw QBitException("Unexpected counter value type for $fact") | ||
) | ||
} else fact | ||
} else fact | ||
} |
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.
Может resolveAttr уже в поля засунуть, раз такая пьянка?