jackrabbit-oak-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jukka Zitting <jukka.zitt...@gmail.com>
Subject Re: Adding a 'restore' method to the NodeStore apis
Date Wed, 05 Feb 2014 15:13:15 GMT
Hi,

On Wed, Jan 29, 2014 at 11:59 AM, Jukka Zitting <jukka.zitting@gmail.com> 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);
    }

}

Mime
View raw message