Search in sources :

Example 66 with MutableLong

use of org.apache.commons.lang3.mutable.MutableLong in project apex-malhar by apache.

the class MovingBoundaryTimeBucketAssignerTest method testTimeBucketKeyExpiry.

@Test
public void testTimeBucketKeyExpiry() {
    final MutableLong purgeLessThanEqualTo = new MutableLong(-2);
    testMeta.timeBucketAssigner.setExpireBefore(Duration.standardSeconds(1));
    testMeta.timeBucketAssigner.setBucketSpan(Duration.standardSeconds(1));
    testMeta.timeBucketAssigner.setPurgeListener(new TimeBucketAssigner.PurgeListener() {

        @Override
        public void purgeTimeBucketsLessThanEqualTo(long timeBucket) {
            purgeLessThanEqualTo.setValue(timeBucket);
        }
    });
    long referenceTime = testMeta.timeBucketAssigner.getReferenceInstant().getMillis();
    testMeta.timeBucketAssigner.setup(testMeta.mockManagedStateContext);
    Assert.assertEquals("purgeLessThanEqualTo", -2L, purgeLessThanEqualTo.longValue());
    long time0 = Duration.standardSeconds(0).getMillis() + referenceTime;
    Assert.assertEquals("time bucket", 1, testMeta.timeBucketAssigner.getTimeBucket(time0));
    testMeta.timeBucketAssigner.endWindow();
    Assert.assertEquals("purgeLessThanEqualTo", -1, purgeLessThanEqualTo.longValue());
    long time1 = Duration.standardSeconds(9).getMillis() + referenceTime;
    Assert.assertEquals("time bucket", 10, testMeta.timeBucketAssigner.getTimeBucket(time1));
    testMeta.timeBucketAssigner.endWindow();
    Assert.assertEquals("purgeLessThanEqualTo", 8, purgeLessThanEqualTo.longValue());
    long time2 = Duration.standardSeconds(10).getMillis() + referenceTime;
    Assert.assertEquals("time bucket", 11, testMeta.timeBucketAssigner.getTimeBucket(time2));
    testMeta.timeBucketAssigner.endWindow();
    Assert.assertEquals("purgeLessThanEqualTo", 9, purgeLessThanEqualTo.longValue());
    // Check for expiry of time1 now
    Assert.assertEquals("time bucket", -1, testMeta.timeBucketAssigner.getTimeBucket(time1));
    testMeta.timeBucketAssigner.endWindow();
    Assert.assertEquals("purgeLessThanEqualTo", 9, purgeLessThanEqualTo.longValue());
    testMeta.timeBucketAssigner.teardown();
}
Also used : MutableLong(org.apache.commons.lang3.mutable.MutableLong) Test(org.junit.Test)

Example 67 with MutableLong

use of org.apache.commons.lang3.mutable.MutableLong in project apex-malhar by apache.

the class AbstractSingleFileOutputOperatorTest method checkpoint.

private CheckPointOutputOperator checkpoint(AbstractSingleFileOutputOperator<Integer> writer) {
    CheckPointOutputOperator checkPointWriter = new CheckPointOutputOperator();
    checkPointWriter.counts = Maps.newHashMap();
    for (String keys : writer.counts.keySet()) {
        checkPointWriter.counts.put(keys, new MutableLong(writer.counts.get(keys).longValue()));
    }
    checkPointWriter.endOffsets = Maps.newHashMap();
    for (String keys : writer.endOffsets.keySet()) {
        checkPointWriter.endOffsets.put(keys, new MutableLong(writer.endOffsets.get(keys).longValue()));
    }
    checkPointWriter.openPart = Maps.newHashMap();
    for (String keys : writer.openPart.keySet()) {
        checkPointWriter.openPart.put(keys, new MutableInt(writer.openPart.get(keys).intValue()));
    }
    checkPointWriter.filePath = writer.filePath;
    checkPointWriter.maxOpenFiles = writer.maxOpenFiles;
    checkPointWriter.replication = writer.replication;
    checkPointWriter.totalBytesWritten = writer.totalBytesWritten;
    checkPointWriter.maxLength = writer.maxLength;
    checkPointWriter.rollingFile = writer.rollingFile;
    checkPointWriter.outputFileName = writer.outputFileName;
    return checkPointWriter;
}
Also used : MutableLong(org.apache.commons.lang.mutable.MutableLong) MutableInt(org.apache.commons.lang3.mutable.MutableInt)

Example 68 with MutableLong

use of org.apache.commons.lang3.mutable.MutableLong in project knime-core by knime.

the class RuleEngine2PortsNodeModel method computeRearrangerWithoutPMML.

/**
 * @param spec
 * @param rules
 * @param flowVars
 * @param ruleIdx
 * @param outcomeIdx
 * @param outputColumnName
 * @return
 * @throws InterruptedException
 * @throws InvalidSettingsException
 */
