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 d743d99..5fb2fbc 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 @@ -1,27 +1,20 @@ package space.kscience.snark.storage import java.nio.file.Path +import kotlin.io.path.* public interface Directory : AutoCloseable { - @Deprecated("Use Path, not String") - public suspend fun get(filename: String): FileReader - // get file from subtree public suspend fun get(filename: Path): FileReader @Deprecated("Use put") public suspend fun create(filename: String, ignoreIfExists: Boolean = false) - @Deprecated("Use Path, not String") - 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 operator fun div(path: Path): Directory = getSubdir(path) - @Deprecated("Directories are created on put") public suspend fun createSubdir(dirname: String, ignoreIfExists: Boolean = false): Directory @@ -29,6 +22,15 @@ public interface Directory : AutoCloseable { public val path: Path } + +public suspend fun Directory.get(filename: String): FileReader = get(Path(filename)) + +public suspend fun Directory.put(filename: String): FileWriter = put(Path(filename)) + +public suspend operator fun Directory.div(path: Path): Directory = getSubdir(path) + +public suspend operator fun Directory.div(path: String): Directory = getSubdir(Path(path)) + public interface FileReader : AutoCloseable { public suspend fun readAll(): ByteArray } 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 d6c5e94..ea452e7 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 @@ -19,7 +19,7 @@ internal class LocalFile(private val path: Path) : FileReader, FileWriter { path.parent.createDirectories() try { path.createFile() - } catch (ex: java.nio.file.FileAlreadyExistsException) { + } catch (ex: Exception) { // Do nothing } path.writeBytes(bytes) @@ -33,9 +33,6 @@ internal class LocalDirectory(private val root: Path, private val currentDir: Pa override fun close() {} - @Deprecated("Use Path, not String") - override suspend fun get(filename: String): LocalFile = LocalFile(realpath(filename)) - override suspend fun get(filename: Path): LocalFile = LocalFile(realpath(filename)) @Deprecated("Use put") @@ -51,9 +48,6 @@ internal class LocalDirectory(private val root: Path, private val currentDir: Pa } } - @Deprecated("Use Path, not String") - override suspend fun put(filename: String): LocalFile = get(filename) - override suspend fun put(filename: Path): LocalFile = get(filename) override suspend fun getSubdir(path: Path): LocalDirectory = LocalDirectory(root, currentDir / path) 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 6416dc3..8ec96a5 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 @@ -12,10 +12,6 @@ internal class S3Directory( private val bucketName: String, private val currentDir: Path, ) : Directory { - @Deprecated("Use Path, not String") - override suspend fun get(filename: String): FileReader = - S3FileReader(client, bucketName, currentDir / filename) - override suspend fun get(filename: Path): FileReader = S3FileReader(client, bucketName, currentDir / filename) @@ -26,10 +22,6 @@ internal class S3Directory( } } - @Deprecated("Use Path, not String") - override suspend fun put(filename: String): FileWriter = - S3FileWriter(client, bucketName, currentDir / filename) - override suspend fun put(filename: Path): FileWriter = S3FileWriter(client, bucketName, currentDir / filename) diff --git a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/s3/S3Root.kt b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/s3/S3Root.kt index 8f32607..19ad43f 100644 --- a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/s3/S3Root.kt +++ b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/s3/S3Root.kt @@ -21,22 +21,16 @@ internal fun splitPathIntoBucketAndPath(path: Path): Pair { } internal class S3Root(private val client: S3Client) : Directory { - override suspend fun get(filename: String): FileReader { - throw NoSuchFileException(Path(filename).toFile()) - } override suspend fun get(filename: Path): FileReader { throw NoSuchFileException(filename.toFile()) } + @Deprecated("Use put") override suspend fun create(filename: String, ignoreIfExists: Boolean) { throw NoSuchFileException(Path(filename).toFile()) } - override suspend fun put(filename: String): FileWriter { - throw NoSuchFileException(Path(filename).toFile()) - } - override suspend fun put(filename: Path): FileWriter { throw NoSuchFileException(filename.toFile()) } @@ -51,6 +45,7 @@ internal class S3Root(private val client: S3Client) : Directory { throw AccessDeniedException(path.toFile(), reason = ex.message) } + @Deprecated("Directories are created on put") override suspend fun createSubdir(dirname: String, ignoreIfExists: Boolean): Directory = try { val (bucketName, filePath) = splitPathIntoBucketAndPath(Path(dirname)) client.createBucket { diff --git a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/unzip/Unzip.kt b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/unzip/Unzip.kt index 5201930..b60cc9c 100644 --- a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/unzip/Unzip.kt +++ b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/unzip/Unzip.kt @@ -1,6 +1,6 @@ package space.kscience.snark.storage.unzip -import space.kscience.snark.storage.Directory +import space.kscience.snark.storage.* import java.io.FileInputStream import java.util.zip.ZipInputStream diff --git a/snark-storage-driver/src/test/kotlin/space/kscience/snark/storage/local/Example.kt b/snark-storage-driver/src/test/kotlin/space/kscience/snark/storage/local/Example.kt new file mode 100644 index 0000000..324120f --- /dev/null +++ b/snark-storage-driver/src/test/kotlin/space/kscience/snark/storage/local/Example.kt @@ -0,0 +1,47 @@ +package space.kscience.snark.storage.local + +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.BeforeAll +import space.kscience.snark.storage.* +import java.nio.file.Path +import kotlin.io.path.createTempDirectory +import kotlin.io.path.deleteExisting +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.io.path.* +import kotlin.test.assertEquals + +class Example { + var tempDir: Path? = null + var somedir: Directory? = null + + @BeforeTest + fun setUp() { + tempDir = createTempDirectory() + somedir = localStorage(tempDir!!) + } + + @AfterTest + fun tearDown() { + tempDir!!.toFile().deleteRecursively() + somedir = null + } + + @Test + fun exampleTest() = runBlocking { + somedir!!.put(Path("somefile")).write("hello".toByteArray()) + assertEquals("hello", somedir!!.get(Path("somefile")).readAll().decodeToString()) + } + + @Test + fun subdirExample() = runBlocking { + val dir1 = somedir!! / "tmp1" + dir1.put("somefile").write("hello".toByteArray()) + + val dir2 = somedir!! / "tmp1" + val data = dir2.get("somefile").readAll() + + assertEquals("hello", data.decodeToString()) + } +} \ No newline at end of file diff --git a/snark-storage-driver/src/test/kotlin/space/kscience/snark/storage/local/Tests.kt b/snark-storage-driver/src/test/kotlin/space/kscience/snark/storage/local/Tests.kt index ec5765e..f47e7fa 100644 --- a/snark-storage-driver/src/test/kotlin/space/kscience/snark/storage/local/Tests.kt +++ b/snark-storage-driver/src/test/kotlin/space/kscience/snark/storage/local/Tests.kt @@ -2,7 +2,7 @@ package space.kscience.snark.storage.local import kotlinx.coroutines.runBlocking -import space.kscience.snark.storage.Directory +import space.kscience.snark.storage.* import java.io.File import java.nio.file.Path import kotlin.io.path.* diff --git a/snark-storage-driver/src/test/kotlin/space/kscience/snark/storage/unzip/Tests.kt b/snark-storage-driver/src/test/kotlin/space/kscience/snark/storage/unzip/Tests.kt index 3b6e054..b567948 100644 --- a/snark-storage-driver/src/test/kotlin/space/kscience/snark/storage/unzip/Tests.kt +++ b/snark-storage-driver/src/test/kotlin/space/kscience/snark/storage/unzip/Tests.kt @@ -2,8 +2,7 @@ package space.kscience.snark.storage.unzip import kotlinx.coroutines.runBlocking -import space.kscience.snark.storage.Directory -import space.kscience.snark.storage.local.LocalDirectory +import space.kscience.snark.storage.* import space.kscience.snark.storage.local.localStorage import java.io.* import java.nio.file.Files