Skip to content

Commit

Permalink
Bump deps and remove connection mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 17, 2024
1 parent 801bb9e commit 2260612
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 434 deletions.
2 changes: 1 addition & 1 deletion app/src/androidMain/kotlin/app/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import user.UsersModel
import user.UsersRepo

val appModule = module {
single { Database(get<Context>().getDatabasePath("btcmap-2024-05-15.db").absolutePath) }
single { Database(get<Context>().getDatabasePath("btcmap-2024-05-15.db").absolutePath).conn }

single { ApiImpl() }.bind(Api::class)

Expand Down
107 changes: 48 additions & 59 deletions app/src/androidMain/kotlin/area/AreaQueries.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package area

import androidx.sqlite.SQLiteConnection
import androidx.sqlite.use
import db.Database
import db.getInstant
import db.getJsonObject
import db.getZonedDateTime
import db.transaction
import java.time.ZonedDateTime

class AreaQueries(private val db: Database) {
class AreaQueries(private val conn: SQLiteConnection) {

companion object {
const val CREATE_TABLE = """
Expand All @@ -20,7 +21,7 @@ class AreaQueries(private val db: Database) {
}

fun insertOrReplace(areas: List<Area>) {
db.transaction { conn ->
conn.transaction { conn ->
areas.forEach { area ->
conn.prepare("INSERT OR REPLACE INTO area(id, tags, updated_at) VALUES(?1, ?2, ?3)")
.use {
Expand All @@ -34,65 +35,58 @@ class AreaQueries(private val db: Database) {
}

fun selectById(id: Long): Area? {
return db.withConn { conn ->
conn.prepare("SELECT id, tags, updated_at FROM area WHERE id = ?1").use {
it.bindLong(1, id)
return conn.prepare("SELECT id, tags, updated_at FROM area WHERE id = ?1").use {
it.bindLong(1, id)

if (it.step()) {
Area(
id = it.getLong(0),
tags = it.getJsonObject(1),
updatedAt = it.getInstant(2),
)
} else {
null
}
if (it.step()) {
Area(
id = it.getLong(0),
tags = it.getJsonObject(1),
updatedAt = it.getInstant(2),
)
} else {
null
}
}
}

fun selectByType(type: String): List<Area> {
return db.withConn { conn ->
conn.prepare(
"""
return conn.prepare(
"""
SELECT id, tags, updated_at
FROM area
WHERE json_extract(tags, '$.type') = ?1
"""
).use {
it.bindText(1, type)
).use {
it.bindText(1, type)

buildList {
while (it.step()) {
add(
Area(
id = it.getLong(0),
tags = it.getJsonObject(1),
updatedAt = it.getInstant(2),
)
buildList {
while (it.step()) {
add(
Area(
id = it.getLong(0),
tags = it.getJsonObject(1),
updatedAt = it.getInstant(2),
)
}
)
}
}
}
}

fun selectMaxUpdatedAt(): ZonedDateTime? {
return db.withConn { conn ->
conn.prepare("SELECT max(updated_at) FROM area").use {
if (it.step()) {
it.getZonedDateTime(0)
} else {
null
}
return conn.prepare("SELECT max(updated_at) FROM area").use {
if (it.step()) {
it.getZonedDateTime(0)
} else {
null
}
}
}

fun selectMeetups(): List<Meetup> {
return db.withConn { conn ->
conn.prepare(
"""
return conn.prepare(
"""
SELECT
json_extract(tags, '$.meetup_lat') AS lat,
json_extract(tags, '$.meetup_lon') AS lon,
Expand All @@ -102,37 +96,32 @@ class AreaQueries(private val db: Database) {
lat IS NOT NULL
AND lon IS NOT NULL
"""
).use {
buildList {
while (it.step()) {
add(
Meetup(
lat = it.getDouble(0),
lon = it.getDouble(1),
areaId = it.getLong(2),
)
).use {
buildList {
while (it.step()) {
add(
Meetup(
lat = it.getDouble(0),
lon = it.getDouble(1),
areaId = it.getLong(2),
)
}
)
}
}
}
}

fun selectCount(): Long {
return db.withConn { conn ->
conn.prepare("SELECT count(*) FROM area").use {
it.step()
it.getLong(0)
}
return conn.prepare("SELECT count(*) FROM area").use {
it.step()
it.getLong(0)
}
}

fun deleteById(id: Long) {
db.withConn { conn ->
conn.prepare("DELETE FROM area WHERE id = ?1").use {
it.bindLong(1, id)
it.step()
}
return conn.prepare("DELETE FROM area WHERE id = ?1").use {
it.bindLong(1, id)
it.step()
}
}
}
43 changes: 21 additions & 22 deletions app/src/androidMain/kotlin/conf/ConfQueries.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package conf

import androidx.sqlite.SQLiteConnection
import androidx.sqlite.execSQL
import androidx.sqlite.use
import db.Database
import db.getZonedDateTime
import db.transaction

class ConfQueries(private val db: Database) {
class ConfQueries(private val conn: SQLiteConnection) {

companion object {
const val CREATE_TABLE = """
Expand All @@ -24,7 +25,7 @@ class ConfQueries(private val db: Database) {
}

fun insertOrReplace(conf: Conf) {
db.transaction { conn ->
conn.transaction { conn ->
conn.execSQL("DELETE FROM conf")

conn.prepare(
Expand Down Expand Up @@ -58,9 +59,8 @@ class ConfQueries(private val db: Database) {
}

fun select(): Conf? {
return db.withConn { conn ->
conn.prepare(
"""
return conn.prepare(
"""
SELECT
last_sync_date,
viewport_north_lat,
Expand All @@ -73,22 +73,21 @@ class ConfQueries(private val db: Database) {
show_all_new_elements
FROM conf
"""
).use {
if (it.step()) {
Conf(
lastSyncDate = it.getZonedDateTime(0),
viewportNorthLat = it.getDouble(1),
viewportEastLon = it.getDouble(2),
viewportSouthLat = it.getDouble(3),
viewportWestLon = it.getDouble(4),
showAtms = it.getBoolean(5),
showOsmAttribution = it.getBoolean(6),
showSyncSummary = it.getBoolean(7),
showAllNewElements = it.getBoolean(8),
)
} else {
null
}
).use {
if (it.step()) {
Conf(
lastSyncDate = it.getZonedDateTime(0),
viewportNorthLat = it.getDouble(1),
viewportEastLon = it.getDouble(2),
viewportSouthLat = it.getDouble(3),
viewportWestLon = it.getDouble(4),
showAtms = it.getBoolean(5),
showOsmAttribution = it.getBoolean(6),
showSyncSummary = it.getBoolean(7),
showAllNewElements = it.getBoolean(8),
)
} else {
null
}
}
}
Expand Down
33 changes: 9 additions & 24 deletions app/src/androidMain/kotlin/db/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ import kotlinx.coroutines.flow.MutableStateFlow
import reports.ReportQueries
import user.UserQueries
import java.time.LocalDateTime
import java.util.concurrent.locks.ReentrantLock

class Database(path: String) {

private val conn: SQLiteConnection =
val conn: SQLiteConnection =
BundledSQLiteDriver().open(
path,
SQLITE_OPEN_READWRITE or SQLITE_OPEN_CREATE or SQLITE_OPEN_FULLMUTEX
Expand All @@ -46,29 +44,16 @@ class Database(path: String) {
execSQL("PRAGMA user_version=1")
}
}
}

private val connLock = ReentrantLock()

fun <T> withConn(action: (conn: SQLiteConnection) -> T): T {
connLock.lock()
return try {
action(conn)
} finally {
connLock.unlock()
}
}

fun <T> transaction(action: (conn: SQLiteConnection) -> T) {
return withConn { conn ->
conn.execSQL("BEGIN TRANSACTION")
fun <T> SQLiteConnection.transaction(action: (conn: SQLiteConnection) -> T) {
this.execSQL("BEGIN TRANSACTION")

try {
action(conn)
conn.execSQL("END TRANSACTION")
} catch (t: Throwable) {
conn.execSQL("ROLLBACK TRANSACTION")
}
}
try {
action(this)
this.execSQL("END TRANSACTION")
} catch (t: Throwable) {
this.execSQL("ROLLBACK TRANSACTION")
}
}

Expand Down
Loading

0 comments on commit 2260612

Please sign in to comment.