Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
memri
iOS client for Memri
Commits
d54d9f57
Commit
d54d9f57
authored
Jul 27, 2021
by
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
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
MemriApp/Controllers/Database/Schema.swift
View file @
d54d9f57
...
...
@@ -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
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment