From 5b33419b370b51c86dc4f15c4f650f227742bc1c Mon Sep 17 00:00:00 2001 From: Kirill Grachev Date: Thu, 4 May 2023 21:11:29 +0300 Subject: [PATCH 1/4] SNRK-68: Add methods --- .../kotlin/space/kscience/snark/storage/Driver.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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..58870d3 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,27 @@ 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 + + public val path: Path } public interface FileReader : AutoCloseable { From 634eb2d69df5af862d6c13a87295069f17dc22ae Mon Sep 17 00:00:00 2001 From: Kirill Grachev Date: Thu, 4 May 2023 21:11:56 +0300 Subject: [PATCH 2/4] SNRK-68: Implement s3 storage --- .../space/kscience/snark/storage/s3/S3Directory.kt | 9 +++++++++ 1 file changed, 9 insertions(+) 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 From b6db0ba4796dcc93b1a4cd4597632bcaa11c6865 Mon Sep 17 00:00:00 2001 From: Kirill Grachev Date: Thu, 4 May 2023 21:12:23 +0300 Subject: [PATCH 3/4] SNRK-68: Implement local storage --- .../snark/storage/local/LocalDriver.kt | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) 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 } From 4441172ef782b01061741e7f9c60a6871a02b870 Mon Sep 17 00:00:00 2001 From: Kirill Grachev Date: Thu, 4 May 2023 21:14:59 +0300 Subject: [PATCH 4/4] SNRK-68: Add warning --- .../src/main/kotlin/space/kscience/snark/storage/Driver.kt | 4 ++++ 1 file changed, 4 insertions(+) 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 58870d3..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 @@ -23,6 +23,10 @@ public interface Directory : AutoCloseable { 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 }