Search in sources :

Example 6 with ValueFactory

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

the class ItemSequenceTest method testSingletonPropertySequence.

public void testSingletonPropertySequence() throws RepositoryException {
    Comparator<String> order = Rank.<String>comparableComparator();
    TreeManager treeManager = new BTreeManager(testNode, 2, 4, order, true);
    PropertySequence properties = ItemSequence.createPropertySequence(treeManager, errorHandler);
    ValueFactory vFactory = testNode.getSession().getValueFactory();
    properties.addProperty("key", vFactory.createValue("key_"));
    assertTrue(properties.hasItem("key"));
    Iterator<Property> propertyIt = properties.iterator();
    assertTrue(propertyIt.hasNext());
    assertEquals("key", propertyIt.next().getName());
    assertFalse(propertyIt.hasNext());
    properties.removeProperty("key");
    assertEmpty(properties);
}
Also used : PropertySequence(org.apache.jackrabbit.commons.flat.PropertySequence) TreeManager(org.apache.jackrabbit.commons.flat.TreeManager) BTreeManager(org.apache.jackrabbit.commons.flat.BTreeManager) ValueFactory(javax.jcr.ValueFactory) Property(javax.jcr.Property) BTreeManager(org.apache.jackrabbit.commons.flat.BTreeManager)

Example 7 with ValueFactory

use of javax.jcr.ValueFactory 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 8 with ValueFactory

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

the class GCConcurrentTest method testGC.

public void testGC() throws Exception {
    Node root = testRootNode;
    Session session = root.getSession();
    GCThread gc = new GCThread(session);
    Thread gcThread = new Thread(gc, "Datastore Garbage Collector");
    int len = 10 * getTestScale();
    boolean started = false;
    for (int i = 0; i < len; i++) {
        if (!started && i > 5 + len / 100) {
            started = true;
            gcThread.start();
        }
        Node n = node(root, "test" + i);
        ValueFactory vf = session.getValueFactory();
        n.setProperty("data", vf.createBinary(randomInputStream(i)));
        session.save();
        LOG.debug("saved: " + i);
    }
    Thread.sleep(10);
    for (int i = 0; i < len; i++) {
        Node n = root.getNode("test" + i);
        Property p = n.getProperty("data");
        InputStream in = p.getBinary().getStream();
        InputStream expected = randomInputStream(i);
        checkStreams(expected, in);
        n.remove();
        LOG.debug("removed: " + i);
        session.save();
    }
    Thread.sleep(10);
    gc.setStop(true);
    Thread.sleep(10);
    gcThread.join();
    gc.throwException();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) Property(javax.jcr.Property) Session(javax.jcr.Session)

Example 9 with ValueFactory

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

the class CopyValueTest method doTestCopy.

private void doTestCopy(int length) throws Exception {
    Node root = superuser.getRootNode();
    if (root.hasNode("testCopy")) {
        root.getNode("testCopy").remove();
        superuser.save();
    }
    Node testRoot = root.addNode("testCopy");
    Node n = testRoot.addNode("a");
    superuser.save();
    byte[] data = new byte[length + 1];
    ValueFactory vf = superuser.getValueFactory();
    n.setProperty("data", vf.createBinary(new ByteArrayInputStream(data)));
    superuser.save();
    data = new byte[length];
    n.setProperty("data", vf.createBinary(new ByteArrayInputStream(data)));
    Property p = testRoot.getNode("a").getProperty("data");
    assertEquals(length, p.getLength());
    superuser.getWorkspace().copy("/testCopy/a", "/testCopy/b");
    assertEquals(length, p.getLength());
    p = testRoot.getNode("b").getProperty("data");
    assertEquals(length + 1, p.getLength());
    testRoot.remove();
    superuser.save();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) Property(javax.jcr.Property)

Example 10 with ValueFactory

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

the class TestTwoGetStreams method testContentIdentity.

/**
     * Test the JackrabbitValue.getContentIdentity feature.
     */
public void testContentIdentity() throws Exception {
    Node root = superuser.getRootNode();
    ValueFactory vf = superuser.getValueFactory();
    long time = System.currentTimeMillis();
    root.setProperty("p1", vf.createBinary(new RandomInputStream(1, STREAM_LENGTH)));
    superuser.save();
    long saveOne = System.currentTimeMillis() - time;
    root.setProperty("p2", vf.createBinary(new RandomInputStream(1, STREAM_LENGTH)));
    superuser.save();
    Value v1 = root.getProperty("p1").getValue();
    Value v2 = root.getProperty("p2").getValue();
    if (v1 instanceof JackrabbitValue && v2 instanceof JackrabbitValue) {
        JackrabbitValue j1 = (JackrabbitValue) v1;
        JackrabbitValue j2 = (JackrabbitValue) v2;
        String id1 = j1.getContentIdentity();
        String id2 = j2.getContentIdentity();
        assertNotNull(id1);
        assertEquals(id1, id2);
    }
    // copying a value should not stream the content
    time = System.currentTimeMillis();
    for (int i = 0; i < 100; i++) {
        Value v = root.getProperty("p1").getValue();
        root.setProperty("p3", v);
    }
    superuser.save();
    time = System.currentTimeMillis() - time;
    // streaming 1 MB again and again takes about 4.3 seconds
    // on my computer; copying the data identifier takes about 16 ms
    // here we test if copying 100 objects took less than saving 50 new objects
    assertTrue("time: " + time, time < saveOne * 50);
}
Also used : JackrabbitValue(org.apache.jackrabbit.api.JackrabbitValue) Node(javax.jcr.Node) Value(javax.jcr.Value) JackrabbitValue(org.apache.jackrabbit.api.JackrabbitValue) ValueFactory(javax.jcr.ValueFactory)

Aggregations

ValueFactory (javax.jcr.ValueFactory)105 Value (javax.jcr.Value)51 Node (javax.jcr.Node)50 Session (javax.jcr.Session)40 Test (org.junit.Test)17 RepositoryException (javax.jcr.RepositoryException)16 InputStream (java.io.InputStream)13 AccessControlManager (javax.jcr.security.AccessControlManager)13 HashMap (java.util.HashMap)12 Privilege (javax.jcr.security.Privilege)12 Property (javax.jcr.Property)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Query (javax.jcr.query.Query)8 Calendar (java.util.Calendar)7 QueryManager (javax.jcr.query.QueryManager)7 RowIterator (javax.jcr.query.RowIterator)7 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)7 NodeIterator (javax.jcr.NodeIterator)6 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)6 IOException (java.io.IOException)5