blob: e8aba19318c36cd9261fd412ad333b58371c1752 [file] [log] [blame]
yoshi28bac132014-01-22 11:00:17 -08001/* 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
16package edu.stanford.ramcloud;
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -080017import java.nio.charset.StandardCharsets;
Yoshi Muroi2c170602014-02-15 08:31:28 -080018import java.util.Arrays;
Yoshi Muroie7693b12014-02-19 19:41:17 -080019import java.util.LinkedList;
yoshi28bac132014-01-22 11:00:17 -080020
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 */
34public class JRamCloud {
35 static {
36 System.loadLibrary("edu_stanford_ramcloud_JRamCloud");
37 }
38
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -080039 public static final long VERSION_NONEXISTENT = 0L;
40
yoshi28bac132014-01-22 11:00:17 -080041 /// 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 HIGUCHI1d6a22a2014-02-21 19:17:42 -080048 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;
yoshi28bac132014-01-22 11:00:17 -080057
58 public RejectRules() {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -080059 clear();
yoshi28bac132014-01-22 11:00:17 -080060 }
61
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -080062 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) {
yoshi28bac132014-01-22 11:00:17 -080078 setVersion(version);
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -080079 this.flags |= VersionLeGiven;
80 return this;
yoshi28bac132014-01-22 11:00:17 -080081 }
82
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -080083 public RejectRules rejectIfExists() {
84 this.flags |= Exists;
85 return this;
yoshi28bac132014-01-22 11:00:17 -080086 }
87
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -080088 public RejectRules rejectIfDoesntExists() {
89 this.flags |= DoesntExist;
90 return this;
yoshi28bac132014-01-22 11:00:17 -080091 }
92
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -080093 public RejectRules rejectIfNeVersion(long version) {
yoshi28bac132014-01-22 11:00:17 -080094 setVersion(version);
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -080095 this.flags |= VersionNeGiven;
96 return this;
yoshi28bac132014-01-22 11:00:17 -080097 }
98
99 private void setVersion(long version) {
100 this.givenVersion = version;
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800101 }
yoshi28bac132014-01-22 11:00:17 -0800102 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800103
Yoshi Muroie7693b12014-02-19 19:41:17 -0800104 public static class MultiReadObject {
105 public long[] tableId;
106 public byte[] key[];
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800107 public short[] keyLength;
108
Yoshi Muroie7693b12014-02-19 19:41:17 -0800109 public MultiReadObject(int size){
110 this.tableId = new long[size];
111 this.key = new byte[size][];
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800112 this.keyLength = new short[size];
yoshi28bac132014-01-22 11:00:17 -0800113 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800114
Ray Milkey269ffb92014-04-03 14:43:30 -0700115 public void setObject(int num, long tableId, byte key[]){
Yoshi Muroie7693b12014-02-19 19:41:17 -0800116 this.tableId[num] = tableId;
117 this.key[num] = key;
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800118 this.keyLength[num] = (short) this.key[num].length;
Ray Milkey269ffb92014-04-03 14:43:30 -0700119 }
yoshi28bac132014-01-22 11:00:17 -0800120 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800121
yoshi28bac132014-01-22 11:00:17 -0800122 public static class MultiWriteObject {
Yoshi Muroie7693b12014-02-19 19:41:17 -0800123 public long[] tableId;
124 public byte[] key[];
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800125 public short[] keyLength;
Yoshi Muroie7693b12014-02-19 19:41:17 -0800126 public byte[] value[];
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800127 public int[] valueLength;
Yoshi Muroie7693b12014-02-19 19:41:17 -0800128 public RejectRules[] rules;
yoshi28bac132014-01-22 11:00:17 -0800129
Yoshi Muroie7693b12014-02-19 19:41:17 -0800130 public MultiWriteObject(int size) {
131 this.tableId = new long[size];
132 this.key = new byte[size][];
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800133 this.keyLength = new short[size];
Yoshi Muroie7693b12014-02-19 19:41:17 -0800134 this.value = new byte[size][];
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800135 this.valueLength = new int[size];
Yoshi Muroie7693b12014-02-19 19:41:17 -0800136 this.rules = new RejectRules[size];
yoshi28bac132014-01-22 11:00:17 -0800137 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800138
Ray Milkey269ffb92014-04-03 14:43:30 -0700139 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 HIGUCHI1d6a22a2014-02-21 19:17:42 -0800147
yoshi28bac132014-01-22 11:00:17 -0800148 }
149
Yoshi Muroie7693b12014-02-19 19:41:17 -0800150 public static class MultiWriteRspObject {
yoshi28bac132014-01-22 11:00:17 -0800151 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 Muroie7693b12014-02-19 19:41:17 -0800175 public static class Object {
yoshi28bac132014-01-22 11:00:17 -0800176 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 HIGUCHI1d6a22a2014-02-21 19:17:42 -0800186 return new String(key,StandardCharsets.UTF_8);
yoshi28bac132014-01-22 11:00:17 -0800187 }
188
189 public String
190 getValue()
191 {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800192 return new String(value,StandardCharsets.UTF_8);
yoshi28bac132014-01-22 11:00:17 -0800193 }
194
195 final public byte[] key;
196 final public byte[] value;
197 final public long version;
198 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800199
Yoshi Muroie7693b12014-02-19 19:41:17 -0800200 public static class TableEnumeratorObject {
Ray Milkey269ffb92014-04-03 14:43:30 -0700201 TableEnumeratorObject(Object[] _object, long _nextHash)
202 {
203 object = _object;
204 nextHash = _nextHash;
205 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800206
Ray Milkey269ffb92014-04-03 14:43:30 -0700207 final public Object[] object;
208 final public long nextHash;
Yoshi Muroie7693b12014-02-19 19:41:17 -0800209 }
yoshi28bac132014-01-22 11:00:17 -0800210
Yuta HIGUCHIe316d5c2014-04-19 13:37:38 -0700211 @Deprecated
yoshi28bac132014-01-22 11:00:17 -0800212 public class TableEnumerator {
213 private long tableEnumeratorObjectPointer = 0;
214 private long ramCloudObjectPointer = 0;
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800215
yoshi28bac132014-01-22 11:00:17 -0800216 public TableEnumerator(long tableId)
217 {
218 ramCloudObjectPointer = ramcloudObjectPointer;
219 tableEnumeratorObjectPointer = init(tableId);
220 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800221
yoshi28bac132014-01-22 11:00:17 -0800222 private native long init(long tableId);
223 public native boolean hasNext();
224 public native Object next();
225 }
226
Yoshi Muroie7693b12014-02-19 19:41:17 -0800227 public class TableEnumerator2 {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800228
Ray Milkey269ffb92014-04-03 14:43:30 -0700229 protected long tableId;
230 protected LinkedList<JRamCloud.Object> rcobjs = null;
231 protected long nextHash = 0;
232 protected boolean done = false;
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800233
Yoshi Muroie7693b12014-02-19 19:41:17 -0800234 public TableEnumerator2(long tableId)
235 {
Ray Milkey269ffb92014-04-03 14:43:30 -0700236 this.tableId = tableId;
237 rcobjs = new LinkedList<>();
Yoshi Muroie7693b12014-02-19 19:41:17 -0800238 }
239 public boolean hasNext() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700240 if (rcobjs.isEmpty())
241 {
242 if (done) {
243 return false;
244 }
245 JRamCloud.TableEnumeratorObject o = getTableObjects(this.tableId, this.nextHash);
246 if (o.nextHash == 0L) {
247 done = true;
248 }
249 this.nextHash = o.nextHash;
250 rcobjs.addAll(Arrays.asList(o.object));
251 if (rcobjs.isEmpty()) {
252 return false;
253 }
254 }
255 return true;
256 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800257
Ray Milkey269ffb92014-04-03 14:43:30 -0700258 public Object next()
259 {
260 return rcobjs.pop();
261 }
Yoshi Muroie7693b12014-02-19 19:41:17 -0800262 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800263
yoshi28bac132014-01-22 11:00:17 -0800264 /**
265 * Connect to the RAMCloud cluster specified by the given coordinator's
266 * service locator string. This causes the JNI code to instantiate the
267 * underlying RamCloud C++ object.
268 */
269 public
270 JRamCloud(String coordinatorLocator)
271 {
272 ramcloudObjectPointer = connect(coordinatorLocator);
273 }
274
275 /**
276 * Disconnect from the RAMCloud cluster. This causes the JNI code to
277 * destroy the underlying RamCloud C++ object.
278 */
279 public void
280 disconnect()
281 {
282 if (ramcloudObjectPointer != 0) {
283 disconnect(ramcloudObjectPointer);
284 ramcloudObjectPointer = 0;
285 }
286 }
287
288 /**
289 * This method is called by the garbage collector before destroying the
290 * object. The user really should have called disconnect, but in case
291 * they did not, be sure to clean up after them.
292 */
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800293 @Override
294 protected void
yoshi28bac132014-01-22 11:00:17 -0800295 finalize()
296 {
297 System.err.println("warning: JRamCloud::disconnect() was not called " +
298 "prior to the finalizer. You should disconnect " +
299 "your JRamCloud object when you're done with it.");
300 disconnect();
301 }
302
303 /**
304 * Convenience read() wrapper that take a String key argument.
305 */
306 public Object
307 read(long tableId, String key)
308 {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800309 return read(tableId, key.getBytes(StandardCharsets.UTF_8));
yoshi28bac132014-01-22 11:00:17 -0800310 }
311
312 /**
313 * Convenience read() wrapper that take a String key argument.
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800314 * @throws RejectRulesException
yoshi28bac132014-01-22 11:00:17 -0800315 */
316 public Object
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800317 read(long tableId, String key, RejectRules rules) throws RejectRulesException
yoshi28bac132014-01-22 11:00:17 -0800318 {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800319 return read(tableId, key.getBytes(StandardCharsets.UTF_8), rules);
yoshi28bac132014-01-22 11:00:17 -0800320 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800321
yoshi28bac132014-01-22 11:00:17 -0800322 /**
323 * Convenience remove() wrapper that take a String key argument.
324 */
325 public long
326 remove(long tableId, String key)
327 {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800328 return remove(tableId, key.getBytes(StandardCharsets.UTF_8));
yoshi28bac132014-01-22 11:00:17 -0800329 }
330
331 /**
332 * Convenience remove() wrapper that take a String key argument.
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800333 * @throws RejectRulesException
yoshi28bac132014-01-22 11:00:17 -0800334 */
335 public long
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800336 remove(long tableId, String key, RejectRules rules) throws RejectRulesException
yoshi28bac132014-01-22 11:00:17 -0800337 {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800338 return remove(tableId, key.getBytes(StandardCharsets.UTF_8), rules);
yoshi28bac132014-01-22 11:00:17 -0800339 }
340
341 /**
342 * Convenience write() wrapper that take String key and value arguments.
343 */
344 public long
345 write(long tableId, String key, String value)
346 {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800347 return write(tableId, key.getBytes(StandardCharsets.UTF_8), value.getBytes(StandardCharsets.UTF_8));
yoshi28bac132014-01-22 11:00:17 -0800348 }
349
350 /**
351 * Convenience write() wrapper that take String key and value arguments.
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800352 * @throws RejectRulesException
yoshi28bac132014-01-22 11:00:17 -0800353 */
354 public long
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800355 write(long tableId, String key, String value, RejectRules rules) throws RejectRulesException
yoshi28bac132014-01-22 11:00:17 -0800356 {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800357 return write(tableId, key.getBytes(StandardCharsets.UTF_8), value.getBytes(StandardCharsets.UTF_8), rules);
yoshi28bac132014-01-22 11:00:17 -0800358 }
359
360 /**
361 * Convenience write() wrapper that takes a String key and a byte[] value
362 * argument.
363 */
364 public long
365 write(long tableId, String key, byte[] value)
366 {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800367 return write(tableId, key.getBytes(StandardCharsets.UTF_8), value);
yoshi28bac132014-01-22 11:00:17 -0800368 }
369
370 /**
371 * Convenience write() wrapper that takes a String key and a byte[] value
372 * argument.
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800373 * @throws RejectRulesException
yoshi28bac132014-01-22 11:00:17 -0800374 */
375 public long
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800376 write(long tableId, String key, byte[] value, RejectRules rules) throws RejectRulesException
yoshi28bac132014-01-22 11:00:17 -0800377 {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800378 return write(tableId, key.getBytes(StandardCharsets.UTF_8), value, rules);
yoshi28bac132014-01-22 11:00:17 -0800379 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800380
yoshi28bac132014-01-22 11:00:17 -0800381 private static native long connect(String coordinatorLocator);
382 private static native void disconnect(long ramcloudObjectPointer);
383
384 public native long createTable(String name);
385 public native long createTable(String name, int serverSpan);
386 public native void dropTable(String name);
387 public native long getTableId(String name);
388 public native Object read(long tableId, byte[] key);
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800389 public native Object read(long tableId, byte[] key, RejectRules rules) throws RejectRulesException;
Yoshi Muroi2c170602014-02-15 08:31:28 -0800390 public native Object[] multiRead(long[] tableId, byte[] keydata[], short[] keyDataSize, int requestNum);
yoshi28bac132014-01-22 11:00:17 -0800391 public native long remove(long tableId, byte[] key);
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800392 public native long remove(long tableId, byte[] key, RejectRules rules) throws RejectRulesException;
yoshi28bac132014-01-22 11:00:17 -0800393 public native long write(long tableId, byte[] key, byte[] value);
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800394 public native long write(long tableId, byte[] key, byte[] value, RejectRules rules) throws RejectRulesException;
395 @Deprecated
yoshi28bac132014-01-22 11:00:17 -0800396 public native long writeRule(long tableId, byte[] key, byte[] value, RejectRules rules);
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800397 public native MultiWriteRspObject[] multiWrite(long[] tableId, byte[] key[], short[] keyDataSize, byte[] value[], int[] valueDataSize, int requestNum, RejectRules[] rules);
Yoshi Muroie7693b12014-02-19 19:41:17 -0800398 public native TableEnumeratorObject getTableObjects(long tableId, long nextHash);
Yuta HIGUCHId47eac32014-04-07 13:44:47 -0700399 public native long increment(long tableId, byte[] key, long incrementValue) throws ObjectDoesntExistException;
yoshi28bac132014-01-22 11:00:17 -0800400
401 /*
402 * The following exceptions may be thrown by the JNI functions:
403 */
404
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800405 public static class TableDoesntExistException extends RuntimeException {
yoshi28bac132014-01-22 11:00:17 -0800406 public TableDoesntExistException(String message)
407 {
408 super(message);
409 }
410 }
411
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800412 public static class ObjectDoesntExistException extends RejectRulesException {
yoshi28bac132014-01-22 11:00:17 -0800413 public ObjectDoesntExistException(String message)
414 {
415 super(message);
416 }
417 }
418
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800419 public static class ObjectExistsException extends RejectRulesException {
yoshi28bac132014-01-22 11:00:17 -0800420 public ObjectExistsException(String message)
421 {
422 super(message);
423 }
424 }
425
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800426 public static class WrongVersionException extends RejectRulesException {
yoshi28bac132014-01-22 11:00:17 -0800427 public WrongVersionException(String message)
428 {
429 super(message);
430 }
431 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800432
433 public static class InvalidObjectException extends RuntimeException {
yoshi28bac132014-01-22 11:00:17 -0800434 public InvalidObjectException(String message) {
435 super(message);
436 }
437 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800438
439 public static class RejectRulesException extends Exception {
yoshi28bac132014-01-22 11:00:17 -0800440 public RejectRulesException(String message) {
441 super(message);
442 }
443 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800444
Yoshi Muroie7693b12014-02-19 19:41:17 -0800445 public static void tableEnumeratorTest(JRamCloud ramcloud) {
446 long startTime = 0;
447 for (int x = 0 ; x < 2 ; x ++){
448 for(int N = 1000; N < 10000; N += 1000) {
449 long EnumerateTesttable = ramcloud.createTable("EnumerateTest");
450 for(int i = 0 ; i < N ; ++i) {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800451 String key = String.valueOf(i);
452 ramcloud.write(EnumerateTesttable, key.getBytes(StandardCharsets.UTF_8), "Hello, World!".getBytes(StandardCharsets.UTF_8));
Yoshi Muroie7693b12014-02-19 19:41:17 -0800453 }
yoshi28bac132014-01-22 11:00:17 -0800454
Yoshi Muroie7693b12014-02-19 19:41:17 -0800455 long tableIdList[] = new long[N];
456 byte[] keydata[] = new byte[N][];
457 short keydataSize[] = new short[N];
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800458 startTime = System.nanoTime();
Yoshi Muroie7693b12014-02-19 19:41:17 -0800459 for (int j = 0 ; j < N ; ++j) {
460 tableIdList[j] = EnumerateTesttable;
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800461 String key = String.valueOf(j);
462 keydata[j] = key.getBytes(StandardCharsets.UTF_8);
Yoshi Muroie7693b12014-02-19 19:41:17 -0800463 keydataSize[j] = (short) keydata[j].length;
464 }
465 JRamCloud.Object out[] = ramcloud.multiRead(tableIdList, keydata, keydataSize, N);
466 for (int i = 0; i < N; ++i) {
467 if(out[i].version == 0) {
468 System.out.println("Verify fail " + out[i].getKey() + " V:" + out[i].getValue());
469 }
470 }
471
472 System.out.println("multiRead : " + N + " Time : " + (System.nanoTime()-startTime));
473
474 startTime = System.nanoTime();
475 JRamCloud.TableEnumerator tableEnum = ramcloud.new TableEnumerator(EnumerateTesttable);
476 while (tableEnum.hasNext()) {
477 Object tableEntry = tableEnum.next();
478 if (tableEntry != null) {
479 System.out.println("tableEnumerator object: key = [" + tableEntry.getKey() + "], value = [" + tableEntry.getValue() + "]");
480 }
481 }
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800482 System.out.println("old TableEnumerator : " + N + " Time : " + (System.nanoTime()-startTime));
Yoshi Muroie7693b12014-02-19 19:41:17 -0800483
484 startTime = System.nanoTime();
485 JRamCloud.TableEnumerator2 tableEnum2 = ramcloud.new TableEnumerator2(EnumerateTesttable);
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800486 while (tableEnum2.hasNext()) {
487 Object tableEntry2 = tableEnum2.next();
488 if (tableEntry2 != null) {
Yoshi Muroie7693b12014-02-19 19:41:17 -0800489 System.out.println("tableEnumerator2 object: key = [" + tableEntry2.getKey() + "], value = [" + tableEntry2.getValue() + "]");
490 }
491 }
492 System.out.println("new TableEnumerator : " + N + " Time : " + (System.nanoTime()-startTime));
493 ramcloud.dropTable("EnumerateTest");
494 }
495 }
496 }
yoshi28bac132014-01-22 11:00:17 -0800497 /**
498 * A simple end-to-end test of the java bindings.
499 */
500 public static void
501 main(String argv[])
502 {
503 JRamCloud ramcloud = new JRamCloud(argv[0]);
504 long tableId = ramcloud.createTable("hi");
505 System.out.println("created table, id = " + tableId);
506 long tableId2 = ramcloud.getTableId("hi");
507 System.out.println("getTableId says tableId = " + tableId2);
508
509 System.out.println("wrote obj version = " +
510 ramcloud.write(tableId, "thisIsTheKey", "thisIsTheValue"));
511
512 JRamCloud.Object o = ramcloud.read(tableId, "thisIsTheKey");
513 System.out.println("read object: key = [" + o.getKey() + "], value = ["
514 + o.getValue() + "], version = " + o.version);
515
516 ramcloud.remove(tableId, "thisIsTheKey");
517
518 try {
519 ramcloud.read(tableId, "thisIsTheKey");
520 System.out.println("Error: shouldn't have read successfully!");
521 } catch (Exception e) {
522 // OK
523 }
524
525 ramcloud.write(tableId, "thisIsTheKey", "thisIsTheValue");
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800526
yoshi28bac132014-01-22 11:00:17 -0800527 long before = System.nanoTime();
528 for (int i = 0; i < 1000; i++) {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800529 @SuppressWarnings("unused")
yoshi28bac132014-01-22 11:00:17 -0800530 JRamCloud.Object unused = ramcloud.read(tableId, "thisIsTheKey");
531 }
532 long after = System.nanoTime();
533 System.out.println("Avg read latency: " +
534 ((double)(after - before) / 1000 / 1000) + " usec");
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800535
yoshi28bac132014-01-22 11:00:17 -0800536 // multiRead test
537 long tableId4 = ramcloud.createTable("table4");
538 System.out.println("table4 id " + tableId4);
539 ramcloud.write(tableId4, "object1-1", "value:1-1");
540 ramcloud.write(tableId4, "object1-2", "value:1-2");
541 ramcloud.write(tableId4, "object1-3", "value:1-3");
542 long tableId5 = ramcloud.createTable("table5");
543 System.out.println("table5 id " + tableId5);
544 ramcloud.write(tableId5, "object2-1", "value:2-1");
545 long tableId6 = ramcloud.createTable("table6");
546 ramcloud.write(tableId6, "object3-1", "value:3-1");
547 ramcloud.write(tableId6, "object3-2", "value:3-2");
548
Ray Milkey269ffb92014-04-03 14:43:30 -0700549 MultiReadObject mr = new MultiReadObject(2);
550 MultiWriteObject mw = new MultiWriteObject(2);
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800551
Ray Milkey269ffb92014-04-03 14:43:30 -0700552 mr.setObject(0, tableId4, "object1-1".getBytes(StandardCharsets.UTF_8));
553 mr.setObject(1, tableId5, "object2-1".getBytes(StandardCharsets.UTF_8));
Yoshi Muroi2c170602014-02-15 08:31:28 -0800554
Yoshi Muroie7693b12014-02-19 19:41:17 -0800555 JRamCloud.Object out[] = ramcloud.multiRead(mr.tableId, mr.key, mr.keyLength, 2);
yoshi28bac132014-01-22 11:00:17 -0800556 for (int i = 0 ; i < 2 ; i++){
557 System.out.println("multi read object: key = [" + out[i].getKey() + "], value = ["
558 + out[i].getValue() + "]");
yoshi28bac132014-01-22 11:00:17 -0800559 }
Yoshi Muroi2c170602014-02-15 08:31:28 -0800560
yoshi28bac132014-01-22 11:00:17 -0800561 for (int i = 0; i < 1000; i++) {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800562 String key1 = "key1" + String.valueOf(i);
563 String key2 = "key2" + String.valueOf(i);
564
565 mw.setObject(0, tableId4, key1.getBytes(StandardCharsets.UTF_8), "v0-value".getBytes(StandardCharsets.UTF_8), null);
566 mw.setObject(1, tableId5, key2.getBytes(StandardCharsets.UTF_8), "v1".getBytes(StandardCharsets.UTF_8), null);
567
Yoshi Muroie7693b12014-02-19 19:41:17 -0800568 MultiWriteRspObject[] rsp = ramcloud.multiWrite(mw.tableId, mw.key, mw.keyLength, mw.value, mw.valueLength, 2, mw.rules);
yoshi28bac132014-01-22 11:00:17 -0800569 if (rsp != null) {
570 for (int j = 0; j < rsp.length; j++) {
571 System.out.println("multi write rsp(" + j + ") status:version " + rsp[j].getStatus() + ":" + rsp[j].getVersion());
572 }
573 }
574 }
575 for (int i = 0; i < 1000; i++) {
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800576 String key1 = "key1" + String.valueOf(i);
577 String key2 = "key2" + String.valueOf(i);
Yoshi Muroi2c170602014-02-15 08:31:28 -0800578
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800579 mr.setObject(0, tableId4, key1.getBytes(StandardCharsets.UTF_8));
580 mr.setObject(1, tableId5, key2.getBytes(StandardCharsets.UTF_8));
581
Yoshi Muroie7693b12014-02-19 19:41:17 -0800582 out = ramcloud.multiRead(mr.tableId, mr.key, mr.keyLength, 2);
yoshi28bac132014-01-22 11:00:17 -0800583 for (int j = 0; j < 2; j++) {
584 System.out.println("multi read object: key = [" + out[j].getKey() + "], value = [" + out[j].getValue() + "]");
585 }
586 }
Yoshi Muroi2c170602014-02-15 08:31:28 -0800587
Yuta HIGUCHI1d6a22a2014-02-21 19:17:42 -0800588 tableEnumeratorTest(ramcloud);
589
yoshi28bac132014-01-22 11:00:17 -0800590 ramcloud.dropTable("table4");
591 ramcloud.dropTable("table5");
592 ramcloud.dropTable("table6");
593 ramcloud.disconnect();
594 }
595}