From 5e33bdbce7c7f99939506165cc3988e72a992903 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 27 Nov 2021 18:42:08 +0300 Subject: [PATCH] directory watcher --- .../dataforge/storage/files/FileStorage.kt | 17 ++++---- .../numass/data/storage/NumassDataLoader.kt | 5 +-- .../{NumassStorage.kt => NumassDirectory.kt} | 40 ++++++++++++++++++- .../inr/numass/viewer/DataController.kt | 2 +- .../kotlin/inr/numass/viewer/StorageView.kt | 16 +++++++- 5 files changed, 66 insertions(+), 14 deletions(-) rename numass-core/src/main/kotlin/inr/numass/data/storage/{NumassStorage.kt => NumassDirectory.kt} (86%) diff --git a/dataforge-storage/src/main/kotlin/hep/dataforge/storage/files/FileStorage.kt b/dataforge-storage/src/main/kotlin/hep/dataforge/storage/files/FileStorage.kt index e98089dd..23fcdd11 100644 --- a/dataforge-storage/src/main/kotlin/hep/dataforge/storage/files/FileStorage.kt +++ b/dataforge-storage/src/main/kotlin/hep/dataforge/storage/files/FileStorage.kt @@ -35,7 +35,10 @@ import hep.dataforge.storage.StorageManager import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll 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 /** @@ -100,12 +103,12 @@ class FileStorage( } } - /** - * Creating a watch service or reusing one from parent - */ - private val watchService: WatchService by lazy { - (parent as? FileStorage)?.watchService ?: path.fileSystem.newWatchService() - } +// /** +// * Creating a watch service or reusing one from parent +// */ +// private val watchService: WatchService by lazy { +// (parent as? FileStorage)?.watchService ?: path.fileSystem.newWatchService() +// } //TODO actually watch for file change diff --git a/numass-core/src/main/kotlin/inr/numass/data/storage/NumassDataLoader.kt b/numass-core/src/main/kotlin/inr/numass/data/storage/NumassDataLoader.kt index cc2f035f..b1682a8d 100644 --- a/numass-core/src/main/kotlin/inr/numass/data/storage/NumassDataLoader.kt +++ b/numass-core/src/main/kotlin/inr/numass/data/storage/NumassDataLoader.kt @@ -56,9 +56,9 @@ class NumassDataLoader( override fun getConnectionHelper(): ConnectionHelper = _connectionHelper - override val meta: Meta by lazy { + override val meta: Meta get() { 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? { @@ -73,7 +73,6 @@ class NumassDataLoader( } } - private val pointEnvelopes: List by lazy { Files.list(path) .filter { it.fileName.toString().startsWith(POINT_FRAGMENT_NAME) } diff --git a/numass-core/src/main/kotlin/inr/numass/data/storage/NumassStorage.kt b/numass-core/src/main/kotlin/inr/numass/data/storage/NumassDirectory.kt similarity index 86% rename from numass-core/src/main/kotlin/inr/numass/data/storage/NumassStorage.kt rename to numass-core/src/main/kotlin/inr/numass/data/storage/NumassDirectory.kt index 8342acae..5d4cb0c2 100644 --- a/numass-core/src/main/kotlin/inr/numass/data/storage/NumassStorage.kt +++ b/numass-core/src/main/kotlin/inr/numass/data/storage/NumassDirectory.kt @@ -15,19 +15,39 @@ */ package inr.numass.data.storage +import hep.dataforge.connections.ConnectionHelper import hep.dataforge.context.Context import hep.dataforge.context.Global import hep.dataforge.events.Event import hep.dataforge.events.EventBuilder +import hep.dataforge.io.envelopes.Envelope import hep.dataforge.meta.Meta +import hep.dataforge.nullable import hep.dataforge.storage.StorageElement +import hep.dataforge.storage.StorageManager import hep.dataforge.storage.files.FileStorage import hep.dataforge.storage.files.FileStorageElement +import hep.dataforge.storage.files.FileStorageElementType import inr.numass.data.NumassEnvelopeType import kotlinx.coroutines.runBlocking import java.nio.file.Files 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 */ @@ -44,7 +64,25 @@ class NumassDirectory : FileStorage.Directory() { return if (Files.isDirectory(path) && meta != null) { NumassDataLoader(context, parent, path.fileName.toString(), path) } 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().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().register(it) + } + } } } diff --git a/numass-viewer/src/main/kotlin/inr/numass/viewer/DataController.kt b/numass-viewer/src/main/kotlin/inr/numass/viewer/DataController.kt index 7f45e9a6..83c30d09 100644 --- a/numass-viewer/src/main/kotlin/inr/numass/viewer/DataController.kt +++ b/numass-viewer/src/main/kotlin/inr/numass/viewer/DataController.kt @@ -102,7 +102,7 @@ class DataController : Controller(), ContextAware { if (watchPath != null) { Files.list(watchPath).toList() .filter { - !Files.isDirectory(it) && it.fileName.startsWith(NumassDataLoader.POINT_FRAGMENT_NAME) + !Files.isDirectory(it) && it.fileName.toString().startsWith(NumassDataLoader.POINT_FRAGMENT_NAME) } .sortedBy { file -> val attr = Files.readAttributes(file, BasicFileAttributes::class.java) diff --git a/numass-viewer/src/main/kotlin/inr/numass/viewer/StorageView.kt b/numass-viewer/src/main/kotlin/inr/numass/viewer/StorageView.kt index 15334251..c083915e 100644 --- a/numass-viewer/src/main/kotlin/inr/numass/viewer/StorageView.kt +++ b/numass-viewer/src/main/kotlin/inr/numass/viewer/StorageView.kt @@ -8,8 +8,10 @@ import hep.dataforge.names.AlphanumComparator import hep.dataforge.storage.Storage import hep.dataforge.storage.files.FileTableLoader import hep.dataforge.storage.tables.TableLoader +import inr.numass.data.NumassDataUtils import inr.numass.data.api.NumassPoint import inr.numass.data.api.NumassSet +import inr.numass.data.storage.EnvelopeStorageElement import inr.numass.data.storage.NumassDataLoader import javafx.beans.property.SimpleBooleanProperty import javafx.beans.property.SimpleObjectProperty @@ -81,8 +83,18 @@ class StorageView : View(title = "Numass storage", icon = dfIconView) { val children: ObservableList? by lazy { when (content) { - is Storage -> content.getChildren().map { - buildContainer(it, this) + is Storage -> content.getChildren().mapNotNull { + 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() is NumassSet -> content.points .sortedBy { it.index }