Skip to content

Commit 6930990

Browse files
committed
feat: new HTTP API for the Git like model data structure
The previous API was just a dumb key value store where the model server didn't understand the data that is stored. The new API allows the server to work closer together with the client to improve the performance and consistency.
1 parent 684cee1 commit 6930990

File tree

39 files changed

+1620
-179
lines changed

39 files changed

+1620
-179
lines changed

model-api/src/commonMain/kotlin/org/modelix/model/api/PNodeAdapter.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ open class PNodeAdapter(val nodeId: Long, val branch: IBranch) : INode {
153153
}
154154

155155
override fun getReferenceRoles(): List<String> {
156-
return branch.transaction.getPropertyRoles(nodeId).toList()
156+
return branch.transaction.getReferenceRoles(nodeId).toList()
157157
}
158158

159159
override fun equals(o: Any?): Boolean {
@@ -205,3 +205,6 @@ open class PNodeAdapter(val nodeId: Long, val branch: IBranch) : INode {
205205
notifyAccess()
206206
}
207207
}
208+
209+
fun IBranch.getNode(id: Long): INode = PNodeAdapter(id, this)
210+
fun IBranch.getRootNode(): INode = getNode(ITree.ROOT_ID)

model-api/src/commonMain/kotlin/org/modelix/model/data/ModelData.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,16 @@ fun NodeData.uid(model: ModelData): String {
7474
(id ?: throw IllegalArgumentException("Node has no ID"))
7575
}
7676
fun ModelData.nodeUID(node: NodeData): String = node.uid(this)
77+
78+
fun INode.asData(): NodeData = NodeData(
79+
id = reference.serialize(),
80+
concept = concept?.getUID(),
81+
role = roleInParent,
82+
properties = getPropertyRoles().associateWithNotNull { getPropertyValue(it) },
83+
references = getReferenceRoles().associateWithNotNull { getReferenceTargetRef(it)?.serialize() },
84+
children = allChildren.map { it.asData() }
85+
)
86+
87+
public inline fun <K, V : Any> Iterable<K>.associateWithNotNull(valueSelector: (K) -> V?): Map<K, V> {
88+
return associateWith { valueSelector(it) }.filterValues { it != null } as Map<K, V>
89+
}

model-client/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ tasks.named("check") {
3434
val kotlinCoroutinesVersion: String by rootProject
3535
val kotlinLoggingVersion: String by rootProject
3636
val ktorVersion: String by rootProject
37+
val kotlinxSerializationVersion: String by rootProject
3738

3839
kotlin {
3940
jvm()
@@ -51,11 +52,16 @@ kotlin {
5152
val commonMain by getting {
5253
dependencies {
5354
api(project(":model-api"))
55+
implementation(project(":model-server-api"))
5456
kotlin("stdlib-common")
5557
implementation("org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.4")
5658
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesVersion")
5759
implementation("io.github.microutils:kotlin-logging:$kotlinLoggingVersion")
5860
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
61+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinxSerializationVersion")
62+
implementation("io.ktor:ktor-client-core:$ktorVersion")
63+
implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
64+
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
5965
}
6066
}
6167
val commonTest by getting {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.modelix.model
15+
16+
import org.modelix.model.api.ITree
17+
18+
interface IVersion {
19+
fun getShaHash(): String
20+
fun getTree(): ITree
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.modelix.model.client2
15+
16+
import org.modelix.model.IVersion
17+
import org.modelix.model.api.IIdGenerator
18+
import org.modelix.model.lazy.BranchReference
19+
import org.modelix.model.lazy.RepositoryId
20+
import org.modelix.model.server.api.ModelQuery
21+
22+
interface IModelClientV2 {
23+
fun getClientId(): Int
24+
fun getIdGenerator(): IIdGenerator
25+
fun getUserId(): String?
26+
27+
suspend fun initRepository(repository: RepositoryId): IVersion
28+
suspend fun listRepositories(): List<RepositoryId>
29+
30+
suspend fun listBranches(repository: RepositoryId): List<BranchReference>
31+
32+
suspend fun loadVersion(versionHash: String, baseVersion: IVersion?): IVersion
33+
34+
/**
35+
* The pushed version is merged automatically by the server with the current head.
36+
* The merge result is returned.
37+
*/
38+
suspend fun push(branch: BranchReference, version: IVersion): IVersion
39+
40+
suspend fun pull(branch: BranchReference, lastKnownVersion: IVersion?): IVersion
41+
suspend fun pull(branch: BranchReference, lastKnownVersion: IVersion?, filter: ModelQuery): IVersion
42+
43+
suspend fun poll(branch: BranchReference, lastKnownVersion: IVersion?): IVersion
44+
suspend fun poll(branch: BranchReference, lastKnownVersion: IVersion?, filter: ModelQuery): IVersion
45+
}

0 commit comments

Comments
 (0)