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