@@ -42,12 +42,27 @@ import org.modelix.model.persistent.CPVersion
42
42
import org.modelix.model.server.store.IStoreClient
43
43
import org.modelix.model.server.store.LocalModelClient
44
44
import org.modelix.model.server.store.pollEntry
45
+ import org.modelix.model.server.store.runTransactionSuspendable
45
46
import java.lang.ref.SoftReference
46
47
import java.util.UUID
47
48
48
49
class RepositoriesManager (val client : LocalModelClient ) {
49
50
50
51
init {
52
+ fun migrateLegacyRepositoriesList () {
53
+ val legacyRepositories = listLegacyRepositories().groupBy { it.repositoryId }
54
+ if (legacyRepositories.isNotEmpty()) {
55
+ // To not use `runTransactionSuspendable` like everywhere else,
56
+ // because this is blocking initialization code anyways.
57
+ store.runTransaction {
58
+ ensureRepositoriesAreInList(legacyRepositories.keys)
59
+ for ((legacyRepository, legacyBranches) in legacyRepositories) {
60
+ ensureBranchesAreInList(legacyRepository, legacyBranches.map { it.branchName }.toSet())
61
+ }
62
+ }
63
+ }
64
+ }
65
+
51
66
migrateLegacyRepositoriesList()
52
67
}
53
68
@@ -75,8 +90,8 @@ class RepositoriesManager(val client: LocalModelClient) {
75
90
* If the server ID was created previously but is only stored under a legacy database key,
76
91
* it also gets stored under the current and all legacy database keys.
77
92
*/
78
- fun maybeInitAndGetSeverId (): String {
79
- return store.runTransaction {
93
+ suspend fun maybeInitAndGetSeverId (): String {
94
+ return store.runTransactionSuspendable {
80
95
var serverId = store[SERVER_ID_KEY ]
81
96
if (serverId == null ) {
82
97
serverId = store[LEGACY_SERVER_ID_KEY2 ]
@@ -102,9 +117,9 @@ class RepositoriesManager(val client: LocalModelClient) {
102
117
103
118
private fun repositoryExists (repositoryId : RepositoryId ) = getRepositories().contains(repositoryId)
104
119
105
- fun createRepository (repositoryId : RepositoryId , userName : String? , useRoleIds : Boolean = true): CLVersion {
120
+ suspend fun createRepository (repositoryId : RepositoryId , userName : String? , useRoleIds : Boolean = true): CLVersion {
106
121
var initialVersion: CLVersion ? = null
107
- store.runTransaction {
122
+ store.runTransactionSuspendable {
108
123
val masterBranch = repositoryId.getBranchReference()
109
124
if (repositoryExists(repositoryId)) throw RepositoryAlreadyExistsException (repositoryId.id)
110
125
val existingRepositories = getRepositories()
@@ -160,10 +175,10 @@ class RepositoriesManager(val client: LocalModelClient) {
160
175
}
161
176
}
162
177
163
- fun removeRepository (repository : RepositoryId ): Boolean {
164
- return store.runTransaction {
178
+ suspend fun removeRepository (repository : RepositoryId ): Boolean {
179
+ return store.runTransactionSuspendable {
165
180
if (! repositoryExists(repository)) {
166
- return @runTransaction false
181
+ return @runTransactionSuspendable false
167
182
}
168
183
169
184
for (branchName in getBranchNames(repository)) {
@@ -178,9 +193,9 @@ class RepositoriesManager(val client: LocalModelClient) {
178
193
}
179
194
}
180
195
181
- fun removeBranches (repository : RepositoryId , branchNames : Set <String >) {
196
+ suspend fun removeBranches (repository : RepositoryId , branchNames : Set <String >) {
182
197
if (branchNames.isEmpty()) return
183
- store.runTransaction {
198
+ store.runTransactionSuspendable {
184
199
val key = branchListKey(repository)
185
200
val existingBranches = store[key]?.lines()?.toSet() ? : emptySet()
186
201
val remainingBranches = existingBranches - branchNames
@@ -191,11 +206,10 @@ class RepositoriesManager(val client: LocalModelClient) {
191
206
}
192
207
}
193
208
194
- fun mergeChanges (branch : BranchReference , newVersionHash : String ): String {
209
+ suspend fun mergeChanges (branch : BranchReference , newVersionHash : String ): String {
195
210
var result: String? = null
196
- store.runTransaction {
197
- val branchKey = branchKey(branch)
198
- val headHash = getVersionHash(branch)
211
+ store.runTransactionSuspendable {
212
+ val headHash = getVersionHashInsideTransaction(branch)
199
213
val mergedHash = if (headHash == null ) {
200
214
newVersionHash
201
215
} else {
@@ -217,17 +231,20 @@ class RepositoriesManager(val client: LocalModelClient) {
217
231
return result!!
218
232
}
219
233
220
- fun getVersion (branch : BranchReference ): CLVersion ? {
234
+ suspend fun getVersion (branch : BranchReference ): CLVersion ? {
221
235
return getVersionHash(branch)?.let { CLVersion .loadFromHash(it, client.storeCache) }
222
236
}
223
237
224
- fun getVersionHash (branch : BranchReference ): String? {
225
- return store.runTransaction {
226
- store[branchKey(branch)]
227
- ? : store[legacyBranchKey(branch)]?.also { store.put(branchKey(branch), it, true ) }
238
+ suspend fun getVersionHash (branch : BranchReference ): String? {
239
+ return store.runTransactionSuspendable {
240
+ getVersionHashInsideTransaction(branch)
228
241
}
229
242
}
230
243
244
+ private fun getVersionHashInsideTransaction (branch : BranchReference ): String? {
245
+ return store[branchKey(branch)] ? : store[legacyBranchKey(branch)]?.also { store.put(branchKey(branch), it, true ) }
246
+ }
247
+
231
248
private fun putVersionHash (branch : BranchReference , hash : String? ) {
232
249
store.put(branchKey(branch), hash, false )
233
250
store.put(legacyBranchKey(branch), hash, false )
@@ -295,18 +312,6 @@ class RepositoriesManager(val client: LocalModelClient) {
295
312
296
313
private fun branchListKey (repositoryId : RepositoryId ) = " $KEY_PREFIX :repositories:${repositoryId.id} :branches"
297
314
298
- fun migrateLegacyRepositoriesList () {
299
- val legacyRepositories = listLegacyRepositories().groupBy { it.repositoryId }
300
- if (legacyRepositories.isNotEmpty()) {
301
- store.runTransaction {
302
- ensureRepositoriesAreInList(legacyRepositories.keys)
303
- for ((legacyRepository, legacyBranches) in legacyRepositories) {
304
- ensureBranchesAreInList(legacyRepository, legacyBranches.map { it.branchName }.toSet())
305
- }
306
- }
307
- }
308
- }
309
-
310
315
private fun listLegacyRepositories (): Set <BranchReference > {
311
316
val result: MutableSet <BranchReference > = HashSet ()
312
317
val infoVersionHash = client[RepositoryId (" info" ).getBranchReference().getKey()] ? : return emptySet()
0 commit comments