Skip to content

Commit

Permalink
Merge pull request #6259 from seadowg/optimized-entities
Browse files Browse the repository at this point in the history
Add partial parsing for entities
  • Loading branch information
grzesiek2010 authored Jul 10, 2024
2 parents b0b0212 + 337d738 commit edcffcc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,37 @@ internal class LocalEntitiesFileInstanceParser(private val entitiesRepositoryPro
ExternalInstanceParser.FileInstanceParser {

override fun parse(instanceId: String, path: String): TreeElement {
return parse(instanceId, path, false)
}

override fun parse(instanceId: String, path: String, partial: Boolean): TreeElement {
val root = TreeElement("root", 0)

val entitiesRepository = entitiesRepositoryProvider()
entitiesRepository.getEntities(instanceId).forEachIndexed { index, entity ->
val name = TreeElement(EntityItemElement.ID)
name.value = StringData(entity.id)

val label = TreeElement(EntityItemElement.LABEL)
label.value = StringData(entity.label)

val version = TreeElement(EntityItemElement.VERSION)
version.value = StringData(entity.version.toString())

val item = TreeElement("item", index)
if (!partial) {
name.value = StringData(entity.id)
label.value = StringData(entity.label)
version.value = StringData(entity.version.toString())
}

val item = TreeElement("item", index, partial)
item.addChild(name)
item.addChild(label)
item.addChild(version)

entity.properties.forEach { property ->
addChild(item, property)
val propertyElement = TreeElement(property.first)

if (!partial) {
propertyElement.value = StringData(property.second)
}

item.addChild(propertyElement)
}

root.addChild(item)
Expand All @@ -42,15 +53,4 @@ internal class LocalEntitiesFileInstanceParser(private val entitiesRepositoryPro
val entitiesRepository = entitiesRepositoryProvider()
return entitiesRepository.getLists().contains(instanceId)
}

private fun addChild(
element: TreeElement,
nameAndValue: Pair<String, String>
) {
element.addChild(
TreeElement(nameAndValue.first).also {
it.value = StringData(nameAndValue.second)
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,28 @@ class LocalEntitiesFileInstanceParserTest {
assertThat(item.numChildren, equalTo(3))
assertThat(item.getFirstChild(EntityItemElement.VERSION)?.value?.value, equalTo("1"))
}

@Test
fun `partial parse returns elements without values`() {
val entity =
Entity(
"people",
"1",
"Shiv Roy",
properties = listOf(Pair("age", "35")),
version = 1
)
entitiesRepository.save(entity)

val parser = LocalEntitiesFileInstanceParser { entitiesRepository }
val instance = parser.parse("people", "people.csv", true)
assertThat(instance.numChildren, equalTo(1))

val item = instance.getChildAt(0)!!
assertThat(item.isPartial, equalTo(true))
assertThat(item.numChildren, equalTo(4))
0.until(item.numChildren).forEach {
assertThat(item.getChildAt(it).value?.value, equalTo(null))
}
}
}

0 comments on commit edcffcc

Please sign in to comment.