Skip to content

Commit

Permalink
Remove unused persisted index from implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Jul 12, 2024
1 parent c37abf5 commit d3ebf19
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository {
}

override fun getEntities(list: String): List<Entity.Saved> {
return readEntities().filter { it.list == list }
return readEntities().filter { it.list == list }.mapIndexed { index, entity ->
Entity.Saved(
entity.list,
entity.id,
entity.label,
entity.version,
entity.properties,
entity.state,
index
)
}
}

override fun save(vararg entities: Entity) {
val storedEntities = readEntities()
var maxIndex = storedEntities.maxOfOrNull { it.index } ?: -1

entities.forEach { entity ->
val existing = storedEntities.find { it.id == entity.id }
Expand All @@ -34,26 +43,24 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository {

storedEntities.remove(existing)
storedEntities.add(
Entity.Saved(
Entity.New(
entity.list,
entity.id,
entity.label ?: existing.label,
version = entity.version,
properties = mergeProperties(existing, entity),
state = state,
existing.index
state = state
)
)
} else {
storedEntities.add(
Entity.Saved(
Entity.New(
entity.list,
entity.id,
entity.label,
entity.version,
entity.properties,
entity.state,
++maxIndex
entity.state
)
)
}
Expand All @@ -79,15 +86,10 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository {
override fun delete(id: String) {
val existing = readEntities()
existing.removeIf { it.id == id }

val withAdjustedIndex = existing.mapIndexed { index, entity ->
entity.copy(index = index)
}

writeEntities(withAdjustedIndex)
writeEntities(existing)
}

private fun writeEntities(entities: List<Entity.Saved>) {
private fun writeEntities(entities: List<Entity.New>) {
val map = mutableMapOf<String, MutableList<JsonEntity>>()
entities.forEach {
map.getOrPut(it.list) { mutableListOf() }.add(it.toJson())
Expand All @@ -96,7 +98,7 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository {
writeJson(map)
}

private fun readEntities(): MutableList<Entity.Saved> {
private fun readEntities(): MutableList<Entity.New> {
return readJson().entries.flatMap { (list, entities) ->
entities.map { it.toEntity(list) }
}.toMutableList()
Expand Down Expand Up @@ -158,36 +160,33 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository {
val label: String?,
val version: Int,
val properties: Map<String, String>,
val offline: Boolean,
val index: Int
val offline: Boolean
)

private fun JsonEntity.toEntity(list: String): Entity.Saved {
private fun JsonEntity.toEntity(list: String): Entity.New {
val state = if (this.offline) {
Entity.State.OFFLINE
} else {
Entity.State.ONLINE
}

return Entity.Saved(
return Entity.New(
list,
this.id,
this.label,
this.version,
this.properties.entries.map { Pair(it.key, it.value) },
state,
this.index
state
)
}

private fun Entity.Saved.toJson(): JsonEntity {
private fun Entity.New.toJson(): JsonEntity {
return JsonEntity(
this.id,
this.label,
this.version,
this.properties.toMap(),
this.state == Entity.State.OFFLINE,
this.index
this.state == Entity.State.OFFLINE
)
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package org.odk.collect.entities.storage

class InMemEntitiesRepository(startingIndex: Int = 0) : EntitiesRepository {
class InMemEntitiesRepository : EntitiesRepository {

private val lists = mutableSetOf<String>()
private val entities = mutableListOf<Entity.Saved>()

private var indexCounter = startingIndex
private val entities = mutableListOf<Entity.New>()

override fun getLists(): Set<String> {
return lists
}

override fun getEntities(list: String): List<Entity.Saved> {
return entities.filter { it.list == list }
return entities.filter { it.list == list }.mapIndexed { index, entity ->
Entity.Saved(
entity.list,
entity.id,
entity.label,
entity.version,
entity.properties,
entity.state,
index
)
}
}

override fun clear() {
Expand All @@ -26,14 +34,6 @@ class InMemEntitiesRepository(startingIndex: Int = 0) : EntitiesRepository {

override fun delete(id: String) {
entities.removeIf { it.id == id }

indexCounter--
val withAdjustedIndex = entities.mapIndexed { index, entity ->
entity.copy(index = index)
}

entities.clear()
entities.addAll(withAdjustedIndex)
}

override fun save(vararg entities: Entity) {
Expand All @@ -49,26 +49,24 @@ class InMemEntitiesRepository(startingIndex: Int = 0) : EntitiesRepository {

this.entities.remove(existing)
this.entities.add(
Entity.Saved(
Entity.New(
entity.list,
entity.id,
entity.label ?: existing.label,
version = entity.version,
properties = mergeProperties(existing, entity),
state = state,
existing.index
state = state
)
)
} else {
this.entities.add(
Entity.Saved(
Entity.New(
entity.list,
entity.id,
entity.label,
entity.version,
entity.properties,
entity.state,
indexCounter++
entity.state
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ class LocalEntitiesFileInstanceParserTest {
"Shiv Roy",
version = 1
)
val repository = InMemEntitiesRepository(11)
val repository = InMemEntitiesRepository()
repository.save(entity)

val parser = LocalEntitiesFileInstanceParser { repository }
val instance = parser.parse("people", "people.csv", true)

val item = instance.getChildAt(0)!!
assertThat(item.multiplicity, equalTo(11))
assertThat(item.multiplicity, equalTo(0))
}
}

0 comments on commit d3ebf19

Please sign in to comment.