Search in sources :

Example 6 with GarbageCollector

use of org.apache.jackrabbit.core.gc.GarbageCollector 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)

Example 7 with GarbageCollector

use of org.apache.jackrabbit.core.gc.GarbageCollector in project jackrabbit by apache.

the class GCSubtreeMoveTest method test.

public void test() {
    setupRepository();
    GarbageCollector garbageCollector = setupGarbageCollector();
    // To make sure even listener for NODE_ADDED is registered in GC.
    garbageCollector.setPersistenceManagerScan(false);
    assertEquals(0, getBinaryCount(garbageCollector));
    setupNodes();
    assertEquals(1, getBinaryCount(garbageCollector));
    garbageCollector.getDataStore().clearInUse();
    garbageCollector.setMarkEventListener(new MarkEventListener() {

        public void beforeScanning(Node node) throws RepositoryException {
            String path = node.getPath();
            if (path.startsWith("/node")) {
                log("Traversing: " + node.getPath());
            }
            if ("/node1".equals(node.getPath())) {
                String from = "/node2/node3";
                String to = "/node0/node3";
                log("Moving " + from + " -> " + to);
                sessionMover.move(from, to);
                sessionMover.save();
                sleepForFile();
            }
        }
    });
    try {
        garbageCollector.getDataStore().clearInUse();
        garbageCollector.mark();
        garbageCollector.stopScan();
        sleepForFile();
        int numberOfDeleted = garbageCollector.sweep();
        log("Number of deleted: " + numberOfDeleted);
        // Binary data should still be there.
        assertEquals(1, getBinaryCount(garbageCollector));
    } catch (RepositoryException e) {
        e.printStackTrace();
        failWithException(e);
    } finally {
        garbageCollector.close();
    }
}
Also used : Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) MarkEventListener(org.apache.jackrabbit.api.management.MarkEventListener) GarbageCollector(org.apache.jackrabbit.core.gc.GarbageCollector)

Aggregations

GarbageCollector (org.apache.jackrabbit.core.gc.GarbageCollector)7 DataStoreGarbageCollector (org.apache.jackrabbit.api.management.DataStoreGarbageCollector)4 SessionImpl (org.apache.jackrabbit.core.SessionImpl)4 Node (javax.jcr.Node)3 RepositoryException (javax.jcr.RepositoryException)3 Session (javax.jcr.Session)2 ValueFactory (javax.jcr.ValueFactory)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 Binary (javax.jcr.Binary)1 MarkEventListener (org.apache.jackrabbit.api.management.MarkEventListener)1 IterablePersistenceManager (org.apache.jackrabbit.core.persistence.IterablePersistenceManager)1 PersistenceManager (org.apache.jackrabbit.core.persistence.PersistenceManager)1 InternalVersionManagerImpl (org.apache.jackrabbit.core.version.InternalVersionManagerImpl)1