@@ -38,8 +38,10 @@ internal class CacheDatastore(
38
38
}
39
39
40
40
private val dbGuard = Any ()
41
+
41
42
// note - guarded by dbHelperGuard
42
43
private var dbHelper: DbHelper ? = null
44
+
43
45
// note - guarded by dbHelperGuard
44
46
private var sqlDb: SQLiteDatabase ? = null
45
47
@@ -62,8 +64,6 @@ internal class CacheDatastore(
62
64
ensureDirs()
63
65
64
66
val newHelper = DbHelper (context.applicationContext, indexDbDir())
65
- // acquire an extra reference until closed. prevents DB underneath from closing/reopening
66
- // newHelper.writableDatabase.acquireReference()
67
67
this .dbHelper = newHelper
68
68
this .sqlDb = newHelper.writableDatabase
69
69
}
@@ -117,52 +117,50 @@ internal class CacheDatastore(
117
117
118
118
fun readRecordByLookupKey (key : String ): FileRecord ? {
119
119
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\t for url $url "
137
+ )
138
+ return databaseOrThrow().run {
139
+ query(
120
140
IndexSql .Files .name, null ,
121
141
" ${IndexSql .Files .Columns .lookupKey} is ?" ,
122
- arrayOf(key ),
142
+ arrayOf(safeCacheKey( URL (url)) ),
123
143
null , null , null
124
144
).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\t cursor closed ${cursor.isClosed} \n\t db closed ${! isOpen} "
152
+ )
125
153
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\t for $url "
158
+ )
159
+ }
127
160
} else {
128
161
null
129
162
}
130
- }
131
- }
132
-
133
- fun readRecordByUrl (url : String ): FileRecord ? {
134
- logger.i(" CacheInvest" , " readRecordByUrl() tid=${Thread .currentThread().id} called for datastore $this \n\t for 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\t cursor 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\t for $url "
156
- )
157
- }
158
- } else {
159
- null
160
- }
161
- }
162
163
}
163
- } catch (e: Exception ) {
164
- logger.e(" CacheInvest" , " rethrowing DB error from tid ${Thread .currentThread().id} for datastore $this " , e)
165
- throw e
166
164
}
167
165
}
168
166
@@ -205,7 +203,7 @@ internal class CacheDatastore(
205
203
206
204
@Throws(IOException ::class )
207
205
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 " )
209
207
synchronized(dbGuard) {
210
208
sqlDb?.close()
211
209
dbHelper?.close()
@@ -422,6 +420,7 @@ private class DbHelper(
422
420
) {
423
421
424
422
companion object {
423
+ private const val TAG = " CacheDatastore"
425
424
private const val DB_FILE = " mux-player-cache.db"
426
425
}
427
426
0 commit comments