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();
}
}
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;
}
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;
}
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;
}
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();
}
}
Aggregations