Search in sources :

Example 81 with Session

use of javax.jcr.Session in project jackrabbit-oak by apache.

the class ConcurrentReadTest method beforeSuite.

@Override
public void beforeSuite() throws Exception {
    Session session = loginWriter();
    Node root = session.getRootNode().addNode(ROOT_NODE_NAME, "nt:unstructured");
    for (int i = 0; i < NODE_COUNT; i++) {
        Node node = root.addNode("node" + i, "nt:unstructured");
        for (int j = 0; j < NODE_COUNT; j++) {
            node.addNode("node" + j, "nt:unstructured");
        }
        session.save();
    }
    if (foregroundIsReader) {
        foregroundTask = new Reader();
    } else {
        foregroundTask = new Writer();
    }
    for (int i = 0; i < backgroundReaderCount; i++) {
        addBackgroundJob(new Reader());
    }
    for (int i = 0; i < backgroundWriterCount; i++) {
        addBackgroundJob(new Writer());
    }
}
Also used : Node(javax.jcr.Node) Session(javax.jcr.Session)

Example 82 with Session

use of javax.jcr.Session in project jackrabbit by apache.

the class GarbageCollector method scanNodes.

private void scanNodes(SessionImpl session) throws RepositoryException {
    // add a listener to get 'moved' nodes
    Session clonedSession = session.createSession(session.getWorkspace().getName());
    listeners.add(new Listener(this, clonedSession));
    // adding a link to a BLOB updates the modified date
    // reading usually doesn't, but when scanning, it does
    recurse(session.getRootNode(), sleepBetweenNodes);
}
Also used : SynchronousEventListener(org.apache.jackrabbit.core.observation.SynchronousEventListener) MarkEventListener(org.apache.jackrabbit.api.management.MarkEventListener) Session(javax.jcr.Session)

Example 83 with Session

use of javax.jcr.Session in project jackrabbit by apache.

the class ConcurrentQueriesWithUpdatesTest method testQueriesWithUpdates.

public void testQueriesWithUpdates() throws Exception {
    final List<Exception> exceptions = Collections.synchronizedList(new ArrayList<Exception>());
    final AtomicBoolean running = new AtomicBoolean(true);
    // track executed queries and do updates at most at the given rate
    final BlockingQueue<Object> queryExecuted = new LinkedBlockingQueue<Object>();
    Thread queries = new Thread(new Runnable() {

        public void run() {
            try {
                runTask(new Task() {

                    public void execute(Session session, Node test) throws RepositoryException {
                        QueryManager qm = session.getWorkspace().getQueryManager();
                        while (running.get()) {
                            Query q = qm.createQuery(testPath + "//element(*, nt:unstructured) order by @jcr:score descending", Query.XPATH);
                            NodeIterator nodes = q.execute().getNodes();
                            assertEquals("wrong result set size", numNodes, nodes.getSize());
                            queryExecuted.offer(new Object());
                        }
                    }
                }, 5, testRootNode.getPath());
            } catch (RepositoryException e) {
                exceptions.add(e);
            }
        }
    });
    queries.start();
    Thread update = new Thread(new Runnable() {

        public void run() {
            try {
                runTask(new Task() {

                    public void execute(Session session, Node test) throws RepositoryException {
                        Random rand = new Random();
                        QueryManager qm = session.getWorkspace().getQueryManager();
                        for (int i = 0; i < NUM_UPDATES; i++) {
                            try {
                                // wait at most 10 seconds
                                queryExecuted.poll(10, TimeUnit.SECONDS);
                            } catch (InterruptedException e) {
                            // ignore
                            }
                            Query q = qm.createQuery(testPath + "//node" + rand.nextInt(numNodes) + " order by @jcr:score descending", Query.XPATH);
                            NodeIterator nodes = q.execute().getNodes();
                            if (nodes.hasNext()) {
                                Node n = nodes.nextNode();
                                n.setProperty("foo", "bar");
                                session.save();
                            }
                        }
                    }
                }, 1, testRootNode.getPath());
            } catch (RepositoryException e) {
                exceptions.add(e);
            }
        }
    });
    update.start();
    update.join();
    running.set(false);
    queries.join();
}
Also used : NodeIterator(javax.jcr.NodeIterator) Query(javax.jcr.query.Query) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) RepositoryException(javax.jcr.RepositoryException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) QueryManager(javax.jcr.query.QueryManager) Session(javax.jcr.Session)

