Merge remote-tracking branch 'origin/main' into feature/SNRK-71/document-builder-implementation

This commit is contained in:
liubar.pa 2023-05-05 00:09:20 +03:00
commit 637a6810fc
3 changed files with 40 additions and 6 deletions

View File

@ -3,13 +3,31 @@ package space.kscience.snark.storage
import java.nio.file.Path import java.nio.file.Path
public interface Directory : AutoCloseable { public interface Directory : AutoCloseable {
@Deprecated(
message = "Use Path, not String",
level = DeprecationLevel.WARNING,
)
public suspend fun get(filename: String): FileReader 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) 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 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 getSubdir(path: Path): Directory
public suspend fun createSubdir(dirname: String, ignoreIfExists: Boolean = false): 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 { public interface FileReader : AutoCloseable {

View File

@ -7,7 +7,7 @@ import java.nio.file.Path
import kotlin.io.path.* import kotlin.io.path.*
public fun localStorage(rootPath: Path): Directory { public fun localStorage(rootPath: Path): Directory {
return LocalDirectory(rootPath) return LocalDirectory(rootPath, Path(""))
} }
private class LocalFile(private val path: Path) : FileReader, FileWriter { 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) override suspend fun write(bytes: ByteArray) = path.writeBytes(bytes)
} }
private class LocalDirectory(private val path: Path) : Directory { private class LocalDirectory(private val root: Path, private val currentDir: Path) : Directory {
private fun child(child: String): Path = path / child private fun child(child: String): Path = root / currentDir / child
private fun child(child: Path): Path = path / child private fun child(child: Path): Path = root / currentDir / child
override fun close() {} override fun close() {}
override suspend fun get(filename: String): FileReader = LocalFile(child(filename)) 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) { override suspend fun create(filename: String, ignoreIfExists: Boolean) {
try { try {
child(filename).createFile() 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 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 { override suspend fun createSubdir(dirname: String, ignoreIfExists: Boolean): Directory {
val dir = child(dirname) val dir = child(dirname)
try { try {
@ -47,6 +51,9 @@ private class LocalDirectory(private val path: Path) : Directory {
throw ex throw ex
} }
} }
return LocalDirectory(dir) return LocalDirectory(root, dir)
} }
override val path: Path
get() = currentDir
} }

View File

@ -15,6 +15,9 @@ internal class S3Directory(
override suspend fun get(filename: String): FileReader = override suspend fun get(filename: String): FileReader =
S3FileReader(client, bucketName, currentDir / filename) 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) { override suspend fun create(filename: String, ignoreIfExists: Boolean) {
if (!ignoreIfExists) { if (!ignoreIfExists) {
TODO("could not check if file exists") TODO("could not check if file exists")
@ -24,6 +27,9 @@ internal class S3Directory(
override suspend fun put(filename: String): FileWriter = override suspend fun put(filename: String): FileWriter =
S3FileWriter(client, bucketName, currentDir / filename) S3FileWriter(client, bucketName, currentDir / filename)
override suspend fun put(filename: Path): FileWriter =
S3FileWriter(client, bucketName, currentDir / filename)
override suspend fun getSubdir(path: Path): S3Directory = override suspend fun getSubdir(path: Path): S3Directory =
S3Directory(client, bucketName, currentDir / path) S3Directory(client, bucketName, currentDir / path)
@ -34,6 +40,9 @@ internal class S3Directory(
S3Directory(client, bucketName, currentDir / dirname) S3Directory(client, bucketName, currentDir / dirname)
} }
override val path: Path
get() = currentDir
override fun close() { override fun close() {
} }
} }