Skip to content

Commit

Permalink
[#1440] Db image malformed while scanning blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
HonzaR committed Apr 17, 2024
1 parent 6a96fa3 commit 2c09776
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ package cash.z.ecc.android.sdk.internal.db
import android.database.sqlite.SQLiteDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.SupportSQLiteQueryBuilder
import kotlinx.coroutines.Dispatchers
import cash.z.ecc.android.sdk.internal.SdkDispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import java.util.Locale
import kotlin.coroutines.CoroutineContext

/**
* Performs a query on a background thread.
* Performs a query on a background thread using the dedicated [SdkDispatchers.DATABASE_IO] dispatcher.
*
* Note that this method is best for small queries, as Cursor has an in-memory window of cached data. If iterating
* through a large number of items that exceeds the window, the Cursor may perform additional IO.
Expand All @@ -28,7 +27,6 @@ internal fun <T> SQLiteDatabase.queryAndMap(
orderBy: String? = null,
limit: String? = null,
offset: String? = null,
coroutineContext: CoroutineContext = Dispatchers.IO,
cursorParser: CursorParser<T>
) = flow<T> {
// TODO [#703]: Support blobs for argument binding
Expand Down Expand Up @@ -64,10 +62,10 @@ internal fun <T> SQLiteDatabase.queryAndMap(
emit(cursorParser.newObject(it))
}
}
}.flowOn(coroutineContext)
}.flowOn(SdkDispatchers.DATABASE_IO)

/**
* Performs a query on a background thread.
* Performs a query on a background thread using the dedicated [SdkDispatchers.DATABASE_IO] dispatcher.
*
* Note that this method is best for small queries, as Cursor has an in-memory window of cached data. If iterating
* through a large number of items that exceeds the window, the Cursor may perform additional IO.
Expand All @@ -83,7 +81,6 @@ internal fun <T> SupportSQLiteDatabase.queryAndMap(
orderBy: String? = null,
limit: String? = null,
offset: String? = null,
coroutineContext: CoroutineContext = Dispatchers.IO,
cursorParser: CursorParser<T>
) = flow<T> {
val qb =
Expand Down Expand Up @@ -111,4 +108,4 @@ internal fun <T> SupportSQLiteDatabase.queryAndMap(
emit(cursorParser.newObject(it))
}
}
}.flowOn(coroutineContext)
}.flowOn(SdkDispatchers.DATABASE_IO)
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import cash.z.ecc.android.sdk.internal.model.EncodedTransaction
import cash.z.ecc.android.sdk.model.BlockHeight
import cash.z.ecc.android.sdk.model.FirstClassByteArray
import cash.z.ecc.android.sdk.model.ZcashNetwork
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.withContext
import java.util.Locale

internal class TransactionTable(
Expand Down Expand Up @@ -57,13 +55,11 @@ internal class TransactionTable(
}

suspend fun count() =
withContext(Dispatchers.IO) {
sqliteDatabase.queryAndMap(
table = TransactionTableDefinition.TABLE_NAME,
columns = PROJECTION_COUNT,
cursorParser = { it.getLong(0) }
).first()
}
sqliteDatabase.queryAndMap(
table = TransactionTableDefinition.TABLE_NAME,
columns = PROJECTION_COUNT,
cursorParser = { it.getLong(0) }
).first()

suspend fun countUnmined() =
sqliteDatabase.queryAndMap(
Expand Down

0 comments on commit 2c09776

Please sign in to comment.