-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IO: Add NSW stops proto parser and resources (#629)
Create Proto Parser for NSW Stops Added a proto parser to read NSW stops data from a bundled protobuf file using Compose Resources. The parser: - Loads NSW_STOPS.pb file from compose resources - Decodes the protobuf data into NswStopList model - Measures and logs parsing performance metrics - Integrates with dependency injection via Koin - Initializes parsing in SavedTripsViewModel Links: - [Compose Resources Documentation](https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-multiplatform-resources-usage.html#raw-files)
- Loading branch information
1 parent
70cfbbb
commit a3cd5d2
Showing
16 changed files
with
156 additions
and
9 deletions.
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
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 |
---|---|---|
@@ -1,11 +1,5 @@ | ||
android { | ||
namespace = "xyz.ksharma.krail.gtfs_static" | ||
|
||
buildTypes { | ||
debug {} | ||
|
||
release {} | ||
} | ||
} | ||
|
||
plugins { | ||
|
File renamed without changes.
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
Binary file not shown.
16 changes: 16 additions & 0 deletions
16
io/gtfs/src/commonMain/kotlin/xyz.ksharma.krail.io.gtfs/di/GtfsModule.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,16 @@ | ||
package xyz.ksharma.krail.io.gtfs.di | ||
|
||
import org.koin.core.qualifier.named | ||
import org.koin.dsl.module | ||
import xyz.ksharma.krail.core.di.DispatchersComponent.Companion.IODispatcher | ||
import xyz.ksharma.krail.io.gtfs.nswstops.ProtoParser | ||
import xyz.ksharma.krail.io.gtfs.nswstops.StopsProtoParser | ||
|
||
val gtfsModule = module { | ||
single<ProtoParser> { | ||
StopsProtoParser( | ||
ioDispatcher = get(named(IODispatcher)), | ||
sandook = get(), | ||
) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
io/gtfs/src/commonMain/kotlin/xyz.ksharma.krail.io.gtfs/nswstops/ProtoParser.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,11 @@ | ||
package xyz.ksharma.krail.io.gtfs.nswstops | ||
|
||
import app.krail.kgtfs.proto.NswStopList | ||
|
||
interface ProtoParser { | ||
/** | ||
* Reads and decodes the NSW stops from a protobuf file, then inserts the stops into the database. | ||
* @return The decoded `NswStopList` containing the NSW stops. | ||
*/ | ||
suspend fun parseAndInsertStops(): NswStopList? | ||
} |
76 changes: 76 additions & 0 deletions
76
io/gtfs/src/commonMain/kotlin/xyz.ksharma.krail.io.gtfs/nswstops/StopsProtoParser.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,76 @@ | ||
package xyz.ksharma.krail.io.gtfs.nswstops | ||
|
||
import app.krail.kgtfs.proto.NswStopList | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.DateTimeUnit | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.until | ||
import krail.io.gtfs.generated.resources.Res | ||
import org.jetbrains.compose.resources.ExperimentalResourceApi | ||
import xyz.ksharma.krail.core.log.log | ||
import xyz.ksharma.krail.sandook.Sandook | ||
|
||
class StopsProtoParser( | ||
private val ioDispatcher: CoroutineDispatcher, | ||
private val sandook: Sandook, | ||
) : ProtoParser { | ||
|
||
/** | ||
* Reads and decodes the NSW stops from a protobuf file, then inserts the stops into the database. | ||
* | ||
* @return The decoded `NswStopList` containing the NSW stops. | ||
*/ | ||
@OptIn(ExperimentalResourceApi::class) | ||
override suspend fun parseAndInsertStops(): NswStopList = withContext(ioDispatcher) { | ||
var start = Clock.System.now() | ||
|
||
val byteArray = Res.readBytes("files/NSW_STOPS.pb") | ||
val decodedStops = NswStopList.ADAPTER.decode(byteArray) | ||
|
||
var duration = start.until( | ||
Clock.System.now(), DateTimeUnit.MILLISECOND, | ||
TimeZone.currentSystemDefault(), | ||
) | ||
log("Decoded #Stops: ${decodedStops.nswStops.size} - duration: $duration ms") | ||
|
||
log("Start inserting stops. Currently ${sandook.stopsCount()} stops in the database") | ||
start = Clock.System.now() | ||
insertStopsInTransaction(decodedStops) | ||
duration = start.until( | ||
Clock.System.now(), DateTimeUnit.MILLISECOND, TimeZone.currentSystemDefault() | ||
) | ||
log("Inserted #Stops: ${decodedStops.nswStops.size} in duration: $duration ms") | ||
|
||
decodedStops | ||
} | ||
|
||
private suspend fun insertStopsInTransaction(decoded: NswStopList) = withContext(ioDispatcher) { | ||
val start = Clock.System.now() | ||
sandook.insertTransaction { | ||
decoded.nswStops.forEach { nswStop -> | ||
sandook.insertNswStop( | ||
stopId = nswStop.stopId, | ||
stopName = nswStop.stopName, | ||
stopLat = nswStop.lat, | ||
stopLon = nswStop.lon | ||
) | ||
nswStop.productClass.forEach { productClass -> | ||
sandook.insertNswStopProductClass( | ||
stopId = nswStop.stopId, | ||
productClass = productClass | ||
) | ||
} | ||
} | ||
} | ||
|
||
val duration = start.until( | ||
Clock.System.now(), DateTimeUnit.MILLISECOND, TimeZone.currentSystemDefault(), | ||
) | ||
// Log less frequently, for example once after the transaction completes | ||
println("Inserted ${decoded.nswStops.size} stops in a single transaction in $duration ms") | ||
// TODO - analytics track how much time it took to insert stops. | ||
// Also track based on Firebase for performance. | ||
} | ||
} |
File renamed without changes.
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