3

I am trying to Create a Oak's JCR repository to store content with " Apache Oak over MongoDB".

(which i have absolutely no idea) Here's what iv'e been doing.

MongoClient connection = new MongoClient("127.0.0.1", 27017);
                    DB db = connection.getDB("test");
                    MongoMK.Builder m = new MongoMK.Builder();
                    MongoMK kernel = m.setMongoDB(db).open();
                    Repository repo = new Jcr().createRepository();
                    session = repo.login(); // Error javax.jcr.NoSuchWorkspaceException

Was trying to link "Repository " to "MongoMK" - which seems like a nightmare.

I have tried doing

 Repository repo = new Jcr(kernel).createRepository(); //Error

I found something similiar @ [How to create repository instance in JackRabbit Oak using MicroKernel , that didn't help either.

My question being, is there anyway to link-up MongMK - Repository ??

P.S - Tried using "NodeStore".

Community
  • 1
  • 1
chaty
  • 41
  • 8

1 Answers1

4

Yes, this was not well documented. The following should work:

import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.Session;
import org.apache.jackrabbit.oak.Oak;
import org.apache.jackrabbit.oak.plugins.document.DocumentMK;
import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore;
import org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider;
import com.mongodb.DB;
import com.mongodb.MongoClient;

public class Test {
    public static void main(String... args) throws Exception {
        DB db = new MongoClient("127.0.0.1", 27017).getDB("test2");
        DocumentNodeStore ns = new DocumentMK.Builder().
                setMongoDB(db).getNodeStore();
        Repository repo = new Jcr(new Oak(ns))
                .with(new OpenSecurityProvider())
                .createRepository();
        Session session = repo.login();
        Node root = session.getRootNode();
        if (root.hasNode("hello")) {
            Node hello = root.getNode("hello");
            long count = hello.getProperty("count").getLong();
            hello.setProperty("count", count + 1);
            System.out.println("found the hello node, count = " + count);
        } else {
            System.out.println("creating the hello node");
            root.addNode("hello").setProperty("count", 1);
        }
        session.save();
        session.logout();
        ns.dispose();
    }
}

This is now also documented.

Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132