Database bug fix (#5902)

* make database function calls suspending and update room version

* replace MainScope with coroutineScope for database operations

* add suspend keyword and refactor code
This commit is contained in:
Rohit Verma 2024-10-27 19:08:40 +05:30 committed by GitHub
parent bc065c8792
commit cdc4f89da5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 28 additions and 34 deletions

View file

@ -15,19 +15,19 @@ abstract class NotForUploadStatusDao {
* Insert into Not For Upload status. * Insert into Not For Upload status.
*/ */
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun insert(notForUploadStatus: NotForUploadStatus) abstract suspend fun insert(notForUploadStatus: NotForUploadStatus)
/** /**
* Delete Not For Upload status entry. * Delete Not For Upload status entry.
*/ */
@Delete @Delete
abstract fun delete(notForUploadStatus: NotForUploadStatus) abstract suspend fun delete(notForUploadStatus: NotForUploadStatus)
/** /**
* Query Not For Upload status with image sha1. * Query Not For Upload status with image sha1.
*/ */
@Query("SELECT * FROM images_not_for_upload_table WHERE imageSHA1 = (:imageSHA1) ") @Query("SELECT * FROM images_not_for_upload_table WHERE imageSHA1 = (:imageSHA1) ")
abstract fun getFromImageSHA1(imageSHA1: String): NotForUploadStatus? abstract suspend fun getFromImageSHA1(imageSHA1: String): NotForUploadStatus?
/** /**
* Asynchronous image sha1 query. * Asynchronous image sha1 query.
@ -38,7 +38,7 @@ abstract class NotForUploadStatusDao {
* Deletion Not For Upload status with image sha1. * Deletion Not For Upload status with image sha1.
*/ */
@Query("DELETE FROM images_not_for_upload_table WHERE imageSHA1 = (:imageSHA1) ") @Query("DELETE FROM images_not_for_upload_table WHERE imageSHA1 = (:imageSHA1) ")
abstract fun deleteWithImageSHA1(imageSHA1: String) abstract suspend fun deleteWithImageSHA1(imageSHA1: String)
/** /**
* Asynchronous image sha1 deletion. * Asynchronous image sha1 deletion.
@ -49,5 +49,5 @@ abstract class NotForUploadStatusDao {
* Check whether the imageSHA1 is present in database * Check whether the imageSHA1 is present in database
*/ */
@Query("SELECT COUNT() FROM images_not_for_upload_table WHERE imageSHA1 = (:imageSHA1) ") @Query("SELECT COUNT() FROM images_not_for_upload_table WHERE imageSHA1 = (:imageSHA1) ")
abstract fun find(imageSHA1: String): Int abstract suspend fun find(imageSHA1: String): Int
} }

View file

