Search in sources :

Example 1 with PagingIterable

use of com.datastax.driver.core.PagingIterable in project eventapis by kloiasoft.

the class CassandraViewQuery method queryByOpId.

@Override
public List<E> queryByOpId(String opId, Function<String, E> findOne) throws EventStoreException {
    Select select = QueryBuilder.select(CassandraEventRecorder.ENTITY_ID, CassandraEventRecorder.VERSION).from(tableNameByOps);
    select.where(QueryBuilder.eq(CassandraEventRecorder.OP_ID, opId));
    List<Row> entityEventDatas = cassandraSession.execute(select, PagingIterable::all);
    Map<String, E> resultList = new HashMap<>();
    for (Row entityEvent : entityEventDatas) {
        String entityId = entityEvent.getString(CassandraEventRecorder.ENTITY_ID);
        int version = entityEvent.getInt(CassandraEventRecorder.VERSION);
        E snapshot = findOne.apply(entityId);
        E newEntity = null;
        if (snapshot == null || snapshot.getVersion() > version) {
            newEntity = queryEntity(entityId);
        } else if (snapshot.getVersion() < version) {
            newEntity = queryEntity(entityId, version, snapshot);
        } else {
            log.debug("Up-to-date Snapshot:" + snapshot);
        }
        if (!resultList.containsKey(entityId)) {
            if (newEntity != null)
                resultList.put(entityId, newEntity);
        }
    }
    return new ArrayList<>(resultList.values());
}
Also used : HashMap(java.util.HashMap) PagingIterable(com.datastax.driver.core.PagingIterable) Select(com.datastax.driver.core.querybuilder.Select) ArrayList(java.util.ArrayList) Row(com.datastax.driver.core.Row)

Example 2 with PagingIterable

use of com.datastax.driver.core.PagingIterable in project eventapis by kloiasoft.

the class CassandraViewQuery method queryEntityInternal.

private E queryEntityInternal(String entityId, Select select, E previousEntity) throws EventStoreException {
    E currentEntity = previousEntity;
    List<Row> entityEventDatas = cassandraSession.execute(select, PagingIterable::all);
    for (Row entityEventData : entityEventDatas) {
        EntityEvent entityEvent = convertToEntityEvent(entityEventData);
        if (entityEvent.getStatus() == EventState.CREATED) {
            EntityFunctionSpec<E, ?> functionSpec = functionMap.get(entityEvent.getEventType());
            if (functionSpec != null) {
                EntityEventWrapper eventWrapper = new EntityEventWrapper<>(functionSpec.getQueryType(), objectMapper, entityEvent);
                EntityFunction<E, ?> entityFunction = functionSpec.getEntityFunction();
                currentEntity = (E) entityFunction.apply(currentEntity, eventWrapper);
            } else
                log.trace("Function Spec is not available for " + entityEvent.getEventType() + " EntityId:" + entityId + " Table:" + tableName);
        }
        if (currentEntity != null) {
            currentEntity.setId(entityId);
            currentEntity.setVersion(entityEvent.getEventKey().getVersion());
        }
    }
    return (currentEntity == null || currentEntity.getId() == null) ? null : currentEntity;
}
Also used : PagingIterable(com.datastax.driver.core.PagingIterable) EntityEventWrapper(com.kloia.eventapis.view.EntityEventWrapper) Row(com.datastax.driver.core.Row)

Example 3 with PagingIterable

use of com.datastax.driver.core.PagingIterable in project eventapis by kloiasoft.

the class CassandraEventRecorder method markFail.

