-
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
Open
ajani2001
wants to merge
5
commits into
d-r-q:crdt-wip
Choose a base branch
from
ajani2001:op-based-counter
base: crdt-wip
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
op-based counter #141
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
.values.map { RawEntity(it.first().gid, it) } | ||
} | ||
|
||
fun logAEntities(): List<RawEntity> { | ||
|
@@ -52,6 +55,15 @@ data class LogsDiff( | |
} | ||
} | ||
|
||
// This snippet is probably useless and should be wiped out | ||
fun logBOperations(resolveAttrName: (String) -> Attr<Any>?): List<RawEntity> { | ||
return writesFromB.entries | ||
.filter { DataType.ofCode(resolveAttrName(it.key.attr)!!.type)!!.isCounter() } | ||
.flatMap { operationFromB -> | ||
operationFromB.value.map { RawEntity(operationFromB.key.gid, listOf<Eav>(it.eav)) } | ||
} | ||
} | ||
|
||
private fun List<PersistedEav>.lastByTimestamp() = | ||
maxByOrNull { it.timestamp } | ||
|
||
|
@@ -68,6 +80,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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
qbit-core/src/commonMain/kotlin/qbit/trx/Operationalization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (previous is Byte && fact.value is Byte) previous + fact.value | ||
else if (previous is Int && fact.value is Int) previous + fact.value | ||
else if (previous is Long && fact.value is Long) previous + fact.value | ||
else throw QBitException("Unexpected counter value type for $fact") | ||
) | ||
} else fact | ||
} else fact | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
qbit-core/src/commonTest/kotlin/qbit/OperationalizationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package qbit | ||
|
||
import qbit.api.gid.nextGids | ||
import qbit.factoring.serializatoin.KSFactorizer | ||
import qbit.test.model.IntCounterEntity | ||
import qbit.trx.operationalize | ||
import qbit.typing.qbitCoreTestsSerialModule | ||
import kotlin.js.JsName | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class OperationalizationTest { | ||
|
||
private val gids = qbit.api.gid.Gid(0, 0).nextGids() | ||
|
||
val factor = KSFactorizer(qbitCoreTestsSerialModule)::factor | ||
|
||
val emptyDb = dbOf(gids, *(bootstrapSchema.values + testSchema).toTypedArray()) | ||
|
||
@JsName("Counter_not_persisted_in_db_should_pass_as_is") | ||
@Test | ||
fun `Counter not persisted in db should pass as-is`() { | ||
val counterEntity = IntCounterEntity(null, 10) | ||
val facts = operationalize(emptyDb, factor(counterEntity, emptyDb::attr, gids).entityFacts.values.first()) | ||
assertEquals(1, facts.size, "Factoring of single entity with single attr should produce single fact") | ||
assertEquals("IntCounterEntity/counter", facts[0].attr) | ||
assertEquals(10, facts[0].value) | ||
} | ||
|
||
@JsName("Persisted_counter_should_turn_into_difference") | ||
@Test | ||
fun `Persisted counter should turn into difference`() { | ||
val counterEntity = IntCounterEntity(1, 10) | ||
val updatedDb = emptyDb.with(factor(counterEntity, emptyDb::attr, gids)) | ||
|
||
counterEntity.counter = 100 | ||
val facts = operationalize(updatedDb, factor(counterEntity, updatedDb::attr, gids).entityFacts.values.first()) | ||
assertEquals(1, facts.size, "Factoring of single entity with single attr should produce single fact") | ||
assertEquals("IntCounterEntity/counter", facts[0].attr) | ||
assertEquals(90, facts[0].value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Во-первых тут идея ругается, что надо it.value.isNotEmpty()
Во-вторых чёт мне эта фильтрация в целом не очень нравится, давай хотя бы коммент напишем откуда там пустые значения
В-третьих, чёт меня малёха смущает, что это приведёт к утере атрибута сущности, а тут вроде везде речь о сущностях целиком