Search in sources :

Example 1 with StringValue

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

the class BroadcastTest method benchmark.

private static void benchmark() throws IOException {
    FileUtils.deleteDirectory(new File("target/broadcastTest"));
    new File("target/broadcastTest").mkdirs();
    String type = "tcp:key 1;ports 9700 9800";
    ArrayList<PersistentCache> nodeList = new ArrayList<PersistentCache>();
    for (int nodes = 1; nodes < 20; nodes++) {
        PersistentCache pc = new PersistentCache("target/broadcastTest/p" + nodes + ",broadcast=" + type);
        Cache<PathRev, StringValue> cache = openCache(pc);
        String key = "/test" + Math.random();
        PathRev k = new PathRev(key, new RevisionVector(new Revision(0, 0, 0)));
        long time = System.currentTimeMillis();
        for (int i = 0; i < 2000; i++) {
            cache.put(k, new StringValue("Hello World " + i));
            cache.invalidate(k);
            cache.getIfPresent(k);
        }
        time = System.currentTimeMillis() - time;
        System.out.println("nodes: " + nodes + " time: " + time);
        nodeList.add(pc);
    }
    for (PersistentCache c : nodeList) {
        c.close();
    }
}
Also used : Revision(org.apache.jackrabbit.oak.plugins.document.Revision) PathRev(org.apache.jackrabbit.oak.plugins.document.PathRev) RevisionVector(org.apache.jackrabbit.oak.plugins.document.RevisionVector) ArrayList(java.util.ArrayList) StringValue(org.apache.jackrabbit.oak.plugins.document.util.StringValue) File(java.io.File)

Example 2 with StringValue

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

the class BroadcastTest method broadcastTry.

private static boolean broadcastTry(String type, int minPercentCorrect, boolean tryOnly) throws Exception {
    FileUtils.deleteDirectory(new File("target/broadcastTest"));
    new File("target/broadcastTest").mkdirs();
    PersistentCache p1 = new PersistentCache("target/broadcastTest/p1,broadcast=" + type);
    PersistentCache p2 = new PersistentCache("target/broadcastTest/p2,broadcast=" + type);
    Cache<PathRev, StringValue> c1 = openCache(p1);
    Cache<PathRev, StringValue> c2 = openCache(p2);
    String key = "/test" + Math.random();
    PathRev k = new PathRev(key, new RevisionVector(new Revision(0, 0, 0)));
    int correct = 0;
    for (int i = 0; i < 50; i++) {
        c1.put(k, new StringValue("Hello World " + i));
        waitFor(c2, k, 10000);
        StringValue v2 = c2.getIfPresent(k);
        if (v2 != null && v2.toString().equals("Hello World " + i)) {
            correct++;
        }
        c2.invalidate(k);
        assertNull(c2.getIfPresent(k));
        waitFor(c1, k, null, 10000);
        StringValue v1 = c1.getIfPresent(k);
        if (v1 == null) {
            correct++;
        }
    }
    p1.close();
    p2.close();
    if (correct >= minPercentCorrect) {
        return true;
    }
    if (tryOnly) {
        return false;
    }
    Assert.fail("min: " + minPercentCorrect + " got: " + correct);
    return false;
}
Also used : Revision(org.apache.jackrabbit.oak.plugins.document.Revision) PathRev(org.apache.jackrabbit.oak.plugins.document.PathRev) RevisionVector(org.apache.jackrabbit.oak.plugins.document.RevisionVector) StringValue(org.apache.jackrabbit.oak.plugins.document.util.StringValue) File(java.io.File)

Example 3 with StringValue

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

the class MemoryDiffCache method isChildUnchanged.

private boolean isChildUnchanged(@Nonnull final RevisionVector from, @Nonnull final RevisionVector to, @Nonnull final String parent, @Nonnull final String name) {
    PathRev parentKey = diffCacheKey(parent, from, to);
    StringValue parentCachedEntry = diffCache.getIfPresent(parentKey);
    boolean unchanged;
    if (parentCachedEntry == null) {
        if (denotesRoot(parent)) {
            // reached root and we don't know whether name
            // changed between from and to
            unchanged = false;
        } else {
            // recurse down
            unchanged = isChildUnchanged(from, to, getParentPath(parent), getName(parent));
        }
    } else {
        unchanged = parseJsopDiff(parentCachedEntry.asString(), new Diff() {

            @Override
            public boolean childNodeAdded(String n) {
                return !name.equals(n);
            }

            @Override
            public boolean childNodeChanged(String n) {
                return !name.equals(n);
            }

            @Override
            public boolean childNodeDeleted(String n) {
                return !name.equals(n);
            }
        });
    }
    return unchanged;
}
Also used : StringValue(org.apache.jackrabbit.oak.plugins.document.util.StringValue)

