refactor file reading

This commit is contained in:
Alexander Nozik 2022-05-25 19:00:12 +03:00
parent 4833128857
commit b8869570ce
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
2 changed files with 19 additions and 13 deletions

View File

@ -37,7 +37,6 @@ public fun interface IOWriter<in T> {
public fun writeObject(output: Output, obj: T) public fun writeObject(output: Output, obj: T)
} }
/** /**
* And interface for reading and writing objects into with IO streams * And interface for reading and writing objects into with IO streams
*/ */
@ -83,7 +82,7 @@ public object DoubleIOFormat : IOFormat<Double>, IOFormatFactory<Double> {
override val type: KType get() = typeOf<Double>() override val type: KType get() = typeOf<Double>()
override fun writeObject(output: Output, obj: kotlin.Double) { override fun writeObject(output: Output, obj: Double) {
output.writeDouble(obj) output.writeDouble(obj)
} }

View File

@ -4,6 +4,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import space.kscience.dataforge.context.error
import space.kscience.dataforge.context.logger
import space.kscience.dataforge.data.* import space.kscience.dataforge.data.*
import space.kscience.dataforge.io.* import space.kscience.dataforge.io.*
import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.Meta
@ -48,7 +50,7 @@ public class FileData<T> internal constructor(private val data: Data<T>) : Data<
public val META_FILE_PATH_KEY: Name = META_FILE_KEY + "path" public val META_FILE_PATH_KEY: Name = META_FILE_KEY + "path"
public val META_FILE_EXTENSION_KEY: Name = META_FILE_KEY + "extension" public val META_FILE_EXTENSION_KEY: Name = META_FILE_KEY + "extension"
public val META_FILE_CREATE_TIME_KEY: Name = META_FILE_KEY + "created" public val META_FILE_CREATE_TIME_KEY: Name = META_FILE_KEY + "created"
public val META_FILE_UPDATE_TIME_KEY: Name = META_FILE_KEY + "update" public val META_FILE_UPDATE_TIME_KEY: Name = META_FILE_KEY + "updated"
} }
} }
@ -229,16 +231,21 @@ public fun <T : Any> DataSetBuilder<T>.file(
path: Path, path: Path,
formatResolver: FileFormatResolver<out T>, formatResolver: FileFormatResolver<out T>,
) { ) {
//If path is a single file or a special directory, read it as single datum try {
if (!Files.isDirectory(path) || Files.list(path).allMatch { it.fileName.toString().startsWith("@") }) {
val data = readDataFile(path, formatResolver) //If path is a single file or a special directory, read it as single datum
val name = data.meta[Envelope.ENVELOPE_NAME_KEY].string ?: path.nameWithoutExtension if (!Files.isDirectory(path) || Files.list(path).allMatch { it.fileName.toString().startsWith("@") }) {
data(name, data) val data = readDataFile(path, formatResolver)
} else { val name = data.meta[Envelope.ENVELOPE_NAME_KEY].string ?: path.nameWithoutExtension
//otherwise, read as directory data(name, data)
val data = readDataDirectory(dataType, path, formatResolver) } else {
val name = data.meta[Envelope.ENVELOPE_NAME_KEY].string ?: path.nameWithoutExtension //otherwise, read as directory
node(name, data) val data = readDataDirectory(dataType, path, formatResolver)
val name = data.meta[Envelope.ENVELOPE_NAME_KEY].string ?: path.nameWithoutExtension
node(name, data)
}
} catch (ex: Exception) {
logger.error { "Failed to read file or directory at $path: ${ex.message}" }
} }
} }