Search in sources :

Example 1 with Version

use of voldemort.versioning.Version in project voldemort by voldemort.

the class KratiStorageEngine method delete.

@Override
public boolean delete(ByteArray key, Version maxVersion) throws VoldemortException {
    StoreUtils.assertValidKey(key);
    synchronized (this.locks.lockFor(key.get())) {
        if (maxVersion == null) {
            try {
                return datastore.delete(key.get());
            } catch (Exception e) {
                logger.error("Failed to delete key: ", e);
                throw new VoldemortException("Failed to delete key: " + key, e);
            }
        }
        List<Versioned<byte[]>> returnedValuesList = this.get(key, null);
        // Case if there is nothing to delete
        if (returnedValuesList.size() == 0) {
            return false;
        }
        Iterator<Versioned<byte[]>> iter = returnedValuesList.iterator();
        while (iter.hasNext()) {
            Versioned<byte[]> currentValue = iter.next();
            Version currentVersion = currentValue.getVersion();
            if (currentVersion.compare(maxVersion) == Occurred.BEFORE) {
                iter.remove();
            }
        }
        try {
            if (returnedValuesList.size() == 0)
                return datastore.delete(key.get());
            else
                return datastore.put(key.get(), assembleValues(returnedValuesList));
        } catch (Exception e) {
            String message = "Failed to delete key " + key;
            logger.error(message, e);
            throw new VoldemortException(message, e);
        }
    }
}
Also used : Versioned(voldemort.versioning.Versioned) Version(voldemort.versioning.Version) VoldemortException(voldemort.VoldemortException) ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) VoldemortException(voldemort.VoldemortException) IOException(java.io.IOException)

Example 2 with Version

use of voldemort.versioning.Version in project voldemort by voldemort.

the class R2Store method parseGetVersionResponse.

private List<Version> parseGetVersionResponse(ByteString entity) {
    byte[] bytes = new byte[entity.length()];
    entity.copyBytes(bytes, 0);
    String vectorClockListStr = new String(bytes);
    List<Version> vectorClockList = RestUtils.deserializeVectorClocks(vectorClockListStr);
    return vectorClockList;
}
Also used : Version(voldemort.versioning.Version) ByteString(com.linkedin.data.ByteString)

Example 3 with Version

use of voldemort.versioning.Version in project voldemort by voldemort.

the class R2Store method getVersions.

@Override
public List<Version> getVersions(ByteArray key) {
    List<Version> resultList = new ArrayList<Version>();
    String base64Key = RestUtils.encodeVoldemortKey(key.get());
    RestRequestBuilder rb = null;
    try {
        rb = new RestRequestBuilder(new URI(this.restBootstrapURL + "/" + getName() + "/" + base64Key));
        String timeoutStr = Long.toString(this.config.getTimeoutConfig().getOperationTimeout(VoldemortOpCode.GET_VERSION_OP_CODE));
        rb.setHeader(RestMessageHeaders.X_VOLD_GET_VERSION, "true");
        RestResponse response = fetchGetResponse(rb, timeoutStr);
        final ByteString entity = response.getEntity();
        if (entity != null) {
            resultList = parseGetVersionResponse(entity);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Did not get any response!");
            }
        }
    } catch (ExecutionException e) {
        if (e.getCause() instanceof RestException) {
            RestException exception = (RestException) e.getCause();
            if (logger.isDebugEnabled()) {
                logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
            }
        } else {
            throw new VoldemortException("Unknown HTTP request execution exception: " + e.getMessage(), e);
        }
    } catch (InterruptedException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Operation interrupted : " + e.getMessage(), e);
        }
        throw new VoldemortException("Operation interrupted exception: " + e.getMessage(), e);
    } catch (URISyntaxException e) {
        throw new VoldemortException("Illegal HTTP URL" + e.getMessage(), e);
    }
    return resultList;
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) ArrayList(java.util.ArrayList) RestException(com.linkedin.r2.message.rest.RestException) ByteString(com.linkedin.data.ByteString) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) VoldemortException(voldemort.VoldemortException) Version(voldemort.versioning.Version) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with Version

use of voldemort.versioning.Version in project voldemort by voldemort.

the class SampleRESTClient method main.

