-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVssBackupClientLdk.kt
More file actions
113 lines (105 loc) · 4.12 KB
/
VssBackupClientLdk.kt
File metadata and controls
113 lines (105 loc) · 4.12 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package to.bitkit.data.backup
import com.synonym.vssclient.KeyVersion
import com.synonym.vssclient.LdkNamespace
import com.synonym.vssclient.VssItem
import com.synonym.vssclient.vssDeleteLdk
import com.synonym.vssclient.vssGetLdk
import com.synonym.vssclient.vssListKeysLdk
import com.synonym.vssclient.vssListLdk
import com.synonym.vssclient.vssStoreLdk
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import to.bitkit.di.IoDispatcher
import to.bitkit.utils.Logger
class VssBackupClientLdk @AssistedInject constructor(
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
@Assisted private val awaitSetup: suspend () -> Unit,
) {
companion object {
private const val TAG = "VssBackupClientLdk"
}
@AssistedFactory
interface Factory {
fun create(awaitSetup: suspend () -> Unit): VssBackupClientLdk
}
suspend fun getObject(
key: String,
namespace: LdkNamespace = LdkNamespace.Default,
): Result<VssItem?> = withContext(ioDispatcher) {
awaitSetup()
Logger.verbose("VSS LDK 'getObject' call for '$key'", context = TAG)
runCatching {
vssGetLdk(key = key, namespace = namespace)
}.onSuccess {
if (it == null) {
Logger.verbose("VSS LDK 'getObject' success null for '$key'", context = TAG)
} else {
Logger.verbose("VSS LDK 'getObject' success for '$key'", context = TAG)
}
}.onFailure {
Logger.verbose("VSS LDK 'getObject' error for '$key'", it, context = TAG)
}
}
suspend fun putObject(
key: String,
data: ByteArray,
namespace: LdkNamespace = LdkNamespace.Default,
): Result<VssItem> = withContext(ioDispatcher) {
awaitSetup()
Logger.verbose("VSS LDK 'putObject' call for '$key'", context = TAG)
runCatching {
vssStoreLdk(key = key, value = data, namespace = namespace)
}.onSuccess {
Logger.verbose("VSS LDK 'putObject' success for '$key' at version: '${it.version}'", context = TAG)
}.onFailure {
Logger.verbose("VSS LDK 'putObject' error for '$key'", it, context = TAG)
}
}
suspend fun deleteObject(
key: String,
namespace: LdkNamespace = LdkNamespace.Default,
): Result<Boolean> = withContext(ioDispatcher) {
awaitSetup()
Logger.verbose("VSS LDK 'deleteObject' call for '$key'", context = TAG)
runCatching {
vssDeleteLdk(key = key, namespace = namespace)
}.onSuccess { wasDeleted ->
if (wasDeleted) {
Logger.verbose("VSS LDK 'deleteObject' success for '$key' - key was deleted", context = TAG)
} else {
Logger.verbose("VSS LDK 'deleteObject' success for '$key' - key did not exist", context = TAG)
}
}.onFailure {
Logger.verbose("VSS LDK 'deleteObject' error for '$key'", it, context = TAG)
}
}
suspend fun listKeys(
namespace: LdkNamespace = LdkNamespace.Default,
): Result<List<KeyVersion>> = withContext(ioDispatcher) {
awaitSetup()
Logger.verbose("VSS LDK 'listKeys' call", context = TAG)
runCatching {
vssListKeysLdk(namespace = namespace)
}.onSuccess {
Logger.verbose("VSS LDK 'listKeys' success - found ${it.size} key(s)", context = TAG)
}.onFailure {
Logger.verbose("VSS LDK 'listKeys' error", it, context = TAG)
}
}
suspend fun listItems(
namespace: LdkNamespace = LdkNamespace.Default,
): Result<List<VssItem>> = withContext(ioDispatcher) {
awaitSetup()
Logger.verbose("VSS LDK 'listItems' call", context = TAG)
runCatching {
vssListLdk(namespace = namespace)
}.onSuccess {
Logger.verbose("VSS LDK 'listItems' success - found ${it.size} item(s)", context = TAG)
}.onFailure {
Logger.verbose("VSS LDK 'listItems' error", it, context = TAG)
}
}
}