Skip to content

Commit 3296665

Browse files
committed
fix(model-server): allow to delete branches
The old API allowed to delete branches by setting them to null. Restored this behavior to avoid breaking changes.
1 parent aa4a5c8 commit 3296665

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class KeyValueLikeModelServer(val repositoriesManager: RepositoriesManager) {
285285
// Now we use the RepositoriesManager to merge changes instead of just overwriting a branch.
286286

287287
val hashedObjects = LinkedHashMap<String, String>()
288-
val branchChanges = LinkedHashMap<BranchReference, String>()
288+
val branchChanges = LinkedHashMap<BranchReference, String?>()
289289
val userDefinedEntries = LinkedHashMap<String, String?>()
290290

291291
for ((key, value) in newEntries) {
@@ -294,7 +294,7 @@ class KeyValueLikeModelServer(val repositoriesManager: RepositoriesManager) {
294294
hashedObjects[key] = value ?: throw IllegalArgumentException("No value provided for $key")
295295
}
296296
BranchReference.tryParseBranch(key) != null -> {
297-
branchChanges[BranchReference.tryParseBranch(key)!!] = value ?: throw IllegalArgumentException("Deleting branch $key not allowed")
297+
branchChanges[BranchReference.tryParseBranch(key)!!] = value
298298
}
299299
key.startsWith(PROTECTED_PREFIX) -> {
300300
throw NoPermissionException("Access to keys starting with '$PROTECTED_PREFIX' is only permitted to the model server itself.")
@@ -317,7 +317,11 @@ class KeyValueLikeModelServer(val repositoriesManager: RepositoriesManager) {
317317
storeClient.putAll(hashedObjects)
318318
storeClient.putAll(userDefinedEntries)
319319
for ((branch, value) in branchChanges) {
320-
repositoriesManager.mergeChanges(branch, value)
320+
if (value == null) {
321+
repositoriesManager.removeBranches(branch.repositoryId, setOf(branch.branchName))
322+
} else {
323+
repositoriesManager.mergeChanges(branch, value)
324+
}
321325
}
322326
}
323327
}

model-server/src/main/kotlin/org/modelix/model/server/handlers/RepositoriesManager.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,31 @@ class RepositoriesManager(val client: LocalModelClient) {
121121
}
122122
}
123123

124+
fun removeRepository(repository: RepositoryId) {
125+
store.runTransaction {
126+
for (branchName in getBranchNames(repository)) {
127+
putVersionHash(repository.getBranchReference(branchName), null)
128+
}
129+
store.put(branchListKey(repository), null)
130+
val existingRepositories = getRepositories()
131+
val remainingRepositories = existingRepositories - repository
132+
store.put(REPOSITORIES_LIST_KEY, remainingRepositories.joinToString("\n") { it.id })
133+
}
134+
}
135+
136+
fun removeBranches(repository: RepositoryId, branchNames: Set<String>) {
137+
if (branchNames.isEmpty()) return
138+
store.runTransaction {
139+
val key = branchListKey(repository)
140+
val existingBranches = store[key]?.lines()?.toSet() ?: emptySet()
141+
val remainingBranches = existingBranches - branchNames
142+
store.put(key, remainingBranches.joinToString("\n"))
143+
for (branchName in branchNames) {
144+
putVersionHash(repository.getBranchReference(branchName), null)
145+
}
146+
}
147+
}
148+
124149
fun mergeChanges(branch: BranchReference, newVersionHash: String): String {
125150
var result: String? = null
126151
store.runTransaction {
@@ -155,7 +180,7 @@ class RepositoriesManager(val client: LocalModelClient) {
155180
?: store[legacyBranchKey(branch)]?.also { store.put(branchKey(branch), it, true) }
156181
}
157182

158-
private fun putVersionHash(branch: BranchReference, hash: String) {
183+
private fun putVersionHash(branch: BranchReference, hash: String?) {
159184
store.put(branchKey(branch), hash, false)
160185
store.put(legacyBranchKey(branch), hash, false)
161186
}

0 commit comments

Comments
 (0)