Refactor blocking code in VFS
authorLanius Trolling <lanius@laniustrolling.dev>
Sun, 28 Apr 2024 16:08:50 +0000 (12:08 -0400)
committerLanius Trolling <lanius@laniustrolling.dev>
Sun, 28 Apr 2024 16:11:12 +0000 (12:11 -0400)
src/jvmMain/kotlin/info/mechyrdia/data/DataFiles.kt

index 165e4fd50119576ec93e9431bd1209ec86ea3375..6858d2c2e012249e21e2fd607ecdd1ed072bb0ff 100644 (file)
@@ -163,9 +163,8 @@ private class FlatFileStorage(val root: File) : FileStorage {
                if (file.isFile) return false
                if (file.isDirectory) return true
                
-               if (!file.parentFile.exists())
-                       if (!createDir(file.parentFile))
-                               return false
+               if (!file.parentFile.exists() && !createDir(file.parentFile))
+                       return false
                
                file.mkdir()
                return true
@@ -174,8 +173,8 @@ private class FlatFileStorage(val root: File) : FileStorage {
        private fun createFile(file: File): Boolean {
                if (!file.exists()) {
                        val containingDir = file.parentFile
-                       if (!containingDir.isDirectory)
-                               if (!createDir(containingDir)) return false
+                       if (!containingDir.isDirectory && !createDir(containingDir))
+                               return false
                }
                
                return true
@@ -199,24 +198,24 @@ private class FlatFileStorage(val root: File) : FileStorage {
        }
        
        override suspend fun createDir(dir: StoragePath): Boolean {
-               return withContext(Dispatchers.IO) { createDir(resolveFile(dir)) }
+               return runInterruptible(Dispatchers.IO) { createDir(resolveFile(dir)) }
        }
        
        override suspend fun listDir(dir: StoragePath): Map<String, StoredFileType>? {
-               return withContext(Dispatchers.IO) { resolveFile(dir).listFiles()?.associate { renderEntry(it) } }
+               return runInterruptible(Dispatchers.IO) { resolveFile(dir).listFiles()?.associate { renderEntry(it) } }
        }
        
        override suspend fun deleteDir(dir: StoragePath): Boolean {
                if (dir.isRoot) return false
                val file = resolveFile(dir)
                if (!file.isDirectory) return true
-               return withContext(Dispatchers.IO) { file.deleteRecursively() }
+               return runInterruptible(Dispatchers.IO) { file.deleteRecursively() }
        }
        
        override suspend fun statFile(path: StoragePath): StoredFileStats? {
                val file = resolveFile(path)
                if (!file.isFile) return null
-               return withContext(Dispatchers.IO) {
+               return runInterruptible(Dispatchers.IO) {
                        val basicAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes::class.java)
                        StoredFileStats(
                                basicAttributes.creationTime().toInstant(),
@@ -229,7 +228,7 @@ private class FlatFileStorage(val root: File) : FileStorage {
        override suspend fun writeFile(path: StoragePath, content: ByteArray): Boolean {
                val file = resolveFile(path)
                
-               return withContext(Dispatchers.IO) {
+               return runInterruptible(Dispatchers.IO) {
                        if (createFile(file)) {
                                file.writeBytes(content)
                                true
@@ -241,7 +240,7 @@ private class FlatFileStorage(val root: File) : FileStorage {
                val file = resolveFile(path)
                if (!file.isFile) return null
                
-               return withContext(Dispatchers.IO) {
+               return runInterruptible(Dispatchers.IO) {
                        file.readBytes()
                }
        }
@@ -252,7 +251,7 @@ private class FlatFileStorage(val root: File) : FileStorage {
                
                if (!sourceFile.isFile) return false
                
-               withContext(Dispatchers.IO) {
+               runInterruptible(Dispatchers.IO) {
                        sourceFile.copyTo(targetFile, overwrite = true)
                }
                
@@ -262,7 +261,7 @@ private class FlatFileStorage(val root: File) : FileStorage {
        override suspend fun eraseFile(path: StoragePath): Boolean {
                val file = resolveFile(path)
                if (!file.isFile) return true
-               return withContext(Dispatchers.IO) { file.delete() }
+               return runInterruptible(Dispatchers.IO) { file.delete() }
        }
 }
 
@@ -340,9 +339,9 @@ private class GridFsStorage(val table: DocumentTable<GridFsEntry>, val bucket: G
                        ?.objectId
                        ?.let { return it }
                
-               val bytesPublisher = { ByteBuffer.allocate(0) }
-                       .asFlow()
-                       .asPublisher(CoroutineName("grid-fs-writer") + Dispatchers.IO)
+               val bytesPublisher = flow {
+                       emit(ByteBuffer.allocate(0))
+               }.asPublisher(CoroutineName("grid-fs-writer") + Dispatchers.IO)
                
                return bucket.uploadFromPublisher(GRID_FS_KEEP, bytesPublisher).awaitFirst()
        }
@@ -419,7 +418,7 @@ private class GridFsStorage(val table: DocumentTable<GridFsEntry>, val bucket: G
                return ByteArrayOutputStream().also { content ->
                        bucket.downloadToPublisher(gridFsId).asFlow().collect { buffer ->
                                val array = buffer.slice().moveToByteArray()
-                               withContext(Dispatchers.IO) { content.write(array) }
+                               runInterruptible(Dispatchers.IO) { content.write(array) }
                        }
                }.toByteArray()
        }