Search in sources :

Example 46 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class FileNodePersistor method loadBufferedDataTable.

private BufferedDataTable loadBufferedDataTable(final Node node, final NodeSettingsRO settings, final ExecutionMonitor execMon, final Map<Integer, BufferedDataTable> loadTblRep, final int index, final WorkflowDataRepository dataRepository) throws InvalidSettingsException, IOException, CanceledExecutionException {
    // in 1.1.x and before the settings.xml contained the location
    // of the data table specs file (spec_0.xml, e.g.). From 1.2.0 on,
    // the spec is saved in data/data_0/spec.xml
    boolean isVersion11x = settings.containsKey(CFG_SPEC_FILES);
    ExecutionMonitor execSubData = execMon.createSubProgress(0.25);
    ReferencedFile nodeDirectory = getNodeDirectory();
    if (isVersion11x) {
        /* In version 1.1.x the data was stored in a different way. The
             * data.xml that is now contained in the data/data_x/ directory was
             * aggregated in a data.xml file directly in the m_nodeDir. Also the
             * spec was located at a different location.
             */
        String dataConfigFileName = settings.getString(CFG_DATA_FILE);
        File nodeDir = nodeDirectory.getFile();
        // dataConfigFile = data.xml in node dir
        File dataConfigFile = new File(nodeDir, dataConfigFileName);
        NodeSettingsRO dataSettings = NodeSettings.loadFromXML(new BufferedInputStream(new FileInputStream(dataConfigFile)));
        String dataPath = dataSettings.getString(CFG_DATA_FILE_DIR);
        // dataDir = /data
        ReferencedFile dataDirRef = new ReferencedFile(nodeDirectory, dataPath);
        // note: we do not check for existence here - in some cases
        // this directory may not exist (when exported and empty
        // directories are pruned)
        NodeSettingsRO portSettings = dataSettings.getNodeSettings(CFG_OUTPUT_PREFIX + index);
        String dataName = portSettings.getString(CFG_DATA_FILE_DIR);
        // dir = /data/data_i
        ReferencedFile dirRef = new ReferencedFile(dataDirRef, dataName);
        BufferedDataTable t = BufferedDataTable.loadFromFile(dirRef, portSettings, execSubData, loadTblRep, // no blobs or file stores in 1.1.x
        WorkflowDataRepository.OLD_WORKFLOWS_INSTANCE);
        t.setOwnerRecursively(node);
        return t;
    } else {
        NodeSettingsRO dataSettings = settings.getNodeSettings(CFG_DATA_FILE);
        String dataDirStr = dataSettings.getString(CFG_DATA_FILE_DIR);
        ReferencedFile dataDirRef = new ReferencedFile(nodeDirectory, dataDirStr);
        NodeSettingsRO portSettings = dataSettings.getNodeSettings(CFG_OUTPUT_PREFIX + index);
        String dataName = portSettings.getString(CFG_DATA_FILE_DIR);
        ReferencedFile dirRef = new ReferencedFile(dataDirRef, dataName);
        File dir = dirRef.getFile();
        readDirectory(dir);
        BufferedDataTable t = BufferedDataTable.loadFromFile(dirRef, /* ignored in 1.2.0+ */
        null, execMon, loadTblRep, dataRepository);
        t.setOwnerRecursively(node);
        return t;
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 47 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class FileNodePersistor method saveInternalHeldTables.

private static void saveInternalHeldTables(final Node node, final ReferencedFile nodeDirRef, final NodeSettingsWO settings, final Set<Integer> savedTableIDs, final ExecutionMonitor exec, final boolean saveData) throws IOException, CanceledExecutionException {
    PortObject[] internalObjects = node.getInternalHeldPortObjects();
    if (internalObjects == null || !saveData) {
        return;
    }
    final int internalTblsCount = internalObjects.length;
    NodeSettingsWO subSettings = settings.addNodeSettings("internalObjects");
    String subDirName = INTERNAL_TABLE_FOLDER_PREFIX;
    ReferencedFile subDirFile = new ReferencedFile(nodeDirRef, subDirName);
    subSettings.addString("location", subDirName);
    NodeSettingsWO portSettings = subSettings.addNodeSettings("content");
    FileUtil.deleteRecursively(subDirFile.getFile());
    subDirFile.getFile().mkdirs();
    exec.setMessage("Saving internally held objects");
    for (int i = 0; i < internalTblsCount; i++) {
        PortObject t = internalObjects[i];
        String objName = "object_" + i;
        ExecutionMonitor subProgress = exec.createSubProgress(1.0 / internalTblsCount);
        NodeSettingsWO singlePortSetting = portSettings.addNodeSettings(objName);
        ReferencedFile portDirRef = new ReferencedFile(subDirFile, objName);
        File portDir = portDirRef.getFile();
        singlePortSetting.addInt("index", i);
        if (t == null) {
            singlePortSetting.addString("type", "null");
        } else if (t instanceof BufferedDataTable) {
            BufferedDataTable table = (BufferedDataTable) t;
            saveBufferedDataTable(table, savedTableIDs, createDirectory(portDir), exec);
            singlePortSetting.addString("type", "table");
            singlePortSetting.addString("table_dir_location", objName);
        } else {
            // other type of port object - object might be in output
            int outputPortIndex = IntStream.range(0, node.getNrOutPorts()).filter(p -> node.getOutputObject(p) == t).findFirst().orElse(-1);
            if (outputPortIndex >= 0) {
                singlePortSetting.addString("type", "referenced_output");
                singlePortSetting.addInt("outport", outputPortIndex);
            } else {
                singlePortSetting.addString("type", "non-table");
                singlePortSetting.addString("port_dir_location", objName);
                savePortObject(t.getSpec(), t, createDirectory(portDir), singlePortSetting, exec);
            }
        }
        subProgress.setProgress(1.0);
    }
}
Also used : Arrays(java.util.Arrays) BufferedInputStream(java.io.BufferedInputStream) ReferencedFile(org.knime.core.internal.ReferencedFile) FileStoreUtil(org.knime.core.data.filestore.FileStoreUtil) IWriteFileStoreHandler(org.knime.core.data.filestore.internal.IWriteFileStoreHandler) TableStoreFormatUnknownException(org.knime.core.data.container.storage.TableStoreFormatUnknownException) PortObjectSpecSerializer(org.knime.core.node.port.PortObjectSpec.PortObjectSpecSerializer) Map(java.util.Map) WriteFileStoreHandler(org.knime.core.data.filestore.internal.WriteFileStoreHandler) InactiveBranchPortObjectSpec(org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec) PortType(org.knime.core.node.port.PortType) PortObjectZipOutputStream(org.knime.core.node.port.PortObjectZipOutputStream) Set(java.util.Set) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) FileStoreKey(org.knime.core.data.filestore.FileStoreKey) List(java.util.List) Optional(java.util.Optional) CheckUtils(org.knime.core.node.util.CheckUtils) PortObject(org.knime.core.node.port.PortObject) IntStream(java.util.stream.IntStream) PortObjectZipInputStream(org.knime.core.node.port.PortObjectZipInputStream) NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) DataTableSpec(org.knime.core.data.DataTableSpec) FileNativeNodeContainerPersistor(org.knime.core.node.workflow.FileNativeNodeContainerPersistor) LoadVersion(org.knime.core.util.LoadVersion) PortObjectSpecZipInputStream(org.knime.core.node.port.PortObjectSpecZipInputStream) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PortTypeRegistry(org.knime.core.node.port.PortTypeRegistry) PortObjectSerializer(org.knime.core.node.port.PortObject.PortObjectSerializer) PortUtil(org.knime.core.node.port.PortUtil) LoadResult(org.knime.core.node.workflow.WorkflowPersistor.LoadResult) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) WorkflowLoadHelper(org.knime.core.node.workflow.WorkflowLoadHelper) FileOutputStream(java.io.FileOutputStream) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) EmptyFileStoreHandler(org.knime.core.data.filestore.internal.EmptyFileStoreHandler) PortObjectSpecZipOutputStream(org.knime.core.node.port.PortObjectSpecZipOutputStream) NodeContext(org.knime.core.node.workflow.NodeContext) File(java.io.File) WorkflowPersistor(org.knime.core.node.workflow.WorkflowPersistor) WorkflowDataRepository(org.knime.core.node.workflow.WorkflowDataRepository) FileUtil(org.knime.core.util.FileUtil) IFileStoreHandler(org.knime.core.data.filestore.internal.IFileStoreHandler) FlowVariablePortObjectSpec(org.knime.core.node.port.flowvariable.FlowVariablePortObjectSpec) PortObject(org.knime.core.node.port.PortObject) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 48 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class FileNodePersistor method loadFileStoreHandler.

