-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic Storage interface for use on all client and on the server. #834
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use iosMain, iosTest instead of appleMain, appleTest? I think that's more correct since we only target iOS not MacOS...