System.err.println("Unknown comand.");
System.exit(1);
}
}
System.out.println("Checkpoints " + path);
FileStore store = new FileStore(new File(path), 256, TAR_STORAGE_MEMORY_MAPPED);
try {
if ("list".equals(op)) {
NodeState ns = store.getHead().getChildNode("checkpoints");
for (ChildNodeEntry cne : ns.getChildNodeEntries()) {
NodeState cneNs = cne.getNodeState();
System.out.printf("- %s created %s expires %s%n",
cne.getName(),
new Timestamp(cneNs.getLong("created")),
new Timestamp(cneNs.getLong("timestamp")));
}
System.out.println("Found "
+ ns.getChildNodeCount(Integer.MAX_VALUE)
+ " checkpoints");
}
if ("rm-all".equals(op)) {
long time = System.currentTimeMillis();
SegmentNodeState head = store.getHead();
NodeBuilder builder = head.builder();
NodeBuilder cps = builder.getChildNode("checkpoints");
long cnt = cps.getChildNodeCount(Integer.MAX_VALUE);
builder.setChildNode("checkpoints");
boolean ok = store.setHead(head,
(SegmentNodeState) builder.getNodeState());
time = System.currentTimeMillis() - time;
if (ok) {
System.out.println("Removed " + cnt + " checkpoints in "
+ time + "ms.");
} else {
System.err.println("Failed to remove all checkpoints.");
}
}
if ("rm-unreferenced".equals(op)) {
long time = System.currentTimeMillis();
SegmentNodeState head = store.getHead();
String ref = null;
PropertyState refPS = head.getChildNode("root")
.getChildNode(":async").getProperty("async");
if (refPS != null) {
ref = refPS.getValue(Type.STRING);
}
if (ref != null) {
System.out
.println("Referenced checkpoint from /:async@async is "
+ ref);
}
NodeBuilder builder = head.builder();
NodeBuilder cps = builder.getChildNode("checkpoints");
long cnt = 0;
for (String c : cps.getChildNodeNames()) {
if (c.equals(ref)) {
continue;
}
cps.getChildNode(c).remove();
cnt++;
}
boolean ok = cnt == 0 || store.setHead(head,
(SegmentNodeState) builder.getNodeState());
time = System.currentTimeMillis() - time;
if (ok) {
System.out.println("Removed " + cnt + " checkpoints in "
+ time + "ms.");
} else {
System.err.println("Failed to remove unreferenced checkpoints.");
}
}
if ("rm".equals(op)) {
if (args.length != 3) {
System.err.println("Missing checkpoint id");
System.exit(1);
}
long time = System.currentTimeMillis();
String cp = args[2];
SegmentNodeState head = store.getHead();
NodeBuilder builder = head.builder();
NodeBuilder cpn = builder.getChildNode("checkpoints")
.getChildNode(cp);
if (cpn.exists()) {
cpn.remove();
boolean ok = store.setHead(head,
(SegmentNodeState) builder.getNodeState());
time = System.currentTimeMillis() - time;
if (ok) {
System.err.println("Removed checkpoint " + cp + " in "
+ time + "ms.");
} else {
System.err.println("Failed to remove checkpoint " + cp);
}
} else {
System.err.println("Checkpoint '" + cp + "' not found.");
System.exit(1);
}
}
} finally {
store.close();
}
}