directory watcher

This commit is contained in:
Alexander Nozik 2021-11-27 18:42:08 +03:00
parent ce6da61fcf
commit 5e33bdbce7
5 changed files with 66 additions and 14 deletions

View File

@ -35,7 +35,10 @@ import hep.dataforge.storage.StorageManager
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import java.nio.file.* import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
import kotlin.streams.asSequence import kotlin.streams.asSequence
/** /**
@ -100,12 +103,12 @@ class FileStorage(
} }
} }
/** // /**
* Creating a watch service or reusing one from parent // * Creating a watch service or reusing one from parent
*/ // */
private val watchService: WatchService by lazy { // private val watchService: WatchService by lazy {
(parent as? FileStorage)?.watchService ?: path.fileSystem.newWatchService() // (parent as? FileStorage)?.watchService ?: path.fileSystem.newWatchService()
} // }
//TODO actually watch for file change //TODO actually watch for file change

View File

@ -56,9 +56,9 @@ class NumassDataLoader(
override fun getConnectionHelper(): ConnectionHelper = _connectionHelper override fun getConnectionHelper(): ConnectionHelper = _connectionHelper
override val meta: Meta by lazy { override val meta: Meta get() {
val metaPath = path.resolve("meta") val metaPath = path.resolve("meta")
NumassEnvelopeType.infer(metaPath)?.reader?.read(metaPath)?.meta ?: Meta.empty() return NumassEnvelopeType.infer(metaPath)?.reader?.read(metaPath)?.meta ?: Meta.empty()
} }
override suspend fun getHvData(): Table? { override suspend fun getHvData(): Table? {
@ -73,7 +73,6 @@ class NumassDataLoader(
} }
} }
private val pointEnvelopes: List<Envelope> by lazy { private val pointEnvelopes: List<Envelope> by lazy {
Files.list(path) Files.list(path)
.filter { it.fileName.toString().startsWith(POINT_FRAGMENT_NAME) } .filter { it.fileName.toString().startsWith(POINT_FRAGMENT_NAME) }

View File

@ -15,19 +15,39 @@
*/ */
package inr.numass.data.storage package inr.numass.data.storage
import hep.dataforge.connections.ConnectionHelper
import hep.dataforge.context.Context import hep.dataforge.context.Context
import hep.dataforge.context.Global import hep.dataforge.context.Global
import hep.dataforge.events.Event import hep.dataforge.events.Event
import hep.dataforge.events.EventBuilder import hep.dataforge.events.EventBuilder
import hep.dataforge.io.envelopes.Envelope
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
import hep.dataforge.nullable
import hep.dataforge.storage.StorageElement import hep.dataforge.storage.StorageElement
import hep.dataforge.storage.StorageManager
import hep.dataforge.storage.files.FileStorage import hep.dataforge.storage.files.FileStorage
import hep.dataforge.storage.files.FileStorageElement import hep.dataforge.storage.files.FileStorageElement
import hep.dataforge.storage.files.FileStorageElementType
import inr.numass.data.NumassEnvelopeType import inr.numass.data.NumassEnvelopeType
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
class EnvelopeStorageElement(
override val context: Context,
override val name: String,
override val meta: Meta,
override val path: Path,
override val parent: StorageElement?,
) : FileStorageElement {
private val _connectionHelper by lazy { ConnectionHelper(this) }
override fun getConnectionHelper(): ConnectionHelper = _connectionHelper
val envelope: Envelope? get() = NumassEnvelopeType.infer(path)?.reader?.read(path)
}
/** /**
* Numass storage directory. Works as a normal directory, but creates a numass loader from each directory with meta * Numass storage directory. Works as a normal directory, but creates a numass loader from each directory with meta
*/ */
@ -44,7 +64,25 @@ class NumassDirectory : FileStorage.Directory() {
return if (Files.isDirectory(path) && meta != null) { return if (Files.isDirectory(path) && meta != null) {
NumassDataLoader(context, parent, path.fileName.toString(), path) NumassDataLoader(context, parent, path.fileName.toString(), path)
} else { } else {
super.read(context, path, parent, meta) val name = meta?.optString("name").nullable ?: path.fileName.toString()
val type = meta?.optString("type").nullable?.let {
context.load<StorageManager>().getType(it)
} as? FileStorageElementType
if (type == null || type is FileStorage.Directory) {
// Read path as directory if type not found and path is directory
if (Files.isDirectory(path)) {
FileStorage(context, name, meta ?: Meta.empty(), path, parent, this)
} else {
EnvelopeStorageElement(context, name, meta ?: Meta.empty(), path, parent)
}
} else {
//Otherwise, delegate to the type
type.read(context, path, parent)
}.also {
if (it != null && parent == null) {
context.load<StorageManager>().register(it)
}
}
} }
} }

View File

@ -102,7 +102,7 @@ class DataController : Controller(), ContextAware {
if (watchPath != null) { if (watchPath != null) {
Files.list(watchPath).toList() Files.list(watchPath).toList()
.filter { .filter {
!Files.isDirectory(it) && it.fileName.startsWith(NumassDataLoader.POINT_FRAGMENT_NAME) !Files.isDirectory(it) && it.fileName.toString().startsWith(NumassDataLoader.POINT_FRAGMENT_NAME)
} }
.sortedBy { file -> .sortedBy { file ->
val attr = Files.readAttributes(file, BasicFileAttributes::class.java) val attr = Files.readAttributes(file, BasicFileAttributes::class.java)

View File

@ -8,8 +8,10 @@ import hep.dataforge.names.AlphanumComparator
import hep.dataforge.storage.Storage import hep.dataforge.storage.Storage
import hep.dataforge.storage.files.FileTableLoader import hep.dataforge.storage.files.FileTableLoader
import hep.dataforge.storage.tables.TableLoader import hep.dataforge.storage.tables.TableLoader
import inr.numass.data.NumassDataUtils
import inr.numass.data.api.NumassPoint import inr.numass.data.api.NumassPoint
import inr.numass.data.api.NumassSet import inr.numass.data.api.NumassSet
import inr.numass.data.storage.EnvelopeStorageElement
import inr.numass.data.storage.NumassDataLoader import inr.numass.data.storage.NumassDataLoader
import javafx.beans.property.SimpleBooleanProperty import javafx.beans.property.SimpleBooleanProperty
import javafx.beans.property.SimpleObjectProperty import javafx.beans.property.SimpleObjectProperty
@ -81,8 +83,18 @@ class StorageView : View(title = "Numass storage", icon = dfIconView) {
val children: ObservableList<Container>? by lazy { val children: ObservableList<Container>? by lazy {
when (content) { when (content) {
is Storage -> content.getChildren().map { is Storage -> content.getChildren().mapNotNull {
buildContainer(it, this) if (it is EnvelopeStorageElement) {
it.envelope?.let { envelope ->
try {
buildContainer(NumassDataUtils.read(envelope), this)
} catch (ex: Exception) {
null
}
}
} else {
buildContainer(it, this)
}
}.sortedWith(Comparator.comparing({ it.id }, AlphanumComparator)).asObservable() }.sortedWith(Comparator.comparing({ it.id }, AlphanumComparator)).asObservable()
is NumassSet -> content.points is NumassSet -> content.points
.sortedBy { it.index } .sortedBy { it.index }