Search in sources :

Example 1 with QueryExecutionException

use of com.datastax.oss.driver.api.core.servererrors.QueryExecutionException in project automatiko-engine by automatiko-io.

the class CassandraProcessInstances method create.

@Override
public void create(String id, ProcessInstance instance) {
    String resolvedId = resolveId(id, instance);
    if (isActive(instance)) {
        LOGGER.debug("create() called for instance {}", resolvedId);
        byte[] data = codec.encode(marshaller.marhsallProcessInstance(instance));
        if (data == null) {
            return;
        }
        Collection<String> tags = new LinkedHashSet<>(instance.tags().values());
        tags.add(resolvedId);
        if (instance.businessKey() != null) {
            tags.add(instance.businessKey());
        }
        Insert insert = insertInto(config.keyspace().orElse("automatiko"), tableName).value(INSTANCE_ID_FIELD, literal(resolvedId)).value(VERSION_FIELD, literal(((AbstractProcessInstance<?>) instance).getVersionTracker())).value(STATUS_FIELD, literal(((AbstractProcessInstance<?>) instance).status())).value(CONTENT_FIELD, bindMarker()).value(TAGS_FIELD, bindMarker()).ifNotExists();
        try {
            ResultSet rs = cqlSession.execute(cqlSession.prepare(insert.build()).bind(ByteBuffer.wrap(data), tags));
            if (!rs.wasApplied()) {
                throw new ProcessInstanceDuplicatedException(id);
            }
        } catch (QueryExecutionException e) {
            throw new ProcessInstanceDuplicatedException(id);
        } finally {
            cachedInstances.remove(resolvedId);
            cachedInstances.remove(id);
            disconnect(instance);
        }
    } else if (isPending(instance)) {
        if (cachedInstances.putIfAbsent(resolvedId, instance) != null) {
            throw new ProcessInstanceDuplicatedException(id);
        }
    } else {
        cachedInstances.remove(resolvedId);
        cachedInstances.remove(id);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) ProcessInstanceDuplicatedException(io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException) QueryExecutionException(com.datastax.oss.driver.api.core.servererrors.QueryExecutionException) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Insert(com.datastax.oss.driver.api.querybuilder.insert.Insert)

Aggregations

ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)1 QueryExecutionException (com.datastax.oss.driver.api.core.servererrors.QueryExecutionException)1 Insert (com.datastax.oss.driver.api.querybuilder.insert.Insert)1 ProcessInstanceDuplicatedException (io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException)1 AbstractProcessInstance (io.automatiko.engine.workflow.AbstractProcessInstance)1 LinkedHashSet (java.util.LinkedHashSet)1