@@ -49,6 +49,8 @@ import org.modelix.model.server.api.v2.VersionDelta
49
49
import org.modelix.model.server.api.v2.VersionDeltaStream
50
50
import org.modelix.model.server.api.v2.VersionDeltaStreamV2
51
51
import org.modelix.model.server.store.StoreManager
52
+ import org.modelix.model.server.store.runReadIO
53
+ import org.modelix.model.server.store.runWriteIO
52
54
import org.modelix.modelql.core.IMemoizationPersistence
53
55
import org.modelix.modelql.core.IStepOutput
54
56
import org.modelix.modelql.core.MonoUnboundQuery
@@ -89,16 +91,15 @@ class ModelReplicationServer(
89
91
90
92
override suspend fun PipelineContext <Unit , ApplicationCall >.getRepositories () {
91
93
call.respondText(
92
- repositoriesManager.getRepositories()
94
+ runRead { repositoriesManager.getRepositories() }
93
95
.filter { call.hasPermission(ModelServerPermissionSchema .repository(it).list) }
94
96
.joinToString(" \n " ) { it.id },
95
97
)
96
98
}
97
99
98
100
override suspend fun PipelineContext <Unit , ApplicationCall >.getRepositoryBranches (repository : String ) {
99
101
call.respondText(
100
- repositoriesManager
101
- .getBranchNames(repositoryId(repository))
102
+ runRead { repositoriesManager.getBranchNames(repositoryId(repository)) }
102
103
.filter { call.hasPermission(ModelServerPermissionSchema .repository(repository).branch(it).list) }
103
104
.joinToString(" \n " ),
104
105
)
@@ -111,7 +112,9 @@ class ModelReplicationServer(
111
112
) {
112
113
checkPermission(ModelServerPermissionSchema .repository(repository).branch(branch).pull)
113
114
val branchRef = repositoryId(repository).getBranchReference(branch)
114
- val versionHash = repositoriesManager.getVersionHash(branchRef) ? : throw BranchNotFoundException (branchRef)
115
+ val versionHash = runRead {
116
+ repositoriesManager.getVersionHash(branchRef) ? : throw BranchNotFoundException (branchRef)
117
+ }
115
118
call.respondDelta(RepositoryId (repository), versionHash, lastKnown)
116
119
}
117
120
@@ -122,7 +125,7 @@ class ModelReplicationServer(
122
125
) {
123
126
checkPermission(ModelServerPermissionSchema .repository(repository).branch(branch).pull)
124
127
val branchRef = repositoryId(repository).getBranchReference(branch)
125
- val versionHash = repositoriesManager.getVersionHash(branchRef) ? : throw BranchNotFoundException (branchRef)
128
+ val versionHash = runRead { repositoriesManager.getVersionHash(branchRef) ? : throw BranchNotFoundException (branchRef) }
126
129
call.respond(BranchV1 (branch, versionHash))
127
130
}
128
131
@@ -138,11 +141,13 @@ class ModelReplicationServer(
138
141
139
142
checkPermission(ModelServerPermissionSchema .repository(repositoryId).branch(branch).delete)
140
143
141
- if (! repositoriesManager.getBranchNames(repositoryId).contains(branch)) {
142
- throw BranchNotFoundException (branch, repositoryId.id)
143
- }
144
+ runWrite {
145
+ if (! repositoriesManager.getBranchNames(repositoryId).contains(branch)) {
146
+ throw BranchNotFoundException (branch, repositoryId.id)
147
+ }
144
148
145
- repositoriesManager.removeBranches(repositoryId, setOf (branch))
149
+ repositoriesManager.removeBranches(repositoryId, setOf (branch))
150
+ }
146
151
147
152
call.respond(HttpStatusCode .NoContent )
148
153
}
@@ -153,7 +158,7 @@ class ModelReplicationServer(
153
158
) {
154
159
checkPermission(ModelServerPermissionSchema .repository(repository).branch(branch).pull)
155
160
val branchRef = repositoryId(repository).getBranchReference(branch)
156
- val versionHash = repositoriesManager.getVersionHash(branchRef) ? : throw BranchNotFoundException (branchRef)
161
+ val versionHash = runRead { repositoriesManager.getVersionHash(branchRef) ? : throw BranchNotFoundException (branchRef) }
157
162
call.respondText(versionHash)
158
163
}
159
164
@@ -163,19 +168,23 @@ class ModelReplicationServer(
163
168
legacyGlobalStorage : Boolean? ,
164
169
) {
165
170
checkPermission(ModelServerPermissionSchema .repository(repository).create)
166
- val initialVersion = repositoriesManager.createRepository(
167
- repositoryId(repository),
168
- call.getUserName(),
169
- useRoleIds ? : true ,
170
- legacyGlobalStorage ? : false ,
171
- )
171
+ val initialVersion = runWrite {
172
+ repositoriesManager.createRepository(
173
+ repositoryId(repository),
174
+ call.getUserName(),
175
+ useRoleIds ? : true ,
176
+ legacyGlobalStorage ? : false ,
177
+ )
178
+ }
172
179
call.respondDelta(RepositoryId (repository), initialVersion.getContentHash(), null )
173
180
}
174
181
175
182
override suspend fun PipelineContext <Unit , ApplicationCall >.deleteRepository (repository : String ) {
176
183
checkPermission(ModelServerPermissionSchema .repository(repository).delete)
177
184
178
- val foundAndDeleted = repositoriesManager.removeRepository(repositoryId(repository))
185
+ val foundAndDeleted = runWrite {
186
+ repositoriesManager.removeRepository(repositoryId(repository))
187
+ }
179
188
if (foundAndDeleted) {
180
189
call.respond(HttpStatusCode .NoContent )
181
190
} else {
@@ -191,8 +200,10 @@ class ModelReplicationServer(
191
200
val branchRef = repositoryId(repository).getBranchReference(branch)
192
201
val deltaFromClient = call.receive<VersionDelta >()
193
202
deltaFromClient.checkObjectHashes()
194
- repositoriesManager.getStoreClient(RepositoryId (repository)).putAll(deltaFromClient.getAllObjects())
195
- val mergedHash = repositoriesManager.mergeChanges(branchRef, deltaFromClient.versionHash)
203
+ repositoriesManager.getStoreClient(RepositoryId (repository), true ).putAll(deltaFromClient.getAllObjects())
204
+ val mergedHash = runWrite {
205
+ repositoriesManager.mergeChanges(branchRef, deltaFromClient.versionHash)
206
+ }
196
207
call.respondDelta(RepositoryId (repository), mergedHash, deltaFromClient.versionHash)
197
208
}
198
209
@@ -217,7 +228,7 @@ class ModelReplicationServer(
217
228
}
218
229
219
230
val objects = withContext(Dispatchers .IO ) {
220
- repositoriesManager.getStoreClient(RepositoryId (repository)).getAll(keys)
231
+ repositoriesManager.getStoreClient(RepositoryId (repository), true ).getAll(keys)
221
232
}
222
233
223
234
for (entry in objects) {
@@ -260,7 +271,7 @@ class ModelReplicationServer(
260
271
) {
261
272
val branchRef = repositoryId(repository).getBranchReference(branchName)
262
273
checkPermission(ModelServerPermissionSchema .branch(branchRef).query)
263
- val version = repositoriesManager.getVersion(branchRef) ? : throw BranchNotFoundException (branchRef)
274
+ val version = runRead { repositoriesManager.getVersion(branchRef) ? : throw BranchNotFoundException (branchRef) }
264
275
LOG .trace(" Running query on {} @ {}" , branchRef, version)
265
276
val initialTree = version.getTree()
266
277
@@ -290,7 +301,9 @@ class ModelReplicationServer(
290
301
baseVersion = version,
291
302
operations = ops.map { it.getOriginalOp() }.toTypedArray(),
292
303
)
293
- repositoriesManager.mergeChanges(branchRef, newVersion.getContentHash())
304
+ runWrite {
305
+ repositoriesManager.mergeChanges(branchRef, newVersion.getContentHash())
306
+ }
294
307
}
295
308
}
296
309
})
@@ -330,7 +343,7 @@ class ModelReplicationServer(
330
343
}
331
344
332
345
withContext(Dispatchers .IO ) {
333
- repositoriesManager.getStoreClient(RepositoryId (repository)).putAll(entries, true )
346
+ repositoriesManager.getStoreClient(RepositoryId (repository), true ).putAll(entries, true )
334
347
}
335
348
call.respondText(" ${entries.size} objects received" )
336
349
}
@@ -341,7 +354,7 @@ class ModelReplicationServer(
341
354
lastKnown : String? ,
342
355
) {
343
356
checkPermission(ModelServerPermissionSchema .legacyGlobalObjects.read)
344
- if (stores.getGlobalStoreClient()[versionHash] == null ) {
357
+ if (runRead { stores.getGlobalStoreClient()[versionHash] } == null ) {
345
358
throw VersionNotFoundException (versionHash)
346
359
}
347
360
call.respondDelta(null , versionHash, lastKnown)
@@ -402,6 +415,14 @@ class ModelReplicationServer(
402
415
}
403
416
}
404
417
}
418
+
419
+ private suspend fun <R > runRead (body : () -> R ): R {
420
+ return repositoriesManager.getTransactionManager().runReadIO(body)
421
+ }
422
+
423
+ private suspend fun <R > runWrite (body : () -> R ): R {
424
+ return repositoriesManager.getTransactionManager().runWriteIO(body)
425
+ }
405
426
}
406
427
407
428
/* *
0 commit comments