package me.prettyprint.cassandra.model;

import org.apache.cassandra.thrift.Clock;

import me.prettyprint.cassandra.model.ConsistencyLevelPolicy.OperationType;
import me.prettyprint.cassandra.service.CassandraClient;
import me.prettyprint.cassandra.service.Cluster;
import me.prettyprint.cassandra.service.Keyspace;
import me.prettyprint.cassandra.utils.Assert;

public /*final*/ class KeyspaceOperator {

  private ConsistencyLevelPolicy consistencyLevelPolicy;

  private final Cluster cluster;
  private final String keyspace;

  public KeyspaceOperator(String keyspace, Cluster cluster,
      ConsistencyLevelPolicy consistencyLevelPolicy) {
    Assert.noneNull(keyspace, cluster, consistencyLevelPolicy);
    this.keyspace = keyspace;
    this.cluster = cluster;
    this.consistencyLevelPolicy = consistencyLevelPolicy;
  }

  public void setConsistencyLevelPolicy(ConsistencyLevelPolicy cp) {
    this.consistencyLevelPolicy = cp;
  }

  public Cluster getCluster() {
    return cluster;
  }

  @Override
  public String toString() {
    return "KeyspaceOperator(" + keyspace +"," + cluster + ")";
  }

  public Clock createClock() {
    return cluster.createClock();
  }

  /*package*/ <T> ExecutionResult<T> doExecute(KeyspaceOperationCallback<T> koc) throws HectorException {
    CassandraClient c = null;
    Keyspace ks = null;
    try {
        c = cluster.borrowClient();
        ks = c.getKeyspace(keyspace, consistencyLevelPolicy.get(OperationType.READ));
        return koc.doInKeyspaceAndMeasure(ks);
    } finally {
      cluster.releaseClient(ks.getClient());
    }
  }
}

@@ -1524,24 +1524,52 @@
 %7D finally %7B%0A
+      if ( ks != null ) %7B%0A  
       cluste
@@ -1597,24 +1597,32 @@
 tClient());%0A
+      %7D%0A
     %7D%0A  %7D%0A%7D%0A

check for null on release so NPE does not mask underlying exceptions per issue 60