Colton Idle
09/07/2023, 6:02 PMColton Idle
09/07/2023, 6:05 PMsuspend fun insertAllNames(list: List<String>) =
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
list.forEach {
personQueries.insert(it)
}
}
jw
09/07/2023, 6:08 PMhfhbd
09/07/2023, 6:10 PMhfhbd
09/07/2023, 6:11 PMColton Idle
09/07/2023, 6:20 PMhfhbd
09/07/2023, 6:21 PMColton Idle
09/07/2023, 6:59 PMColton Idle
09/07/2023, 7:21 PMhfhbd
09/07/2023, 7:21 PMhfhbd
09/07/2023, 7:23 PMColton Idle
09/07/2023, 7:29 PMmyanmarking
09/07/2023, 8:01 PMkevin.cianfarini
09/07/2023, 9:12 PMjw
09/07/2023, 9:17 PMColton Idle
09/08/2023, 4:33 AMColton Idle
09/08/2023, 5:01 AMColton Idle
09/13/2023, 10:23 AMclass MyUseCase @Inject constructor(private val persistence: PersistenceLayer) {
suspend fun sync() = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
val client = OkHttpClient()
val request = Request.Builder()
.url("<https://example.com/150k_records.CSV>")
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) throw IOException("Unexpected code $response")
val bufferedReader = BufferedReader(StringReader(response.body!!.string()))
persistence.deleteAll()
var currentLine: String?
Log.e("TIMER", "START inserting")
persistence.myQueries.transaction {
while (bufferedReader.readLine().also { currentLine = it } != null) {
persistence.myQueries.insert(currentLine)
}
}
Log.e("TIMER", "STOP inserting")
}
}
}
The only thing I'm really not happy with is that my persistenceLayer (which is supposed to abstract away SQLDelight) had to now expose myQueries
so that I could wrap the insert in a transaction.jw
09/13/2023, 10:54 AMhfhbd
09/13/2023, 10:58 AMPersistentLayer
to inherit Transacter
(and implement it using delegation of schema.myQueries
).jw
09/13/2023, 10:58 AMjw
09/13/2023, 10:59 AMjw
09/13/2023, 11:00 AMColton Idle
09/13/2023, 11:11 AMclient.dispatcher().executorService().shutdown()
to shutdown
Not sure how to read line by line if I don't put it into a string, but lemme look it up.Colton Idle
09/13/2023, 11:43 AMval bufferedReader = BufferedReader(InputStreamReader(response.body!!.byteStream()))
then creating the buffer is instant, but inserting takes a bit longer at like ~6 seconds. Overall the time saved by not converting to a string first seems worth it.
Curious if there are any other optimizations there, but that seems pretty good.jw
09/13/2023, 11:44 AMjw
09/13/2023, 11:45 AMColton Idle
09/13/2023, 12:59 PMjw
09/13/2023, 12:59 PMjw
09/13/2023, 1:00 PMColton Idle
09/13/2023, 1:00 PMColton Idle
09/13/2023, 1:01 PMjw
09/13/2023, 1:01 PMColton Idle
09/13/2023, 1:02 PMjw
09/13/2023, 1:04 PMsource().skip(Long.MAX_VALUE)
which will discard the bytes without any additional processing or copiesColton Idle
09/13/2023, 4:19 PMColton Idle
09/13/2023, 4:27 PMColton Idle
09/16/2023, 1:31 AMresponse.body?.source()?.skip(Long.MAX_VALUE)
gives an EOF exception. Am I good with just catching that if i wanna do some really crude "benchmarking" or would you recommend some other technique?