} else if (version == 2){
maps.setProtocolVersion(MapsService.Versions.V2);
}
// Perform action.
BaseEntry entry;
switch (action) {
// For now, just get the whole feed.
case query:
pp(maps, client.readFeed(uid, mid, fid), pw);
break;
// Create an entry from scratch or read from input.
case create:
client.trimFormat("/%s"); // sleazy hack to remove trailing id
entry = title == null
? parseEntry(client, is, maps, preview, pw)
: client.buildEntry(title, content, summary);
if (id.length() > 0) {
entry.setExtension(new ResourceId(id));
}
setProperties(props, entry);
if (preview) {
pp(maps, entry, pw);
}
pp(maps, client.createEntry(entry, uid, mid), pw);
break;
// Read an individual entry.
case read:
pp(maps, client.readEntry(uid, mid, fid), pw);
break;
// Update an existing entry.
case update:
entry = title == null
? parseEntry(client, is, maps, preview, pw)
: client.buildEntry(title, content, summary);
setProperties(props, entry);
if (preview) {
pp(maps, entry, pw);
}
pp(maps, client.updateEntry(entry, uid, mid, fid), pw);
break;
// Delete an existing entry.
case delete:
String err = client.deleteEntry(uid, mid, fid);
if (null != err) {
System.err.println("delete error: " + err);
}
break;
case batch:
client.trimFormat("/%s"); // sleazy hack to remove trailing id
client.addFormat("/batch");
BaseFeed bf = parseFeed(client, is, maps, preview, pw);
pp(maps, client.createFeed(bf, uid, mid), pw);
break;
// Clear out a feed.
case clear:
client.trimFormat("/%s"); // sleazy hack to remove trailing id
bf = client.readFeed(uid, mid, fid);
client.addFormat("/batch");
BaseFeed batch = client.newFeed();
count = 0;
for (Object o : bf.getEntries()) {
String entryId = ((BaseEntry) o).getSelfLink().getHref();
if (count == 0 && fudge) {
entryId += "-xx";
}
BaseEntry delete = client.newEntry();
delete.setId(replaceUserId(uid, entryId));
BatchUtils.setBatchOperationType(delete, BatchOperationType.DELETE);
BatchUtils.setBatchId(delete, Integer.toString(++count));
batch.getEntries().add(delete);
}
if (preview) {
pp(maps, batch, pw);
}
BaseFeed response = maps.batch(client.makeUri(uid, mid, fid), batch);
pp(maps, response, pw);
break;
// Test bulk insert latency.
case bulk:
client.trimFormat("/%s"); // sleazy hack to remove trailing id
String baseXml = "";
if (title == null) {
char buf[] = new char[1024 * 64];
int n = new InputStreamReader(is).read(buf);
baseXml = new String(buf, 0, n);
}
client.addFormat("/batch");
for (int i = 0; i < count; i++) {
batch = client.newFeed();
long t0 = System.currentTimeMillis();
for (int j = 0; j < chunk; j++) {
String entryId = "" + i + "-" + j;
String xml = baseXml.replace("COUNT", entryId);
System.err.println(xml);
entry = title == null
? parseEntry(client, new ByteArrayInputStream(xml.getBytes()),
maps, preview, pw)
: client.buildEntry(title, content, summary);
setProperties(props, entry);
entry.setId(entryId);
BatchUtils.setBatchId(entry, entryId);
BatchUtils.setBatchOperationType(entry, BatchOperationType.INSERT);
batch.getEntries().add(entry);
}
if (preview) {
pp(maps, batch, pw);
}
response = maps.batch(
client.makeUri(uid, mid, fid), batch);
pp(maps, response, pw);
System.err.println("chunk insert time: " +
(System.currentTimeMillis() - t0) + "ms");
}
break;
// Post a file as is, no interpretation.
case rawpost:
break;
// Update a bloc of entries.
case revise:
client.trimFormat("/%s"); // sleazy hack to remove trailing id
baseXml = "";
if (title == null) {
char buf[] = new char[1024 * 64];
int n = new InputStreamReader(is).read(buf);
baseXml = new String(buf, 0, n);
}
bf = client.readFeed(uid, mid, fid);
int n = bf.getEntries().size();
client.addFormat("/batch");
int dex = 0;
count = Math.min(count, n / chunk);
for (int i = 0; i < count; i++) {
long t0 = System.currentTimeMillis();
batch = client.newFeed();
for (int j = 0; j < Math.min(n, chunk); j++) {
Object o = bf.getEntries().get(j + i*chunk);
String entryId = ((BaseEntry) o).getSelfLink().
getHref().replaceAll("\\.", sep);
if (dex == 0 && fudge) {
entryId += "-xx";
}
String xml = baseXml.replace("COUNT", entryId.substring(entryId.lastIndexOf('/')));
BaseEntry update = title == null
? parseEntry(client, new ByteArrayInputStream(xml.getBytes()),
maps, preview, pw)
: client.buildEntry(title, content, summary);
update.setId(replaceUserId(uid, entryId));
setProperties(props, update);
BatchUtils.setBatchOperationType(update, BatchOperationType.UPDATE);
BatchUtils.setBatchId(update, Integer.toString(++dex));
batch.getEntries().add(update);
}