Skip to content
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

Migrate PolylinesSerializer to kotlinx-io #5307

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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