Example 84 with Session

use of javax.jcr.Session in project jackrabbit by apache.

the class ConcurrentQueryTest method testConcurrentQueryWithDeletes.

/**
     * Deletes 1000 nodes in transactions of 5 nodes while
     * other threads query the workspace. Query results must always return
     * a consistent view of the workspace, that is:<br/>
     * <code>result.numNodes % 5 == 0</code>
     */
public void testConcurrentQueryWithDeletes() throws Exception {
    // create 1000 nodes
    for (int i = 0; i < 20; i++) {
        Node n = testRootNode.addNode("node" + i);
        for (int j = 0; j < 10; j++) {
            Node n1 = n.addNode("node" + j);
            for (int k = 0; k < 5; k++) {
                n1.addNode("node" + k).setProperty("testprop", "foo");
            }
        }
        testRootNode.save();
    }
    final List<Exception> exceptions = Collections.synchronizedList(new ArrayList<Exception>());
    List<QueryWorker> readers = new ArrayList<QueryWorker>();
    String query = "/jcr:root" + testRoot + "//*[@testprop = 'foo']";
    for (Session s : readSessions) {
        readers.add(new QueryWorker(s, query, exceptions, log));
    }
    Thread writer = new Thread() {

        public void run() {
            try {
                for (int i = 0; i < 20; i++) {
                    Node n = testRootNode.getNode("node" + i);
                    for (int j = 0; j < 10; j++) {
                        Node n1 = n.getNode("node" + j);
                        for (int k = 0; k < 5; k++) {
                            n1.getNode("node" + k).remove();
                        }
                        testRootNode.save();
                    }
                }
            } catch (Exception e) {
                exceptions.add(e);
            }
        }
    };
    // start the threads
    writer.start();
    for (Thread t : readers) {
        t.start();
    }
    // wait for writer thread to finish its work
    writer.join();
    // request readers to finish
    for (QueryWorker t : readers) {
        t.finish();
        t.join();
    }
    // fail in case of exceptions
    if (!exceptions.isEmpty()) {
        fail(exceptions.get(0).toString());
    }
}
Also used : Node(javax.jcr.Node) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

Example 85 with Session

use of javax.jcr.Session in project jackrabbit by apache.

the class ConcurrentQueryTest method tearDown.

/**
     * Logs out the sessions acquired in setUp().
     */
protected void tearDown() throws Exception {
    super.tearDown();
    for (Session s : readSessions) {
        s.logout();
    }
    readSessions.clear();
}
Also used : Session(javax.jcr.Session)

Aggregations

Session (javax.jcr.Session)1177 Node (javax.jcr.Node)645 Test (org.junit.Test)359 RepositoryException (javax.jcr.RepositoryException)206 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)158 SimpleCredentials (javax.jcr.SimpleCredentials)86 Property (javax.jcr.Property)78 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)77 Privilege (javax.jcr.security.Privilege)76 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)64 Value (javax.jcr.Value)63 Query (javax.jcr.query.Query)58 NodeIterator (javax.jcr.NodeIterator)55 QueryManager (javax.jcr.query.QueryManager)53 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)50 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)48 AccessControlManager (javax.jcr.security.AccessControlManager)47 HashMap (java.util.HashMap)44 UserManager (org.apache.jackrabbit.api.security.user.UserManager)43 ArrayList (java.util.ArrayList)41