@ -17,31 +17,31 @@ abstract class UploadedStatusDao {
* Insert into uploaded status. * Insert into uploaded status.
*/ */
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun insert(uploadedStatus: UploadedStatus) abstract suspend fun insert(uploadedStatus: UploadedStatus)
/** /**
* Update uploaded status entry. * Update uploaded status entry.
*/ */
@Update @Update
abstract fun update(uploadedStatus: UploadedStatus) abstract suspend fun update(uploadedStatus: UploadedStatus)
/** /**
* Delete uploaded status entry. * Delete uploaded status entry.
*/ */
@Delete @Delete
abstract fun delete(uploadedStatus: UploadedStatus) abstract suspend fun delete(uploadedStatus: UploadedStatus)
/** /**
* Query uploaded status with image sha1. * Query uploaded status with image sha1.
*/ */
@Query("SELECT * FROM uploaded_table WHERE imageSHA1 = (:imageSHA1) ") @Query("SELECT * FROM uploaded_table WHERE imageSHA1 = (:imageSHA1) ")
abstract fun getFromImageSHA1(imageSHA1: String): UploadedStatus? abstract suspend fun getFromImageSHA1(imageSHA1: String): UploadedStatus?
/** /**
* Query uploaded status with modified image sha1. * Query uploaded status with modified image sha1.
*/ */
@Query("SELECT * FROM uploaded_table WHERE modifiedImageSHA1 = (:modifiedImageSHA1) ") @Query("SELECT * FROM uploaded_table WHERE modifiedImageSHA1 = (:modifiedImageSHA1) ")
abstract fun getFromModifiedImageSHA1(modifiedImageSHA1: String): UploadedStatus? abstract suspend fun getFromModifiedImageSHA1(modifiedImageSHA1: String): UploadedStatus?
/** /**
* Asynchronous insert into uploaded status table. * Asynchronous insert into uploaded status table.
@ -55,7 +55,7 @@ abstract class UploadedStatusDao {
* Check whether the imageSHA1 is present in database * Check whether the imageSHA1 is present in database
*/ */
@Query("SELECT COUNT() FROM uploaded_table WHERE imageSHA1 = (:imageSHA1) AND imageResult = (:imageResult) ") @Query("SELECT COUNT() FROM uploaded_table WHERE imageSHA1 = (:imageSHA1) AND imageResult = (:imageResult) ")
abstract fun findByImageSHA1( abstract suspend fun findByImageSHA1(
imageSHA1: String, imageSHA1: String,
imageResult: Boolean, imageResult: Boolean,
): Int ): Int
@ -66,7 +66,7 @@ abstract class UploadedStatusDao {
@Query( @Query(
"SELECT COUNT() FROM uploaded_table WHERE modifiedImageSHA1 = (:modifiedImageSHA1) AND modifiedImageResult = (:modifiedImageResult) ", "SELECT COUNT() FROM uploaded_table WHERE modifiedImageSHA1 = (:modifiedImageSHA1) AND modifiedImageResult = (:modifiedImageResult) ",
) )
abstract fun findByModifiedImageSHA1( abstract suspend fun findByModifiedImageSHA1(
modifiedImageSHA1: String, modifiedImageSHA1: String,
modifiedImageResult: Boolean, modifiedImageResult: Boolean,
): Int ): Int

View file

@ -17,7 +17,7 @@ import fr.free.nrw.commons.utils.CustomSelectorUtils
import fr.free.nrw.commons.utils.CustomSelectorUtils.Companion.checkWhetherFileExistsOnCommonsUsingSHA1 import fr.free.nrw.commons.utils.CustomSelectorUtils.Companion.checkWhetherFileExistsOnCommonsUsingSHA1
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.util.Calendar import java.util.Calendar
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -65,7 +65,7 @@ class ImageLoader
/** /**
* Coroutine Scope. * Coroutine Scope.
*/ */
private val scope: CoroutineScope = MainScope() private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO)
/** /**
* Query image and setUp the view. * Query image and setUp the view.

View file

@ -37,15 +37,11 @@ public abstract class PlaceDao {
*/ */
public Completable save(final Place place) { public Completable save(final Place place) {
return Completable return Completable
.fromAction(() -> { .fromAction(() -> saveSynchronous(place));
saveSynchronous(place);
});
} }
/** /**
* Deletes all Place objects from the database. * Deletes all Place objects from the database.
*
* @return A Completable that completes once the deletion operation is done.
*/ */
@Query("DELETE FROM place") @Query("DELETE FROM place")
public abstract void deleteAllSynchronous(); public abstract void deleteAllSynchronous();
@ -53,11 +49,9 @@ public abstract class PlaceDao {
/** /**
* Deletes all Place objects from the database. * Deletes all Place objects from the database.
* *
* @return A Completable that completes once the deletion operation is done.
*/ */
public Completable deleteAll() { public Completable deleteAll() {
return Completable return Completable.fromAction(this::deleteAllSynchronous);
.fromAction(() -> {
deleteAllSynchronous();
});
} }
} }

View file

