This repository was archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIndexAggregation.kt
More file actions
69 lines (59 loc) · 2.32 KB
/
Copy pathIndexAggregation.kt
File metadata and controls
69 lines (59 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package dev.arkbuilders.arklib.data.index
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.merge
import dev.arkbuilders.arklib.ResourceId
import java.nio.file.Path
/**
* [IndexAggregation] is useful for "aggregated" navigation mode — a mode in
* which we navigate through multiple indexed folders (roots). For a single-root
* navigation [RootIndex] can be used, and both [IndexAggregation]
* and [RootIndex] should be transparently interchangeable, meaning that
* both implementations support the same methods and they should not be used
* otherwise as by [ResourceIndex] interface. Any component using [ResourceIndex]
* should function in the same fashion independent on implementation of the index,
* and only capability to look into multiple roots is gained by passing
* [IndexAggregation] into the component.
*
* @param shards A collection of individual [RootIndex] to be aggregated.
*/
class IndexAggregation(
private val shards: Collection<RootIndex>
) : ResourceIndex {
override val roots: Set<RootIndex> = shards.toSet()
override val updates: Flow<ResourceUpdates> = shards
.map { it.updates }
.asIterable()
.merge()
override fun allResources(): Map<ResourceId, Resource> =
shards
.map { it.allResources() }
.fold(hashMapOf()) { acc, curr ->
acc.putAll(curr)
acc
}
override fun getResource(id: ResourceId): Resource? =
shards.firstNotNullOfOrNull { it.getResource(id) }
override fun allPaths(): Map<ResourceId, Path> =
shards
.map { it.allPaths() }
.fold(hashMapOf()) { acc, curr ->
acc.putAll(curr)
acc
}
override fun getPath(id: ResourceId): Path? =
shards.firstNotNullOfOrNull { it.getPath(id) }
override suspend fun updateAll() {
shards.forEach { it.updateAll() }
}
override suspend fun updateOne(
resourcePath: Path,
oldId: ResourceId
): ResourceUpdates {
return shards.find { resourcePath.startsWith(it.path) }
?.updateOne(resourcePath, oldId)
?: error(
"At least one shard must contain the passed path" +
"shards: ${shards.map { it.path }} path: $resourcePath"
)
}
}