Skip to content

Commit

Permalink
Insert data to db in parser files (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz authored Sep 6, 2024
1 parent ffac848 commit 12f210a
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 138 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ dependencies {
implementation(libs.lifecycle.runtime.ktx)
implementation(libs.activity.compose)
implementation(libs.timber)
implementation(project(":core:utils"))

androidTestImplementation(libs.androidx.test.ext.junit)
testImplementation(libs.ui.test.junit4)
Expand Down
32 changes: 29 additions & 3 deletions app/src/main/java/xyz/ksharma/krail/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import timber.log.Timber
import xyz.ksharma.krail.database.sydney.trains.database.api.SydneyTrainsStaticDB
import xyz.ksharma.krail.design.system.theme.StartTheme
import xyz.ksharma.krail.sydney.trains.database.real.parser.StopTimesParser.parseStopTimes
import xyz.ksharma.krail.model.sydneytrains.GTFSFeedFileNames
import xyz.ksharma.krail.sydney.trains.network.api.repository.SydneyTrainsRepository
import xyz.ksharma.krail.utils.toPath
import java.time.Instant
import java.time.temporal.ChronoUnit
import javax.inject.Inject

@AndroidEntryPoint
Expand All @@ -30,9 +37,28 @@ class MainActivity : ComponentActivity() {

lifecycleScope.launch {
repository.fetchStaticSydneyTrainsScheduleAndCache()
realSydneyTrainsStaticDb.insertStopTimes()
val x = realSydneyTrainsStaticDb.getStopTimes()
Timber.d("$x")

delay(5000)
val startTime = Instant.now()
parseStopTimes(
path = applicationContext.toPath(GTFSFeedFileNames.STOP_TIMES.fileName),
ioDispatcher = Dispatchers.IO,
db = realSydneyTrainsStaticDb
)

val endTime = Instant.now()
val diff = ChronoUnit.SECONDS.between(startTime, endTime)
Timber.d("Time taken - $diff")

/*
val dataSize = realSydneyTrainsStaticDb.getStopTimes().size
Timber.d("DATA SIZE: $dataSize")
*/
/*
realSydneyTrainsStaticDb.insertStopTimes()
val x = realSydneyTrainsStaticDb.getStopTimes()
Timber.d("$x")
*/
}

setContent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package xyz.ksharma.krail.domain

import android.content.Context
import dagger.hilt.android.qualifiers.ApplicationContext
import timber.log.Timber
import xyz.ksharma.krail.domain.parser.AgencyParser.parseAgency
import xyz.ksharma.krail.model.sydneytrains.GTFSFeedFileNames
import xyz.ksharma.krail.utils.toPath
import javax.inject.Inject

/**
Expand Down Expand Up @@ -35,7 +31,7 @@ class DemoUseCaseImpl @Inject constructor(
//Timber.d("occupancyList: ${occupancyList.size}")
//val calendarList = context.toPath(GTFSFeedFileNames.CALENDAR.fileName).parseCalendar()
//Timber.d("calendarList: ${calendarList}")
val agencyList = context.toPath(GTFSFeedFileNames.AGENCY.fileName).parseAgency()
Timber.d("agencyList: $agencyList")
/* val agencyList = context.toPath(GTFSFeedFileNames.AGENCY.fileName).parseAgency()
Timber.d("agencyList: $agencyList")*/
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ import xyz.ksharma.krail.sydney.trains.database.StopTimes

interface SydneyTrainsStaticDB {

suspend fun insertStopTimes()
suspend fun insertStopTimes(
tripId: String,
arrivalTime: String,
departureTime: String,
stopId: String,
stopSequence: Int?,
stopHeadsign: String,
pickupType: Int?,
dropOffType: Int?
)

suspend fun clearStopTimes()

suspend fun getStopTimes(): List<StopTimes>

suspend fun insertStopTimesBatch(stopTimesList: List<StopTimes>)
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
CREATE TABLE stopTimes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
trip_id TEXT NOT NULL,
arrival_time TEXT,
departure_time TEXT,
stop_id TEXT,
stop_sequence INTEGER,
stop_headsign TEXT,
pickup_type INTEGER,
drop_off_type INTEGER
drop_off_type INTEGER,
PRIMARY KEY (trip_id, arrival_time, stop_id)
);

insertIntoStopTime:
INSERT INTO stopTimes (
INSERT OR IGNORE INTO stopTimes (
trip_id,
arrival_time,
departure_time,
Expand Down
3 changes: 2 additions & 1 deletion feature/sydney-trains/database/real/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ android {
dependencies {
implementation(projects.core.coroutinesExt)
implementation(projects.core.di)
implementation(projects.core.model)
implementation(projects.core.utils)
implementation(projects.krail.feature.sydneyTrains.database.api)

implementation(libs.sqlite.android.driver)
implementation(platform(libs.okhttp.bom))
implementation(libs.okhttp)
implementation(project(":core:utils"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import timber.log.Timber
import xyz.ksharma.krail.database.sydney.trains.database.api.SydneyTrainsStaticDB
import xyz.ksharma.krail.di.AppDispatchers
import xyz.ksharma.krail.di.Dispatcher
Expand Down Expand Up @@ -36,16 +37,25 @@ class RealSydneyTrainsStaticDb @Inject constructor(

private suspend fun getSydneyTrainsDb(): KrailDB = sydneyTrainsDB.await()

override suspend fun insertStopTimes() {
override suspend fun insertStopTimes(
tripId: String,
arrivalTime: String,
departureTime: String,
stopId: String,
stopSequence: Int?,
stopHeadsign: String,
pickupType: Int?,
dropOffType: Int?,
) {
getSydneyTrainsDb().stoptimesQueries.insertIntoStopTime(
trip_id = "id_1",
arrival_time = "arr",
departure_time = "depart",
stop_id = "",
stop_sequence = 1,
stop_headsign = "",
pickup_type = 1,
drop_off_type = 1,
trip_id = tripId,
arrival_time = arrivalTime,
departure_time = departureTime,
stop_id = stopId,
stop_sequence = stopSequence?.toLong(),
stop_headsign = stopHeadsign,
pickup_type = pickupType?.toLong(),
drop_off_type = dropOffType?.toLong(),
)
}

Expand All @@ -54,6 +64,26 @@ class RealSydneyTrainsStaticDb @Inject constructor(
return all.executeAsList()
}

override suspend fun insertStopTimesBatch(stopTimesList: List<StopTimes>) =
with(sydneyTrainsDB.await().stoptimesQueries) {
// Timber.d("insertStopTimesBatch: size:${stopTimesList.size} -> first(trip_id):${stopTimesList.first().trip_id}")

getSydneyTrainsDb().transaction {
stopTimesList.forEach { stopTime ->
insertIntoStopTime(
trip_id = stopTime.trip_id,
arrival_time = stopTime.arrival_time,
departure_time = stopTime.departure_time,
stop_id = stopTime.stop_id,
stop_sequence = stopTime.stop_sequence,
stop_headsign = stopTime.stop_headsign,
pickup_type = stopTime.pickup_type,
drop_off_type = stopTime.drop_off_type,
)
}
}
}

override suspend fun clearStopTimes() {
TODO("Not yet implemented")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import java.io.IOException
import okhttp3.Response
import timber.log.Timber
import xyz.ksharma.krail.coroutines.ext.safeResult
import xyz.ksharma.krail.database.sydney.trains.database.api.SydneyTrainsStaticDB
import xyz.ksharma.krail.model.sydneytrains.GTFSFeedFileNames
import xyz.ksharma.krail.utils.toPath
import java.io.File
import java.nio.file.Files
Expand All @@ -23,6 +25,7 @@ import javax.inject.Singleton
class RealZipCacheManager @Inject constructor(
@Dispatcher(AppDispatchers.IO) val ioDispatcher: CoroutineDispatcher,
@ApplicationContext val context: Context,
// val sydneyTrainsStaticDB : SydneyTrainsStaticDB
) : ZipEntryCacheManager {

override suspend fun cacheZipResponse(response: Response) = safeResult(ioDispatcher) {
Expand All @@ -35,50 +38,53 @@ class RealZipCacheManager @Inject constructor(
ZipInputStream(responseBody.byteStream()).use { inputStream ->
// List files in zip
var zipEntry = inputStream.nextEntry
Timber.d("zipEntry: $zipEntry")

while (zipEntry != null) {
val isDirectory = zipEntry.name.endsWith(File.separator)
val path: Path = context.toPath(zipEntry.name)

Timber.d("zipEntry: $zipEntry")

/*
if (zipEntry.name == GTFSFeedFileNames.STOP_TIMES.fileName) {
sydneyTrainsStaticDB.insertStopTimes()
}
inputStream.writeToCache(isDirectory, path)
*/

zipEntry = inputStream.nextEntry
}
inputStream.closeEntry()
}
response.close()
}.getOrElse { error ->
Timber.d("cacheZipResponse", error)
}.getOrElse { exception ->
Timber.e("Error while trying to cacheZipResponse", exception)
}
}


/**
* Extracts a ZIP entry to a specified cache path.
*
* If the entry is a directory, it creates the directory structure.
* If the entry is a file, it copies the file contents to the cache path.
*
* **Note:** If the target file already exists, it will be overwritten.
*
* ZipInputStream - The input stream containing the ZIP entry data.
*
* @param isDirectory Indicates whether the entry is a directory.
* @param path The target path in the cache directory.
*/
internal fun ZipInputStream.writeToCache(
isDirectory: Boolean,
path: Path,
) {
if (isDirectory) {
Files.createDirectories(path)
} else {
// Handle creation of parent directories
if (path.parent != null && Files.notExists(path.parent)) {
Files.createDirectories(path.parent)
/**
* Extracts a ZIP entry to a specified cache path.
*
* If the entry is a directory, it creates the directory structure.
* If the entry is a file, it copies the file contents to the cache path.
*
* **Note:** If the target file already exists, it will be overwritten.
*
* ZipInputStream - The input stream containing the ZIP entry data.
*
* @param isDirectory Indicates whether the entry is a directory.
* @param path The target path in the cache directory.
*/ // TODO - can be reusable across modules?
private fun ZipInputStream.writeToCache(
isDirectory: Boolean,
path: Path,
) {
if (isDirectory) {
Files.createDirectories(path)
} else {
// Handle creation of parent directories
if (path.parent != null && Files.notExists(path.parent)) {
Files.createDirectories(path.parent)
}
Files.copy(this, path, StandardCopyOption.REPLACE_EXISTING)
}
Files.copy(this, path, StandardCopyOption.REPLACE_EXISTING)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.ksharma.krail.domain.parser
package xyz.ksharma.krail.sydney.trains.database.real.parser

import timber.log.Timber
import xyz.ksharma.krail.model.gtfs_static.Agency
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.ksharma.krail.domain.parser
package xyz.ksharma.krail.sydney.trains.database.real.parser

import timber.log.Timber
import xyz.ksharma.krail.model.gtfs_static.Calendar
Expand Down
Loading

0 comments on commit 12f210a

Please sign in to comment.