/**
   * If the key is to be associated with a valid value, a mutation is created for it with the given
   * table and columns. In the event the value in the column is missing (i.e., null), then it is
   * marked for {@link Deletion}. Similarly, if the entire value for a key is missing (i.e., null),
   * then the entire key is marked for {@link Deletion}.
   *
   * @param keyColumns the key to write.
   * @param values the values to write.
   * @throws IOException
   */
  @Override
  public void write(Map<String, ByteBuffer> keyColumns, List<ByteBuffer> values)
      throws IOException {
    TokenRange range = ringCache.getRange(getPartitionKey(keyColumns));

    // get the client for the given range, or create a new one
    final InetAddress address = ringCache.getEndpoints(range).get(0);
    RangeClient client = clients.get(address);
    if (client == null) {
      // haven't seen keys for this range: create new client
      client = new RangeClient(ringCache.getEndpoints(range));
      client.start();
      clients.put(address, client);
    }

    // add primary key columns to the bind variables
    List<ByteBuffer> allValues = new ArrayList<ByteBuffer>(values);
    for (ColumnMetadata column : partitionKeyColumns)
      allValues.add(keyColumns.get(column.getName()));
    for (ColumnMetadata column : clusterColumns) allValues.add(keyColumns.get(column.getName()));

    client.put(allValues);

    if (progressable != null) progressable.progress();
    if (context != null) HadoopCompat.progress(context);
  }
  private ByteBuffer getPartitionKey(Map<String, ByteBuffer> keyColumns) {
    ByteBuffer partitionKey;
    if (partitionKeyColumns.size() > 1) {
      ByteBuffer[] keys = new ByteBuffer[partitionKeyColumns.size()];
      for (int i = 0; i < keys.length; i++)
        keys[i] = keyColumns.get(partitionKeyColumns.get(i).getName());

      partitionKey = CompositeType.build(keys);
    } else {
      partitionKey = keyColumns.get(partitionKeyColumns.get(0).getName());
    }
    return partitionKey;
  }
  @Override
  public void close() throws IOException {
    // close all the clients before throwing anything
    IOException clientException = null;
    for (RangeClient client : clients.values()) {
      try {
        client.close();
      } catch (IOException e) {
        clientException = e;
      }
    }

    if (clientException != null) throw clientException;
  }