This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proper implementation of CatalogVfs (#582)
Fixes #486
- Loading branch information
Showing
5 changed files
with
109 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
korio/src/commonMain/kotlin/com/soywiz/korio/file/std/CatalogVfs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.soywiz.korio.file.std | ||
|
||
import com.soywiz.kds.* | ||
import com.soywiz.klock.* | ||
import com.soywiz.korio.dynamic.* | ||
import com.soywiz.korio.file.* | ||
import com.soywiz.korio.serialization.json.* | ||
import kotlinx.coroutines.flow.* | ||
|
||
fun VfsFile.withCatalog(): VfsFile = CatalogVfs(this).root | ||
fun VfsFile.withCatalogJail(): VfsFile = CatalogVfs(this.jail()).root | ||
|
||
open class CatalogVfs(val parent: VfsFile) : Vfs.Proxy() { | ||
|
||
override suspend fun access(path: String): VfsFile = parent[path] | ||
|
||
override suspend fun stat(path: String): VfsStat { | ||
val normalizedPath = PathInfo(path).normalize() | ||
if (normalizedPath == "/" || normalizedPath == "") { | ||
return createExistsStat("/", isDirectory = true, size = 0L, cache = true) | ||
} | ||
val baseName = PathInfo(normalizedPath).baseName | ||
val info = cachedListSimpleStats(PathInfo(normalizedPath).parent.fullPath) | ||
return info[baseName] ?: createNonExistsStat(normalizedPath, cache = true) | ||
} | ||
|
||
override suspend fun listFlow(path: String): Flow<VfsFile> = listSimple(path).asFlow() | ||
|
||
override suspend fun listSimple(path: String): List<VfsFile> = | ||
cachedListSimpleStats(path).map { it.value.enrichedFile } | ||
|
||
private val catalogCache = FastStringMap<Map<String, VfsStat>>() | ||
|
||
suspend fun cachedListSimpleStats(path: String): Map<String, VfsStat> { | ||
val key = PathInfo(path).normalize() | ||
return catalogCache.getOrPut(key) { listSimpleStats(key) } | ||
} | ||
|
||
suspend fun listSimpleStats(path: String): Map<String, VfsStat> { | ||
val catalogJsonString = parent[path]["\$catalog.json"].readString() | ||
val data = Json.parse(catalogJsonString).dyn | ||
|
||
return data.list.map { | ||
val localName = PathInfo(it["name"].str).baseName | ||
createExistsStat( | ||
path = "$path/$localName", | ||
isDirectory = it["isDirectory"].bool, | ||
size = it["size"].long, | ||
createTime = DateTime.fromUnix(it["createTime"].long), | ||
modifiedTime = DateTime.fromUnix(it["modifiedTime"].long), | ||
cache = true | ||
) | ||
}.associateBy { it.baseName } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
korio/src/commonTest/kotlin/com/soywiz/korio/vfs/CatalogVfsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.soywiz.korio.vfs | ||
|
||
import com.soywiz.korio.async.* | ||
import com.soywiz.korio.file.* | ||
import com.soywiz.korio.file.std.* | ||
import kotlinx.coroutines.flow.* | ||
import kotlin.test.* | ||
|
||
class CatalogVfsTest { | ||
@Test | ||
fun test() = suspendTest { | ||
val vfs = MemoryVfsMix( | ||
"/\$catalog.json" to """[ | ||
{"name": "demo", "size": 96, "modifiedTime": 0, "createTime": 0, "isDirectory": true}, | ||
{"name": "korge.png", "size": 14015, "modifiedTime": 1, "createTime": 1, "isDirectory": false}, | ||
{"name": "test.txt", "size": 11, "modifiedTime": 2, "createTime": 2, "isDirectory": false}, | ||
]""", | ||
"/demo/\$catalog.json" to """[ | ||
{"name": "test.txt", "size": 12, "modifiedTime": 2, "createTime": 2, "isDirectory": false}, | ||
]""", | ||
).withCatalog() | ||
|
||
assertEquals( | ||
"/demo,/demo/test.txt,/korge.png,/test.txt", | ||
vfs.listRecursive().toList().joinToString(",") { it.fullPathNormalized } | ||
) | ||
|
||
assertEquals(0L, vfs["/"].size()) | ||
assertEquals(96L, vfs["/demo"].size()) | ||
assertEquals(14015L, vfs["/korge.png"].size()) | ||
assertEquals(11L, vfs["/test.txt"].size()) | ||
assertEquals(12L, vfs["/demo/test.txt"].size()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters