BatchWriterOpts bwOpts = new BatchWriterOpts();
ScannerOpts scanOpts = new ScannerOpts();
opts.parseArgs(TestBinaryRows.class.getName(), args, scanOpts, bwOpts);
try {
Connector connector = opts.getConnector();
final Text CF = new Text("cf"), CQ = new Text("cq");
final byte[] CF_BYTES = "cf".getBytes(Constants.UTF8), CQ_BYTES = "cq".getBytes(Constants.UTF8);
if (opts.mode.equals("ingest") || opts.mode.equals("delete")) {
BatchWriter bw = connector.createBatchWriter(opts.tableName, bwOpts.getBatchWriterConfig());
boolean delete = opts.mode.equals("delete");
for (long i = 0; i < opts.num; i++) {
byte[] row = encodeLong(i + opts.start);
String value = "" + (i + opts.start);
Mutation m = new Mutation(new Text(row));
if (delete) {
m.putDelete(CF, CQ);
} else {
m.put(CF, CQ, new Value(value.getBytes(Constants.UTF8)));
}
bw.addMutation(m);
}
bw.close();
} else if (opts.mode.equals("verifyDeleted")) {
Scanner s = connector.createScanner(opts.tableName, opts.auths);
s.setBatchSize(scanOpts.scanBatchSize);
Key startKey = new Key(encodeLong(opts.start), CF_BYTES, CQ_BYTES, new byte[0], Long.MAX_VALUE);
Key stopKey = new Key(encodeLong(opts.start + opts.num - 1), CF_BYTES, CQ_BYTES, new byte[0], 0);
s.setBatchSize(50000);
s.setRange(new Range(startKey, stopKey));
for (Entry<Key,Value> entry : s) {
System.err.println("ERROR : saw entries in range that should be deleted ( first value : " + entry.getValue().toString() + ")");
System.err.println("exiting...");
System.exit(1);
}
} else if (opts.mode.equals("verify")) {
long t1 = System.currentTimeMillis();
Scanner s = connector.createScanner(opts.tableName, opts.auths);
Key startKey = new Key(encodeLong(opts.start), CF_BYTES, CQ_BYTES, new byte[0], Long.MAX_VALUE);
Key stopKey = new Key(encodeLong(opts.start + opts.num - 1), CF_BYTES, CQ_BYTES, new byte[0], 0);
s.setBatchSize(scanOpts.scanBatchSize);
s.setRange(new Range(startKey, stopKey));
long i = opts.start;
for (Entry<Key,Value> e : s) {
Key k = e.getKey();
Value v = e.getValue();
// System.out.println("v = "+v);
checkKeyValue(i, k, v);
i++;
}
if (i != opts.start + opts.num) {
System.err.println("ERROR : did not see expected number of rows, saw " + (i - opts.start) + " expected " + opts.num);
System.err.println("exiting... ARGHHHHHH");
System.exit(1);
}
long t2 = System.currentTimeMillis();
System.out.printf("time : %9.2f secs%n", ((t2 - t1) / 1000.0));
System.out.printf("rate : %9.2f entries/sec%n", opts.num / ((t2 - t1) / 1000.0));
} else if (opts.mode.equals("randomLookups")) {
int numLookups = 1000;
Random r = new Random();
long t1 = System.currentTimeMillis();
for (int i = 0; i < numLookups; i++) {
long row = ((r.nextLong() & 0x7fffffffffffffffl) % opts.num) + opts.start;
Scanner s = connector.createScanner(opts.tableName, opts.auths);
s.setBatchSize(scanOpts.scanBatchSize);
Key startKey = new Key(encodeLong(row), CF_BYTES, CQ_BYTES, new byte[0], Long.MAX_VALUE);
Key stopKey = new Key(encodeLong(row), CF_BYTES, CQ_BYTES, new byte[0], 0);
s.setRange(new Range(startKey, stopKey));
Iterator<Entry<Key,Value>> si = s.iterator();
if (si.hasNext()) {
Entry<Key,Value> e = si.next();
Key k = e.getKey();
Value v = e.getValue();
checkKeyValue(row, k, v);
if (si.hasNext()) {
System.err.println("ERROR : lookup on " + row + " returned more than one result ");
System.err.println("exiting...");
System.exit(1);
}
} else {
System.err.println("ERROR : lookup on " + row + " failed ");
System.err.println("exiting...");
System.exit(1);
}
}
long t2 = System.currentTimeMillis();
System.out.printf("time : %9.2f secs%n", ((t2 - t1) / 1000.0));
System.out.printf("lookups : %9d keys%n", numLookups);
System.out.printf("rate : %9.2f lookups/sec%n", numLookups / ((t2 - t1) / 1000.0));
} else if (opts.mode.equals("split")) {
TreeSet<Text> splits = new TreeSet<Text>();
int shift = (int) opts.start;
int count = (int) opts.num;
for (long i = 0; i < count; i++) {
long splitPoint = i << shift;
splits.add(new Text(encodeLong(splitPoint)));
System.out.printf("added split point 0x%016x %,12d%n", splitPoint, splitPoint);
}
connector.tableOperations().create(opts.tableName);
connector.tableOperations().addSplits(opts.tableName, splits);
} else {
System.err.println("ERROR : " + opts.mode + " is not a valid operation.");
System.exit(1);
}