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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
package com.google.devtools.ksp
import com.intellij.util.io.DataExternalizer
import com.intellij.util.io.IOUtil
import com.intellij.util.io.KeyDescriptor
import com.intellij.util.io.PersistentHashMap
import java.io.DataInput
import java.io.DataInputStream
import java.io.DataOutput
import java.io.File
abstract class PersistentMap<K, V>(
storageFile: File,
keyDescriptor: KeyDescriptor<K>,
dataExternalizer: DataExternalizer<V>
) : PersistentHashMap<K, V>(
storageFile,
keyDescriptor,
dataExternalizer
) {
val keys: Collection<K>
get() = mutableListOf<K>().also { list ->
this.processKeysWithExistingMapping { key -> list.add(key) }
}
operator fun set(key: K, value: V) = put(key, value)
fun clear() {
keys.forEach {
remove(it)
}
}
fun flush() = force()
}
object FileKeyDescriptor : KeyDescriptor<File> {
override fun read(input: DataInput): File {
return File(IOUtil.readString(input))
}
override fun save(output: DataOutput, value: File) {
IOUtil.writeString(value.path, output)
}
override fun getHashCode(value: File): Int = value.hashCode()
override fun isEqual(val1: File, val2: File): Boolean = val1 == val2
}
object FileExternalizer : DataExternalizer<File> {
override fun read(input: DataInput): File = File(IOUtil.readString(input))
override fun save(output: DataOutput, value: File) {
IOUtil.writeString(value.path, output)
}
}
class ListExternalizer<T>(
private val elementExternalizer: DataExternalizer<T>,
) : DataExternalizer<List<T>> {
override fun save(output: DataOutput, value: List<T>) {
value.forEach { elementExternalizer.save(output, it) }
}
override fun read(input: DataInput): List<T> {
val result = mutableListOf<T>()
val stream = input as DataInputStream
while (stream.available() > 0) {
result.add(elementExternalizer.read(stream))
}
return result
}
}
class FileToFilesMap(
storageFile: File,
) : PersistentMap<File, List<File>>(
storageFile,
FileKeyDescriptor,
ListExternalizer(FileExternalizer)
)
|