@ -22,21 +22,21 @@ abstract class DepictsDao {
private val maxItemsAllowed = 10 private val maxItemsAllowed = 10
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun insert(depictedItem: Depicts) abstract suspend fun insert(depictedItem: Depicts)
@Query("Select * From depicts_table order by lastUsed DESC") @Query("Select * From depicts_table order by lastUsed DESC")
abstract fun getAllDepicts(): List<Depicts> abstract suspend fun getAllDepicts(): List<Depicts>
@Query("Select * From depicts_table order by lastUsed DESC LIMIT :n OFFSET 10") @Query("Select * From depicts_table order by lastUsed DESC LIMIT :n OFFSET 10")
abstract fun getDepictsForDeletion(n: Int): List<Depicts> abstract suspend fun getDepictsForDeletion(n: Int): List<Depicts>
@Delete @Delete
abstract fun delete(depicts: Depicts) abstract suspend fun delete(depicts: Depicts)
/** /**
* Gets all Depicts objects from the database, ordered by lastUsed in descending order. * Gets all Depicts objects from the database, ordered by lastUsed in descending order.
* *
* @return A list of Depicts objects. * @return Deferred list of Depicts objects.
*/ */
fun depictsList(): Deferred<List<Depicts>> = fun depictsList(): Deferred<List<Depicts>> =
CoroutineScope(Dispatchers.IO).async { CoroutineScope(Dispatchers.IO).async {
@ -48,7 +48,7 @@ abstract class DepictsDao {
* *
* @param depictedItem The Depicts object to insert. * @param depictedItem The Depicts object to insert.
*/ */
private fun insertDepict(depictedItem: Depicts) = fun insertDepict(depictedItem: Depicts) =
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
insert(depictedItem) insert(depictedItem)
} }
@ -59,7 +59,7 @@ abstract class DepictsDao {
* @param n The number of depicts to delete. * @param n The number of depicts to delete.
* @return A list of Depicts objects to delete. * @return A list of Depicts objects to delete.
*/ */
private suspend fun depictsForDeletion(n: Int): Deferred<List<Depicts>> = fun depictsForDeletion(n: Int): Deferred<List<Depicts>> =
CoroutineScope(Dispatchers.IO).async { CoroutineScope(Dispatchers.IO).async {
getDepictsForDeletion(n) getDepictsForDeletion(n)
} }
@ -69,7 +69,7 @@ abstract class DepictsDao {
* *
* @param depicts The Depicts object to delete. * @param depicts The Depicts object to delete.
*/ */
private suspend fun deleteDepicts(depicts: Depicts) = fun deleteDepicts(depicts: Depicts) =
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
delete(depicts) delete(depicts)
} }

View file

@ -41,8 +41,8 @@ import fr.free.nrw.commons.upload.UploadProgressActivity
import fr.free.nrw.commons.upload.UploadResult import fr.free.nrw.commons.upload.UploadResult
import fr.free.nrw.commons.wikidata.WikidataEditService import fr.free.nrw.commons.wikidata.WikidataEditService
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
@ -534,7 +534,7 @@ class UploadWorker(
contribution.contentUri?.let { contribution.contentUri?.let {
val imageSha1 = contribution.imageSHA1.toString() val imageSha1 = contribution.imageSHA1.toString()
val modifiedSha1 = fileUtilsWrapper.getSHA1(fileUtilsWrapper.getFileInputStream(contribution.localUri?.path)) val modifiedSha1 = fileUtilsWrapper.getSHA1(fileUtilsWrapper.getFileInputStream(contribution.localUri?.path))
MainScope().launch { CoroutineScope(Dispatchers.IO).launch {
uploadedStatusDao.insertUploaded( uploadedStatusDao.insertUploaded(
UploadedStatus( UploadedStatus(
imageSha1, imageSha1,

View file

@ -20,7 +20,7 @@ android.enableR8.fullMode=false
KOTLIN_VERSION=1.9.22 KOTLIN_VERSION=1.9.22
LEAK_CANARY_VERSION=2.10 LEAK_CANARY_VERSION=2.10
DAGGER_VERSION=2.23 DAGGER_VERSION=2.23
ROOM_VERSION=2.5.0 ROOM_VERSION=2.6.1
PREFERENCE_VERSION=1.1.0 PREFERENCE_VERSION=1.1.0
CORE_KTX_VERSION=1.9.0 CORE_KTX_VERSION=1.9.0
ADAPTER_DELEGATES_VERSION=4.3.0 ADAPTER_DELEGATES_VERSION=4.3.0