@@ -9,7 +9,6 @@ import kotlinx.coroutines.sync.withLock
9
9
import org.json.JSONArray
10
10
import org.json.JSONException
11
11
import org.json.JSONObject
12
- import java.io.BufferedReader
13
12
import java.io.File
14
13
import java.io.FileNotFoundException
15
14
import java.io.FileOutputStream
@@ -28,8 +27,8 @@ class EventsFileManager(
28
27
) {
29
28
private val fileIndexKey = " amplitude.events.file.index.$storageKey "
30
29
private val storageVersionKey = " amplitude.events.file.version.$storageKey "
31
- val filePathSet: MutableSet <String > = Collections .newSetFromMap(ConcurrentHashMap ())
32
- val curFile: MutableMap <String , File > = ConcurrentHashMap <String , File >()
30
+ private val filePathSet: MutableSet <String > = Collections .newSetFromMap(ConcurrentHashMap ())
31
+ private val curFile: MutableMap <String , File > = ConcurrentHashMap <String , File >()
33
32
34
33
companion object {
35
34
const val MAX_FILE_SIZE = 975_000 // 975KB
@@ -38,7 +37,7 @@ class EventsFileManager(
38
37
val readMutexMap = ConcurrentHashMap <String , Mutex >()
39
38
}
40
39
41
- val writeMutex = writeMutexMap.getOrPut(storageKey) { Mutex () }
40
+ private val writeMutex = writeMutexMap.getOrPut(storageKey) { Mutex () }
42
41
private val readMutex = readMutexMap.getOrPut(storageKey) { Mutex () }
43
42
44
43
init {
@@ -100,20 +99,24 @@ class EventsFileManager(
100
99
*/
101
100
fun read (): List <String > {
102
101
// we need to filter out .temp file, since it's operating on the writing thread
103
- val fileList =
104
- directory.listFiles { _, name ->
105
- name.contains(storageKey) && ! name.endsWith(" .tmp" ) && ! name.endsWith(" .properties" )
106
- } ? : emptyArray()
107
- return fileList.sortedBy { it ->
108
- getSortKeyForFile(it)
102
+ val fileList = directory.listFiles { _, name ->
103
+ name.contains(storageKey) && ! name.endsWith(" .tmp" ) && ! name.endsWith(" .properties" )
104
+ } ? : emptyArray()
105
+
106
+ return fileList.sortedBy { file ->
107
+ val name = file.nameWithoutExtension.replace(" $storageKey -" , " " )
108
+
109
+ val dashIndex = name.indexOf(' -' )
110
+ if (dashIndex >= 0 ) {
111
+ name.substring(0 , dashIndex).padStart(10 , ' 0' ) + name.substring(dashIndex)
112
+ } else {
113
+ name
114
+ }
109
115
}.map {
110
116
it.absolutePath
111
117
}
112
118
}
113
119
114
- /* *
115
- * deletes the file at filePath
116
- */
117
120
fun remove (filePath : String ): Boolean {
118
121
filePathSet.remove(filePath)
119
122
return File (filePath).delete()
@@ -160,8 +163,8 @@ class EventsFileManager(
160
163
return @withLock " "
161
164
}
162
165
filePathSet.add(filePath)
163
- File (filePath).bufferedReader().use< BufferedReader , String > {
164
- val content = it .readText()
166
+ File (filePath).bufferedReader().use { reader ->
167
+ val content = reader .readText()
165
168
val isCurrentVersion = content.endsWith(DELIMITER )
166
169
if (isCurrentVersion) {
167
170
// handle current version
@@ -247,15 +250,6 @@ class EventsFileManager(
247
250
return curFile[storageKey]!!
248
251
}
249
252
250
- private fun getSortKeyForFile (file : File ): String {
251
- val name = file.nameWithoutExtension.replace(" $storageKey -" , " " )
252
- val dashIndex = name.indexOf(' -' )
253
- if (dashIndex >= 0 ) {
254
- return name.substring(0 , dashIndex).padStart(10 , ' 0' ) + name.substring(dashIndex)
255
- }
256
- return name
257
- }
258
-
259
253
// write to underlying file
260
254
private fun writeToFile (
261
255
content : ByteArray ,
0 commit comments