Search in sources :

Example 1 with DataStoreGarbageCollector

use of org.apache.jackrabbit.api.management.DataStoreGarbageCollector in project jackrabbit by apache.

the class GarbageCollectorTest method testConcurrentGC.

public void testConcurrentGC() throws Exception {
    Node root = testRootNode;
    Session session = root.getSession();
    final SynchronousChannel sync = new SynchronousChannel();
    final Node node = root.addNode("slowBlob");
    final int blobLength = 1000;
    final ValueFactory vf = session.getValueFactory();
    new Thread() {

        public void run() {
            try {
                node.setProperty("slowBlob", vf.createBinary(new InputStream() {

                    int pos;

                    public int read() throws IOException {
                        pos++;
                        if (pos < blobLength) {
                            return pos % 80 == 0 ? '\n' : '.';
                        } else if (pos == blobLength) {
                            try {
                                sync.put("x");
                                // deleted
                                sync.take();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            return 'x';
                        }
                        return -1;
                    }
                }));
                node.getSession().save();
                sync.put("saved");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();
    assertEquals("x", sync.take());
    DataStoreGarbageCollector gc = ((SessionImpl) session).createDataStoreGarbageCollector();
    gc.setPersistenceManagerScan(false);
    gc.mark();
    gc.sweep();
    sync.put("deleted");
    assertEquals("saved", sync.take());
    InputStream in = node.getProperty("slowBlob").getBinary().getStream();
    for (int pos = 1; pos < blobLength; pos++) {
        int expected = pos % 80 == 0 ? '\n' : '.';
        assertEquals(expected, in.read());
    }
    assertEquals('x', in.read());
    in.close();
    gc.close();
}
Also used : InputStream(java.io.InputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) IOException(java.io.IOException) SessionImpl(org.apache.jackrabbit.core.SessionImpl) SynchronousChannel(EDU.oswego.cs.dl.util.concurrent.SynchronousChannel) IOException(java.io.IOException) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session) DataStoreGarbageCollector(org.apache.jackrabbit.api.management.DataStoreGarbageCollector)

Example 2 with DataStoreGarbageCollector

use of org.apache.jackrabbit.api.management.DataStoreGarbageCollector in project jackrabbit by apache.

the class GarbageCollectorTest method testCloseSessionWhileRunningGc.

public void testCloseSessionWhileRunningGc() throws Exception {
    final Session session = getHelper().getReadWriteSession();
    final DataStoreGarbageCollector gc = ((SessionImpl) session).createDataStoreGarbageCollector();
    gc.setPersistenceManagerScan(false);
    final Exception[] ex = new Exception[1];
    gc.setMarkEventListener(new MarkEventListener() {

        boolean closed;

        public void beforeScanning(Node n) throws RepositoryException {
            closeTest();
        }

        private void closeTest() {
            if (closed) {
                ex[0] = new Exception("Scanning after the session is closed");
            }
            closed = true;
            session.logout();
        }
    });
    try {
        gc.mark();
        fail("Exception 'session has been closed' expected");
    } catch (RepositoryException e) {
        LOG.debug("Expected exception caught: " + e.getMessage());
    }
    if (ex[0] != null) {
        throw ex[0];
    }
    gc.close();
}
Also used : Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) SessionImpl(org.apache.jackrabbit.core.SessionImpl) MarkEventListener(org.apache.jackrabbit.api.management.MarkEventListener) IOException(java.io.IOException) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session) DataStoreGarbageCollector(org.apache.jackrabbit.api.management.DataStoreGarbageCollector)

Example 3 with DataStoreGarbageCollector

use of org.apache.jackrabbit.api.management.DataStoreGarbageCollector in project jackrabbit by apache.

the class DataStoreAPITest method testDataStoreGarbageCollection.

/**
     * Test data store garbage collection.
     */
public void testDataStoreGarbageCollection() throws RepositoryException {
    JackrabbitRepositoryFactory rf = new RepositoryFactoryImpl();
    Properties prop = new Properties();
    prop.setProperty("org.apache.jackrabbit.repository.home", TEST_DIR);
    prop.setProperty("org.apache.jackrabbit.repository.conf", TEST_DIR + "/repository.xml");
    JackrabbitRepository rep = (JackrabbitRepository) rf.getRepository(prop);
    RepositoryManager rm = rf.getRepositoryManager(rep);
    // need to login to start the repository
    Session session = rep.login();
    DataStoreGarbageCollector gc = rm.createDataStoreGarbageCollector();
    try {
        gc.mark();
        gc.sweep();
    } finally {
        gc.close();
    }
    session.logout();
    rm.stop();
}
Also used : RepositoryFactoryImpl(org.apache.jackrabbit.core.RepositoryFactoryImpl) RepositoryManager(org.apache.jackrabbit.api.management.RepositoryManager) Properties(java.util.Properties) JackrabbitRepository(org.apache.jackrabbit.api.JackrabbitRepository) JackrabbitRepositoryFactory(org.apache.jackrabbit.api.JackrabbitRepositoryFactory) Session(javax.jcr.Session) DataStoreGarbageCollector(org.apache.jackrabbit.api.management.DataStoreGarbageCollector)

Example 4 with DataStoreGarbageCollector

use of org.apache.jackrabbit.api.management.DataStoreGarbageCollector in project jackrabbit by apache.

the class GCConcurrentTest method testConcurrentDelete.

public void testConcurrentDelete() throws Exception {
    Node root = testRootNode;
    Session session = root.getSession();
    final String testNodeName = "testConcurrentDelete";
    node(root, testNodeName);
    session.save();
    DataStoreGarbageCollector gc = ((SessionImpl) session).createDataStoreGarbageCollector();
    gc.setPersistenceManagerScan(false);
    gc.setMarkEventListener(new MarkEventListener() {

        public void beforeScanning(Node n) throws RepositoryException {
            if (n.getName().equals(testNodeName)) {
                n.remove();
                n.getSession().save();
            }
        }
    });
    gc.mark();
    gc.close();
}
Also used : Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) SessionImpl(org.apache.jackrabbit.core.SessionImpl) MarkEventListener(org.apache.jackrabbit.api.management.MarkEventListener) Session(javax.jcr.Session) DataStoreGarbageCollector(org.apache.jackrabbit.api.management.DataStoreGarbageCollector)