public static void main(String[] args) {
    // Create the client
    Properties props = new Properties();
    props.setProperty(ClientConfig.BOOTSTRAP_URLS_PROPERTY, "http://localhost:8080");
    props.setProperty(ClientConfig.ROUTING_TIMEOUT_MS_PROPERTY, "1500");
    RESTClientFactoryConfig mainConfig = new RESTClientFactoryConfig(props, null);
    RESTClientFactory factory = new RESTClientFactory(mainConfig);
    StoreClient<String, String> storeClient = factory.getStoreClient("test");
    try {
        // Sample put
        System.out.println("First valid put");
        storeClient.put("a", "Howdy!!!!");
        System.out.println("Second valid put");
        storeClient.put("b", "Partner!!!!");
        // Do a sample get operation:
        Versioned<String> versionedValue = storeClient.get("a");
        System.out.println("Received response : " + versionedValue);
        Version obsoleteVersion = ((VectorClock) versionedValue.getVersion()).clone();
        // Do a versioned put operation:
        System.out.println("First versioned put");
        versionedValue.setObject("New Value !!!");
        System.out.println("************* original version : " + versionedValue.getVersion());
        Version putVersion = storeClient.put("a", versionedValue);
        System.out.println("************* Updated version : " + putVersion);
        // Obsolete version put
        System.out.println("Obsolete put");
        Versioned<String> obsoleteVersionedValue = new Versioned<String>("Obsolete value", obsoleteVersion);
        try {
            storeClient.put("a", obsoleteVersionedValue);
            System.err.println(" **************** Should not reach this point **************** ");
        } catch (Exception e) {
            System.out.println("Exception occurred as expected: " + e.getMessage());
        }
        // Do a get again on the last versioned put operation:
        versionedValue = storeClient.get("a");
        System.out.println("Received response on the versioned put: " + versionedValue);
        System.out.println("Versioned put based on the last put ");
        Versioned<String> newVersionedPut = new Versioned<String>("Yet another value !!!", putVersion);
        storeClient.put("a", newVersionedPut);
        // Do a get again on the last versioned put operation:
        versionedValue = storeClient.get("a");
        System.out.println("Received response on the (second) versioned put: " + versionedValue);
        List<String> keyList = new ArrayList<String>();
        keyList.add("a");
        keyList.add("b");
        System.out.println("Received response : " + storeClient.getAll(keyList));
    } finally {
        factory.close();
    }
}
Also used : Versioned(voldemort.versioning.Versioned) Version(voldemort.versioning.Version) VectorClock(voldemort.versioning.VectorClock) ArrayList(java.util.ArrayList) Properties(java.util.Properties)

Example 5 with Version

use of voldemort.versioning.Version in project voldemort by voldemort.

the class RestServiceR2StoreTest method testGetVersions.

@Override
@Test
public void testGetVersions() throws Exception {
    List<ByteArray> keys = getKeys(2);
    ByteArray key = keys.get(0);
    byte[] value = getValue();
    VectorClock vc = getClock(0, 0);
    Store<ByteArray, byte[], byte[]> store = getStore();
    store.put(key, Versioned.value(value, vc), null);
    List<Versioned<byte[]>> versioneds = store.get(key, null);
    List<Version> versions = store.getVersions(key);
    assertEquals(1, versioneds.size());
    assertTrue(versions.size() > 0);
    for (int i = 0; i < versions.size(); i++) assertEquals(versioneds.get(0).getVersion(), versions.get(i));
    assertEquals(0, store.getVersions(keys.get(1)).size());
}
Also used : Versioned(voldemort.versioning.Versioned) Version(voldemort.versioning.Version) VectorClock(voldemort.versioning.VectorClock) ByteArray(voldemort.utils.ByteArray) AbstractByteArrayStoreTest(voldemort.store.AbstractByteArrayStoreTest) Test(org.junit.Test)

Aggregations

Version (voldemort.versioning.Version)35 VectorClock (voldemort.versioning.VectorClock)16 Versioned (voldemort.versioning.Versioned)13 Test (org.junit.Test)11 ByteArray (voldemort.utils.ByteArray)10 VoldemortException (voldemort.VoldemortException)7 ArrayList (java.util.ArrayList)6 AbstractByteArrayStoreTest (voldemort.store.AbstractByteArrayStoreTest)5 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 List (java.util.List)3 ByteString (com.linkedin.data.ByteString)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 InvalidMetadataException (voldemort.store.InvalidMetadataException)2 Pair (voldemort.utils.Pair)2 ObsoleteVersionException (voldemort.versioning.ObsoleteVersionException)2 ImmutableList (com.google.common.collect.ImmutableList)1 RestException (com.linkedin.r2.message.rest.RestException)1 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)1