Hi,
On Wed, Jan 29, 2014 at 11:59 AM, Jukka Zitting <[email protected]> wrote:
> Alternatively, it should be fairly straightforward to adapt the
> existing Oak backup code to work also with MongoMK target, as most of
> that code uses only on standard NodeState functionality.
See a quick proof of concept below.
BR,
Jukka Zitting
// MongoBackup.java
public class MongoBackup {
private static final Logger log =
LoggerFactory.getLogger(MongoBackup.class);
private static final long DEFAULT_LIFETIME = TimeUnit.HOURS.toMillis(1);
public static void backup(NodeStore store, DB db)
throws CommitFailedException {
long s = System.currentTimeMillis();
// 1. create a new checkpoint with the current state
String checkpoint = store.checkpoint(DEFAULT_LIFETIME);
NodeState current = store.retrieve(checkpoint);
if (current == null) {
// unable to retrieve the checkpoint; use root state instead
current = store.getRoot();
}
// 2. init the backup DocumentNodeStore
DocumentNodeStore backup =
new DocumentMK.Builder().setMongoDB(db).getNodeStore();
try {
NodeState state = backup.getRoot();
NodeBuilder builder = state.builder();
String beforeCheckpoint = state.getString(":checkpoint");
if (beforeCheckpoint != null) {
// 3 try to retrieve the previously backed up checkpoint
NodeState before = store.retrieve(beforeCheckpoint);
if (before != null) {
state = before;
// TODO: if this branch is not taken, the comparison
// below can be slooooow
}
}
current.compareAgainstBaseState(state, new ApplyDiff(builder));
builder.setProperty(":checkpoint", checkpoint);
// 4. commit the backup
backup.merge(builder, EmptyHook.INSTANCE, null);
} finally {
backup.dispose();
}
log.debug("Backup done in {} ms.", System.currentTimeMillis() - s);
}
}