Search in sources :

Example 1 with Counter

use of org.pentaho.di.core.Counter in project pentaho-kettle by pentaho.

the class AddSequence method init.

public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
    meta = (AddSequenceMeta) smi;
    data = (AddSequenceData) sdi;
    if (super.init(smi, sdi)) {
        data.realSchemaName = environmentSubstitute(meta.getSchemaName());
        data.realSequenceName = environmentSubstitute(meta.getSequenceName());
        if (meta.isDatabaseUsed()) {
            Database db = new Database(this, meta.getDatabase());
            db.shareVariablesWith(this);
            data.setDb(db);
            try {
                if (getTransMeta().isUsingUniqueConnections()) {
                    synchronized (getTrans()) {
                        data.getDb().connect(getTrans().getTransactionId(), getPartitionID());
                    }
                } else {
                    data.getDb().connect(getPartitionID());
                }
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "AddSequence.Log.ConnectedDB"));
                }
                return true;
            } catch (KettleDatabaseException dbe) {
                logError(BaseMessages.getString(PKG, "AddSequence.Log.CouldNotConnectToDB") + dbe.getMessage());
            }
        } else if (meta.isCounterUsed()) {
            // Do the environment translations of the counter values.
            boolean doAbort = false;
            try {
                data.start = Long.parseLong(environmentSubstitute(meta.getStartAt()));
            } catch (NumberFormatException ex) {
                logError(BaseMessages.getString(PKG, "AddSequence.Log.CouldNotParseCounterValue", "start", meta.getStartAt(), environmentSubstitute(meta.getStartAt()), ex.getMessage()));
                doAbort = true;
            }
            try {
                data.increment = Long.parseLong(environmentSubstitute(meta.getIncrementBy()));
            } catch (NumberFormatException ex) {
                logError(BaseMessages.getString(PKG, "AddSequence.Log.CouldNotParseCounterValue", "increment", meta.getIncrementBy(), environmentSubstitute(meta.getIncrementBy()), ex.getMessage()));
                doAbort = true;
            }
            try {
                data.maximum = Long.parseLong(environmentSubstitute(meta.getMaxValue()));
            } catch (NumberFormatException ex) {
                logError(BaseMessages.getString(PKG, "AddSequence.Log.CouldNotParseCounterValue", "increment", meta.getMaxValue(), environmentSubstitute(meta.getMaxValue()), ex.getMessage()));
                doAbort = true;
            }
            if (doAbort) {
                return false;
            }
            String realCounterName = environmentSubstitute(meta.getCounterName());
            if (!Utils.isEmpty(realCounterName)) {
                data.setLookup("@@sequence:" + meta.getCounterName());
            } else {
                data.setLookup("@@sequence:" + meta.getValuename());
            }
            if (getTrans().getCounters() != null) {
                // check if counter exists
                synchronized (getTrans().getCounters()) {
                    data.counter = getTrans().getCounters().get(data.getLookup());
                    if (data.counter == null) {
                        // create a new one
                        data.counter = new Counter(data.start, data.increment, data.maximum);
                        getTrans().getCounters().put(data.getLookup(), data.counter);
                    } else {
                        // defined counter with the same name.
                        if ((data.counter.getStart() != data.start) || (data.counter.getIncrement() != data.increment) || (data.counter.getMaximum() != data.maximum)) {
                            logError(BaseMessages.getString(PKG, "AddSequence.Log.CountersWithDifferentCharacteristics", data.getLookup()));
                            return false;
                        }
                    }
                }
                return true;
            } else {
                logError(BaseMessages.getString(PKG, "AddSequence.Log.TransformationCountersHashtableNotAllocated"));
            }
        } else {
            logError(BaseMessages.getString(PKG, "AddSequence.Log.NeedToSelectSequence"));
        }
    }
    return false;
}
Also used : Counter(org.pentaho.di.core.Counter) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) Database(org.pentaho.di.core.database.Database)

Example 2 with Counter

use of org.pentaho.di.core.Counter in project pentaho-kettle by pentaho.

the class Database method getNextValue.

public synchronized Long getNextValue(Hashtable<String, Counter> counters, String schemaName, String tableName, String val_key) throws KettleDatabaseException {
    Long nextValue = null;
    String schemaTable = databaseMeta.getQuotedSchemaTableCombination(schemaName, tableName);
    String lookup = schemaTable + "." + databaseMeta.quoteField(val_key);
    // Try to find the previous sequence value...
    Counter counter = null;
    if (counters != null) {
        counter = counters.get(lookup);
    }
    if (counter == null) {
        RowMetaAndData rmad = getOneRow("SELECT MAX(" + databaseMeta.quoteField(val_key) + ") FROM " + schemaTable);
        if (rmad != null) {
            long previous;
            try {
                Long tmp = rmad.getRowMeta().getInteger(rmad.getData(), 0);
                // null.
                if (tmp != null) {
                    previous = tmp.longValue();
                } else {
                    previous = 0L;
                }
            } catch (KettleValueException e) {
                throw new KettleDatabaseException("Error getting the first long value from the max value returned from table : " + schemaTable);
            }
            counter = new Counter(previous + 1, 1);
            nextValue = Long.valueOf(counter.next());
            if (counters != null) {
                counters.put(lookup, counter);
            }
        } else {
            throw new KettleDatabaseException("Couldn't find maximum key value from table " + schemaTable);
        }
    } else {
        nextValue = Long.valueOf(counter.next());
    }
    return nextValue;
}
Also used : Counter(org.pentaho.di.core.Counter) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 3 with Counter

use of org.pentaho.di.core.Counter in project pentaho-kettle by pentaho.

the class KettleDatabaseRepositoryConnectionDelegate method getNextID.

public synchronized LongObjectId getNextID(String tableName, String fieldName) throws KettleException {
    String counterName = tableName + "." + fieldName;
    Counter counter = Counters.getInstance().getCounter(counterName);
    if (counter == null) {
        LongObjectId id = getNextTableID(tableName, fieldName);
        counter = new Counter(id.longValue());
        Counters.getInstance().setCounter(counterName, counter);
        return new LongObjectId(counter.next());
    } else {
        return new LongObjectId(counter.next());
    }
}
Also used : Counter(org.pentaho.di.core.Counter) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) LongObjectId(org.pentaho.di.repository.LongObjectId)

Aggregations

Counter (org.pentaho.di.core.Counter)3 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)2 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)2 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)1 Database (org.pentaho.di.core.database.Database)1 KettleValueException (org.pentaho.di.core.exception.KettleValueException)1 LongObjectId (org.pentaho.di.repository.LongObjectId)1