Search in sources :

Example 1 with TableStoreFormat

use of org.knime.core.data.container.storage.TableStoreFormat in project knime-core by knime.

the class BufferedDataTable method save.

/**
 * Saves the table to a directory and writes some settings to the argument
 * NodeSettingsWO object. It will also write the reference table in case
 * this node is responsible for it (i.e. this node created the reference
 * table).
 * @param dir The directory to write to.
 * @param savedTableIDs Ids of tables that were previously saved, used to identify
 * tables that are referenced by the same nodes multiple times.
 * @param exec The progress monitor for cancellation.
 * @throws IOException If writing fails.
 * @throws CanceledExecutionException If canceled.
 */
void save(final File dir, final Set<Integer> savedTableIDs, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    NodeSettings s = new NodeSettings(CFG_TABLE_META);
    Integer bufferedTableID = getBufferedTableId();
    s.addInt(CFG_TABLE_ID, bufferedTableID);
    File outFile = new File(dir, TABLE_FILE);
    if (!savedTableIDs.add(bufferedTableID)) {
        s.addString(CFG_TABLE_TYPE, TABLE_TYPE_REFERENCE_IN_SAME_NODE);
    } else if (m_delegate instanceof BufferedContainerTable) {
        final TableStoreFormat format = ((BufferedContainerTable) m_delegate).getTableStoreFormat();
        if (!DefaultTableStoreFormat.class.equals(format.getClass())) {
            // use different identifier to cause old versions of KNIME to fail loading newer workflows
            s.addString(CFG_TABLE_TYPE, TABLE_TYPE_CONTAINER_CUSTOM);
            s.addString(CFG_TABLE_CONTAINER_FORMAT, format.getClass().getName());
            s.addString(CFG_TABLE_CONTAINER_FORMAT_VERSION, format.getVersion());
        } else {
            final DefaultTableStoreFormat defaultFormat = (DefaultTableStoreFormat) format;
            if (!Arrays.asList(NONE, GZIP).contains(defaultFormat.getCompressionFormat())) {
                s.addString(CFG_TABLE_TYPE, TABLE_TYPE_CONTAINER_COMPRESS);
                s.addString(CFG_TABLE_COMPRESSION_FORMAT, defaultFormat.getCompressionFormat().toString());
            } else {
                s.addString(CFG_TABLE_TYPE, TABLE_TYPE_CONTAINER);
            }
        }
        m_delegate.saveToFile(outFile, s, exec);
    } else {
        if (m_delegate instanceof RearrangeColumnsTable) {
            final ContainerTable containerTable = ((RearrangeColumnsTable) m_delegate).getAppendTable();
            if (containerTable != null && containerTable instanceof BufferedContainerTable) {
                final BufferedContainerTable appendTable = (BufferedContainerTable) containerTable;
                final TableStoreFormat format = appendTable.getTableStoreFormat();
                if (!DefaultTableStoreFormat.class.equals(format.getClass())) {
                    // use different identifier to cause old versions of KNIME to fail loading newer workflows
                    s.addString(CFG_TABLE_TYPE, TABLE_TYPE_REARRANGE_COLUMN_CUSTOM);
                    s.addString(CFG_TABLE_CONTAINER_FORMAT, appendTable.getTableStoreFormat().getClass().getName());
                    s.addString(CFG_TABLE_CONTAINER_FORMAT_VERSION, appendTable.getTableStoreFormat().getVersion());
                } else {
                    final DefaultTableStoreFormat defaultFormat = (DefaultTableStoreFormat) format;
                    if (!Arrays.asList(NONE, GZIP).contains(defaultFormat.getCompressionFormat())) {
                        s.addString(CFG_TABLE_TYPE, TABLE_TYPE_REARRANGE_COLUMN_COMPRESS);
                        s.addString(CFG_TABLE_COMPRESSION_FORMAT, defaultFormat.getCompressionFormat().toString());
                    } else {
                        s.addString(CFG_TABLE_TYPE, TABLE_TYPE_REARRANGE_COLUMN);
                    }
                }
            } else {
                s.addString(CFG_TABLE_TYPE, TABLE_TYPE_REARRANGE_COLUMN);
            }
        } else if (m_delegate instanceof TableSpecReplacerTable) {
            s.addString(CFG_TABLE_TYPE, TABLE_TYPE_NEW_SPEC);
        } else if (m_delegate instanceof WrappedTable) {
            s.addString(CFG_TABLE_TYPE, TABLE_TYPE_WRAPPED);
        } else if (m_delegate instanceof JoinedTable) {
            s.addString(CFG_TABLE_TYPE, TABLE_TYPE_JOINED);
        } else if (m_delegate instanceof VoidTable) {
            s.addString(CFG_TABLE_TYPE, TABLE_TYPE_VOID);
        } else if (m_delegate instanceof ConcatenateTable) {
            s.addString(CFG_TABLE_TYPE, TABLE_TYPE_CONCATENATE);
        } else {
            assert m_delegate instanceof ExtensionTable;
            s.addString(CFG_TABLE_TYPE, TABLE_TYPE_EXTENSION);
        }
        BufferedDataTable[] references = m_delegate.getReferenceTables();
        ArrayList<String> referenceDirs = new ArrayList<String>();
        for (BufferedDataTable reference : references) {
            if (reference.getOwner() == getOwner() && !savedTableIDs.contains(reference.getBufferedTableId())) {
                int index = referenceDirs.size();
                String dirName = "r" + index;
                File subDir = new File(dir, dirName);
                if (!subDir.mkdir() && !subDir.isDirectory()) {
                    throw new IOException("Could not create directory " + subDir.getAbsolutePath());
                }
                if (!subDir.canWrite()) {
                    throw new IOException("Unable to write directory " + subDir.getAbsolutePath());
                }
                referenceDirs.add(dirName);
                reference.save(subDir, savedTableIDs, exec);
            }
        }
        s.addStringArray(CFG_TABLE_REFERENCE, referenceDirs.toArray(new String[referenceDirs.size()]));
        m_delegate.saveToFile(outFile, s, exec);
    }
    // only write the data file to the settings if it has been created
    if (outFile.exists()) {
        s.addString(CFG_TABLE_FILE_NAME, TABLE_FILE);
    } else {
        s.addString(CFG_TABLE_FILE_NAME, null);
    }
    saveSpec(getDataTableSpec(), dir);
    File dataXML = new File(dir, TABLE_DESCRIPTION_FILE);
    try (OutputStream out = new BufferedOutputStream(new FileOutputStream(dataXML))) {
        s.saveToXML(out);
    }
}
Also used : VoidTable(org.knime.core.data.container.VoidTable) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) JoinedTable(org.knime.core.data.container.JoinedTable) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ContainerTable(org.knime.core.data.container.ContainerTable) BufferedContainerTable(org.knime.core.data.container.BufferedContainerTable) WrappedTable(org.knime.core.data.container.WrappedTable) BufferedContainerTable(org.knime.core.data.container.BufferedContainerTable) RearrangeColumnsTable(org.knime.core.data.container.RearrangeColumnsTable) DefaultTableStoreFormat(org.knime.core.data.container.DefaultTableStoreFormat) TableStoreFormat(org.knime.core.data.container.storage.TableStoreFormat) FileOutputStream(java.io.FileOutputStream) DefaultTableStoreFormat(org.knime.core.data.container.DefaultTableStoreFormat) ConcatenateTable(org.knime.core.data.container.ConcatenateTable) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) TableSpecReplacerTable(org.knime.core.data.container.TableSpecReplacerTable)

Example 2 with TableStoreFormat

use of org.knime.core.data.container.storage.TableStoreFormat in project knime-core by knime.

the class BufferSettings method getOutputFormat.

/**
 * Returns the {@link TableStoreFormat} used to read and write the {@link Buffer Buffer's} content.
 *
 * @param spec the spec of the table to read/write
 * @return the {@link TableStoreFormat}
 */
TableStoreFormat getOutputFormat(final DataTableSpec spec) {
    if (m_outputFormat.accepts(spec)) {
        LOGGER.debugWithFormat("Using table format %s", m_outputFormat.getClass().getName());
        return m_outputFormat;
    }
    final TableStoreFormat storeFormat = TableStoreFormatRegistry.getInstance().getFormatFor(spec);
    LOGGER.debugWithFormat("Cannot use table format '%s' as it does not support the table schema, " + "using '%s' instead", m_outputFormat.getClass().getName(), storeFormat.getClass().getName());
    return storeFormat;
}
Also used : TableStoreFormat(org.knime.core.data.container.storage.TableStoreFormat)

Example 3 with TableStoreFormat

use of org.knime.core.data.container.storage.TableStoreFormat in project knime-core by knime.

the class BufferSettingsTest method testChangedValues.

/**
 * Tests that values can be changed and that the default instance stays unchanged.
 */
