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());
}
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;
}
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());
}
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());
}
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);
}
Aggregations