Skip to content
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

Remove storage_key_reduction flag (default to on). #7157

Merged
merged 1 commit into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions java/arcs/android/storage/database/DatabaseImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ import arcs.flags.BuildFlagDisabledError
import arcs.flags.BuildFlags
import arcs.jvm.util.JvmTime
import kotlin.coroutines.coroutineContext
import kotlin.math.roundToLong
import kotlin.reflect.KClass
import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.updateAndGet
Expand Down Expand Up @@ -2412,11 +2411,7 @@ class DatabaseImpl(

companion object {
@VisibleForTesting
val DB_VERSION get() = when {
BuildFlags.STORAGE_KEY_REDUCTION -> 9
else -> 8
}

val DB_VERSION = 9
// Crdt actor used for edits applied by this class.
const val DATABASE_CRDT_ACTOR = "db"

Expand Down Expand Up @@ -2907,27 +2902,12 @@ class DatabaseImpl(
* A unique component to the key. This is required because there may be multiple inline
* entities stored against a single fieldName (for collections and lists).
*/
private val unique: String = if (BuildFlags.STORAGE_KEY_REDUCTION) {
Random.nextVersionMapSafeString(10)
} else {
(Math.random() * Long.MAX_VALUE).roundToLong().toString()
}
private val unique: String = Random.nextVersionMapSafeString(10)

override fun toKeyString(): String {
return if (BuildFlags.STORAGE_KEY_REDUCTION) {
"{${parentKey.embed()}}$unique"
} else {
"{${parentKey.embed()}}!$unique/$fieldName"
}
}
override fun toKeyString() = "{${parentKey.embed()}}$unique"

override fun newKeyWithComponent(component: String): StorageKey {
val newComponent = if (BuildFlags.STORAGE_KEY_REDUCTION) {
component
} else {
"$fieldName/$component"
}
return InlineStorageKey(parentKey, newComponent)
return InlineStorageKey(parentKey, component)
}

companion object {
Expand Down
10 changes: 2 additions & 8 deletions java/arcs/core/storage/CapabilitiesResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import arcs.core.data.ReferenceType
import arcs.core.data.toSchema
import arcs.core.storage.referencemode.ReferenceModeStorageKey
import arcs.core.type.Type
import arcs.flags.BuildFlags

/**
* [CapabilitiesResolver] is a factory class that creates [StorageKey]s based on the registered
Expand Down Expand Up @@ -56,13 +55,8 @@ class CapabilitiesResolver(
val containerKey = factory.create(
StorageKeyFactory.ContainerStorageKeyOptions(options.arcId, type.toSchema())
)
val containerChildKey = if (BuildFlags.STORAGE_KEY_REDUCTION) {
// The ArcId needs to be encoded in the new storage key.
containerKey.newKeyWithComponent("${options.arcId}/handle/$handleId")
} else {
// The ArcId is encoded in containerKey and will appear in the child key.
containerKey.newKeyWithComponent("handle/$handleId")
}
// The ArcId needs to be encoded in the new storage key.
val containerChildKey = containerKey.newKeyWithComponent("${options.arcId}/handle/$handleId")
if (type is ReferenceType<*>) {
return containerChildKey
}
Expand Down
5 changes: 0 additions & 5 deletions java/arcs/core/storage/StorageKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ abstract class StorageKey(val protocol: StorageKeyProtocol) {
* Callers must ensure that the component is unique (or sufficiently random) if they want the
* returned storage key to be unique. Other, non-component, properties of the [StorageKey] will be
* preserved (if any exist for the relevant [StorageKey] subclass).
*
* If the [arcs.flags.BuildFlags.STORAGE_KEY_REDUCTION] flag is disabled, the new [StorageKey]
* that is created will be a "child" of the current [StorageKey], with the given [component]
* appended to the current key's component. If the flag is enabled, the current key's component is
* discarded entirely.
*/
abstract fun newKeyWithComponent(component: String): StorageKey

Expand Down
8 changes: 2 additions & 6 deletions java/arcs/core/storage/StorageKeyManagerImpl.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package arcs.core.storage

import arcs.flags.BuildFlags

/** Implementation of [StorageKeyManager] that allows adding new parsers and resetting. */
open class StorageKeyManagerImpl(
vararg initialSet: StorageKeyParser<*>
Expand All @@ -11,10 +9,8 @@ open class StorageKeyManagerImpl(

override fun parse(rawKeyString: String): StorageKey {
var match: MatchResult? = null
if (BuildFlags.STORAGE_KEY_REDUCTION) {
// Try matching the short-form protocol first.
match = SHORT_PROTOCOL_PATTERN.matchEntire(rawKeyString)
}
// Try matching the short-form protocol first.
match = SHORT_PROTOCOL_PATTERN.matchEntire(rawKeyString)
if (match == null) {
// Fall back to the long-form protocol.
match = requireNotNull(LONG_PROTOCOL_PATTERN.matchEntire(rawKeyString)) {
Expand Down
20 changes: 5 additions & 15 deletions java/arcs/core/storage/StorageKeyProtocol.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package arcs.core.storage

import arcs.flags.BuildFlags

/** All [StorageKey] protocols understood by Arcs. */
enum class StorageKeyProtocol(
/** Full name of the protocol, e.g. "ramdisk", "reference-mode". */
Expand All @@ -21,15 +19,8 @@ enum class StorageKeyProtocol(
Dummy("dummy", "u"),
Volatile("volatile", "v");

val id: String
get() {
return if (BuildFlags.STORAGE_KEY_REDUCTION) shortId else longId
}

val protocol: String
get() {
return if (BuildFlags.STORAGE_KEY_REDUCTION) "$shortId|" else "$longId://"
}
val id = shortId
val protocol = "$shortId|"

override fun toString() = protocol

Expand All @@ -38,10 +29,9 @@ enum class StorageKeyProtocol(
private val LONG_PROTOCOLS = values().associateBy { it.longId }

fun parseProtocol(protocol: String): StorageKeyProtocol {
if (BuildFlags.STORAGE_KEY_REDUCTION) {
// Try short protocol first.
SHORT_PROTOCOLS[protocol]?.let { return it }
}
// Try short protocol first.
SHORT_PROTOCOLS[protocol]?.let { return it }

// Fall back to long protocol.
return LONG_PROTOCOLS[protocol] ?: throw IllegalArgumentException(
"Unknown storage key protocol: $protocol"
Expand Down
3 changes: 1 addition & 2 deletions java/arcs/core/storage/api/DriverAndKeyConfigurator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
package arcs.core.storage.api

import arcs.core.data.CreatableStorageKey
import arcs.core.data.SchemaRegistry
import arcs.core.storage.CapabilitiesResolver
import arcs.core.storage.DefaultDriverFactory
import arcs.core.storage.DriverProvider
Expand Down Expand Up @@ -45,7 +44,7 @@ object DriverAndKeyConfigurator {
)
// Only register the database driver provider if a database manager was provided.
databaseManager?.let {
driverProviders += DatabaseDriverProvider.configure(it, SchemaRegistry::getSchema)
driverProviders += DatabaseDriverProvider.configure(it)
}

DefaultDriverFactory.update(driverProviders)
Expand Down
55 changes: 7 additions & 48 deletions java/arcs/core/storage/driver/DatabaseDriverProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,20 @@ package arcs.core.storage.driver
import arcs.core.crdt.CrdtEntity
import arcs.core.crdt.CrdtSet
import arcs.core.crdt.CrdtSingleton
import arcs.core.data.Schema
import arcs.core.data.toSchema
import arcs.core.storage.Driver
import arcs.core.storage.DriverProvider
import arcs.core.storage.StorageKey
import arcs.core.storage.database.DatabaseManager
import arcs.core.storage.driver.DatabaseDriverProvider.getDatabaseInfo
import arcs.core.storage.keys.DatabaseStorageKey
import arcs.core.storage.referencemode.ReferenceModeStorageKey
import arcs.core.type.Type
import arcs.flags.BuildFlags
import kotlin.reflect.KClass

/** [DriverProvider] which provides a [DatabaseDriver]. */
object DatabaseDriverProvider : DriverProvider {
/**
* Whether or not the [DatabaseDriverProvider] has been configured with a [DatabaseManager] and
* a schema lookup function.
* Whether or not the [DatabaseDriverProvider] has been configured with a [DatabaseManager].
*/
val isConfigured: Boolean
get() = _manager != null
Expand All @@ -41,24 +37,8 @@ object DatabaseDriverProvider : DriverProvider {
val manager: DatabaseManager
get() = requireNotNull(_manager) { ERROR_MESSAGE_CONFIGURE_NOT_CALLED }

private val DEFAULT_SCHEMA_LOOKUP: (String) -> Schema? = {
throw IllegalStateException(ERROR_MESSAGE_CONFIGURE_NOT_CALLED)
}

/**
* Function which will be used to determine, at runtime, which [Schema] to associate with its
* hash value.
*/
private var schemaLookup = DEFAULT_SCHEMA_LOOKUP

override fun willSupport(storageKey: StorageKey): Boolean {
val databaseInfo = storageKey.getDatabaseInfo() ?: return false
if (!BuildFlags.STORAGE_KEY_REDUCTION) {
val entitySchemaHash = requireNotNull(databaseInfo.entitySchemaHash) {
"Entity schema hash missing"
}
return schemaLookup(entitySchemaHash) != null
}
storageKey.getDatabaseInfo() ?: return false
return true
}

Expand All @@ -70,18 +50,8 @@ object DatabaseDriverProvider : DriverProvider {
val databaseInfo = requireNotNull(storageKey.getDatabaseInfo()) {
"Unsupported StorageKey: $storageKey for DatabaseDriverProvider"
}
val schema = if (BuildFlags.STORAGE_KEY_REDUCTION) {
// Use schema from the provided type.
type.toSchema()
} else {
// Pull the schema hash out of the storage key.
val hash = requireNotNull(storageKey.getDatabaseInfo()?.entitySchemaHash) {
"Schema hash missing from storage key."
}
requireNotNull(schemaLookup(hash)) {
"Unsupported DatabaseStorageKey: No Schema found with hash: $hash"
}
}
val schema = type.toSchema()

require(
dataClass == CrdtEntity.Data::class ||
dataClass == CrdtSet.DataImpl::class ||
Expand Down Expand Up @@ -119,11 +89,10 @@ object DatabaseDriverProvider : DriverProvider {
}

/**
* Configures the [DatabaseDriverProvider] with the given [schemaLookup].
* Configures the [DatabaseDriverProvider] with the provided [DatabaseManager].
*/
fun configure(databaseManager: DatabaseManager, schemaLookup: (String) -> Schema?) = apply {
fun configure(databaseManager: DatabaseManager) = apply {
this._manager = databaseManager
this.schemaLookup = schemaLookup
}

/**
Expand All @@ -140,18 +109,12 @@ object DatabaseDriverProvider : DriverProvider {
check(backingKey.dbName == storageKey.dbName) {
"Database can support ReferenceModeStorageKey only with a single dbName."
}
if (!BuildFlags.STORAGE_KEY_REDUCTION) {
check(backingKey.entitySchemaHash == storageKey.entitySchemaHash) {
"Database can support ReferenceModeStorageKey only with a single entitySchemaHash."
}
}
check(
backingKey is DatabaseStorageKey.Persistent == storageKey is DatabaseStorageKey.Persistent
) {
"Database can support ReferenceModeStorageKey only if both or neither keys are persistent."
}
return DatabaseInfo(
if (BuildFlags.STORAGE_KEY_REDUCTION) null else backingKey.entitySchemaHash,
backingKey.dbName,
backingKey is DatabaseStorageKey.Persistent
)
Expand All @@ -163,7 +126,6 @@ object DatabaseDriverProvider : DriverProvider {
*/
private fun StorageKey.getDatabaseInfo(): DatabaseInfo? = when (this) {
is DatabaseStorageKey -> DatabaseInfo(
if (BuildFlags.STORAGE_KEY_REDUCTION) null else entitySchemaHash,
dbName,
this is DatabaseStorageKey.Persistent
)
Expand All @@ -172,17 +134,14 @@ object DatabaseDriverProvider : DriverProvider {
}

data class DatabaseInfo(
// TODO(b/179216769): Delete this field from this file entirely.
val entitySchemaHash: String?,
val dbName: String,
val persistent: Boolean
)

private const val ERROR_MESSAGE_CONFIGURE_NOT_CALLED =
"DatabaseDriverProvider.configure(databaseFactory, schemaLookup) has not been called"
"DatabaseDriverProvider.configure(databaseFactory) has not been called"

/* internal */ fun resetForTests() {
this._manager = null
this.schemaLookup = DEFAULT_SCHEMA_LOOKUP
}
}
Loading