@SuppressWarnings("static-method")
@Test
public void testChangedValues() {
    final BufferSettings def = BufferSettings.getDefault();
    final int lruCacheSize = def.getLRUCacheSize() * -1;
    final boolean useLRU = !def.useLRU();
    final TableStoreFormat outputFormat = new DefaultTableStoreFormat();
    final BufferSettings settings = // 
    BufferSettings.getDefault().withOutputFormat(// 
    outputFormat).withLRU(// 
    useLRU).withLRUCacheSize(lruCacheSize);
    assertEquals("Modified settings created wrong LRU cache size", lruCacheSize, settings.getLRUCacheSize());
    assertEquals("Modified settings created wrong enable LRU flag", useLRU, settings.useLRU());
    assertTrue("Modified settings created wrong output format", outputFormat == settings.getOutputFormat(new DataTableSpecCreator().createSpec()));
    assertFalse("Default settings has been modified (output format)", def.getOutputFormat(new DataTableSpecCreator().createSpec()) == settings.getOutputFormat(new DataTableSpecCreator().createSpec()));
}
Also used : TableStoreFormat(org.knime.core.data.container.storage.TableStoreFormat) DataTableSpecCreator(org.knime.core.data.DataTableSpecCreator) Test(org.junit.Test)

Example 4 with TableStoreFormat

use of org.knime.core.data.container.storage.TableStoreFormat in project knime-core by knime.

the class BufferedDataTable method checkFormat.

private static void checkFormat(final NodeSettingsRO settings) throws InvalidSettingsException {
    String formatFQN = CheckUtils.checkSettingNotNull(settings.getString(CFG_TABLE_CONTAINER_FORMAT), "Container format is null");
    TableStoreFormat format = TableStoreFormatRegistry.getInstance().getTableStoreFormat(formatFQN);
    String versionString = CheckUtils.checkSettingNotNull(settings.getString(CFG_TABLE_CONTAINER_FORMAT_VERSION), "Version string is null");
    CheckUtils.checkSetting(format.validateVersion(versionString), "Unsupported version \"%s\" for table format \"%s\"", versionString, format.getClass().getName());
}
Also used : DefaultTableStoreFormat(org.knime.core.data.container.DefaultTableStoreFormat) TableStoreFormat(org.knime.core.data.container.storage.TableStoreFormat)

Aggregations

TableStoreFormat (org.knime.core.data.container.storage.TableStoreFormat)4 DefaultTableStoreFormat (org.knime.core.data.container.DefaultTableStoreFormat)2 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 DataTableSpecCreator (org.knime.core.data.DataTableSpecCreator)1 BufferedContainerTable (org.knime.core.data.container.BufferedContainerTable)1 ConcatenateTable (org.knime.core.data.container.ConcatenateTable)1 ContainerTable (org.knime.core.data.container.ContainerTable)1 JoinedTable (org.knime.core.data.container.JoinedTable)1 RearrangeColumnsTable (org.knime.core.data.container.RearrangeColumnsTable)1 TableSpecReplacerTable (org.knime.core.data.container.TableSpecReplacerTable)1 VoidTable (org.knime.core.data.container.VoidTable)1 WrappedTable (org.knime.core.data.container.WrappedTable)1 ReferencedFile (org.knime.core.internal.ReferencedFile)1