SNRK-68: Use extend functions to mark methods as final

This commit is contained in:
Kirill Grachev 2023-05-06 21:25:38 +03:00
parent 29d842b0bf
commit c6ceba2ed5
8 changed files with 63 additions and 34 deletions

View File

@ -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
}

View File

@ -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)

View File

@ -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)

View File

@ -21,22 +21,16 @@ internal fun splitPathIntoBucketAndPath(path: Path): Pair<String, Path> {
}
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 {

View File

@ -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

View File

@ -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())
}
}

View File

@ -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.*

View File

@ -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