@Override
public List<EntityEvent> markFail(String key) {
    Select select = QueryBuilder.select().from(tableNameByOps);
    select.where(QueryBuilder.eq(OP_ID, key));
    List<Row> entityEventDatas = cassandraSession.execute(select, PagingIterable::all);
    return entityEventDatas.stream().map(CassandraViewQuery::convertToEntityEvent).filter(entityEvent -> {
        try {
            Update update = QueryBuilder.update(tableName);
            update.where(QueryBuilder.eq(ENTITY_ID, entityEvent.getEventKey().getEntityId())).and(QueryBuilder.eq(VERSION, entityEvent.getEventKey().getVersion())).ifExists();
            update.with(QueryBuilder.set(STATUS, "FAILED"));
            ResultSet execute = cassandraSession.execute(update);
            log.debug("Failure Mark Result:" + execute.toString() + " Update: " + update.toString());
            return true;
        } catch (Exception e) {
            log.warn(e.getMessage(), e);
            return false;
        }
    }).collect(Collectors.toList());
}
Also used : QueryBuilder(com.datastax.driver.core.querybuilder.QueryBuilder) IdCreationStrategy(com.kloia.eventapis.api.IdCreationStrategy) OperationContext(com.kloia.eventapis.common.OperationContext) Row(com.datastax.driver.core.Row) Date(java.util.Date) EventKey(com.kloia.eventapis.common.EventKey) EventRecorder(com.kloia.eventapis.common.EventRecorder) Function(java.util.function.Function) PagingIterable(com.datastax.driver.core.PagingIterable) RecordedEvent(com.kloia.eventapis.common.RecordedEvent) ResultSet(com.datastax.driver.core.ResultSet) UUIDCreationStrategy(com.kloia.eventapis.api.impl.UUIDCreationStrategy) EventStoreException(com.kloia.eventapis.exception.EventStoreException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Update(com.datastax.driver.core.querybuilder.Update) Insert(com.datastax.driver.core.querybuilder.Insert) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) EventState(com.kloia.eventapis.pojos.EventState) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) IUserContext(com.kloia.eventapis.api.IUserContext) Views(com.kloia.eventapis.api.Views) Optional(java.util.Optional) Select(com.datastax.driver.core.querybuilder.Select) PagingIterable(com.datastax.driver.core.PagingIterable) Select(com.datastax.driver.core.querybuilder.Select) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) Update(com.datastax.driver.core.querybuilder.Update) EventStoreException(com.kloia.eventapis.exception.EventStoreException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 4 with PagingIterable

use of com.datastax.driver.core.PagingIterable in project eventapis by kloiasoft.

the class CassandraViewQuery method queryByOpId.

@Override
public List<E> queryByOpId(String opId) throws EventStoreException {
    Select select = QueryBuilder.select(CassandraEventRecorder.ENTITY_ID).from(tableNameByOps);
    select.where(QueryBuilder.eq(CassandraEventRecorder.OP_ID, opId));
    List<Row> entityEventDatas = cassandraSession.execute(select, PagingIterable::all);
    Map<String, E> resultList = new HashMap<>();
    for (Row entityEvent : entityEventDatas) {
        String entityId = entityEvent.getString(CassandraEventRecorder.ENTITY_ID);
        if (!resultList.containsKey(entityId)) {
            E value = queryEntity(entityId);
            if (value != null)
                resultList.put(entityId, value);
        }
    }
    return new ArrayList<>(resultList.values());
}
Also used : HashMap(java.util.HashMap) PagingIterable(com.datastax.driver.core.PagingIterable) Select(com.datastax.driver.core.querybuilder.Select) ArrayList(java.util.ArrayList) Row(com.datastax.driver.core.Row)

Example 5 with PagingIterable

use of com.datastax.driver.core.PagingIterable in project eventapis by kloiasoft.

the class CassandraViewQuery method queryEvent.

@Override
public EntityEvent queryEvent(String entityId, int version) throws EventStoreException {
    Select select = QueryBuilder.select().from(tableName);
    select.where(QueryBuilder.eq(CassandraEventRecorder.ENTITY_ID, entityId));
    select.where(QueryBuilder.eq(CassandraEventRecorder.VERSION, version));
    Row one = cassandraSession.execute(select, PagingIterable::one);
    return one == null ? null : convertToEntityEvent(one);
}
Also used : PagingIterable(com.datastax.driver.core.PagingIterable) Select(com.datastax.driver.core.querybuilder.Select) Row(com.datastax.driver.core.Row)

Aggregations

PagingIterable (com.datastax.driver.core.PagingIterable)6 Row (com.datastax.driver.core.Row)6 Select (com.datastax.driver.core.querybuilder.Select)4 EventStoreException (com.kloia.eventapis.exception.EventStoreException)2 EntityEventWrapper (com.kloia.eventapis.view.EntityEventWrapper)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ResultSet (com.datastax.driver.core.ResultSet)1 Insert (com.datastax.driver.core.querybuilder.Insert)1 QueryBuilder (com.datastax.driver.core.querybuilder.QueryBuilder)1 Update (com.datastax.driver.core.querybuilder.Update)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IUserContext (com.kloia.eventapis.api.IUserContext)1 IdCreationStrategy (com.kloia.eventapis.api.IdCreationStrategy)1 Views (com.kloia.eventapis.api.Views)1 UUIDCreationStrategy (com.kloia.eventapis.api.impl.UUIDCreationStrategy)1 EventKey (com.kloia.eventapis.common.EventKey)1 EventRecorder (com.kloia.eventapis.common.EventRecorder)1 OperationContext (com.kloia.eventapis.common.OperationContext)1