Example 4 with StringValue

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

the class MemoryDiffCache method getChanges.

@CheckForNull
@Override
public String getChanges(@Nonnull final RevisionVector from, @Nonnull final RevisionVector to, @Nonnull final String path, @Nullable final Loader loader) {
    PathRev key = diffCacheKey(path, from, to);
    StringValue diff;
    if (loader == null) {
        diff = diffCache.getIfPresent(key);
        if (diff == null && isUnchanged(from, to, path)) {
            diff = new StringValue("");
        }
    } else {
        try {
            diff = diffCache.get(key, new Callable<StringValue>() {

                @Override
                public StringValue call() throws Exception {
                    if (isUnchanged(from, to, path)) {
                        return new StringValue("");
                    } else {
                        return new StringValue(loader.call());
                    }
                }
            });
        } catch (ExecutionException e) {
            // try again with loader directly
            diff = new StringValue(loader.call());
        }
    }
    return diff != null ? diff.toString() : null;
}
Also used : StringValue(org.apache.jackrabbit.oak.plugins.document.util.StringValue) ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable) CheckForNull(javax.annotation.CheckForNull)

Example 5 with StringValue

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

the class NodeDocumentCache method get.

/**
     * Return the document matching given key, optionally loading it from an
     * external source.
     * <p>
     * This method can modify the cache, so it's synchronized. The {@link #getIfPresent(String)}
     * is not synchronized and will be faster if you need to get the cached value
     * outside the critical section.
     *
     * @see Cache#get(Object, Callable)
     * @param key document key
     * @param valueLoader object used to retrieve the document
     * @return document matching given key
     */
@Nonnull
public NodeDocument get(@Nonnull final String key, @Nonnull final Callable<NodeDocument> valueLoader) throws ExecutionException {
    Callable<NodeDocument> wrappedLoader = new Callable<NodeDocument>() {

        @Override
        public NodeDocument call() throws Exception {
            for (CacheChangesTracker tracker : changeTrackers) {
                tracker.putDocument(key);
            }
            return valueLoader.call();
        }
    };
    Lock lock = locks.acquire(key);
    try {
        if (isLeafPreviousDocId(key)) {
            return prevDocumentsCache.get(new StringValue(key), wrappedLoader);
        } else {
            return nodeDocumentsCache.get(new StringValue(key), wrappedLoader);
        }
    } finally {
        lock.unlock();
    }
}
Also used : NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) StringValue(org.apache.jackrabbit.oak.plugins.document.util.StringValue) Callable(java.util.concurrent.Callable) Lock(java.util.concurrent.locks.Lock) Nonnull(javax.annotation.Nonnull)

Aggregations

StringValue (org.apache.jackrabbit.oak.plugins.document.util.StringValue)8 File (java.io.File)4 PathRev (org.apache.jackrabbit.oak.plugins.document.PathRev)4 Revision (org.apache.jackrabbit.oak.plugins.document.Revision)3 RevisionVector (org.apache.jackrabbit.oak.plugins.document.RevisionVector)3 Callable (java.util.concurrent.Callable)2 Nonnull (javax.annotation.Nonnull)2 CacheLIRS (org.apache.jackrabbit.oak.cache.CacheLIRS)2 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)2 RemovalCause (com.google.common.cache.RemovalCause)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Lock (java.util.concurrent.locks.Lock)1 CheckForNull (javax.annotation.CheckForNull)1 Nullable (javax.annotation.Nullable)1 CacheStats (org.apache.jackrabbit.oak.cache.CacheStats)1 CacheValue (org.apache.jackrabbit.oak.cache.CacheValue)1 NodeDocumentLocks (org.apache.jackrabbit.oak.plugins.document.locks.NodeDocumentLocks)1