Created
August 30, 2014 21:40
-
-
Save lucperkins/8a6989c50f38fdbcc5eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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