Search in sources :

Example 16 with NodeDocument

use of org.apache.jackrabbit.oak.plugins.document.NodeDocument in project jackrabbit-oak by apache.

the class RDBDocumentSerializerTest method testBlobAndDiff.

@Test
public void testBlobAndDiff() throws UnsupportedEncodingException {
    RDBRow row = new RDBRow("_foo", 1L, false, 1l, 2l, 3l, "\"blob\", [[\"=\", \"foo\", \"bar\"],[\"M\", \"m1\", 1],[\"M\", \"m2\", 3]]", "{\"m1\":2, \"m2\":2}".getBytes("UTF-8"));
    NodeDocument doc = this.ser.fromRow(Collection.NODES, row);
    assertEquals("bar", doc.get("foo"));
    assertEquals(2L, doc.get("m1"));
    assertEquals(3L, doc.get("m2"));
}
Also used : NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) Test(org.junit.Test)

Example 17 with NodeDocument

use of org.apache.jackrabbit.oak.plugins.document.NodeDocument in project jackrabbit-oak by apache.

the class RDBDocumentSerializerTest method testSimpleBlob2.

@Test
public void testSimpleBlob2() throws UnsupportedEncodingException {
    RDBRow row = new RDBRow("_foo", 0L, false, 1l, 2l, 3l, "\"blob\"", "{\"s\":\"string\", \"b\":true, \"i\":1}".getBytes("UTF-8"));
    NodeDocument doc = this.ser.fromRow(Collection.NODES, row);
    assertEquals("_foo", doc.getId());
    assertEquals(false, doc.hasBinary());
    assertEquals(2L, doc.getModCount().longValue());
    assertEquals("string", doc.get("s"));
    assertEquals(Boolean.TRUE, doc.get("b"));
    assertEquals(1L, doc.get("i"));
}
Also used : NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) Test(org.junit.Test)

Example 18 with NodeDocument

use of org.apache.jackrabbit.oak.plugins.document.NodeDocument in project jackrabbit-oak by apache.

the class RDBDocumentSerializerTest method testSimpleStringNonAscii.

@Test
public void testSimpleStringNonAscii() {
    RDBRow row = new RDBRow("_foo", 1L, false, 1l, 2l, 3l, "{\"x\":\"€𝄞\"}", null);
    NodeDocument doc = this.ser.fromRow(Collection.NODES, row);
    assertEquals("_foo", doc.getId());
    assertEquals("€𝄞", doc.get("x"));
}
Also used : NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) Test(org.junit.Test)

Example 19 with NodeDocument

use of org.apache.jackrabbit.oak.plugins.document.NodeDocument in project jackrabbit-oak by apache.

the class MongoDocumentStoreHelper method repair.

public static void repair(MongoDocumentStore store, String path) {
    DBCollection col = store.getDBCollection(NODES);
    String id = Utils.getIdFromPath(path);
    NodeDocument doc = store.find(NODES, id);
    if (doc == null) {
        System.out.println("No document for path " + path);
        return;
    }
    Set<Revision> changes = Sets.newHashSet();
    for (String key : doc.keySet()) {
        if (Utils.isPropertyName(key) || NodeDocument.isDeletedEntry(key)) {
            changes.addAll(NodeDocumentHelper.getLocalMap(doc, key).keySet());
        }
    }
    SortedMap<Revision, String> commitRoot = Maps.newTreeMap(NodeDocumentHelper.getLocalCommitRoot(doc));
    if (!commitRoot.keySet().retainAll(changes)) {
        System.out.println("Nothing to repair on " + path);
        return;
    }
    Number modCount = doc.getModCount();
    if (modCount == null) {
        System.err.println("Document does not have a modCount " + path);
        return;
    }
    DBObject query = QueryBuilder.start(Document.ID).is(id).and(Document.MOD_COUNT).is(modCount).get();
    DBObject cr = new BasicDBObject();
    for (Map.Entry<Revision, String> entry : commitRoot.entrySet()) {
        cr.put(entry.getKey().toString(), entry.getValue());
    }
    DBObject update = new BasicDBObject();
    update.put("$set", new BasicDBObject(NodeDocumentHelper.commitRoot(), cr));
    update.put("$inc", new BasicDBObject(Document.MOD_COUNT, 1L));
    WriteResult result = col.update(query, update);
    if (result.getN() == 1) {
        int num = NodeDocumentHelper.getLocalCommitRoot(doc).size() - commitRoot.size();
        System.out.println("Removed " + num + " _commitRoot entries on " + path);
    } else {
        System.out.println("Unable to repair " + path + " (concurrent update).");
    }
}
Also used : NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) WriteResult(com.mongodb.WriteResult) Revision(org.apache.jackrabbit.oak.plugins.document.Revision) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 20 with NodeDocument

