Skip to content

Instantly share code, notes, and snippets.

@angrycub
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save angrycub/eedca543b0c41339da0e to your computer and use it in GitHub Desktop.

Select an option

Save angrycub/eedca543b0c41339da0e to your computer and use it in GitHub Desktop.
Nonexistent versus Unmodified Objects with the Riak Java Client RawClient
package test;
import java.io.IOException;
import java.util.Date;
import com.basho.riak.client.IRiakObject;
import com.basho.riak.client.RiakException;
import com.basho.riak.client.builders.RiakObjectBuilder;
import com.basho.riak.client.http.RiakClient;
import com.basho.riak.client.raw.FetchMeta;
import com.basho.riak.client.raw.RawClient;
import com.basho.riak.client.raw.RiakResponse;
import com.basho.riak.client.raw.StoreMeta;
import com.basho.riak.client.raw.http.HTTPClientAdapter;
public class App
{
public static void main(String[] args) throws RiakException, IOException {
RiakClient httpClient = new RiakClient("http://127.0.0.1:8098/riak");
RawClient rc = new HTTPClientAdapter(httpClient);
// Build a sample object
IRiakObject cat = RiakObjectBuilder.newBuilder("Pets", "Cat").
withValue("Meow").build();
StoreMeta returnBodyStoreMeta = new StoreMeta.Builder().returnBody(true).build();
RiakResponse storedCatResponse = rc.store(cat, returnBodyStoreMeta);
Date lastModified = storedCatResponse.getRiakObjects()[0].getLastModified();
// Fetch the object to see if updated (Should not fetch anything)
FetchMeta myFetchMeta = new FetchMeta.Builder().
modifiedSince(lastModified).build();
RiakResponse fetchedCat = rc.fetch("Pets", "Cat", myFetchMeta);
// Try to fetch a nonexistent object
RiakResponse fetchedLemur= rc.fetch("Pets", "Lemur", myFetchMeta);
System.out.printf("fetchedCat.getRiakObjects().length:%d unmodified:%s%n",
fetchedCat.getRiakObjects().length,
fetchedCat.isUnmodified() ); // Unmodified object
System.out.printf("fetchedLemur.getRiakObjects().length:%d unmodified:%s%n",
fetchedLemur.getRiakObjects().length,
fetchedLemur.isUnmodified() ); // Nonexistent object
}
}
fetchedCat.getRiakObjects().length:0 unmodified:true
fetchedLemur.getRiakObjects().length:0 unmodified:false
@jmshoffs0812
Copy link
Copy Markdown

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment