All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.basho.riak.client.api.commands.kv.DeleteValue Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
/*
 * Copyright 2013 Basho Technologies Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.basho.riak.client.api.commands.kv;

import com.basho.riak.client.api.RiakCommand;
import com.basho.riak.client.api.cap.Quorum;
import com.basho.riak.client.api.cap.VClock;
import com.basho.riak.client.core.RiakCluster;
import com.basho.riak.client.core.RiakFuture;
import com.basho.riak.client.core.operations.DeleteOperation;
import com.basho.riak.client.api.commands.CoreFutureAdapter;
import com.basho.riak.client.api.commands.RiakOption;
import com.basho.riak.client.core.query.Location;

import java.util.HashMap;
import java.util.Map;

/**
 * Command used to delete a value from Riak.
 * 

* Deleting an object from Riak is a simple matter of supplying a {@link com.basho.riak.client.query.Location} * and executing the DeleteValue operation. *

 * Location loc = new Location("my_bucket")..setBucketType("my_type").setKey("my_key");
 * DeleteValue dv = new DeleteValue.Builder(loc).build();
 * DeleteValue.Response resp = client.execute(dv);
 * 
*

* * * @author Dave Rusek * @since 2.0 */ public final class DeleteValue extends RiakCommand { private final Location location; private final Map, Object> options = new HashMap, Object>(); private final VClock vClock; public DeleteValue(Builder builder) { this.location = builder.location; this.options.putAll(builder.options); this.vClock = builder.vClock; } @Override protected RiakFuture executeAsync(RiakCluster cluster) { RiakFuture coreFuture = cluster.execute(buildCoreOperation()); CoreFutureAdapter future = new CoreFutureAdapter(coreFuture) { @Override protected Void convertResponse(Void coreResponse) { return coreResponse; } @Override protected Location convertQueryInfo(Location coreQueryInfo) { return coreQueryInfo; } }; coreFuture.addListener(future); return future; } private DeleteOperation buildCoreOperation() { DeleteOperation.Builder builder = new DeleteOperation.Builder(location); if (vClock != null) { builder.withVclock(vClock); } for (Map.Entry, Object> optPair : options.entrySet()) { Option option = optPair.getKey(); if (option == Option.DW) { builder.withDw(((Quorum) optPair.getValue()).getIntValue()); } else if (option == Option.N_VAL) { builder.withNVal((Integer) optPair.getValue()); } else if (option == Option.PR) { builder.withPr(((Quorum) optPair.getValue()).getIntValue()); } else if (option == Option.R) { builder.withR(((Quorum) optPair.getValue()).getIntValue()); } else if (option == Option.PW) { builder.withPw(((Quorum) optPair.getValue()).getIntValue()); } else if (option == Option.RW) { builder.withRw(((Quorum) optPair.getValue()).getIntValue()); } else if (option == Option.SLOPPY_QUORUM) { builder.withSloppyQuorum((Boolean) optPair.getValue()); } else if (option == Option.TIMEOUT) { builder.withTimeout((Integer) optPair.getValue()); } else if (option == Option.W) { builder.withW(((Quorum) optPair.getValue()).getIntValue()); } } return builder.build(); } public final static class Option extends RiakOption { /** * Read Write Quorum. * Quorum for both operations (get and put) involved in deleting an object */ public static final Option RW = new Option("RW"); /** * Read Quorum. * How many replicas need to agree when fetching the object. */ public static final Option R = new Option("R"); /** * Write Quorum. * How many replicas to write to before returning a successful response. */ public static final Option W = new Option("W"); /** * Primary Read Quorum. * How many primary replicas need to be available when retrieving the object. */ public static final Option PR = new Option("PR"); /** * Primary Write Quorum. * How many primary nodes must be up when the write is attempted */ public static final Option PW = new Option("PW"); /** * Durable Write Quorum. * How many replicas to commit to durable storage before returning a successful response. */ public static final Option DW = new Option("DW"); /** * Timeout. * Sets the server-side timeout for this operation. The default is 60 seconds. */ public static final Option TIMEOUT = new Option("TIMEOUT"); public static final Option SLOPPY_QUORUM = new Option("SLOPPY_QUORUM"); public static final Option N_VAL = new Option("N_VAL"); private Option(String name) { super(name); } } /** * Used to construct a DeleteValue command. */ public static class Builder { private final Location location; private final Map, Object> options = new HashMap, Object>(); private VClock vClock; public Builder(Location location) { if (location == null) { throw new IllegalArgumentException("Location cannot be null"); } this.location = location; } /** * Specify the VClock to use when deleting the object from Riak. * * @param vClock the vclock * @return this */ public Builder withVClock(VClock vClock) { this.vClock = vClock; return this; } /** * Add a delete option * * @param option the option * @param value the value associated with the option * @param the type required by the option * @return a reference to this object */ public Builder withOption(Option option, T value) { options.put(option, value); return this; } /** * Set the Riak-side timeout value. *

* By default, riak has a 60s timeout for operations. Setting * this value will override that default for this operation. *

* @param timeout the timeout in milliseconds to be sent to riak. * @return a reference to this object. */ public Builder withTimeout(int timeout) { withOption(Option.TIMEOUT, timeout); return this; } /** * Construct a DeleteValue object. * @return a new DeleteValue instance. */ public DeleteValue build() { return new DeleteValue(this); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy