Added a put() method for saving objects and letting Riak generate an ID.

This commit is contained in:
J. Brisbin
2010-12-15 11:03:09 -06:00
parent 81e852d61a
commit f2e27732c5
2 changed files with 64 additions and 1 deletions

View File

@@ -41,6 +41,7 @@ import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.*;
import java.util.concurrent.Future;
@@ -163,6 +164,52 @@ public class RiakTemplate extends AbstractRiakTemplate implements BucketKeyValue
return setWithMetaData(bucket, key, value, metaData, null);
}
/*----------------- Put Operations -----------------*/
/**
* Save an object to Riak and let it generate an ID for it.
*
* @param bucket
* @param value
* @return The generated ID
*/
public <B, V> String put(B bucket, V value) {
return put(bucket, value, null);
}
/**
* Save an object to Riak and let it generate an ID for it.
*
* @param bucket
* @param value
* @param metaData
* @return The generated ID
*/
public <B, V> String put(B bucket, V value, Map<String, String> metaData) {
String bucketName = bucket.toString();
RestTemplate restTemplate = getRestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("X-Riak-ClientId", RIAK_CLIENT_ID);
headers.setContentType(extractMediaType(value));
if (null != metaData) {
for (Map.Entry<String, String> entry : metaData.entrySet()) {
headers.set(entry.getKey(), entry.getValue());
}
}
HttpEntity<V> entity = new HttpEntity<V>(value, headers);
try {
URI uri = restTemplate.postForLocation(defaultUri, entity, bucketName, "");
String suri = uri.toString();
String id = suri.substring(suri.lastIndexOf("/") + 1);
if (log.isDebugEnabled()) {
log.debug("New ID: " + id);
}
return id;
} catch (RestClientException e) {
throw new DataStoreOperationException(e.getMessage(), e);
}
}
/*----------------- Get Operations -----------------*/
public <B, K> RiakMetaData getMetaData(B bucket, K key) {

View File

@@ -40,6 +40,7 @@ class RiakTemplateSpec extends Specification {
int run = 1
@Shared def riakBin = System.properties["bamboo.RIAK_BIN"] ?: "/usr/sbin/riak"
@Shared def p
@Shared def id
/*
def setupSpec() {
p = "$riakBin start".execute()
@@ -68,6 +69,20 @@ class RiakTemplateSpec extends Specification {
}
def "Test generating ID for object"() {
given:
def val = "value"
def objIn = [test: val, integer: 12]
when:
id = riak.put("test", objIn, null)
then:
null != id
}
def "Test custom object"() {
given:
@@ -261,9 +276,10 @@ class RiakTemplateSpec extends Specification {
given:
def testKey = new SimpleBucketKeyPair("test", "test")
def testKey2 = new SimpleBucketKeyPair(TestObject.name, "test")
def testKey3 = new SimpleBucketKeyPair("test", id)
when:
def deleted = riak.deleteKeys(testKey, testKey2)
def deleted = riak.deleteKeys(testKey, testKey2, testKey3)
then:
true == deleted