IFileStoreHandler loadFileStoreHandler(final Node node, final ExecutionMonitor execMon, final NodeSettingsRO settings, final WorkflowDataRepository workflowDataRepository) throws InvalidSettingsException {
    if (getLoadVersion().isOlderThan(LoadVersion.V260)) {
        return new EmptyFileStoreHandler(workflowDataRepository);
    }
    NodeSettingsRO fsSettings = settings.getNodeSettings("filestores");
    String dirNameInFlow = fsSettings.getString("file_store_location");
    if (dirNameInFlow == null) {
        return new EmptyFileStoreHandler(workflowDataRepository);
    } else {
        String uuidS = fsSettings.getString("file_store_id");
        UUID uuid = UUID.fromString(uuidS);
        ReferencedFile subDirFile = new ReferencedFile(getNodeDirectory(), dirNameInFlow);
        IFileStoreHandler fsh = WriteFileStoreHandler.restore(node.getName(), uuid, workflowDataRepository, subDirFile.getFile());
        return fsh;
    }
}
Also used : IFileStoreHandler(org.knime.core.data.filestore.internal.IFileStoreHandler) EmptyFileStoreHandler(org.knime.core.data.filestore.internal.EmptyFileStoreHandler) UUID(java.util.UUID) ReferencedFile(org.knime.core.internal.ReferencedFile)

Example 49 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class FileNodePersistor method savePorts.

private static void savePorts(final Node node, final ReferencedFile nodeDirRef, final NodeSettingsWO settings, final Set<Integer> savedTableIDs, final ExecutionMonitor exec, final boolean saveData) throws IOException, CanceledExecutionException {
    if (node.getNrOutPorts() == 0) {
        return;
    }
    final int portCount = node.getNrOutPorts();
    NodeSettingsWO portSettings = settings.addNodeSettings("ports");
    exec.setMessage("Saving outport data");
    // starting at port 1 (ignore default flow variable output)
    for (int i = 1; i < portCount; i++) {
        String portName = PORT_FOLDER_PREFIX + i;
        ExecutionMonitor subProgress = exec.createSubProgress(1.0 / portCount);
        NodeSettingsWO singlePortSetting = portSettings.addNodeSettings(portName);
        singlePortSetting.addInt("index", i);
        PortObject object = node.getOutputObject(i);
        String portDirName;
        if (object != null && saveData) {
            portDirName = portName;
            ReferencedFile portDirRef = new ReferencedFile(nodeDirRef, portDirName);
            File portDir = portDirRef.getFile();
            subProgress.setMessage("Cleaning directory " + portDir.getAbsolutePath());
            FileUtil.deleteRecursively(portDir);
            if (!portDir.mkdir() && !portDir.isDirectory()) {
                throw new IOException("Cannot create port directory " + portDir.getAbsolutePath() + " (" + "exists: " + portDir.exists() + ", isDir: " + portDir.isDirectory() + ", " + "parent permissions: " + (portDir.getParentFile().canRead() ? "r" : "-") + (portDir.getParentFile().canWrite() ? "w" : "-") + (portDir.getParentFile().canExecute() ? "x" : "-") + ")");
            }
            if (!portDir.canWrite()) {
                throw new IOException("Cannot write to port directory " + portDir.getAbsolutePath());
            }
            savePort(node, portDir, singlePortSetting, savedTableIDs, subProgress, i, saveData);
        } else {
            portDirName = null;
        }
        singlePortSetting.addString("port_dir_location", portDirName);
        subProgress.setProgress(1.0);
    }
}
Also used : IOException(java.io.IOException) PortObject(org.knime.core.node.port.PortObject) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 50 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class FileNodePersistor method save.

