Skip to content

Commit

Permalink
Divide getFile into getFileForRead and getFileForWrite.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslabari committed Jan 11, 2024
1 parent e4b9439 commit 91da1e3
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import java.io.FileNotFoundException

/**
* Handles the reading and writing of objects from the app's cache.
* Previous versions of the SDK used the cache directory for cached files.
* Since v6.3.0, the files directory is used instead.
*/
internal class EmbraceCacheService(
private val storageService: StorageService,
Expand All @@ -22,7 +20,7 @@ internal class EmbraceCacheService(
override fun cacheBytes(name: String, bytes: ByteArray?) {
logger.logDeveloper(TAG, "Attempting to write bytes to $name")
if (bytes != null) {
val file = storageService.getFile(EMBRACE_PREFIX + name)
val file = storageService.getFileForWrite(EMBRACE_PREFIX + name)
try {
file.writeBytes(bytes)
logger.logDeveloper(TAG, "Bytes cached")
Expand All @@ -36,7 +34,7 @@ internal class EmbraceCacheService(

override fun loadBytes(name: String): ByteArray? {
logger.logDeveloper(TAG, "Attempting to read bytes from $name")
val file = storageService.getFile(EMBRACE_PREFIX + name)
val file = storageService.getFileForRead(EMBRACE_PREFIX + name)
try {
return file.readBytes()
} catch (ex: FileNotFoundException) {
Expand All @@ -49,7 +47,7 @@ internal class EmbraceCacheService(

override fun cachePayload(name: String, action: SerializationAction) {
logger.logDeveloper(TAG, "Attempting to write bytes to $name")
val file = storageService.getFile(EMBRACE_PREFIX + name)
val file = storageService.getFileForWrite(EMBRACE_PREFIX + name)
try {
file.outputStream().buffered().use(action)
logger.logDeveloper(TAG, "Bytes cached")
Expand All @@ -62,7 +60,7 @@ internal class EmbraceCacheService(
override fun loadPayload(name: String): SerializationAction {
logger.logDeveloper(TAG, "Attempting to read bytes from $name")
return { stream ->
val file = storageService.getFile(EMBRACE_PREFIX + name)
val file = storageService.getFileForRead(EMBRACE_PREFIX + name)
try {
file.inputStream().buffered().use { input ->
input.copyTo(stream)
Expand All @@ -88,7 +86,7 @@ internal class EmbraceCacheService(
*/
override fun <T> cacheObject(name: String, objectToCache: T, clazz: Class<T>) {
logger.logDeveloper(TAG, "Attempting to cache object: $name")
val file = storageService.getFile(EMBRACE_PREFIX + name)
val file = storageService.getFileForWrite(EMBRACE_PREFIX + name)
try {
serializer.toJson(objectToCache, clazz, file.outputStream())
} catch (ex: Exception) {
Expand All @@ -97,7 +95,7 @@ internal class EmbraceCacheService(
}

override fun <T> loadObject(name: String, clazz: Class<T>): T? {
val file = storageService.getFile(EMBRACE_PREFIX + name)
val file = storageService.getFileForRead(EMBRACE_PREFIX + name)
try {
return serializer.fromJson(file.inputStream(), clazz)
} catch (ex: FileNotFoundException) {
Expand All @@ -110,7 +108,7 @@ internal class EmbraceCacheService(

override fun deleteFile(name: String): Boolean {
logger.logDeveloper("EmbraceCacheService", "Attempting to delete file from cache: $name")
val file = storageService.getFile(EMBRACE_PREFIX + name)
val file = storageService.getFileForRead(EMBRACE_PREFIX + name)
try {
return file.delete()
} catch (ex: Exception) {
Expand All @@ -128,7 +126,7 @@ internal class EmbraceCacheService(
override fun writeSession(name: String, sessionMessage: SessionMessage) {
try {
logger.logDeveloper(TAG, "Attempting to write bytes to $name")
val file = storageService.getFile(EMBRACE_PREFIX + name)
val file = storageService.getFileForWrite(EMBRACE_PREFIX + name)
serializer.toJson(sessionMessage, SessionMessage::class.java, file.outputStream())
logger.logDeveloper(TAG, "Bytes cached")
} catch (ex: Throwable) {
Expand All @@ -137,7 +135,7 @@ internal class EmbraceCacheService(
}

override fun loadOldPendingApiCalls(name: String): List<PendingApiCall>? {
val file = storageService.getFile(EMBRACE_PREFIX + name)
val file = storageService.getFileForRead(EMBRACE_PREFIX + name)
try {
val type = Types.newParameterizedType(List::class.java, PendingApiCall::class.java)
return serializer.fromJson(file.inputStream(), type) as List<PendingApiCall>? ?: emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class CrashModuleImpl(

private val crashMarker: CrashFileMarker by singleton {
val markerFile = lazy {
storageModule.storageService.getFile(CrashFileMarker.CRASH_MARKER_FILE_NAME)
storageModule.storageService.getFileForWrite(CrashFileMarker.CRASH_MARKER_FILE_NAME)
}
CrashFileMarker(markerFile)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ internal class EmbraceNdkService(

private fun installSignals() {
val reportBasePath = storageService.getNativeCrashDir().absolutePath
val markerFilePath = storageService.getFile(CrashFileMarker.CRASH_MARKER_FILE_NAME).absolutePath
val markerFilePath = storageService.getFileForWrite(
CrashFileMarker.CRASH_MARKER_FILE_NAME
).absolutePath

logger.logDeveloper("EmbraceNDKService", "Creating report path at $reportBasePath")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import android.content.Context
import java.io.File
import java.io.FilenameFilter

/**
* Provides File instances for files and directories used to store data.
* Previous versions of the SDK used the cache directory for cached files.
* Since v6.3.0, the files directory is used instead and the cache directory is only used for
* cached config files.
*/
internal class EmbraceStorageService(
private val context: Context
) : StorageService {
Expand All @@ -16,7 +22,7 @@ internal class EmbraceStorageService(
getOrCreateEmbraceFilesDir() ?: cacheDirectory
}

override fun getFile(name: String): File {
override fun getFileForRead(name: String): File {
val fileInFilesDir = File(filesDirectory, name)

if (!fileInFilesDir.exists()) {
Expand All @@ -28,6 +34,10 @@ internal class EmbraceStorageService(
return fileInFilesDir
}

override fun getFileForWrite(name: String): File {
return File(filesDirectory, name)
}

override fun getConfigCacheDir(): File {
return File(cacheDirectory, EMBRACE_CONFIG_CACHE_DIRECTORY)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import java.io.File
import java.io.FilenameFilter

/**
* Provides File instances for files in the cache and files directories.
* Previous versions of the SDK used to store files in the cache directory, but now we use the
* files directory for everything except the config cache.
* Provides File instances for files and directories used to store data.
*/
internal interface StorageService {

Expand All @@ -15,7 +13,12 @@ internal interface StorageService {
* If the file doesn't exist in any of the directories it will return a File instance from
* the files directory.
*/
fun getFile(name: String): File
fun getFileForRead(name: String): File

/**
* Returns a [File] with the specified [name] from the files directory.
*/
fun getFileForWrite(name: String): File

/**
* Returns a [File] instance referencing the directory where the config cache is stored.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ internal class FakeStorageService : StorageService {
Files.createTempDirectory("files_temp").toFile()
}

override fun getFile(name: String) =
override fun getFileForRead(name: String) =
File(filesDirectory, name)

override fun getFileForWrite(name: String) =
File(filesDirectory, name)

override fun getConfigCacheDir() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@ internal class EmbraceStorageServiceTest {
}

@Test
fun `test getFile with fallback false returns File instance from files dir`() {
val file = storageManager.getFile("test.txt")
fun `test getFileForWrite returns File instance from files dir`() {
val file = storageManager.getFileForWrite("test.txt")
assertNotNull(file)
assertEquals("$embraceFilesDir/test.txt", file.absolutePath)
}

@Test
fun `test getFile with fallback true returns File instance from files dir if exists`() {
fun `test getFileForRead returns File instance from files dir if exists`() {
val fileToAdd = File(embraceFilesDir, "test.txt")
val addedFile = Files.createFile(fileToAdd.toPath()).toFile()
val resultFile = storageManager.getFile("test.txt")
val resultFile = storageManager.getFileForRead("test.txt")
assertNotNull(resultFile)
assertEquals(addedFile, resultFile)
}

@Test
fun `test getFile with fallback true returns File instance from cache dir if doesn't exist in files`() {
fun `test getFileForRead returns File instance from cache dir if doesn't exist in files`() {
val fileToAdd = File(cacheDir, "test.txt")
val addedFile = Files.createFile(fileToAdd.toPath()).toFile()
val resultFile = storageManager.getFile("test.txt")
val resultFile = storageManager.getFileForRead("test.txt")
assertNotNull(resultFile)
assertEquals(addedFile, resultFile)
}
Expand Down

0 comments on commit 91da1e3

Please sign in to comment.