Skip to content

Instantly share code, notes, and snippets.

@lucperkins
Created August 30, 2014 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucperkins/8a6989c50f38fdbcc5eb to your computer and use it in GitHub Desktop.
Save lucperkins/8a6989c50f38fdbcc5eb to your computer and use it in GitHub Desktop.
import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.api.commands.kv.FetchValue;
import com.basho.riak.client.core.query.Location;
import com.basho.riak.client.core.query.Namespace;
import com.google.common.base.Optional;
import java.util.concurrent.ExecutionException;
public class RiakDAO<T> {
private final RiakClient client;
private final Class<T> clazz;
public RiakDAO(Class<T> clazz, RiakClient client) {
this.clazz = clazz;
this.client = client;
}
public Optional<T> get(String bucket, String key) {
T maybeObject;
try {
Location loc = new Location(new Namespace(bucket), key);
FetchValue fetchOp = new FetchValue.Builder(loc).build();
maybeObject = client.execute(fetchOp).getValue(clazz);
} catch(ExecutionException | InterruptedException e) {
return Optional.absent();
}
return Optional.of(maybeObject);
}
public Optional<T> get(String bucket, String key, String bucketType) {
T maybeObject;
try {
Location loc = new Location(new Namespace(bucketType, bucket), key);
FetchValue fetchOp = new FetchValue.Builder(loc).build();
maybeObject = client.execute(fetchOp).getValue(clazz);
} catch(ExecutionException | InterruptedException e) {
return Optional.absent();
}
return Optional.of(maybeObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment