Skip to content

Commit 046fdd8

Browse files
authored
storage changes for signals (#211)
Co-authored-by: Wenxi Zeng <[email protected]>
1 parent e4af729 commit 046fdd8

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

android/src/main/java/com/segment/analytics/kotlin/android/Storage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class AndroidStorage(
2525

2626
private val sharedPreferences: SharedPreferences =
2727
context.getSharedPreferences("analytics-android-$writeKey", Context.MODE_PRIVATE)
28-
private val storageDirectory: File = context.getDir("segment-disk-queue", Context.MODE_PRIVATE)
28+
override val storageDirectory: File = context.getDir("segment-disk-queue", Context.MODE_PRIVATE)
2929
internal val eventsFile =
3030
EventsFileManager(storageDirectory, writeKey, AndroidKVS(sharedPreferences))
3131

core/src/main/java/com/segment/analytics/kotlin/core/Storage.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import kotlinx.coroutines.CoroutineDispatcher
44
import kotlinx.serialization.json.Json
55
import kotlinx.serialization.json.JsonObject
66
import sovran.kotlin.Store
7+
import java.io.File
78

89
/**
910
* Storage interface that abstracts storage of
@@ -41,6 +42,8 @@ interface Storage {
4142
DeviceId("segment.device.id")
4243
}
4344

45+
val storageDirectory: File
46+
4447
suspend fun subscribeToStore()
4548
suspend fun write(key: Constants, value: String)
4649
fun read(key: Constants): String?

core/src/main/java/com/segment/analytics/kotlin/core/platform/EventPipeline.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import kotlinx.serialization.json.jsonPrimitive
1919
import java.io.File
2020
import java.io.FileInputStream
2121

22-
internal class EventPipeline(
22+
open class EventPipeline(
2323
private val analytics: Analytics,
2424
private val logTag: String,
2525
apiKey: String,
@@ -31,11 +31,15 @@ internal class EventPipeline(
3131

3232
private var uploadChannel: Channel<String>
3333

34-
private val httpClient: HTTPClient = HTTPClient(apiKey, analytics.configuration.requestFactory)
34+
protected open val httpClient: HTTPClient = HTTPClient(apiKey, analytics.configuration.requestFactory)
3535

36-
private val storage get() = analytics.storage
36+
protected open val storage get() = analytics.storage
3737

38-
private val scope get() = analytics.analyticsScope
38+
protected open val scope get() = analytics.analyticsScope
39+
40+
protected open val fileIODispatcher get() = analytics.fileIODispatcher
41+
42+
protected open val networkIODispatcher get() = analytics.networkIODispatcher
3943

4044
var running: Boolean
4145
private set
@@ -87,7 +91,7 @@ internal class EventPipeline(
8791
unschedule()
8892
}
8993

90-
internal fun stringifyBaseEvent(payload: BaseEvent): String {
94+
open fun stringifyBaseEvent(payload: BaseEvent): String {
9195
val finalPayload = EncodeDefaultsJson.encodeToJsonElement(payload)
9296
.jsonObject.filterNot { (k, v) ->
9397
// filter out empty userId and traits values
@@ -98,7 +102,7 @@ internal class EventPipeline(
98102
return stringVal
99103
}
100104

101-
private fun write() = scope.launch(analytics.fileIODispatcher) {
105+
private fun write() = scope.launch(fileIODispatcher) {
102106
for (event in writeChannel) {
103107
// write to storage
104108
val isPoison = (event.messageId == FLUSH_POISON)
@@ -122,10 +126,10 @@ internal class EventPipeline(
122126
}
123127
}
124128

125-
private fun upload() = scope.launch(analytics.networkIODispatcher) {
129+
private fun upload() = scope.launch(networkIODispatcher) {
126130
uploadChannel.consumeEach {
127131
analytics.log("$logTag performing flush")
128-
withContext(analytics.fileIODispatcher) {
132+
withContext(fileIODispatcher) {
129133
storage.rollover()
130134
}
131135

core/src/main/java/com/segment/analytics/kotlin/core/utilities/StorageImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class StorageImpl(
2222
private val ioDispatcher: CoroutineDispatcher
2323
) : Subscriber, Storage {
2424

25-
private val storageDirectory = File("/tmp/analytics-kotlin/$writeKey")
25+
override val storageDirectory = File("/tmp/analytics-kotlin/$writeKey")
2626
private val storageDirectoryEvents = File(storageDirectory, "events")
2727

2828
internal val propertiesFile = PropertiesFile(storageDirectory, writeKey)

0 commit comments

Comments
 (0)