use of org.apache.jackrabbit.oak.plugins.document.NodeDocument in project jackrabbit-oak by apache.

the class RDBDocumentStore method readDocumentCached.

private <T extends Document> T readDocumentCached(final Collection<T> collection, final String id, int maxCacheAge) {
    if (collection != Collection.NODES) {
        return readDocumentUncached(collection, id, null);
    } else {
        NodeDocument doc = null;
        if (maxCacheAge > 0) {
            // first try without lock
            doc = nodesCache.getIfPresent(id);
            if (doc != null) {
                long lastCheckTime = doc.getLastCheckTime();
                if (lastCheckTime != 0) {
                    if (maxCacheAge == Integer.MAX_VALUE || System.currentTimeMillis() - lastCheckTime < maxCacheAge) {
                        stats.doneFindCached(Collection.NODES, id);
                        return castAsT(unwrap(doc));
                    }
                }
            }
        }
        try {
            Lock lock = locks.acquire(id);
            try {
                // caller really wants the cache to be cleared
                if (maxCacheAge == 0) {
                    invalidateNodesCache(id, true);
                    doc = null;
                }
                final NodeDocument cachedDoc = doc;
                doc = nodesCache.get(id, new Callable<NodeDocument>() {

                    @Override
                    public NodeDocument call() throws Exception {
                        NodeDocument doc = (NodeDocument) readDocumentUncached(collection, id, cachedDoc);
                        if (doc != null) {
                            doc.seal();
                        }
                        return wrap(doc);
                    }
                });
                // inspect the doc whether it can be used
                long lastCheckTime = doc.getLastCheckTime();
                if (lastCheckTime != 0 && (maxCacheAge == 0 || maxCacheAge == Integer.MAX_VALUE)) {
                // we either just cleared the cache or the caller does
                // not care;
                } else if (lastCheckTime != 0 && (System.currentTimeMillis() - lastCheckTime < maxCacheAge)) {
                // is new enough
                } else {
                    // need to at least revalidate
                    NodeDocument ndoc = (NodeDocument) readDocumentUncached(collection, id, cachedDoc);
                    if (ndoc != null) {
                        ndoc.seal();
                    }
                    doc = wrap(ndoc);
                    nodesCache.put(doc);
                }
            } finally {
                lock.unlock();
            }
            return castAsT(unwrap(doc));
        } catch (ExecutionException e) {
            throw new IllegalStateException("Failed to load document with " + id, e);
        }
    }
}
Also used : NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable) Lock(java.util.concurrent.locks.Lock)

Aggregations

NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)43 Test (org.junit.Test)14 Lock (java.util.concurrent.locks.Lock)11 UpdateOp (org.apache.jackrabbit.oak.plugins.document.UpdateOp)8 DBObject (com.mongodb.DBObject)6 ArrayList (java.util.ArrayList)6 Nonnull (javax.annotation.Nonnull)6 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)6 BasicDBObject (com.mongodb.BasicDBObject)5 ExecutionException (java.util.concurrent.ExecutionException)5 Stopwatch (com.google.common.base.Stopwatch)4 DBCollection (com.mongodb.DBCollection)4 Revision (org.apache.jackrabbit.oak.plugins.document.Revision)4 DBCursor (com.mongodb.DBCursor)3 QueryBuilder (com.mongodb.QueryBuilder)3 Callable (java.util.concurrent.Callable)3 AbstractDocumentStoreTest (org.apache.jackrabbit.oak.plugins.document.AbstractDocumentStoreTest)3 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)2 MongoException (com.mongodb.MongoException)2 ReadPreference (com.mongodb.ReadPreference)2