-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generic Storage interface for use on all client and on the server. (#834
) Signed-off-by: Peter Sorotokin <[email protected]>
- Loading branch information
Showing
29 changed files
with
2,542 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...rc/androidInstrumentedTest/kotlin/com/android/identity/storage/testStorageList.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.android.identity.storage | ||
|
||
import android.app.Instrumentation | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.android.identity.storage.android.AndroidStorage | ||
import com.android.identity.storage.ephemeral.EphemeralStorage | ||
import kotlinx.datetime.Clock | ||
import java.io.File | ||
|
||
/** | ||
* Creates a list of empty [Storage] objects for testing. | ||
*/ | ||
actual fun createTransientStorageList(testClock: Clock): List<Storage> { | ||
return listOf<Storage>( | ||
EphemeralStorage(testClock), | ||
/* | ||
TODO: this can be enabled once SqliteStorage is moved into commonMain | ||
com.android.identity.storage.sqlite.SqliteStorage( | ||
connection = AndroidSQLiteDriver().open(":memory:"), | ||
clock = testClock | ||
), | ||
com.android.identity.storage.sqlite.SqliteStorage( | ||
connection = BundledSQLiteDriver().open(":memory:"), | ||
clock = testClock, | ||
// bundled sqlite crashes when used with Dispatchers.IO | ||
coroutineContext = newSingleThreadContext("DB") | ||
), | ||
*/ | ||
AndroidStorage( | ||
databasePath = null, | ||
clock = testClock, | ||
keySize = 3 | ||
) | ||
) | ||
} | ||
|
||
val knownNames = mutableSetOf<String>() | ||
|
||
actual fun createPersistentStorage(name: String, testClock: Clock): Storage? { | ||
val context = InstrumentationRegistry.getInstrumentation().context | ||
val dbFile = context.getDatabasePath("$name.db") | ||
if (knownNames.add(name)) { | ||
dbFile.delete() | ||
} | ||
return AndroidStorage( | ||
databasePath = dbFile.absolutePath, | ||
clock = testClock, | ||
keySize = 3 | ||
) | ||
} |
64 changes: 64 additions & 0 deletions
64
identity/src/androidMain/kotlin/com/android/identity/storage/android/AndroidStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.android.identity.storage.android | ||
|
||
import android.database.sqlite.SQLiteDatabase | ||
import com.android.identity.storage.Storage | ||
import com.android.identity.storage.base.BaseStorage | ||
import com.android.identity.storage.base.BaseStorageTable | ||
import com.android.identity.storage.StorageTableSpec | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import kotlinx.datetime.Clock | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
/** | ||
* [Storage] implementation based on Android [SQLiteDatabase] API. | ||
*/ | ||
class AndroidStorage: BaseStorage { | ||
private val coroutineContext: CoroutineContext | ||
private val databaseFactory: () -> SQLiteDatabase | ||
internal val keySize: Int | ||
private var database: SQLiteDatabase? = null | ||
|
||
constructor( | ||
database: SQLiteDatabase, | ||
clock: Clock, | ||
coroutineContext: CoroutineContext = Dispatchers.IO, | ||
keySize: Int = 9 | ||
): super(clock) { | ||
this.database = database | ||
databaseFactory = { throw IllegalStateException("unexpected call") } | ||
this.coroutineContext = coroutineContext | ||
this.keySize = keySize | ||
} | ||
|
||
constructor( | ||
databasePath: String?, | ||
clock: Clock, | ||
coroutineContext: CoroutineContext = Dispatchers.IO, | ||
keySize: Int = 9 | ||
): super(clock) { | ||
databaseFactory = { | ||
SQLiteDatabase.openOrCreateDatabase(databasePath ?: ":memory:", null) | ||
} | ||
this.coroutineContext = coroutineContext | ||
this.keySize = keySize | ||
} | ||
|
||
override suspend fun createTable(tableSpec: StorageTableSpec): BaseStorageTable { | ||
if (database == null) { | ||
database = databaseFactory() | ||
} | ||
val table = AndroidStorageTable(this, tableSpec) | ||
table.init() | ||
return table | ||
} | ||
|
||
internal suspend fun<T> withDatabase( | ||
block: suspend CoroutineScope.(database: SQLiteDatabase) -> T | ||
): T { | ||
return CoroutineScope(coroutineContext).async { | ||
block(database!!) | ||
}.await() | ||
} | ||
} |
Oops, something went wrong.