diff --git a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/Driver.kt b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/Driver.kt index 6462eff..eaa3e6f 100644 --- a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/Driver.kt +++ b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/Driver.kt @@ -3,13 +3,31 @@ package space.kscience.snark.storage import java.nio.file.Path public interface Directory : AutoCloseable { + @Deprecated( + message = "Use Path, not String", + level = DeprecationLevel.WARNING, + ) public suspend fun get(filename: String): FileReader + // get file from subtree + public suspend fun get(filename: Path): FileReader public suspend fun create(filename: String, ignoreIfExists: Boolean = false) + @Deprecated( + message = "Use Path, not String", + level = DeprecationLevel.WARNING, + ) public suspend fun put(filename: String): FileWriter + // put file to subtree + public suspend fun put(filename: Path): FileWriter public suspend fun getSubdir(path: Path): Directory public suspend fun createSubdir(dirname: String, ignoreIfExists: Boolean = false): Directory + + @Deprecated( + message = "Not a good idea", + level = DeprecationLevel.WARNING, + ) + public val path: Path } public interface FileReader : AutoCloseable { diff --git a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/local/LocalDriver.kt b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/local/LocalDriver.kt index 28e9487..4b26468 100644 --- a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/local/LocalDriver.kt +++ b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/local/LocalDriver.kt @@ -7,7 +7,7 @@ import java.nio.file.Path import kotlin.io.path.* public fun localStorage(rootPath: Path): Directory { - return LocalDirectory(rootPath) + return LocalDirectory(rootPath, Path("")) } private class LocalFile(private val path: Path) : FileReader, FileWriter { @@ -17,14 +17,16 @@ private class LocalFile(private val path: Path) : FileReader, FileWriter { override suspend fun write(bytes: ByteArray) = path.writeBytes(bytes) } -private class LocalDirectory(private val path: Path) : Directory { - private fun child(child: String): Path = path / child - private fun child(child: Path): Path = path / child +private class LocalDirectory(private val root: Path, private val currentDir: Path) : Directory { + private fun child(child: String): Path = root / currentDir / child + private fun child(child: Path): Path = root / currentDir / child override fun close() {} override suspend fun get(filename: String): FileReader = LocalFile(child(filename)) + override suspend fun get(filename: Path): FileReader = LocalFile(child(filename)) + override suspend fun create(filename: String, ignoreIfExists: Boolean) { try { child(filename).createFile() @@ -37,7 +39,9 @@ private class LocalDirectory(private val path: Path) : Directory { override suspend fun put(filename: String): FileWriter = LocalFile(child(filename)) - override suspend fun getSubdir(path: Path): Directory = LocalDirectory(child(path)) + override suspend fun put(filename: Path): FileWriter = LocalFile(child(filename)) + + override suspend fun getSubdir(path: Path): Directory = LocalDirectory(root, child(path)) override suspend fun createSubdir(dirname: String, ignoreIfExists: Boolean): Directory { val dir = child(dirname) try { @@ -47,6 +51,9 @@ private class LocalDirectory(private val path: Path) : Directory { throw ex } } - return LocalDirectory(dir) + return LocalDirectory(root, dir) } + + override val path: Path + get() = currentDir } diff --git a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/s3/S3Directory.kt b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/s3/S3Directory.kt index ed2c743..7dbb52b 100644 --- a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/s3/S3Directory.kt +++ b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/s3/S3Directory.kt @@ -15,6 +15,9 @@ internal class S3Directory( override suspend fun get(filename: String): FileReader = S3FileReader(client, bucketName, currentDir / filename) + override suspend fun get(filename: Path): FileReader = + S3FileReader(client, bucketName, currentDir / filename) + override suspend fun create(filename: String, ignoreIfExists: Boolean) { if (!ignoreIfExists) { TODO("could not check if file exists") @@ -24,6 +27,9 @@ internal class S3Directory( override suspend fun put(filename: String): FileWriter = S3FileWriter(client, bucketName, currentDir / filename) + override suspend fun put(filename: Path): FileWriter = + S3FileWriter(client, bucketName, currentDir / filename) + override suspend fun getSubdir(path: Path): S3Directory = S3Directory(client, bucketName, currentDir / path) @@ -34,6 +40,9 @@ internal class S3Directory( S3Directory(client, bucketName, currentDir / dirname) } + override val path: Path + get() = currentDir + override fun close() { } } \ No newline at end of file