Skip to content

Commit 76781e2

Browse files
authored
Merge pull request #66 from modelix/advanced-model-server-api
MODELIX-54 Higher level API for the advanced-model-client
2 parents 7e20eee + 6930990 commit 76781e2

File tree

51 files changed

+1823
-442
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1823
-442
lines changed

light-model-client/src/jvmTest/kotlin/org/modelix/client/light/LightModelClientTest.kt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,17 @@ import io.ktor.client.request.*
1919
import io.ktor.server.application.*
2020
import io.ktor.server.testing.*
2121
import io.ktor.websocket.*
22-
import kotlinx.coroutines.CoroutineScope
23-
import kotlinx.coroutines.Dispatchers
24-
import kotlinx.coroutines.channels.Channel
25-
import kotlinx.coroutines.channels.ClosedReceiveChannelException
2622
import kotlinx.coroutines.coroutineScope
2723
import kotlinx.coroutines.delay
2824
import kotlinx.coroutines.launch
2925
import kotlinx.coroutines.withTimeout
3026
import org.modelix.authorization.installAuthentication
3127
import org.modelix.model.api.addNewChild
3228
import org.modelix.model.api.getDescendants
33-
import org.modelix.model.server.InMemoryStoreClient
34-
import org.modelix.model.server.JsonModelServer
35-
import org.modelix.model.server.JsonModelServer2
36-
import org.modelix.model.server.LocalModelClient
37-
import org.modelix.model.server.api.MessageFromClient
38-
import org.modelix.model.server.api.MessageFromServer
29+
import org.modelix.model.server.store.InMemoryStoreClient
30+
import org.modelix.model.server.handlers.DeprecatedLightModelServer
31+
import org.modelix.model.server.handlers.LightModelServer
32+
import org.modelix.model.server.store.LocalModelClient
3933
import org.modelix.model.test.RandomModelChangeGenerator
4034
import kotlin.random.Random
4135
import kotlin.test.Ignore
@@ -54,8 +48,8 @@ class LightModelClientTest {
5448
install(io.ktor.server.websocket.WebSockets)
5549
val modelClient = LocalModelClient(InMemoryStoreClient())
5650
localModelClient = modelClient
57-
JsonModelServer(modelClient).init(this)
58-
JsonModelServer2(modelClient).init(this)
51+
DeprecatedLightModelServer(modelClient).init(this)
52+
LightModelServer(modelClient).init(this)
5953
}
6054
val client = createClient {
6155
install(WebSockets)

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)