use of com.datastax.driver.mapping.Mapper in project storm by apache.
the class ObjectMapperCqlStatementMapper method map.
@Override
public List<Statement> map(Map<String, Object> map, Session session, ITuple tuple) {
final ObjectMapperOperation operation = (ObjectMapperOperation) tuple.getValueByField(operationField);
Preconditions.checkNotNull(operation, "Operation must not be null");
final Object value = tuple.getValueByField(valueField);
final Object timestampObject = timestampField != null ? tuple.getValueByField(timestampField) : null;
final Object ttlObject = ttlField != null ? tuple.getValueByField(ttlField) : null;
final ConsistencyLevel consistencyLevel = consistencyLevelField != null ? (ConsistencyLevel) tuple.getValueByField(consistencyLevelField) : null;
final Class<?> valueClass = value.getClass();
final Mapper mapper = getMappingManager(session).mapper(valueClass);
Collection<Option> options = new ArrayList<>();
if (timestampObject != null) {
if (timestampObject instanceof Number) {
options.add(Option.timestamp(((Number) timestampObject).longValue()));
} else if (timestampObject instanceof Instant) {
Instant timestamp = (Instant) timestampObject;
options.add(Option.timestamp(timestamp.getEpochSecond() * 1000_0000L + timestamp.getNano() / 1000L));
}
}
if (ttlObject != null) {
if (ttlObject instanceof Number) {
options.add(Option.ttl(((Number) ttlObject).intValue()));
} else if (ttlObject instanceof Duration) {
Duration ttl = (Duration) ttlObject;
options.add(Option.ttl((int) ttl.getSeconds()));
}
}
if (consistencyLevel != null) {
options.add(Option.consistencyLevel(consistencyLevel));
}
if (operation == ObjectMapperOperation.SAVE) {
options.add(Option.saveNullFields(true));
return Arrays.asList(mapper.saveQuery(value, options.toArray(new Option[options.size()])));
} else if (operation == ObjectMapperOperation.SAVE_IGNORE_NULLS) {
options.add(Option.saveNullFields(false));
return Arrays.asList(mapper.saveQuery(value, options.toArray(new Option[options.size()])));
} else if (operation == ObjectMapperOperation.DELETE) {
return Arrays.asList(mapper.deleteQuery(value, options.toArray(new Option[options.size()])));
} else {
throw new UnsupportedOperationException("Unknown operation: " + operation);
}
}
Aggregations