package me.prettyprint.cassandra.model;

import java.util.HashMap;
import java.util.Map;

import me.prettyprint.cassandra.service.OperationType;
import me.prettyprint.hector.api.HConsistencyLevel;
import me.prettyprint.hector.api.ConsistencyLevelPolicy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Configurable and Runtime adjustable ConsistencyLevelPolicy
 * @author zznate
 */
public class ConfigurableConsistencyLevel implements ConsistencyLevelPolicy {
  private final Logger log = LoggerFactory.getLogger(ConfigurableConsistencyLevel.class);

  private Map<String, HConsistencyLevel> readCfConsistencyLevels = new HashMap<String, HConsistencyLevel>();
  private Map<String, HConsistencyLevel> writeCfConsistencyLevels = new HashMap<String, HConsistencyLevel>();
  private HConsistencyLevel defaultReadConsistencyLevel = HConsistencyLevel.QUORUM;
  private HConsistencyLevel defaultWriteConsistencyLevel = HConsistencyLevel.QUORUM;

  @Override
  public HConsistencyLevel get(OperationType op) {
    return (op == OperationType.READ) ? defaultReadConsistencyLevel : defaultWriteConsistencyLevel;
  }

  @Override
  public HConsistencyLevel get(OperationType op, String cfName) {
    if (op == OperationType.READ) {
      HConsistencyLevel rcf = readCfConsistencyLevels.get(cfName);
      return rcf != null ? rcf : defaultReadConsistencyLevel;
    } else {
      HConsistencyLevel wcf = writeCfConsistencyLevels.get(cfName);
      return wcf != null ? wcf : defaultWriteConsistencyLevel;
    }
  }

  public void setReadCfConsistencyLevels(Map<String, HConsistencyLevel> columnFamilyConsistencyLevels) {
    this.readCfConsistencyLevels = columnFamilyConsistencyLevels;
  }

  public void setWriteCfConsistencyLevels(Map<String, HConsistencyLevel> columnFamilyConsistencyLevels) {
    this.writeCfConsistencyLevels = columnFamilyConsistencyLevels;
  }

  public void setConsistencyLevelForCfOperation(HConsistencyLevel consistencyLevel,
      String columnFamily,
      OperationType operationType) {
    if (operationType == OperationType.READ) {
      readCfConsistencyLevels.put(columnFamily, consistencyLevel);
    } else {
      writeCfConsistencyLevels.put(columnFamily, consistencyLevel);
    }
    log.info("{} ConsistencyLevel set to {} for ColumnFamily {}",
        new Object[]{operationType.toString(),consistencyLevel.toString(),columnFamily});
  }

  public void setDefaultReadConsistencyLevel(HConsistencyLevel defaultReadConsistencyLevel) {
    this.defaultReadConsistencyLevel = defaultReadConsistencyLevel;
  }

  public void setDefaultWriteConsistencyLevel(HConsistencyLevel defaultWriteConsistencyLevel) {
    this.defaultWriteConsistencyLevel = defaultWriteConsistencyLevel;
  }



}

@@ -82,17 +82,16 @@
 il.Map;%0A
-%0A
 import m
@@ -1680,32 +1680,126 @@
 sistencyLevels;%0A
+    log.info(%22Read ColumnFamily ConsistencyLevels set to: %22 + columnFamilyConsistencyLevels);%0A
   %7D%0A%0A  public vo
@@ -1960,16 +1960,111 @@
 Levels;%0A
+    log.info(%22Write ColumnFamily ConsistencyLevels set to: %22 + columnFamilyConsistencyLevels);%0A
   %7D%0A%0A  p
@@ -2728,24 +2728,127 @@
 tencyLevel;%0A
+    log.info(%22Default read ConsistencyLevel set to: %22 + defaultReadConsistencyLevel.toString() + %22.%22);%0A
   %7D%0A%0A  publi
@@ -3002,21 +3002,126 @@
 tencyLevel;%0A
+    log.info(%22Default write ConsistencyLevel set to: %22 + defaultWriteConsistencyLevel.toString() + %22.%22);%0A
   %7D%0A%0A%0A%0A%7D%0A

Implement consistent logging for setters in me.prettyprint.cassandra.model.ConfigurableConsistencyLevel #656