// generic IMetric insertion. All other metric insertion methods could use this one.
public void insertMetrics(Collection<IMetric> metrics, ColumnFamily cf) throws ConnectionException {
Timer.Context ctx = Instrumentation.getWriteTimerContext(cf);
Multimap<Locator, IMetric> map = asMultimap(metrics);
MutationBatch batch = keyspace.prepareMutationBatch();
try {
for (Locator locator : map.keySet()) {
ColumnListMutation<Long> mutation = batch.withRow(cf, locator);
// we want to insert a locator only for non-string, non-boolean metrics. If there happen to be string or
// boolean metrics mixed in with numeric metrics, we still want to insert a locator. If all metrics
// are boolean or string, we DO NOT want to insert a locator.
boolean locatorInsertOk = false;
for (IMetric metric : map.get(locator)) {
boolean shouldPersist = true;
// todo: MetricsPersistenceOptimizerFactory interface needs to be retooled to accept IMetric
if (metric instanceof Metric) {
final boolean isString = DataType.isStringMetric(metric.getMetricValue());
final boolean isBoolean = DataType.isBooleanMetric(metric.getMetricValue());
if (!isString && !isBoolean)
locatorInsertOk = true;
shouldPersist = shouldPersist((Metric)metric);
} else {
locatorInsertOk = true;
}
if (shouldPersist) {
mutation.putColumn(
metric.getCollectionTime(),
metric.getMetricValue(),
(AbstractSerializer) (NumericSerializer.serializerFor(metric.getMetricValue().getClass())),
metric.getTtlInSeconds());
}
}
if (!AstyanaxWriter.isLocatorCurrent(locator)) {
if (locatorInsertOk)
insertLocator(locator, batch);
AstyanaxWriter.setLocatorCurrent(locator);
}
}
try {
batch.execute();
} catch (ConnectionException e) {
Instrumentation.markWriteError(e);
log.error("Connection exception persisting data", e);
throw e;
}