private Pair<ColumnRearranger, PortObject> computeRearrangerWithoutPMML(final DataTableSpec spec, final RowInput rules, final Map<String, FlowVariable> flowVars, final int ruleIdx, final int outcomeIdx, final String outputColumnName) throws InterruptedException, InvalidSettingsException {
    PortObject po;
    ColumnRearranger ret;
    RuleFactory factory = RuleFactory.getInstance(RuleNodeSettings.RuleEngine).cloned();
    po = InactiveBranchPortObject.INSTANCE;
    ret = new ColumnRearranger(spec);
    factory.disableMissingComparisons();
    factory.disableNaNComparisons();
    final List<Rule> ruleList = new ArrayList<>();
    int lineNo = 0;
    DataRow ruleRow;
    while ((ruleRow = rules.poll()) != null) {
        lineNo++;
        DataCell cell = ruleRow.getCell(ruleIdx);
        CheckUtils.checkSetting(!cell.isMissing(), "Missing rule in row: " + ruleRow.getKey());
        if (cell instanceof StringValue) {
            StringValue sv = (StringValue) cell;
            String ruleText = sv.getStringValue();
            if (outcomeIdx >= 0) {
                try {
                    ruleText += " => " + m_settings.asStringFailForMissing(ruleRow.getCell(outcomeIdx));
                } catch (InvalidSettingsException e) {
                    if (RuleSupport.isComment(ruleText)) {
                        ruleText += " => ?";
                    } else {
                        throw e;
                    }
                }
            }
            try {
                Rule rule = factory.parse(ruleText, spec, flowVars);
                if (rule.getCondition().isEnabled()) {
                    ruleList.add(rule);
                }
            } catch (ParseException e) {
                ParseException error = Util.addContext(e, ruleText, lineNo);
                throw new InvalidSettingsException("Wrong rule in line: " + ruleRow.getKey() + "\n" + error.getMessage(), error);
            }
        } else {
            CheckUtils.checkSetting(false, "Wrong type (" + cell.getType() + ") of rule: " + cell + "\nin row: " + ruleRow.getKey());
        }
    }
    // unfortunately we cannot compute the domain and limits of the output column.
    final DataType outType = RuleEngineNodeModel.computeOutputType(ruleList, computeOutcomeType(rules.getDataTableSpec()), RuleNodeSettings.RuleEngine, m_settings.isDisallowLongOutputForCompatibility());
    final MutableLong rowIndex = new MutableLong();
    final ExecutionMonitor exec = new ExecutionMonitor();
    final boolean disallowLongOutputForCompatibility = m_settings.isDisallowLongOutputForCompatibility();
    VariableProvider.SingleCellFactoryProto fac = new VariableProvider.SingleCellFactoryProto(new DataColumnSpecCreator(outputColumnName, outType).createSpec()) {

        @Override
        public DataCell getCell(final DataRow row) {
            setProgress(rowIndex.longValue(), m_rowCount, row.getKey(), exec);
            rowIndex.increment();
            return RuleEngineNodeModel.getRulesOutcome(outType, row, ruleList, disallowLongOutputForCompatibility, this);
        }

        @Override
        public Object readVariable(final String name, final Class<?> type) {
            return RuleEngine2PortsNodeModel.this.readVariable(name, type);
        }

        @Deprecated
        @Override
        public int getRowCount() {
            return RuleEngine2PortsNodeModel.this.getRowCount();
        }

        @Override
        public long getRowCountLong() {
            return RuleEngine2PortsNodeModel.this.getRowCountLong();
        }
    };
    if (m_settings.isReplaceColumn()) {
        ret.replace(fac, outputColumnName);
    } else {
        ret.append(fac);
    }
    return Pair.create(ret, po);
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) RuleFactory(org.knime.base.node.rules.engine.RuleFactory) ArrayList(java.util.ArrayList) DataRow(org.knime.core.data.DataRow) MutableLong(org.apache.commons.lang3.mutable.MutableLong) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) VariableProvider(org.knime.base.node.rules.engine.VariableProvider) FlowVariableProvider(org.knime.ext.sun.nodes.script.calculator.FlowVariableProvider) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType) Rule(org.knime.base.node.rules.engine.Rule) SimpleRule(org.dmg.pmml.SimpleRuleDocument.SimpleRule) ParseException(java.text.ParseException) StringValue(org.knime.core.data.StringValue) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject)

Aggregations

MutableLong (org.apache.commons.lang3.mutable.MutableLong)66 Test (org.junit.Test)45 IOException (java.io.IOException)11 Query (org.apache.apex.malhar.lib.appdata.schemas.Query)6 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)5 PageCache (org.neo4j.io.pagecache.PageCache)4 File (java.io.File)3 Map (java.util.Map)3 KeyedWindowedOperatorImpl (org.apache.apex.malhar.lib.window.impl.KeyedWindowedOperatorImpl)3 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)3 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 TreeMap (java.util.TreeMap)2 ConsoleOutputOperator (org.apache.apex.malhar.lib.io.ConsoleOutputOperator)2 SpillableComplexComponentImpl (org.apache.apex.malhar.lib.state.spillable.SpillableComplexComponentImpl)2 Tuple (org.apache.apex.malhar.lib.window.Tuple)2 WindowOption (org.apache.apex.malhar.lib.window.WindowOption)2 WindowState (org.apache.apex.malhar.lib.window.WindowState)2 SumLong (org.apache.apex.malhar.lib.window.accumulation.SumLong)2 WindowedOperatorImpl (org.apache.apex.malhar.lib.window.impl.WindowedOperatorImpl)2