SNRK-68: Implement new semantics

This commit is contained in:
Kirill Grachev 2023-05-06 20:56:54 +03:00
parent f477af64e6
commit 29d842b0bf
3 changed files with 40 additions and 22 deletions

View File

@ -3,30 +3,29 @@ package space.kscience.snark.storage
import java.nio.file.Path
public interface Directory : AutoCloseable {
@Deprecated(
message = "Use Path, not String",
level = DeprecationLevel.WARNING,
)
@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(
message = "Use Path, not String",
level = DeprecationLevel.WARNING,
)
@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
@Deprecated(
message = "Not a good idea",
level = DeprecationLevel.WARNING,
)
@Deprecated("Not a good idea")
public val path: Path
}

View File

@ -3,9 +3,8 @@ package space.kscience.snark.storage.local
import space.kscience.snark.storage.Directory
import space.kscience.snark.storage.FileReader
import space.kscience.snark.storage.FileWriter
import java.io.File
import java.lang.Exception
import java.nio.file.Path
import java.nio.file.attribute.PosixFilePermission
import kotlin.io.path.*
public fun localStorage(rootPath: Path): Directory {
@ -16,24 +15,35 @@ internal class LocalFile(private val path: Path) : FileReader, FileWriter {
override fun close() {}
override suspend fun readAll(): ByteArray = path.readBytes()
override suspend fun write(bytes: ByteArray) = path.writeBytes(bytes)
override suspend fun write(bytes: ByteArray) {
path.parent.createDirectories()
try {
path.createFile()
} catch (ex: java.nio.file.FileAlreadyExistsException) {
// Do nothing
}
path.writeBytes(bytes)
}
}
internal 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
@Deprecated("Use Path, not String")
private fun realpath(child: String): Path = root / currentDir / child
private fun realpath(child: Path): Path = root / currentDir / child
override fun close() {}
override suspend fun get(filename: String): LocalFile = LocalFile(child(filename))
@Deprecated("Use Path, not String")
override suspend fun get(filename: String): LocalFile = LocalFile(realpath(filename))
override suspend fun get(filename: Path): LocalFile = LocalFile(child(filename))
override suspend fun get(filename: Path): LocalFile = LocalFile(realpath(filename))
@Deprecated("Use put")
override suspend fun create(filename: String, ignoreIfExists: Boolean) {
val dir = child(filename)
val dir = realpath(filename)
dir.parent.createDirectories()
try {
child(filename).createFile()
realpath(filename).createFile()
} catch (ex: java.nio.file.FileAlreadyExistsException) {
if (!ignoreIfExists) {
throw ex
@ -41,13 +51,16 @@ 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)
@Deprecated("Directories are created on put")
override suspend fun createSubdir(dirname: String, ignoreIfExists: Boolean): LocalDirectory {
val dir = child(dirname)
val dir = realpath(dirname)
dir.parent.createDirectories()
try {
dir.createDirectory()
@ -59,6 +72,7 @@ internal class LocalDirectory(private val root: Path, private val currentDir: Pa
return LocalDirectory(root, currentDir / dirname)
}
@Deprecated("Not a good idea")
override val path: Path
get() = currentDir
}

View File

@ -12,18 +12,21 @@ 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)
@Deprecated("Use put")
override suspend fun create(filename: String, ignoreIfExists: Boolean) {
if (!ignoreIfExists) {
TODO("could not check if file exists")
}
}
@Deprecated("Use Path, not String")
override suspend fun put(filename: String): FileWriter =
S3FileWriter(client, bucketName, currentDir / filename)
@ -33,6 +36,7 @@ internal class S3Directory(
override suspend fun getSubdir(path: Path): S3Directory =
S3Directory(client, bucketName, currentDir / path)
@Deprecated("Directories are created on put")
override suspend fun createSubdir(dirname: String, ignoreIfExists: Boolean): S3Directory =
if (!ignoreIfExists) {
TODO("could not check if directory exists")
@ -40,6 +44,7 @@ internal class S3Directory(
S3Directory(client, bucketName, currentDir / dirname)
}
@Deprecated("Not a good idea")
override val path: Path
get() = currentDir