/**
 * Saves the node, node settings, and all internal structures, spec, data, and models, to the given node directory
 * (located at the node file).
 *
 * @param nnc ...
 * @param settings ...
 * @param execMon Used to report progress during saving.
 * @param nodeDirRef Directory associated with node - will create internals folder in it
 * @param isSaveData  ...
 * @throws IOException If the node file can't be found or read.
 * @throws CanceledExecutionException If the saving has been canceled.
 * @since 2.9
 */
public static void save(final NativeNodeContainer nnc, final NodeSettingsWO settings, final ExecutionMonitor execMon, final ReferencedFile nodeDirRef, final boolean isSaveData) throws IOException, CanceledExecutionException {
    final Node node = nnc.getNode();
    saveCustomName(node, settings);
    saveHasContent(node, settings);
    saveIsInactive(node, settings);
    ReferencedFile nodeInternDirRef = getNodeInternDirectory(nodeDirRef);
    File nodeInternDir = nodeInternDirRef.getFile();
    if (nodeInternDir.exists()) {
        FileUtil.deleteRecursively(nodeInternDir);
    }
    ExecutionMonitor internalMon = execMon.createSilentSubProgress(0.2);
    ExecutionMonitor portMon = execMon.createSilentSubProgress(0.5);
    ExecutionMonitor intTblsMon = execMon.createSilentSubProgress(0.1);
    ExecutionMonitor fileStoreMon = execMon.createSilentSubProgress(0.1);
    execMon.setMessage("Internals");
    boolean isSaveInternals = isSaveData;
    if (isSaveInternals) {
        // do not save data for inactive nodes
        isSaveInternals = node.isInactiveBranchConsumer() || !node.isInactive();
    }
    if (isSaveInternals) {
        NodeContext.pushContext(nnc);
        try {
            saveNodeInternDirectory(node, nodeInternDir, settings, internalMon);
        } finally {
            NodeContext.removeLastContext();
        }
    }
    internalMon.setProgress(1.0);
    /* A hash set of all tables that originate from the corresponding node
         * (owner ID of the table equals NodeID), which have already been saved.
         * It is used to avoid multiple saves of the same table, e.g. when one
         * table is returned in multiple outputs or if an output table is used
         * as "internal" held table. See bug 2117. */
    final Set<Integer> savedTableIDs = new HashSet<Integer>();
    execMon.setMessage("Ports");
    savePorts(node, nodeDirRef, settings, savedTableIDs, portMon, isSaveData);
    portMon.setProgress(1.0);
    execMon.setMessage("Internal Tables");
    saveInternalHeldTables(node, nodeDirRef, settings, savedTableIDs, internalMon, isSaveData);
    intTblsMon.setProgress(1.0);
    // save them last as now all tables have been saved (all cells ran through persistor) and all
    // FileStore#getFile() have been called and saved
    execMon.setMessage("File Store Objects");
    saveFileStoreObjects(node, nodeDirRef, settings, fileStoreMon, isSaveData);
    fileStoreMon.setProgress(1.0);
    execMon.setProgress(1.0);
}
Also used : ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

ReferencedFile (org.knime.core.internal.ReferencedFile)58 File (java.io.File)35 IOException (java.io.IOException)28 BufferedInputStream (java.io.BufferedInputStream)15 FileInputStream (java.io.FileInputStream)14 PortObject (org.knime.core.node.port.PortObject)12 FlowVariablePortObject (org.knime.core.node.port.flowvariable.FlowVariablePortObject)12 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)11 FileStorePortObject (org.knime.core.data.filestore.FileStorePortObject)10 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)10 InactiveBranchPortObject (org.knime.core.node.port.inactive.InactiveBranchPortObject)10 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)8 FlowVariablePortObjectSpec (org.knime.core.node.port.flowvariable.FlowVariablePortObjectSpec)8 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)8 InactiveBranchPortObjectSpec (org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec)7 FileOutputStream (java.io.FileOutputStream)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 NodeSettings (org.knime.core.node.NodeSettings)6 DataTableSpec (org.knime.core.data.DataTableSpec)5