Skip to content

Commit 155fa7a

Browse files
committed
one more cleanup
1 parent 4c338d4 commit 155fa7a

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

library/src/main/java/com/mux/player/internal/cache/CacheDatastore.kt

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ internal class CacheDatastore(
3838
}
3939

4040
private val dbGuard = Any()
41+
4142
// note - guarded by dbHelperGuard
4243
private var dbHelper: DbHelper? = null
44+
4345
// note - guarded by dbHelperGuard
4446
private var sqlDb: SQLiteDatabase? = null
4547

@@ -62,8 +64,6 @@ internal class CacheDatastore(
6264
ensureDirs()
6365

6466
val newHelper = DbHelper(context.applicationContext, indexDbDir())
65-
// acquire an extra reference until closed. prevents DB underneath from closing/reopening
66-
//newHelper.writableDatabase.acquireReference()
6767
this.dbHelper = newHelper
6868
this.sqlDb = newHelper.writableDatabase
6969
}
@@ -117,52 +117,50 @@ internal class CacheDatastore(
117117

118118
fun readRecordByLookupKey(key: String): FileRecord? {
119119
return databaseOrThrow().query(
120+
IndexSql.Files.name, null,
121+
"${IndexSql.Files.Columns.lookupKey} is ?",
122+
arrayOf(key),
123+
null, null, null
124+
).use { cursor ->
125+
if (cursor.count > 0 && cursor.moveToFirst()) {
126+
cursor.toFileRecord()
127+
} else {
128+
null
129+
}
130+
}
131+
}
132+
133+
fun readRecordByUrl(url: String): FileRecord? {
134+
logger.i(
135+
TAG,
136+
"readRecordByUrl() tid=${Thread.currentThread().id} called for datastore $this\n\tfor url $url"
137+
)
138+
return databaseOrThrow().run {
139+
query(
120140
IndexSql.Files.name, null,
121141
"${IndexSql.Files.Columns.lookupKey} is ?",
122-
arrayOf(key),
142+
arrayOf(safeCacheKey(URL(url))),
123143
null, null, null
124144
).use { cursor ->
145+
logger.d(
146+
TAG,
147+
"readRecordByUrl() tid=${Thread.currentThread().id} about to talk to the cursor $this\n\t btw is it closed according to Cursor? ${cursor.isClosed}"
148+
)
149+
logger.i(
150+
TAG,
151+
"readRecordByUrl() tid=${Thread.currentThread().id} \n\tcursor closed ${cursor.isClosed}\n\t db closed ${!isOpen}"
152+
)
125153
if (cursor.count > 0 && cursor.moveToFirst()) {
126-
cursor.toFileRecord()
154+
cursor.toFileRecord().also {
155+
logger.i(
156+
TAG,
157+
"readRecordByUrl() tid=${Thread.currentThread().id} returning with record\n\tfor $url"
158+
)
159+
}
127160
} else {
128161
null
129162
}
130-
}
131-
}
132-
133-
fun readRecordByUrl(url: String): FileRecord? {
134-
logger.i("CacheInvest", "readRecordByUrl() tid=${Thread.currentThread().id} called for datastore $this\n\tfor url $url")
135-
try {
136-
return databaseOrThrow().run {
137-
query(
138-
IndexSql.Files.name, null,
139-
"${IndexSql.Files.Columns.lookupKey} is ?",
140-
arrayOf(safeCacheKey(URL(url))),
141-
null, null, null
142-
).use { cursor ->
143-
logger.d(
144-
"CacheInvest",
145-
"readRecordByUrl() tid=${Thread.currentThread().id} about to talk to the cursor $this\n\t btw is it closed according to Cursor? ${cursor.isClosed}"
146-
)
147-
logger.i(
148-
"CacheInvest",
149-
"readRecordByUrl() tid=${Thread.currentThread().id} \n\tcursor closed ${cursor.isClosed}\n\t db closed ${!isOpen}"
150-
)
151-
if (cursor.count > 0 && cursor.moveToFirst()) {
152-
cursor.toFileRecord().also {
153-
logger.i(
154-
"CacheInvest",
155-
"readRecordByUrl() tid=${Thread.currentThread().id} returning with record\n\tfor $url"
156-
)
157-
}
158-
} else {
159-
null
160-
}
161-
}
162163
}
163-
} catch (e: Exception) {
164-
logger.e("CacheInvest", "rethrowing DB error from tid ${Thread.currentThread().id} for datastore $this", e)
165-
throw e
166164
}
167165
}
168166

@@ -205,7 +203,7 @@ internal class CacheDatastore(
205203

206204
@Throws(IOException::class)
207205
override fun close() {
208-
logger.i("CacheInvest", "close() tid=${Thread.currentThread().id} called for datastore $this")
206+
logger.i(TAG, "close() tid=${Thread.currentThread().id} called for datastore $this")
209207
synchronized(dbGuard) {
210208
sqlDb?.close()
211209
dbHelper?.close()
@@ -422,6 +420,7 @@ private class DbHelper(
422420
) {
423421

424422
companion object {
423+
private const val TAG = "CacheDatastore"
425424
private const val DB_FILE = "mux-player-cache.db"
426425
}
427426

0 commit comments

Comments
 (0)