Example 5 with DataStoreGarbageCollector

use of org.apache.jackrabbit.api.management.DataStoreGarbageCollector in project jackrabbit by apache.

the class GCEventListenerTest method doTestEventListener.

private void doTestEventListener(boolean allowPmScan) throws Exception {
    Node root = testRootNode;
    Session session = root.getSession();
    if (root.hasNode(TEST_NODE_NAME)) {
        root.getNode(TEST_NODE_NAME).remove();
        session.save();
    }
    Node test = root.addNode(TEST_NODE_NAME);
    Random random = new Random();
    byte[] data = new byte[10000];
    for (int i = 0; i < 10; i++) {
        Node n = test.addNode("x" + i);
        random.nextBytes(data);
        ValueFactory vf = session.getValueFactory();
        n.setProperty("data", vf.createBinary(new ByteArrayInputStream(data)));
        session.save();
        if (i % 2 == 0) {
            n.remove();
            session.save();
        }
    }
    session.save();
    SessionImpl si = (SessionImpl) session;
    DataStoreGarbageCollector gc = si.createDataStoreGarbageCollector();
    DataStore ds = ((GarbageCollector) gc).getDataStore();
    if (ds != null) {
        ds.clearInUse();
        boolean pmScan = gc.isPersistenceManagerScan();
        gc.setPersistenceManagerScan(allowPmScan);
        gotNullNode = false;
        gotNode = false;
        gc.setMarkEventListener(this);
        gc.mark();
        if (pmScan && allowPmScan) {
            assertTrue("PM scan without null Node", gotNullNode);
            assertFalse("PM scan, but got a real node", gotNode);
        } else {
            assertFalse("Not a PM scan - but got a null Node", gotNullNode);
            assertTrue("Not a PM scan - without a real node", gotNode);
        }
        int deleted = gc.sweep();
        LOG.debug("Deleted " + deleted);
        assertTrue("Should delete at least one item", deleted >= 0);
        gc.close();
    }
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) SessionImpl(org.apache.jackrabbit.core.SessionImpl) Session(javax.jcr.Session) DataStoreGarbageCollector(org.apache.jackrabbit.api.management.DataStoreGarbageCollector) DataStoreGarbageCollector(org.apache.jackrabbit.api.management.DataStoreGarbageCollector) GarbageCollector(org.apache.jackrabbit.core.gc.GarbageCollector)

Aggregations

Session (javax.jcr.Session)5 DataStoreGarbageCollector (org.apache.jackrabbit.api.management.DataStoreGarbageCollector)5 Node (javax.jcr.Node)4 SessionImpl (org.apache.jackrabbit.core.SessionImpl)4 RepositoryException (javax.jcr.RepositoryException)3 IOException (java.io.IOException)2 ValueFactory (javax.jcr.ValueFactory)2 MarkEventListener (org.apache.jackrabbit.api.management.MarkEventListener)2 SynchronousChannel (EDU.oswego.cs.dl.util.concurrent.SynchronousChannel)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Properties (java.util.Properties)1 Random (java.util.Random)1 JackrabbitRepository (org.apache.jackrabbit.api.JackrabbitRepository)1 JackrabbitRepositoryFactory (org.apache.jackrabbit.api.JackrabbitRepositoryFactory)1 RepositoryManager (org.apache.jackrabbit.api.management.RepositoryManager)1 RepositoryFactoryImpl (org.apache.jackrabbit.core.RepositoryFactoryImpl)1 GarbageCollector (org.apache.jackrabbit.core.gc.GarbageCollector)1