Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datastore.ramcloud; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 2 | |
| 3 | import java.io.File; |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 4 | import java.nio.ByteBuffer; |
| 5 | import java.nio.ByteOrder; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 6 | import java.util.ArrayList; |
| 7 | import java.util.Arrays; |
| 8 | import java.util.Collection; |
| 9 | import java.util.Iterator; |
| 10 | import java.util.List; |
| 11 | import java.util.concurrent.ConcurrentHashMap; |
| 12 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.datastore.IKVClient; |
| 14 | import net.onrc.onos.core.datastore.IKVTable; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.datastore.IKVTable.IKVEntry; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 16 | import net.onrc.onos.core.datastore.IKVTableID; |
| 17 | import net.onrc.onos.core.datastore.IMultiEntryOperation; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 18 | import net.onrc.onos.core.datastore.IMultiEntryOperation.STATUS; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 19 | import net.onrc.onos.core.datastore.ObjectDoesntExistException; |
| 20 | import net.onrc.onos.core.datastore.ObjectExistsException; |
| 21 | import net.onrc.onos.core.datastore.WrongVersionException; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.datastore.internal.IModifiableMultiEntryOperation; |
| 23 | import net.onrc.onos.core.datastore.ramcloud.RCTable.Entry; |
| 24 | import net.onrc.onos.core.datastore.utils.ByteArrayUtil; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 25 | |
| 26 | import org.apache.commons.configuration.Configuration; |
| 27 | import org.apache.commons.configuration.ConfigurationException; |
| 28 | import org.apache.commons.configuration.PropertiesConfiguration; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import edu.stanford.ramcloud.JRamCloud; |
| 33 | import edu.stanford.ramcloud.JRamCloud.MultiReadObject; |
| 34 | import edu.stanford.ramcloud.JRamCloud.MultiWriteObject; |
| 35 | import edu.stanford.ramcloud.JRamCloud.MultiWriteRspObject; |
| 36 | import edu.stanford.ramcloud.JRamCloud.RejectRules; |
| 37 | import edu.stanford.ramcloud.JRamCloud.RejectRulesException; |
| 38 | import edu.stanford.ramcloud.JRamCloud.TableEnumerator2; |
| 39 | |
| 40 | public class RCClient implements IKVClient { |
| 41 | private static final Logger log = LoggerFactory.getLogger(RCClient.class); |
| 42 | |
| 43 | private static final String DB_CONFIG_FILE = "conf/ramcloud.conf"; |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 44 | public static final Configuration CONFIG = getConfiguration(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 45 | |
| 46 | // Value taken from RAMCloud's Status.h |
| 47 | // FIXME These constants should be defined by JRamCloud |
| 48 | public static final int STATUS_OK = 0; |
| 49 | |
| 50 | // FIXME come up with a proper way to retrieve configuration |
| 51 | public static final int MAX_MULTI_READS = Math.max(1, Integer |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 52 | .valueOf(System.getProperty("ramcloud.max_multi_reads", "400"))); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 53 | |
| 54 | public static final int MAX_MULTI_WRITES = Math.max(1, Integer |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 55 | .valueOf(System.getProperty("ramcloud.max_multi_writes", "800"))); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 56 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 57 | private static final ThreadLocal<JRamCloud> TLS_RC_CLIENT = new ThreadLocal<JRamCloud>() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 58 | @Override |
| 59 | protected JRamCloud initialValue() { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 60 | return new JRamCloud(getCoordinatorUrl(CONFIG)); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 61 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | /** |
| 65 | * @return JRamCloud instance intended to be used only within the |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | * SameThread. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 67 | * @note Do not store the returned instance in a member variable, etc. which |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 68 | * may be accessed later by another thread. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 69 | */ |
| 70 | static JRamCloud getJRamCloudClient() { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 71 | return TLS_RC_CLIENT.get(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Currently RCClient is state-less |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 75 | private static final RCClient THE_INSTANCE = new RCClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 76 | |
| 77 | public static RCClient getClient() { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 78 | return THE_INSTANCE; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | public static final Configuration getConfiguration() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 82 | final File configFile = new File(System.getProperty("ramcloud.config.path", DB_CONFIG_FILE)); |
| 83 | return getConfiguration(configFile); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public static final Configuration getConfiguration(final File configFile) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 87 | if (configFile == null) { |
| 88 | throw new IllegalArgumentException("Need to specify a configuration file or storage directory"); |
| 89 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 90 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 91 | if (!configFile.isFile()) { |
| 92 | throw new IllegalArgumentException("Location of configuration must be a file"); |
| 93 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 94 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 95 | try { |
| 96 | return new PropertiesConfiguration(configFile); |
| 97 | } catch (ConfigurationException e) { |
| 98 | throw new IllegalArgumentException("Could not load configuration at: " + configFile, e); |
| 99 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | public static String getCoordinatorUrl(final Configuration configuration) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 103 | final String coordinatorIp = configuration.getString("ramcloud.coordinatorIp", "fast+udp:host=127.0.0.1"); |
| 104 | final String coordinatorPort = configuration.getString("ramcloud.coordinatorPort", "port=12246"); |
| 105 | final String coordinatorURL = coordinatorIp + "," + coordinatorPort; |
| 106 | return coordinatorURL; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public IMultiEntryOperation createOp(IKVTableID tableId, byte[] key, byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 111 | return RCMultiEntryOperation.create(tableId, key, value); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param tableId RCTableID instance |
| 116 | */ |
| 117 | @Override |
| 118 | public long create(IKVTableID tableId, byte[] key, byte[] value) |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 119 | throws ObjectExistsException { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 120 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 121 | RCTableID rcTableId = (RCTableID) tableId; |
| 122 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 123 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 124 | RejectRules rules = new RejectRules(); |
| 125 | rules.rejectIfExists(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 126 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 127 | try { |
| 128 | return rcClient.write(rcTableId.getTableID(), key, value, rules); |
| 129 | } catch (JRamCloud.ObjectExistsException e) { |
| 130 | throw new ObjectExistsException(rcTableId, key, e); |
| 131 | } catch (JRamCloud.RejectRulesException e) { |
| 132 | log.error("Unexpected RejectRulesException", e); |
| 133 | return JRamCloud.VERSION_NONEXISTENT; |
| 134 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public IMultiEntryOperation forceCreateOp(IKVTableID tableId, byte[] key, byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 139 | return RCMultiEntryOperation.forceCreate(tableId, key, value); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public long forceCreate(IKVTableID tableId, byte[] key, byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 144 | RCTableID rcTableId = (RCTableID) tableId; |
| 145 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 146 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 147 | long updatedVersion = rcClient.write(rcTableId.getTableID(), key, value); |
| 148 | return updatedVersion; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public IMultiEntryOperation readOp(IKVTableID tableId, byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 153 | return RCMultiEntryOperation.read(tableId, key); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public IKVEntry read(IKVTableID tableId, byte[] key) |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 158 | throws ObjectDoesntExistException { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 159 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 160 | RCTableID rcTableId = (RCTableID) tableId; |
| 161 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 162 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 163 | RejectRules rules = new RejectRules(); |
| 164 | rules.rejectIfDoesntExists(); |
| 165 | try { |
| 166 | JRamCloud.Object rcObj = rcClient.read(rcTableId.getTableID(), key, rules); |
| 167 | return new Entry(rcObj.key, rcObj.value, rcObj.version); |
| 168 | } catch (JRamCloud.ObjectDoesntExistException e) { |
| 169 | throw new ObjectDoesntExistException(rcTableId, key, e); |
| 170 | } catch (JRamCloud.RejectRulesException e) { |
| 171 | log.error("Unexpected RejectRulesException", e); |
| 172 | return null; |
| 173 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 177 | public IMultiEntryOperation updateOp(IKVTableID tableId, byte[] key, byte[] value, long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 178 | return RCMultiEntryOperation.update(tableId, key, value, version); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public long update(IKVTableID tableId, byte[] key, byte[] value, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 183 | long version) throws ObjectDoesntExistException, |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 184 | WrongVersionException { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 185 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 186 | RCTableID rcTableId = (RCTableID) tableId; |
| 187 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 188 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 189 | RejectRules rules = new RejectRules(); |
| 190 | rules.rejectIfDoesntExists(); |
| 191 | rules.rejectIfNeVersion(version); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 192 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 193 | try { |
| 194 | return rcClient.write(rcTableId.getTableID(), key, value, rules); |
| 195 | } catch (JRamCloud.ObjectDoesntExistException e) { |
| 196 | throw new ObjectDoesntExistException(rcTableId, key, e); |
| 197 | } catch (JRamCloud.WrongVersionException e) { |
| 198 | throw new WrongVersionException(rcTableId, key, version, e); |
| 199 | } catch (JRamCloud.RejectRulesException e) { |
| 200 | log.error("Unexpected RejectRulesException", e); |
| 201 | return JRamCloud.VERSION_NONEXISTENT; |
| 202 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
| 206 | @Override |
| 207 | public long update(IKVTableID tableId, byte[] key, byte[] value) |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 208 | throws ObjectDoesntExistException { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 209 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 210 | RCTableID rcTableId = (RCTableID) tableId; |
| 211 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 212 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 213 | RejectRules rules = new RejectRules(); |
| 214 | rules.rejectIfDoesntExists(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 215 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 216 | try { |
| 217 | return rcClient.write(rcTableId.getTableID(), key, value, rules); |
| 218 | } catch (JRamCloud.ObjectDoesntExistException e) { |
| 219 | throw new ObjectDoesntExistException(rcTableId, key, e); |
| 220 | } catch (JRamCloud.RejectRulesException e) { |
| 221 | log.error("Unexpected RejectRulesException", e); |
| 222 | return JRamCloud.VERSION_NONEXISTENT; |
| 223 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 227 | public IMultiEntryOperation deleteOp(IKVTableID tableId, byte[] key, byte[] value, long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 228 | return RCMultiEntryOperation.delete(tableId, key, value, version); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public long delete(IKVTableID tableId, byte[] key, long version) |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 233 | throws ObjectDoesntExistException, WrongVersionException { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 234 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 235 | RCTableID rcTableId = (RCTableID) tableId; |
| 236 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 237 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 238 | RejectRules rules = new RejectRules(); |
| 239 | rules.rejectIfDoesntExists(); |
| 240 | rules.rejectIfNeVersion(version); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 241 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 242 | try { |
| 243 | return rcClient.remove(rcTableId.getTableID(), key, rules); |
| 244 | } catch (JRamCloud.ObjectDoesntExistException e) { |
| 245 | throw new ObjectDoesntExistException(rcTableId, key, e); |
| 246 | } catch (JRamCloud.WrongVersionException e) { |
| 247 | throw new WrongVersionException(rcTableId, key, version, e); |
| 248 | } catch (JRamCloud.RejectRulesException e) { |
| 249 | log.error("Unexpected RejectRulesException", e); |
| 250 | return JRamCloud.VERSION_NONEXISTENT; |
| 251 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public IMultiEntryOperation forceDeleteOp(IKVTableID tableId, byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 256 | return RCMultiEntryOperation.forceDelete(tableId, key); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public long forceDelete(IKVTableID tableId, byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 261 | RCTableID rcTableId = (RCTableID) tableId; |
| 262 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 263 | final long removedVersion = rcClient.remove(rcTableId.getTableID(), key); |
| 264 | return removedVersion; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public Iterable<IKVEntry> getAllEntries(IKVTableID tableId) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 269 | return new RCTableEntryIterable((RCTableID) tableId); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static class RCTableEntryIterable implements Iterable<IKVEntry> { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 273 | private final RCTableID tableId; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 274 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 275 | public RCTableEntryIterable(final RCTableID tableId) { |
| 276 | this.tableId = tableId; |
| 277 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 278 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 279 | @Override |
| 280 | public Iterator<IKVEntry> iterator() { |
| 281 | return new RCClient.RCTableIterator(tableId); |
| 282 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | public static class RCTableIterator implements Iterator<IKVEntry> { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 286 | private final RCTableID tableId; |
| 287 | protected final TableEnumerator2 enumerator; |
| 288 | private JRamCloud.Object last; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 289 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 290 | public RCTableIterator(final RCTableID tableId) { |
| 291 | this.tableId = tableId; |
| 292 | this.enumerator = getJRamCloudClient().new TableEnumerator2(tableId.getTableID()); |
| 293 | this.last = null; |
| 294 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 295 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 296 | @Override |
| 297 | public boolean hasNext() { |
| 298 | return this.enumerator.hasNext(); |
| 299 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 300 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 301 | @Override |
| 302 | public RCTable.Entry next() { |
| 303 | last = enumerator.next(); |
| 304 | return new RCTable.Entry(last.key, last.value, last.version); |
| 305 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 306 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 307 | @Override |
| 308 | public void remove() { |
| 309 | if (last != null) { |
| 310 | getJRamCloudClient(); |
| 311 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 312 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 313 | RejectRules rules = new RejectRules(); |
| 314 | rules.rejectIfNeVersion(last.version); |
| 315 | try { |
| 316 | rcClient.remove(tableId.getTableID(), last.key, rules); |
| 317 | } catch (RejectRulesException e) { |
| 318 | log.trace("remove failed", e); |
| 319 | } |
| 320 | last = null; |
| 321 | } |
| 322 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public boolean multiRead(final Collection<IMultiEntryOperation> ops) { |
| 327 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 328 | if (ops.size() <= MAX_MULTI_READS && ops instanceof ArrayList) { |
Ray Milkey | 7f1567c | 2014-04-08 13:53:32 -0700 | [diff] [blame] | 329 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 330 | final ArrayList<RCMultiEntryOperation> arrays = (ArrayList) ops; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 331 | return multiReadInternal(arrays); |
| 332 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 333 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 334 | boolean failExists = false; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 335 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 336 | ArrayList<RCMultiEntryOperation> req = new ArrayList<>(); |
| 337 | Iterator<IMultiEntryOperation> it = ops.iterator(); |
| 338 | while (it.hasNext()) { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 339 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 340 | req.add((RCMultiEntryOperation) it.next()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 341 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 342 | if (req.size() >= MAX_MULTI_READS) { |
| 343 | // dispatch multiRead |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 344 | failExists |= multiReadInternal(req); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 345 | req.clear(); |
| 346 | } |
| 347 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 348 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 349 | if (!req.isEmpty()) { |
| 350 | // dispatch multiRead |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 351 | failExists |= multiReadInternal(req); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 352 | req.clear(); |
| 353 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 354 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 355 | return failExists; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public boolean multiWrite(final List<IMultiEntryOperation> ops) { |
| 360 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 361 | if (ops.size() <= MAX_MULTI_WRITES && ops instanceof ArrayList) { |
Ray Milkey | 7f1567c | 2014-04-08 13:53:32 -0700 | [diff] [blame] | 362 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 363 | final ArrayList<RCMultiEntryOperation> arrays = (ArrayList) ops; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 364 | return multiWriteInternal(arrays); |
| 365 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 366 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 367 | boolean failExists = false; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 368 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 369 | ArrayList<RCMultiEntryOperation> req = new ArrayList<>(); |
| 370 | Iterator<IMultiEntryOperation> it = ops.iterator(); |
| 371 | while (it.hasNext()) { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 372 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 373 | req.add((RCMultiEntryOperation) it.next()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 374 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 375 | if (req.size() >= MAX_MULTI_WRITES) { |
| 376 | // dispatch multiWrite |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 377 | failExists |= multiWriteInternal(req); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 378 | req.clear(); |
| 379 | } |
| 380 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 381 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 382 | if (!req.isEmpty()) { |
| 383 | // dispatch multiWrite |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 384 | failExists |= multiWriteInternal(req); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 385 | req.clear(); |
| 386 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 387 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 388 | return failExists; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | @Override |
| 392 | public boolean multiDelete(final Collection<IMultiEntryOperation> ops) { |
| 393 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 394 | // TODO implement multiRemove JNI, etc. if we need performance |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 395 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 396 | boolean failExists = false; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 397 | JRamCloud rcClient = getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 398 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 399 | for (IMultiEntryOperation iop : ops) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 400 | RCMultiEntryOperation op = (RCMultiEntryOperation) iop; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 401 | switch (op.getOperation()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 402 | case DELETE: |
| 403 | RejectRules rules = new RejectRules(); |
| 404 | rules.rejectIfDoesntExists(); |
| 405 | rules.rejectIfNeVersion(op.getVersion()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 406 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 407 | try { |
| 408 | final long removedVersion = rcClient.remove(op.tableId.getTableID(), op.entry.getKey(), rules); |
| 409 | op.entry.setVersion(removedVersion); |
| 410 | op.status = STATUS.SUCCESS; |
| 411 | } catch (JRamCloud.ObjectDoesntExistException | JRamCloud.WrongVersionException e) { |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame^] | 412 | log.error("Failed to remove key:" + ByteArrayUtil.toHexStringBuilder(op.entry.getKey(), "") + " from tableID:" + op.tableId, e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 413 | failExists = true; |
| 414 | op.status = STATUS.FAILED; |
| 415 | } catch (JRamCloud.RejectRulesException e) { |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame^] | 416 | log.error("Failed to remove key:" + ByteArrayUtil.toHexStringBuilder(op.entry.getKey(), "") + " from tableID:" + op.tableId, e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 417 | failExists = true; |
| 418 | op.status = STATUS.FAILED; |
| 419 | } |
| 420 | break; |
| 421 | |
| 422 | case FORCE_DELETE: |
| 423 | final long removedVersion = rcClient.remove(op.tableId.getTableID(), op.entry.getKey()); |
| 424 | if (removedVersion != VERSION_NONEXISTENT) { |
| 425 | op.entry.setVersion(removedVersion); |
| 426 | op.status = STATUS.SUCCESS; |
| 427 | } else { |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame^] | 428 | log.error("Failed to remove key:{} from tableID:{}", ByteArrayUtil.toHexStringBuilder(op.entry.getKey(), ""), op.tableId); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 429 | failExists = true; |
| 430 | op.status = STATUS.FAILED; |
| 431 | } |
| 432 | break; |
| 433 | |
| 434 | default: |
| 435 | log.error("Invalid operation {} specified on multiDelete", op.getOperation()); |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 436 | failExists = true; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 437 | op.status = STATUS.FAILED; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 438 | break; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 441 | return failExists; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | private boolean multiReadInternal(final ArrayList<RCMultiEntryOperation> ops) { |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 445 | boolean failExists = false; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 446 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 447 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 448 | final int reqs = ops.size(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 449 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 450 | MultiReadObject multiReadObjects = new MultiReadObject(reqs); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 451 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 452 | // setup multi-read operation objects |
| 453 | for (int i = 0; i < reqs; ++i) { |
| 454 | IMultiEntryOperation op = ops.get(i); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 455 | multiReadObjects.setObject(i, ((RCTableID) op.getTableId()).getTableID(), op.getKey()); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 456 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 457 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 458 | // execute |
| 459 | JRamCloud.Object[] results = rcClient.multiRead(multiReadObjects.tableId, multiReadObjects.key, multiReadObjects.keyLength, reqs); |
| 460 | if (results.length != reqs) { |
| 461 | log.error("multiRead returned unexpected number of results. (requested:{}, returned:{})", reqs, results.length); |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 462 | failExists = true; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 463 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 464 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 465 | for (int i = 0; i < results.length; ++i) { |
| 466 | IModifiableMultiEntryOperation op = ops.get(i); |
| 467 | if (results[i] == null) { |
| 468 | log.error("MultiRead error, skipping {}, {}", op.getTableId(), op); |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 469 | failExists = true; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 470 | op.setStatus(STATUS.FAILED); |
| 471 | continue; |
| 472 | } |
| 473 | assert (Arrays.equals(results[i].key, op.getKey())); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 474 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 475 | op.setValue(results[i].value, results[i].version); |
| 476 | if (results[i].version == JRamCloud.VERSION_NONEXISTENT) { |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 477 | failExists = true; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 478 | op.setStatus(STATUS.FAILED); |
| 479 | } else { |
| 480 | op.setStatus(STATUS.SUCCESS); |
| 481 | } |
| 482 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 483 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 484 | return failExists; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | private boolean multiWriteInternal(final ArrayList<RCMultiEntryOperation> ops) { |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 488 | boolean failExists = false; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 489 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 490 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 491 | final int reqs = ops.size(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 492 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 493 | MultiWriteObject multiWriteObjects = new MultiWriteObject(reqs); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 494 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 495 | for (int i = 0; i < reqs; ++i) { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 496 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 497 | IModifiableMultiEntryOperation op = ops.get(i); |
| 498 | RejectRules rules = new RejectRules(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 499 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 500 | switch (op.getOperation()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 501 | case CREATE: |
| 502 | rules.rejectIfExists(); |
| 503 | break; |
| 504 | case FORCE_CREATE: |
| 505 | // no reject rule |
| 506 | break; |
| 507 | case UPDATE: |
| 508 | rules.rejectIfDoesntExists(); |
| 509 | rules.rejectIfNeVersion(op.getVersion()); |
| 510 | break; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 511 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 512 | default: |
| 513 | log.error("Invalid operation {} specified on multiWriteInternal", op.getOperation()); |
| 514 | failExists = true; |
| 515 | op.setStatus(STATUS.FAILED); |
| 516 | return failExists; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 517 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 518 | multiWriteObjects.setObject(i, ((RCTableID) op.getTableId()).getTableID(), op.getKey(), op.getValue(), rules); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 519 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 520 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 521 | MultiWriteRspObject[] results = rcClient.multiWrite(multiWriteObjects.tableId, multiWriteObjects.key, multiWriteObjects.keyLength, multiWriteObjects.value, multiWriteObjects.valueLength, ops.size(), multiWriteObjects.rules); |
| 522 | if (results.length != reqs) { |
| 523 | log.error("multiWrite returned unexpected number of results. (requested:{}, returned:{})", reqs, results.length); |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 524 | failExists = true; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 525 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 526 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 527 | for (int i = 0; i < results.length; ++i) { |
| 528 | IModifiableMultiEntryOperation op = ops.get(i); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 529 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 530 | if (results[i] != null |
| 531 | && results[i].getStatus() == RCClient.STATUS_OK) { |
| 532 | op.setStatus(STATUS.SUCCESS); |
| 533 | op.setVersion(results[i].getVersion()); |
| 534 | } else { |
| 535 | op.setStatus(STATUS.FAILED); |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 536 | failExists = true; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 539 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 540 | return failExists; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 543 | private static final ConcurrentHashMap<String, RCTable> TABLES = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 544 | |
| 545 | @Override |
| 546 | public IKVTable getTable(final String tableName) { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 547 | RCTable table = TABLES.get(tableName); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 548 | if (table == null) { |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 549 | RCTable newTable = new RCTable(tableName); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 550 | RCTable existingTable = TABLES |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 551 | .putIfAbsent(tableName, newTable); |
| 552 | if (existingTable != null) { |
| 553 | return existingTable; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 554 | } else { |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 555 | return newTable; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | return table; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | @Override |
| 562 | public void dropTable(IKVTable table) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 563 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 564 | rcClient.dropTable(table.getTableId().getTableName()); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 565 | TABLES.remove(table.getTableId().getTableName()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static final long VERSION_NONEXISTENT = JRamCloud.VERSION_NONEXISTENT; |
| 569 | |
| 570 | @Override |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 571 | public long getVersionNonexistant() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 572 | return VERSION_NONEXISTENT; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 573 | } |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 574 | |
| 575 | @Override |
| 576 | public void createCounter(final IKVTableID tableId, final byte[] key, |
| 577 | final long initialValue) |
| 578 | throws ObjectExistsException { |
| 579 | |
| 580 | ByteBuffer valueBytes = ByteBuffer.allocate(8) |
| 581 | .order(ByteOrder.LITTLE_ENDIAN).putLong(initialValue); |
| 582 | valueBytes.flip(); |
| 583 | final long version = create(tableId, key, valueBytes.array()); |
| 584 | if (log.isTraceEnabled()) { |
| 585 | log.trace("Created counter {}-{}={}@{}", |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame^] | 586 | tableId, ByteArrayUtil.toHexStringBuilder(key, ":"), |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 587 | initialValue, version); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | @Override |
| 592 | public void setCounter(final IKVTableID tableId, final byte[] key, |
| 593 | final long value) { |
| 594 | |
| 595 | ByteBuffer valueBytes = ByteBuffer.allocate(8) |
| 596 | .order(ByteOrder.LITTLE_ENDIAN).putLong(value); |
| 597 | valueBytes.flip(); |
| 598 | |
| 599 | final long version = forceCreate(tableId, key, valueBytes.array()); |
| 600 | if (log.isTraceEnabled()) { |
| 601 | log.trace("set counter {}-{}={}@{}", |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame^] | 602 | tableId, ByteArrayUtil.toHexStringBuilder(key, ":"), |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 603 | value, version); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | @Override |
| 608 | public long incrementCounter(final IKVTableID tableId, final byte[] key, |
| 609 | final long incrementValue) { |
| 610 | |
| 611 | RCTableID rcTableId = (RCTableID) tableId; |
| 612 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 613 | |
| 614 | try { |
| 615 | return rcClient.increment(rcTableId.getTableID(), key, incrementValue); |
| 616 | } catch (JRamCloud.ObjectDoesntExistException e) { |
| 617 | log.warn("Counter {}-{} was not present", |
| 618 | tableId, |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame^] | 619 | ByteArrayUtil.toHexStringBuilder(key, ":")); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 620 | try { |
| 621 | // creating counter initialized to 0 |
| 622 | createCounter(rcTableId, key, 0L); |
| 623 | } catch (ObjectExistsException e1) { |
| 624 | // someone concurrently created it |
| 625 | log.debug("Counter {}-{} seemed to be concurrently created.", |
| 626 | tableId, |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame^] | 627 | ByteArrayUtil.toHexStringBuilder(key, ":")); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 628 | } |
| 629 | try { |
| 630 | return rcClient.increment(rcTableId.getTableID(), key, incrementValue); |
| 631 | } catch (edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException e1) { |
| 632 | log.error("Should never happen"); |
| 633 | throw new IllegalStateException("Created counter disappeared."); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | @Override |
| 639 | public void destroyCounter(final IKVTableID tableId, final byte[] key) { |
| 640 | |
| 641 | RCTableID rcTableId = (RCTableID) tableId; |
| 642 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 643 | |
| 644 | rcClient.remove(rcTableId.getTableID(), key); |
| 645 | } |
| 646 | |
| 647 | @Override |
| 648 | public long getCounter(IKVTableID tableId, byte[] key) |
| 649 | throws ObjectDoesntExistException { |
| 650 | |
| 651 | IKVEntry entry = read(tableId, key); |
| 652 | ByteBuffer counter = ByteBuffer.wrap(entry.getValue()).order(ByteOrder.LITTLE_ENDIAN); |
| 653 | return counter.getLong(); |
| 654 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 655 | } |