Change database checkpoint procedure (#406)

This commit is contained in:
vfsfitvnm 2022-10-11 14:11:48 +02:00
parent cfe5dc965f
commit 5e5868bd3c
2 changed files with 14 additions and 23 deletions

View file

@ -2,7 +2,6 @@ package it.vfsfitvnm.vimusic
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase.CONFLICT_IGNORE
import android.os.Parcel
import androidx.core.database.getFloatOrNull
@ -15,6 +14,7 @@ import androidx.room.DeleteTable
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.RawQuery
import androidx.room.RenameColumn
import androidx.room.RenameTable
import androidx.room.RewriteQueriesToDropUnusedColumns
@ -29,6 +29,7 @@ import androidx.room.migration.AutoMigrationSpec
import androidx.room.migration.Migration
import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.SupportSQLiteQuery
import it.vfsfitvnm.vimusic.enums.AlbumSortBy
import it.vfsfitvnm.vimusic.enums.ArtistSortBy
import it.vfsfitvnm.vimusic.enums.PlaylistSortBy
@ -410,6 +411,13 @@ interface Database {
@Delete
fun delete(songPlaylistMap: SongPlaylistMap)
@RawQuery
fun raw(supportSQLiteQuery: SupportSQLiteQuery): Int
fun checkpoint() {
raw(SimpleSQLiteQuery("PRAGMA wal_checkpoint(FULL)"))
}
}
@androidx.room.Database(
@ -635,19 +643,3 @@ fun transaction(block: () -> Unit) = with(DatabaseInitializer.Instance) {
val RoomDatabase.path: String?
get() = openHelper.writableDatabase.path
fun RoomDatabase.checkpoint() {
openHelper.writableDatabase.run {
query("PRAGMA journal_mode").use { cursor ->
if (cursor.moveToFirst()) {
when (cursor.getString(0).lowercase()) {
"wal" -> {
query("PRAGMA wal_checkpoint").use(Cursor::moveToFirst)
query("PRAGMA wal_checkpoint(TRUNCATE)").use(Cursor::moveToFirst)
query("PRAGMA wal_checkpoint").use(Cursor::moveToFirst)
}
}
}
}
}
}

View file

@ -6,7 +6,10 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
@ -17,10 +20,6 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import it.vfsfitvnm.vimusic.Database
import it.vfsfitvnm.vimusic.LocalPlayerAwareWindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.only
import it.vfsfitvnm.vimusic.checkpoint
import it.vfsfitvnm.vimusic.internal
import it.vfsfitvnm.vimusic.path
import it.vfsfitvnm.vimusic.query
@ -56,7 +55,7 @@ fun DatabaseSettings() {
if (uri == null) return@rememberLauncherForActivityResult
query {
Database.internal.checkpoint()
Database.checkpoint()
context.applicationContext.contentResolver.openOutputStream(uri)
?.use { outputStream ->
@ -72,7 +71,7 @@ fun DatabaseSettings() {
if (uri == null) return@rememberLauncherForActivityResult
query {
Database.internal.checkpoint()
Database.checkpoint()
Database.internal.close()
context.applicationContext.contentResolver.openInputStream(uri)