yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2013 Stanford University |
| 2 | * |
| 3 | * Permission to use, copy, modify, and distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR |
| 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | */ |
| 15 | |
| 16 | package edu.stanford.ramcloud; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 17 | import java.nio.charset.StandardCharsets; |
Yoshi Muroi | 2c17060 | 2014-02-15 08:31:28 -0800 | [diff] [blame] | 18 | import java.util.Arrays; |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 19 | import java.util.LinkedList; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 20 | |
Yuta HIGUCHI | ceb99bd | 2014-04-19 11:55:12 -0700 | [diff] [blame] | 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 24 | /* |
| 25 | * This class provides Java bindings for RAMCloud. Right now it is a rather |
| 26 | * simple subset of what RamCloud.h defines. |
| 27 | * |
| 28 | * Running ``javah'' on this file will generate a C header file with the |
| 29 | * appropriate JNI function definitions. The glue interfacing to the C++ |
| 30 | * RAMCloud library can be found in JRamCloud.cc. |
| 31 | * |
| 32 | * For JNI information, the IBM tutorials and Android developer docs are much |
| 33 | * better than Sun's at giving an overall intro: |
| 34 | * http://www.ibm.com/developerworks/java/tutorials/j-jni/section4.html |
| 35 | * http://developer.android.com/training/articles/perf-jni.html |
| 36 | */ |
| 37 | public class JRamCloud { |
Yuta HIGUCHI | ceb99bd | 2014-04-19 11:55:12 -0700 | [diff] [blame] | 38 | private static final Logger log = LoggerFactory.getLogger(JRamCloud.class); |
| 39 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 40 | static { |
Yuta HIGUCHI | ceb99bd | 2014-04-19 11:55:12 -0700 | [diff] [blame] | 41 | try { |
| 42 | System.loadLibrary("edu_stanford_ramcloud_JRamCloud"); |
| 43 | } catch (UnsatisfiedLinkError e) { |
| 44 | System.err.println("Unable to load JNI binding. Please build RAMCloud Java bindings."); |
| 45 | log.error("Unable to load JNI binding. Please build RAMCloud Java bindings.", e); |
| 46 | System.exit(-1); |
| 47 | } |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 50 | public static final long VERSION_NONEXISTENT = 0L; |
| 51 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 52 | /// Pointer to the underlying C++ RAMCloud object associated with this |
| 53 | /// object. |
| 54 | private long ramcloudObjectPointer = 0; |
| 55 | |
| 56 | /** |
| 57 | * See src/RejectRules.h. |
| 58 | */ |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 59 | public static class RejectRules { |
| 60 | // these needs to be in sync with JNI code; |
| 61 | public static final int DoesntExist = 1; |
| 62 | public static final int Exists = 1 << 1; |
| 63 | public static final int VersionLeGiven = 1 << 2; |
| 64 | public static final int VersionNeGiven = 1 << 3; |
| 65 | |
| 66 | protected long givenVersion; |
| 67 | protected int flags; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 68 | |
| 69 | public RejectRules() { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 70 | clear(); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 73 | public void clear() { |
| 74 | this.givenVersion = VERSION_NONEXISTENT; |
| 75 | this.flags = 0x0; |
| 76 | } |
| 77 | |
| 78 | public RejectRules set(int flags, long version) { |
| 79 | this.flags = flags; |
| 80 | this.givenVersion = version; |
| 81 | return this; |
| 82 | } |
| 83 | |
| 84 | public RejectRules set(int flags) { |
| 85 | return set(flags,VERSION_NONEXISTENT); |
| 86 | } |
| 87 | |
| 88 | public RejectRules rejectIfLeVersion(long version) { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 89 | setVersion(version); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 90 | this.flags |= VersionLeGiven; |
| 91 | return this; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 94 | public RejectRules rejectIfExists() { |
| 95 | this.flags |= Exists; |
| 96 | return this; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 99 | public RejectRules rejectIfDoesntExists() { |
| 100 | this.flags |= DoesntExist; |
| 101 | return this; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 104 | public RejectRules rejectIfNeVersion(long version) { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 105 | setVersion(version); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 106 | this.flags |= VersionNeGiven; |
| 107 | return this; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private void setVersion(long version) { |
| 111 | this.givenVersion = version; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 112 | } |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 113 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 114 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 115 | public static class MultiReadObject { |
| 116 | public long[] tableId; |
| 117 | public byte[] key[]; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 118 | public short[] keyLength; |
| 119 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 120 | public MultiReadObject(int size){ |
| 121 | this.tableId = new long[size]; |
| 122 | this.key = new byte[size][]; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 123 | this.keyLength = new short[size]; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 124 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 125 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 126 | public void setObject(int num, long tableId, byte key[]){ |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 127 | this.tableId[num] = tableId; |
| 128 | this.key[num] = key; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 129 | this.keyLength[num] = (short) this.key[num].length; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 130 | } |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 131 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 132 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 133 | public static class MultiWriteObject { |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 134 | public long[] tableId; |
| 135 | public byte[] key[]; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 136 | public short[] keyLength; |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 137 | public byte[] value[]; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 138 | public int[] valueLength; |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 139 | public RejectRules[] rules; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 140 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 141 | public MultiWriteObject(int size) { |
| 142 | this.tableId = new long[size]; |
| 143 | this.key = new byte[size][]; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 144 | this.keyLength = new short[size]; |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 145 | this.value = new byte[size][]; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 146 | this.valueLength = new int[size]; |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 147 | this.rules = new RejectRules[size]; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 148 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 149 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 150 | public void setObject(int num, long tableId, byte key[], byte value[], RejectRules rules){ |
| 151 | this.tableId[num] = tableId; |
| 152 | this.key[num] = key; |
| 153 | this.keyLength[num] = (short) key.length; |
| 154 | this.value[num] = value; |
| 155 | this.valueLength[num] = value.length; |
| 156 | this.rules[num] = rules; |
| 157 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 158 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 161 | public static class MultiWriteRspObject { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 162 | private int status; |
| 163 | private long version; |
| 164 | |
| 165 | public MultiWriteRspObject(int status, long version) { |
| 166 | this.status = status; |
| 167 | this.version = version; |
| 168 | } |
| 169 | public int getStatus() { |
| 170 | return status; |
| 171 | } |
| 172 | |
| 173 | public long getVersion() { |
| 174 | return version; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * This class is returned by Read operations. It encapsulates the entire |
| 180 | * object, including the key, value, and version. |
| 181 | * |
| 182 | * It mostly exists because Java doesn't support primitive out parameters |
| 183 | * or multiple return values, and we don't know the object's size ahead of |
| 184 | * time, so passing in a fixed-length array would be problematic. |
| 185 | */ |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 186 | public static class Object { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 187 | Object(byte[] _key, byte[] _value, long _version) |
| 188 | { |
| 189 | key = _key; |
| 190 | value = _value; |
| 191 | version = _version; |
| 192 | } |
| 193 | |
| 194 | public String |
| 195 | getKey() |
| 196 | { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 197 | return new String(key,StandardCharsets.UTF_8); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | public String |
| 201 | getValue() |
| 202 | { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 203 | return new String(value,StandardCharsets.UTF_8); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | final public byte[] key; |
| 207 | final public byte[] value; |
| 208 | final public long version; |
| 209 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 210 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 211 | public static class TableEnumeratorObject { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 212 | TableEnumeratorObject(Object[] _object, long _nextHash) |
| 213 | { |
| 214 | object = _object; |
| 215 | nextHash = _nextHash; |
| 216 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 217 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 218 | final public Object[] object; |
| 219 | final public long nextHash; |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 220 | } |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 221 | |
Yuta HIGUCHI | e316d5c | 2014-04-19 13:37:38 -0700 | [diff] [blame] | 222 | @Deprecated |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 223 | public class TableEnumerator { |
| 224 | private long tableEnumeratorObjectPointer = 0; |
| 225 | private long ramCloudObjectPointer = 0; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 226 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 227 | public TableEnumerator(long tableId) |
| 228 | { |
| 229 | ramCloudObjectPointer = ramcloudObjectPointer; |
| 230 | tableEnumeratorObjectPointer = init(tableId); |
| 231 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 232 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 233 | private native long init(long tableId); |
| 234 | public native boolean hasNext(); |
| 235 | public native Object next(); |
| 236 | } |
| 237 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 238 | public class TableEnumerator2 { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 239 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 240 | protected long tableId; |
| 241 | protected LinkedList<JRamCloud.Object> rcobjs = null; |
| 242 | protected long nextHash = 0; |
| 243 | protected boolean done = false; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 244 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 245 | public TableEnumerator2(long tableId) |
| 246 | { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 247 | this.tableId = tableId; |
| 248 | rcobjs = new LinkedList<>(); |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 249 | } |
| 250 | public boolean hasNext() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 251 | if (rcobjs.isEmpty()) |
| 252 | { |
| 253 | if (done) { |
| 254 | return false; |
| 255 | } |
| 256 | JRamCloud.TableEnumeratorObject o = getTableObjects(this.tableId, this.nextHash); |
| 257 | if (o.nextHash == 0L) { |
| 258 | done = true; |
| 259 | } |
| 260 | this.nextHash = o.nextHash; |
| 261 | rcobjs.addAll(Arrays.asList(o.object)); |
| 262 | if (rcobjs.isEmpty()) { |
| 263 | return false; |
| 264 | } |
| 265 | } |
| 266 | return true; |
| 267 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 268 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 269 | public Object next() |
| 270 | { |
| 271 | return rcobjs.pop(); |
| 272 | } |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 273 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 274 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 275 | /** |
| 276 | * Connect to the RAMCloud cluster specified by the given coordinator's |
| 277 | * service locator string. This causes the JNI code to instantiate the |
| 278 | * underlying RamCloud C++ object. |
| 279 | */ |
| 280 | public |
Yuta HIGUCHI | d150ece | 2014-04-29 16:25:36 -0700 | [diff] [blame] | 281 | JRamCloud(String coordinatorLocator, String clusterName) |
| 282 | { |
| 283 | ramcloudObjectPointer = connect(coordinatorLocator, clusterName); |
| 284 | } |
| 285 | |
| 286 | public |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 287 | JRamCloud(String coordinatorLocator) |
| 288 | { |
Yuta HIGUCHI | d150ece | 2014-04-29 16:25:36 -0700 | [diff] [blame] | 289 | this(coordinatorLocator, "main"); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Disconnect from the RAMCloud cluster. This causes the JNI code to |
| 294 | * destroy the underlying RamCloud C++ object. |
| 295 | */ |
| 296 | public void |
| 297 | disconnect() |
| 298 | { |
| 299 | if (ramcloudObjectPointer != 0) { |
| 300 | disconnect(ramcloudObjectPointer); |
| 301 | ramcloudObjectPointer = 0; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * This method is called by the garbage collector before destroying the |
| 307 | * object. The user really should have called disconnect, but in case |
| 308 | * they did not, be sure to clean up after them. |
| 309 | */ |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 310 | @Override |
| 311 | protected void |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 312 | finalize() |
| 313 | { |
| 314 | System.err.println("warning: JRamCloud::disconnect() was not called " + |
| 315 | "prior to the finalizer. You should disconnect " + |
| 316 | "your JRamCloud object when you're done with it."); |
| 317 | disconnect(); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Convenience read() wrapper that take a String key argument. |
| 322 | */ |
| 323 | public Object |
| 324 | read(long tableId, String key) |
| 325 | { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 326 | return read(tableId, key.getBytes(StandardCharsets.UTF_8)); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Convenience read() wrapper that take a String key argument. |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 331 | * @throws RejectRulesException |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 332 | */ |
| 333 | public Object |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 334 | read(long tableId, String key, RejectRules rules) throws RejectRulesException |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 335 | { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 336 | return read(tableId, key.getBytes(StandardCharsets.UTF_8), rules); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 337 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 338 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 339 | /** |
| 340 | * Convenience remove() wrapper that take a String key argument. |
| 341 | */ |
| 342 | public long |
| 343 | remove(long tableId, String key) |
| 344 | { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 345 | return remove(tableId, key.getBytes(StandardCharsets.UTF_8)); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Convenience remove() wrapper that take a String key argument. |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 350 | * @throws RejectRulesException |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 351 | */ |
| 352 | public long |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 353 | remove(long tableId, String key, RejectRules rules) throws RejectRulesException |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 354 | { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 355 | return remove(tableId, key.getBytes(StandardCharsets.UTF_8), rules); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Convenience write() wrapper that take String key and value arguments. |
| 360 | */ |
| 361 | public long |
| 362 | write(long tableId, String key, String value) |
| 363 | { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 364 | return write(tableId, key.getBytes(StandardCharsets.UTF_8), value.getBytes(StandardCharsets.UTF_8)); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Convenience write() wrapper that take String key and value arguments. |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 369 | * @throws RejectRulesException |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 370 | */ |
| 371 | public long |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 372 | write(long tableId, String key, String value, RejectRules rules) throws RejectRulesException |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 373 | { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 374 | return write(tableId, key.getBytes(StandardCharsets.UTF_8), value.getBytes(StandardCharsets.UTF_8), rules); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Convenience write() wrapper that takes a String key and a byte[] value |
| 379 | * argument. |
| 380 | */ |
| 381 | public long |
| 382 | write(long tableId, String key, byte[] value) |
| 383 | { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 384 | return write(tableId, key.getBytes(StandardCharsets.UTF_8), value); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Convenience write() wrapper that takes a String key and a byte[] value |
| 389 | * argument. |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 390 | * @throws RejectRulesException |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 391 | */ |
| 392 | public long |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 393 | write(long tableId, String key, byte[] value, RejectRules rules) throws RejectRulesException |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 394 | { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 395 | return write(tableId, key.getBytes(StandardCharsets.UTF_8), value, rules); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 396 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 397 | |
Yuta HIGUCHI | d150ece | 2014-04-29 16:25:36 -0700 | [diff] [blame] | 398 | private static native long connect(String coordinatorLocator, String clusterName); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 399 | private static native void disconnect(long ramcloudObjectPointer); |
| 400 | |
| 401 | public native long createTable(String name); |
| 402 | public native long createTable(String name, int serverSpan); |
| 403 | public native void dropTable(String name); |
| 404 | public native long getTableId(String name); |
| 405 | public native Object read(long tableId, byte[] key); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 406 | public native Object read(long tableId, byte[] key, RejectRules rules) throws RejectRulesException; |
Yoshi Muroi | 2c17060 | 2014-02-15 08:31:28 -0800 | [diff] [blame] | 407 | public native Object[] multiRead(long[] tableId, byte[] keydata[], short[] keyDataSize, int requestNum); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 408 | public native long remove(long tableId, byte[] key); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 409 | public native long remove(long tableId, byte[] key, RejectRules rules) throws RejectRulesException; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 410 | public native long write(long tableId, byte[] key, byte[] value); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 411 | public native long write(long tableId, byte[] key, byte[] value, RejectRules rules) throws RejectRulesException; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 412 | public native MultiWriteRspObject[] multiWrite(long[] tableId, byte[] key[], short[] keyDataSize, byte[] value[], int[] valueDataSize, int requestNum, RejectRules[] rules); |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 413 | public native TableEnumeratorObject getTableObjects(long tableId, long nextHash); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 414 | public native long increment(long tableId, byte[] key, long incrementValue) throws ObjectDoesntExistException; |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 415 | |
| 416 | /* |
| 417 | * The following exceptions may be thrown by the JNI functions: |
| 418 | */ |
| 419 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 420 | public static class TableDoesntExistException extends RuntimeException { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 421 | public TableDoesntExistException(String message) |
| 422 | { |
| 423 | super(message); |
| 424 | } |
| 425 | } |
| 426 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 427 | public static class ObjectDoesntExistException extends RejectRulesException { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 428 | public ObjectDoesntExistException(String message) |
| 429 | { |
| 430 | super(message); |
| 431 | } |
| 432 | } |
| 433 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 434 | public static class ObjectExistsException extends RejectRulesException { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 435 | public ObjectExistsException(String message) |
| 436 | { |
| 437 | super(message); |
| 438 | } |
| 439 | } |
| 440 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 441 | public static class WrongVersionException extends RejectRulesException { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 442 | public WrongVersionException(String message) |
| 443 | { |
| 444 | super(message); |
| 445 | } |
| 446 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 447 | |
| 448 | public static class InvalidObjectException extends RuntimeException { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 449 | public InvalidObjectException(String message) { |
| 450 | super(message); |
| 451 | } |
| 452 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 453 | |
| 454 | public static class RejectRulesException extends Exception { |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 455 | public RejectRulesException(String message) { |
| 456 | super(message); |
| 457 | } |
| 458 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 459 | |
Yuta HIGUCHI | d150ece | 2014-04-29 16:25:36 -0700 | [diff] [blame] | 460 | // TODO Define and support some of ClientException sub-classes. |
| 461 | public static class ClientException extends Exception { |
| 462 | public ClientException(String message) { |
| 463 | super(message); |
| 464 | } |
| 465 | } |
| 466 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 467 | public static void tableEnumeratorTest(JRamCloud ramcloud) { |
| 468 | long startTime = 0; |
| 469 | for (int x = 0 ; x < 2 ; x ++){ |
| 470 | for(int N = 1000; N < 10000; N += 1000) { |
| 471 | long EnumerateTesttable = ramcloud.createTable("EnumerateTest"); |
| 472 | for(int i = 0 ; i < N ; ++i) { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 473 | String key = String.valueOf(i); |
| 474 | ramcloud.write(EnumerateTesttable, key.getBytes(StandardCharsets.UTF_8), "Hello, World!".getBytes(StandardCharsets.UTF_8)); |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 475 | } |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 476 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 477 | long tableIdList[] = new long[N]; |
| 478 | byte[] keydata[] = new byte[N][]; |
| 479 | short keydataSize[] = new short[N]; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 480 | startTime = System.nanoTime(); |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 481 | for (int j = 0 ; j < N ; ++j) { |
| 482 | tableIdList[j] = EnumerateTesttable; |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 483 | String key = String.valueOf(j); |
| 484 | keydata[j] = key.getBytes(StandardCharsets.UTF_8); |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 485 | keydataSize[j] = (short) keydata[j].length; |
| 486 | } |
| 487 | JRamCloud.Object out[] = ramcloud.multiRead(tableIdList, keydata, keydataSize, N); |
| 488 | for (int i = 0; i < N; ++i) { |
| 489 | if(out[i].version == 0) { |
| 490 | System.out.println("Verify fail " + out[i].getKey() + " V:" + out[i].getValue()); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | System.out.println("multiRead : " + N + " Time : " + (System.nanoTime()-startTime)); |
| 495 | |
| 496 | startTime = System.nanoTime(); |
| 497 | JRamCloud.TableEnumerator tableEnum = ramcloud.new TableEnumerator(EnumerateTesttable); |
| 498 | while (tableEnum.hasNext()) { |
| 499 | Object tableEntry = tableEnum.next(); |
| 500 | if (tableEntry != null) { |
| 501 | System.out.println("tableEnumerator object: key = [" + tableEntry.getKey() + "], value = [" + tableEntry.getValue() + "]"); |
| 502 | } |
| 503 | } |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 504 | System.out.println("old TableEnumerator : " + N + " Time : " + (System.nanoTime()-startTime)); |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 505 | |
| 506 | startTime = System.nanoTime(); |
| 507 | JRamCloud.TableEnumerator2 tableEnum2 = ramcloud.new TableEnumerator2(EnumerateTesttable); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 508 | while (tableEnum2.hasNext()) { |
| 509 | Object tableEntry2 = tableEnum2.next(); |
| 510 | if (tableEntry2 != null) { |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 511 | System.out.println("tableEnumerator2 object: key = [" + tableEntry2.getKey() + "], value = [" + tableEntry2.getValue() + "]"); |
| 512 | } |
| 513 | } |
| 514 | System.out.println("new TableEnumerator : " + N + " Time : " + (System.nanoTime()-startTime)); |
| 515 | ramcloud.dropTable("EnumerateTest"); |
| 516 | } |
| 517 | } |
| 518 | } |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 519 | /** |
| 520 | * A simple end-to-end test of the java bindings. |
| 521 | */ |
| 522 | public static void |
| 523 | main(String argv[]) |
| 524 | { |
| 525 | JRamCloud ramcloud = new JRamCloud(argv[0]); |
| 526 | long tableId = ramcloud.createTable("hi"); |
| 527 | System.out.println("created table, id = " + tableId); |
| 528 | long tableId2 = ramcloud.getTableId("hi"); |
| 529 | System.out.println("getTableId says tableId = " + tableId2); |
| 530 | |
| 531 | System.out.println("wrote obj version = " + |
| 532 | ramcloud.write(tableId, "thisIsTheKey", "thisIsTheValue")); |
| 533 | |
| 534 | JRamCloud.Object o = ramcloud.read(tableId, "thisIsTheKey"); |
| 535 | System.out.println("read object: key = [" + o.getKey() + "], value = [" |
| 536 | + o.getValue() + "], version = " + o.version); |
| 537 | |
| 538 | ramcloud.remove(tableId, "thisIsTheKey"); |
| 539 | |
| 540 | try { |
| 541 | ramcloud.read(tableId, "thisIsTheKey"); |
| 542 | System.out.println("Error: shouldn't have read successfully!"); |
| 543 | } catch (Exception e) { |
| 544 | // OK |
| 545 | } |
| 546 | |
| 547 | ramcloud.write(tableId, "thisIsTheKey", "thisIsTheValue"); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 548 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 549 | long before = System.nanoTime(); |
| 550 | for (int i = 0; i < 1000; i++) { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 551 | @SuppressWarnings("unused") |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 552 | JRamCloud.Object unused = ramcloud.read(tableId, "thisIsTheKey"); |
| 553 | } |
| 554 | long after = System.nanoTime(); |
| 555 | System.out.println("Avg read latency: " + |
| 556 | ((double)(after - before) / 1000 / 1000) + " usec"); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 557 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 558 | // multiRead test |
| 559 | long tableId4 = ramcloud.createTable("table4"); |
| 560 | System.out.println("table4 id " + tableId4); |
| 561 | ramcloud.write(tableId4, "object1-1", "value:1-1"); |
| 562 | ramcloud.write(tableId4, "object1-2", "value:1-2"); |
| 563 | ramcloud.write(tableId4, "object1-3", "value:1-3"); |
| 564 | long tableId5 = ramcloud.createTable("table5"); |
| 565 | System.out.println("table5 id " + tableId5); |
| 566 | ramcloud.write(tableId5, "object2-1", "value:2-1"); |
| 567 | long tableId6 = ramcloud.createTable("table6"); |
| 568 | ramcloud.write(tableId6, "object3-1", "value:3-1"); |
| 569 | ramcloud.write(tableId6, "object3-2", "value:3-2"); |
| 570 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 571 | MultiReadObject mr = new MultiReadObject(2); |
| 572 | MultiWriteObject mw = new MultiWriteObject(2); |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 573 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 574 | mr.setObject(0, tableId4, "object1-1".getBytes(StandardCharsets.UTF_8)); |
| 575 | mr.setObject(1, tableId5, "object2-1".getBytes(StandardCharsets.UTF_8)); |
Yoshi Muroi | 2c17060 | 2014-02-15 08:31:28 -0800 | [diff] [blame] | 576 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 577 | JRamCloud.Object out[] = ramcloud.multiRead(mr.tableId, mr.key, mr.keyLength, 2); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 578 | for (int i = 0 ; i < 2 ; i++){ |
| 579 | System.out.println("multi read object: key = [" + out[i].getKey() + "], value = [" |
| 580 | + out[i].getValue() + "]"); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 581 | } |
Yoshi Muroi | 2c17060 | 2014-02-15 08:31:28 -0800 | [diff] [blame] | 582 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 583 | for (int i = 0; i < 1000; i++) { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 584 | String key1 = "key1" + String.valueOf(i); |
| 585 | String key2 = "key2" + String.valueOf(i); |
| 586 | |
| 587 | mw.setObject(0, tableId4, key1.getBytes(StandardCharsets.UTF_8), "v0-value".getBytes(StandardCharsets.UTF_8), null); |
| 588 | mw.setObject(1, tableId5, key2.getBytes(StandardCharsets.UTF_8), "v1".getBytes(StandardCharsets.UTF_8), null); |
| 589 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 590 | MultiWriteRspObject[] rsp = ramcloud.multiWrite(mw.tableId, mw.key, mw.keyLength, mw.value, mw.valueLength, 2, mw.rules); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 591 | if (rsp != null) { |
| 592 | for (int j = 0; j < rsp.length; j++) { |
| 593 | System.out.println("multi write rsp(" + j + ") status:version " + rsp[j].getStatus() + ":" + rsp[j].getVersion()); |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | for (int i = 0; i < 1000; i++) { |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 598 | String key1 = "key1" + String.valueOf(i); |
| 599 | String key2 = "key2" + String.valueOf(i); |
Yoshi Muroi | 2c17060 | 2014-02-15 08:31:28 -0800 | [diff] [blame] | 600 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 601 | mr.setObject(0, tableId4, key1.getBytes(StandardCharsets.UTF_8)); |
| 602 | mr.setObject(1, tableId5, key2.getBytes(StandardCharsets.UTF_8)); |
| 603 | |
Yoshi Muroi | e7693b1 | 2014-02-19 19:41:17 -0800 | [diff] [blame] | 604 | out = ramcloud.multiRead(mr.tableId, mr.key, mr.keyLength, 2); |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 605 | for (int j = 0; j < 2; j++) { |
| 606 | System.out.println("multi read object: key = [" + out[j].getKey() + "], value = [" + out[j].getValue() + "]"); |
| 607 | } |
| 608 | } |
Yoshi Muroi | 2c17060 | 2014-02-15 08:31:28 -0800 | [diff] [blame] | 609 | |
Yuta HIGUCHI | 1d6a22a | 2014-02-21 19:17:42 -0800 | [diff] [blame] | 610 | tableEnumeratorTest(ramcloud); |
| 611 | |
yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 612 | ramcloud.dropTable("table4"); |
| 613 | ramcloud.dropTable("table5"); |
| 614 | ramcloud.dropTable("table6"); |
| 615 | ramcloud.disconnect(); |
| 616 | } |
| 617 | } |