/*
   * (non-Javadoc)
   *
   * @see
   * com.basho.riak.client.raw.RawClient#fetchBucketKeys(java.lang.String)
   */
  public Iterable<String> listKeys(String bucketName) throws IOException {
    if (bucketName == null || bucketName.trim().equals("")) {
      throw new IllegalArgumentException("bucketName cannot be null, empty or all whitespace");
    }

    final KeySource keySource = client.listKeys(ByteString.copyFromUtf8(bucketName));
    final Iterator<String> i =
        new Iterator<String>() {

          private final Iterator<ByteString> delegate = keySource.iterator();

          public boolean hasNext() {
            return delegate.hasNext();
          }

          public String next() {
            return nullSafeToStringUtf8(delegate.next());
          }

          public void remove() {
            delegate.remove();
          }
        };

    return new Iterable<String>() {
      public Iterator<String> iterator() {
        return i;
      }
    };
  }
 /*
  * (non-Javadoc)
  *
  * @see com.basho.riak.client.raw.RawClient#setClientId()
  */
 public void setClientId(byte[] clientId) throws IOException {
   if (clientId == null || clientId.length != 4) {
     throw new IllegalArgumentException(
         "clientId must be 4 bytes. generateAndSetClientId() can do this for you");
   }
   client.setClientID(ByteString.copyFrom(clientId));
 }
  /*
   * (non-Javadoc)
   *
   * @see com.basho.riak.client.raw.RawClient#getClientId()
   */
  public byte[] getClientId() throws IOException {
    final String clientId = client.getClientID();

    if (clientId != null) {
      return CharsetUtils.utf8StringToBytes(clientId);
    } else {
      throw new IOException("null clientId returned by client");
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see com.basho.riak.client.raw.RawClient#fetchBucket(java.lang.String)
   */
  public BucketProperties fetchBucket(String bucketName) throws IOException {
    if (bucketName == null || bucketName.trim().equals("")) {
      throw new IllegalArgumentException("bucketName cannot be null, empty or all whitespace");
    }
    com.basho.riak.pbc.BucketProperties properties =
        client.getBucketProperties(ByteString.copyFromUtf8(bucketName));

    return convert(properties);
  }
  /*
   * (non-Javadoc)
   *
   * @see com.basho.riak.client.raw.RawClient#listBuckets()
   */
  public Set<String> listBuckets() throws IOException {
    final Set<String> response = new HashSet<String>();
    final ByteString[] buckets = client.listBuckets();

    for (ByteString b : buckets) {
      response.add(b.toStringUtf8());
    }
    return response;
  }
  /* (non-Javadoc)
   * @see com.basho.riak.client.raw.RawClient#fetch(java.lang.String, java.lang.String, com.basho.riak.client.raw.FetchMeta)
   */
  public RiakResponse fetch(String bucket, String key, FetchMeta fetchMeta) throws IOException {
    if (bucket == null || bucket.trim().equals("")) {
      throw new IllegalArgumentException(
          "bucket must not be null or empty " + "or just whitespace.");
    }

    if (key == null || key.trim().equals("")) {
      throw new IllegalArgumentException("Key cannot be null or empty or just whitespace");
    }

    FetchResponse fr = client.fetch(bucket, key, convert(fetchMeta));

    if (fr.hasSiblings()) {
      // do a full fetch to get the sibling values
      FetchMeta fm = FetchMeta.Builder.from(fetchMeta).headOnly(false).build();
      fr = client.fetch(bucket, key, convert(fm));
    }

    return convert(fr);
  }
  /*
   * (non-Javadoc)
   *
   * @see com.basho.riak.client.raw.RawClient#fetch(java.lang.String,
   * java.lang.String)
   */
  public RiakResponse fetch(String bucket, String key) throws IOException {
    if (bucket == null || bucket.trim().equals("")) {
      throw new IllegalArgumentException(
          "bucket must not be null or empty " + "or just whitespace.");
    }

    if (key == null || key.trim().equals("")) {
      throw new IllegalArgumentException("Key cannot be null or empty or just whitespace");
    }
    return convert(client.fetch(bucket, key));
  }
 /*
  * (non-Javadoc)
  *
  * @see
  * com.basho.riak.client.raw.RawClient#mapReduce(com.basho.riak.client.query
  * .MapReduceSpec)
  */
 public MapReduceResult mapReduce(MapReduceSpec spec)
     throws IOException, MapReduceTimeoutException {
   IRequestMeta meta = new RequestMeta();
   meta.contentType(Constants.CTYPE_JSON);
   try {
     MapReduceResponseSource resp = client.mapReduce(spec.getJSON(), meta);
     return convert(resp);
   } catch (RiakError e) {
     if (JSONErrorParser.isTimeoutException(e.getMessage())) {
       throw new MapReduceTimeoutException();
     } else {
       throw new IOException(e.getMessage());
     }
   }
 }
  /*
   * (non-Javadoc)
   *
   * @see
   * com.basho.riak.client.raw.RawClient#store(com.basho.riak.client.RiakObject
   * , com.basho.riak.client.raw.StoreMeta)
   */
  public RiakResponse store(IRiakObject riakObject, StoreMeta storeMeta) throws IOException {
    if (riakObject == null || riakObject.getKey() == null || riakObject.getBucket() == null) {
      throw new IllegalArgumentException(
          "object cannot be null, object's key cannot be null, object's bucket cannot be null");
    }

    try {
      return convert(client.store(convert(riakObject), convert(storeMeta, riakObject)));
    } catch (RiakError e) {
      // check for conditional store failure
      if (MATCH_FOUND.equals(e.getMessage())) {
        throw new MatchFoundException();
      } else if (MODIFIED.equals(e.getMessage())) {
        throw new ModifiedException(e);
      }
      throw e;
    }
  }
 public void shutdown() {
   client.shutdown();
 }
 /* (non-Javadoc)
  * @see com.basho.riak.client.raw.RawClient#ping()
  */
 public void ping() throws IOException {
   client.ping();
 }
 /*
  * (non-Javadoc)
  *
  * @see com.basho.riak.client.raw.RawClient#generateClientId()
  */
 public byte[] generateAndSetClientId() throws IOException {
   client.prepareClientID();
   return CharsetUtils.utf8StringToBytes(client.getClientID());
 }
 /*
  * (non-Javadoc)
  *
  * @see
  * com.basho.riak.client.raw.RawClient#updateBucketProperties(com.basho.
  * riak.client.bucket.BucketProperties)
  */
 public void updateBucket(final String name, final BucketProperties bucketProperties)
     throws IOException {
   com.basho.riak.pbc.BucketProperties properties = convert(bucketProperties);
   client.setBucketProperties(ByteString.copyFromUtf8(name), properties);
 }
 /* (non-Javadoc)
  * @see com.basho.riak.client.raw.RawClient#delete(java.lang.String, java.lang.String, com.basho.riak.client.raw.DeleteMeta)
  */
 public void delete(String bucket, String key, DeleteMeta deleteMeta) throws IOException {
   client.delete(bucket, key, convert(deleteMeta));
 }
 /*
  * (non-Javadoc)
  *
  * @see com.basho.riak.client.raw.RawClient#delete(java.lang.String, int)
  */
 public void delete(String bucket, String key, int deleteQuorum) throws IOException {
   client.delete(bucket, key, deleteQuorum);
 }
 /*
  * (non-Javadoc)
  *
  * @see com.basho.riak.client.raw.RawClient#delete(java.lang.String)
  */
 public void delete(String bucket, String key) throws IOException {
   client.delete(bucket, key);
 }