Skip to content

Commit

Permalink
Migrate PolylinesSerializer to kotlinx-io (#5307)
Browse files Browse the repository at this point in the history
* Migrate PolylinesSerializer to kotlinx-io

* Add database migration for deleting elements with their geometries

* add the database upgrade code to the correct file

---------

Co-authored-by: Tobias Zwick <[email protected]>
  • Loading branch information
neonowy and westnordost authored Jan 31, 2024
1 parent 798fe7f commit 8748863
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ dependencies {
// Kotlin
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.3.0")

// Date/time
api("org.jetbrains.kotlinx:kotlinx-datetime:0.5.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import de.westnordost.streetcomplete.quests.oneway_suspects.data.WayTrafficFlowT

/** Creates the database and upgrades it */
object DatabaseInitializer {
const val DB_VERSION = 12
const val DB_VERSION = 13

fun onCreate(db: Database) {
// OSM notes
Expand Down Expand Up @@ -216,6 +216,24 @@ object DatabaseInitializer {
db.exec(LogsTable.CREATE)
db.exec(LogsTable.INDEX_CREATE)
}
if (oldVersion <= 12 && newVersion > 12) {
// Deleting all data related to elements and their geometries, since
// the binary format in which `GEOMETRY_{POLYGONS,POLYLINES}` data from
// `{Way,Relation}GeometryTable` is stored has changed (see #5307)

db.exec("DELETE FROM ${RelationTables.NAME_MEMBERS};")
db.exec("DELETE FROM ${RelationTables.NAME};")

db.exec("DELETE FROM ${WayTables.NAME_NODES};")
db.exec("DELETE FROM ${WayTables.NAME};")

db.exec("DELETE FROM ${NodeTable.NAME};")

db.exec("DELETE FROM ${RelationGeometryTable.NAME};")
db.exec("DELETE FROM ${WayGeometryTable.NAME};")

db.exec("DELETE FROM ${DownloadedTilesTable.NAME};")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package de.westnordost.streetcomplete.data.osm.geometry

import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import kotlinx.io.Buffer
import kotlinx.io.readByteArray
import kotlinx.io.readDouble
import kotlinx.io.writeDouble

/** Serializes a list of a list of latlons into to a byte array and back memory-efficiently.
*
Expand All @@ -21,27 +21,27 @@ import java.io.ObjectOutputStream
class PolylinesSerializer {

fun serialize(polylines: List<List<LatLon>>): ByteArray {
val baos = ByteArrayOutputStream()
ObjectOutputStream(baos).use { oos ->
oos.writeInt(polylines.size)
for (polyline in polylines) {
oos.writeInt(polyline.size)
for (position in polyline) {
oos.writeDouble(position.latitude)
oos.writeDouble(position.longitude)
}
val buffer = Buffer()

buffer.writeInt(polylines.size)
for (polyline in polylines) {
buffer.writeInt(polyline.size)
for (position in polyline) {
buffer.writeDouble(position.latitude)
buffer.writeDouble(position.longitude)
}
}
return baos.toByteArray()

return buffer.readByteArray()
}

fun deserialize(byteArray: ByteArray): List<List<LatLon>> {
val bais = ByteArrayInputStream(byteArray)
return ObjectInputStream(bais).use { ois ->
MutableList(ois.readInt()) {
MutableList(ois.readInt()) {
LatLon(ois.readDouble(), ois.readDouble())
}
val buffer = Buffer()
buffer.write(byteArray)

return MutableList(buffer.readInt()) {
MutableList(buffer.readInt()) {
LatLon(buffer.readDouble(), buffer.readDouble())
}
}
}
Expand Down

0 comments on commit 8748863

Please sign in to comment.