use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.
the class MongoDocumentStore method remove.
@Override
public <T extends Document> int remove(Collection<T> collection, String indexedProperty, long startValue, long endValue) throws DocumentStoreException {
log("remove", collection, indexedProperty, startValue, endValue);
int num = 0;
DBCollection dbCollection = getDBCollection(collection);
Stopwatch watch = startWatch();
try {
QueryBuilder queryBuilder = QueryBuilder.start(indexedProperty);
queryBuilder.greaterThan(startValue);
queryBuilder.lessThan(endValue);
try {
num = dbCollection.remove(queryBuilder.get()).getN();
} catch (Exception e) {
throw DocumentStoreException.convert(e, "Remove failed for " + collection + ": " + indexedProperty + " in (" + startValue + ", " + endValue + ")");
} finally {
if (collection == Collection.NODES) {
// this method is currently being used only for Journal collection while GC.
// But, to keep sanctity of the API, we need to acknowledge that Nodes collection
// could've been used. But, in this signature, there's no useful way to invalidate
// cache.
// So, we use the hammer for this task
invalidateCache();
}
}
} finally {
stats.doneRemove(watch.elapsed(TimeUnit.NANOSECONDS), collection, num);
}
return num;
}
use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.
the class MemoryDocumentStore method internalCreateOrUpdate.
@CheckForNull
private <T extends Document> T internalCreateOrUpdate(Collection<T> collection, UpdateOp update, boolean checkConditions) {
ConcurrentSkipListMap<String, T> map = getMap(collection);
T oldDoc;
Lock lock = rwLock.writeLock();
lock.lock();
try {
// get the node if it's there
oldDoc = map.get(update.getId());
T doc = collection.newDocument(this);
if (oldDoc == null) {
if (!update.isNew()) {
throw new DocumentStoreException("Document does not exist: " + update.getId());
}
} else {
oldDoc.deepCopy(doc);
}
if (checkConditions && !checkConditions(doc, update.getConditions())) {
return null;
}
// update the document
UpdateUtils.applyChanges(doc, update);
doc.seal();
map.put(update.getId(), doc);
return oldDoc;
} finally {
lock.unlock();
}
}
use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.
the class MemoryDocumentStore method query.
@Override
@Nonnull
public <T extends Document> List<T> query(Collection<T> collection, String fromKey, String toKey, String indexedProperty, long startValue, int limit) {
Lock lock = rwLock.readLock();
lock.lock();
try {
ConcurrentSkipListMap<String, T> map = getMap(collection);
ConcurrentNavigableMap<String, T> sub = map.subMap(fromKey + "\0", toKey);
ArrayList<T> list = new ArrayList<T>();
for (T doc : sub.values()) {
if (indexedProperty != null) {
Object value = doc.get(indexedProperty);
if (value instanceof Boolean) {
long test = ((Boolean) value) ? 1 : 0;
if (test < startValue) {
continue;
}
} else if (value instanceof Long) {
if ((Long) value < startValue) {
continue;
}
} else if (value != null) {
throw new DocumentStoreException("unexpected type for property " + indexedProperty + ": " + value.getClass());
}
}
list.add(doc);
if (list.size() >= limit) {
break;
}
}
return list;
} finally {
lock.unlock();
}
}
use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.
the class RDBCacheConsistencyIT method runTest.
private void runTest() throws Throwable {
addNodes(null, "/test", "/test/foo");
final List<Throwable> exceptions = Collections.synchronizedList(new ArrayList<Throwable>());
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
String id = Utils.getIdFromPath("/test/foo");
List<String> ids = Lists.newArrayList();
ids.add(id);
long v = 0;
while (exceptions.isEmpty()) {
try {
UpdateOp op = new UpdateOp(ids.get(0), false);
op.set("p", ++v);
op.set("mb", "update");
store.update(NODES, ids, op);
NodeDocument doc = store.find(NODES, id);
Object p = doc.get("p");
assertEquals("Unexpected result after update-then-find sequence, last modification of document by '" + doc.get("mb") + "' thread @" + doc.getModCount(), v, ((Long) p).longValue());
// System.out.println("u @" + doc.getModCount() + " p=" + v + "; q=" + doc.get("q"));
} catch (Throwable e) {
exceptions.add(e);
}
}
}
}, "update");
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
String id = Utils.getIdFromPath("/test/foo");
long v = 0;
long lastWrittenV = 0;
while (exceptions.isEmpty()) {
try {
UpdateOp op = new UpdateOp(id, false);
op.set("q", ++v);
op.set("mb", "findAndUpdate");
NodeDocument old = store.findAndUpdate(NODES, op);
Object q = old.get("q");
if (q != null) {
assertEquals("Unexpected result after findAndUpdate, last modification of document by '" + old.get("mb") + "' thread @" + old.getModCount(), lastWrittenV, ((Long) q).longValue());
}
lastWrittenV = v;
// System.out.println("f @" + old.getModCount() + " p=" + old.get("p") + "; q=" + q);
} catch (DocumentStoreException e) {
// System.err.println("f update of v to " + v + " failed: " + e.getMessage());
// keep going, RDBDocumentStore might have given up due
// to race conditions
} catch (Throwable e) {
exceptions.add(e);
}
}
}
}, "findAndUpdate");
t2.start();
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
String id = Utils.getIdFromPath("/test/foo");
long p = 0;
long q = 0;
long mc = 0;
while (exceptions.isEmpty()) {
try {
NodeDocument doc = store.find(NODES, id);
if (doc != null) {
Object value = doc.get("p");
if (value != null) {
assertTrue("reader thread at @" + doc.getModCount() + ": observed property value for 'p' (incremented by 'update' thread) decreased, last change by '" + doc.get("mb") + "' thread; previous: " + p + " (at @" + mc + "), now: " + value, (Long) value >= p);
p = (Long) value;
}
value = doc.get("q");
if (value != null) {
assertTrue("reader thread at @" + doc.getModCount() + ": observed property value for 'q' (incremented by 'findAndUpdate' thread) decreased, last change by '" + doc.get("mb") + "' thread; previous: " + q + " (at @" + mc + "), now: " + value, (Long) value >= q);
q = (Long) value;
}
}
mc = doc.getModCount();
} catch (Throwable e) {
exceptions.add(e);
}
}
}
}, "reader");
t3.start();
NodeDocumentCache cache = store.getNodeDocumentCache();
// run for at most five seconds
long end = System.currentTimeMillis() + 1000;
String id = Utils.getIdFromPath("/test/foo");
while (t1.isAlive() && t2.isAlive() && t3.isAlive() && System.currentTimeMillis() < end) {
if (cache.getIfPresent(id) != null) {
Thread.sleep(0, (int) (Math.random() * 100));
// simulate eviction
cache.invalidate(id);
}
}
for (Throwable e : exceptions) {
throw e;
}
exceptions.add(new Exception("end"));
t1.join();
t2.join();
t3.join();
}
use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.
the class UnlockUpgradeCommand method execute.
@Override
public void execute(String... args) throws Exception {
OptionParser parser = new OptionParser();
// RDB specific options
OptionSpec<String> rdbjdbcuser = parser.accepts("rdbjdbcuser", "RDB JDBC user").withOptionalArg().defaultsTo("");
OptionSpec<String> rdbjdbcpasswd = parser.accepts("rdbjdbcpasswd", "RDB JDBC password").withOptionalArg().defaultsTo("");
OptionSpec<String> nonOption = parser.nonOptions("unlockUpgrade {<jdbc-uri> | <mongodb-uri>}");
OptionSpec help = parser.acceptsAll(asList("h", "?", "help"), "show help").forHelp();
OptionSet options = parser.parse(args);
List<String> nonOptions = nonOption.values(options);
if (options.has(help)) {
parser.printHelpOn(System.out);
return;
}
if (nonOptions.isEmpty()) {
parser.printHelpOn(System.err);
return;
}
DocumentStore store = null;
try {
String uri = nonOptions.get(0);
if (uri.startsWith(MONGODB_PREFIX)) {
MongoClientURI clientURI = new MongoClientURI(uri);
if (clientURI.getDatabase() == null) {
System.err.println("Database missing in MongoDB URI: " + clientURI.getURI());
} else {
MongoConnection mongo = new MongoConnection(clientURI.getURI());
store = new MongoDocumentStore(mongo.getDB(), new DocumentMK.Builder());
}
} else if (uri.startsWith("jdbc")) {
DataSource ds = RDBDataSourceFactory.forJdbcUrl(uri, rdbjdbcuser.value(options), rdbjdbcpasswd.value(options));
store = new RDBDocumentStore(ds, new DocumentMK.Builder());
} else {
System.err.println("Unrecognized URI: " + uri);
}
if (store != null && VERSION.writeTo(store)) {
System.out.println("Format version set to " + VERSION);
}
} catch (DocumentStoreException e) {
System.err.println(e.getMessage());
} finally {
if (store != null) {
store.dispose();
}
}
}
Aggregations