Commit d54d9f57 authored by Amirjanyan's avatar Amirjanyan
Browse files

Merge branch 'schema-cache' into 'main'

#89 Improve Schema performance: cache the schema

See merge request !40
parents 2a2db117 391432a2
Pipeline #3210 failed with stage
......@@ -11,7 +11,55 @@ import GRDB
// MARK: SCHEMA STRUCTS
/// A schema definition. This is used to dynamically enforce supported types and properties
struct CachedProperty: Hashable {
let itemType: String
let propertyName: String
func hash(into hasher: inout Hasher) {
hasher.combine(itemType)
hasher.combine(propertyName)
}
struct Result: Hashable {
var value: SchemaValueType? = nil
init(value: SchemaValueType?) {
self.value = value
}
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
}
}
struct CachedEdge: Hashable {
let itemType: String
let edgeName: String
func hash(into hasher: inout Hasher) {
hasher.combine(itemType)
hasher.combine(edgeName)
}
struct Result: Hashable {
var value: String? = nil
init(value: String?) {
self.value = value
}
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
}
}
struct Schema {
static var cachedProperties : [CachedProperty : CachedProperty.Result] = [:]
static var cachedEdges:[CachedEdge : CachedEdge.Result] = [:]
static var shared = Schema()
/// Supported types in the schema
func expectedType(forItemType itemType: String, propertyOrEdgeName: String) -> ResolvedType? {
if let propertyType = expectedType(forItemType: itemType, propertyName: propertyOrEdgeName) {
......@@ -52,6 +100,12 @@ struct Schema {
}
func expectedType(forItemType itemType: String, propertyName: String, db: Database) -> SchemaValueType? {
let key = CachedProperty(itemType: itemType, propertyName: propertyName)
let cachedValue = Schema.cachedProperties[key]
if let cachedValue = cachedValue {
return cachedValue.value
}
var schemaValueType: SchemaValueType? = nil
let sortAlias = TableAlias()
......@@ -73,6 +127,8 @@ struct Schema {
}
}
Schema.cachedProperties[key] = CachedProperty.Result(value: schemaValueType)
return schemaValueType
}
......@@ -83,7 +139,12 @@ struct Schema {
}
func expectedTargetType(forItemType itemType: String, edgeName: String, db: Database) -> String? {
let key = CachedEdge(itemType: itemType, edgeName: edgeName)
let cachedValue = Schema.cachedEdges[key]
if let cachedValue = cachedValue {
return cachedValue.value
}
var targetType: String? = nil
let sortAlias = TableAlias()
......@@ -105,6 +166,8 @@ struct Schema {
}
}
Schema.cachedEdges[key] = CachedEdge.Result(value: targetType)
return targetType
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment