use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.
the class RDBJDBCTools method createInStatement.
public static PreparedStatementComponent createInStatement(final String fieldName, final Collection<String> values, final boolean binary) {
if (values.size() > MAX_IN_CLAUSE) {
throw new IllegalArgumentException("Maximum size of IN clause allowed is " + MAX_IN_CLAUSE + ", but " + values.size() + " was requested");
}
return new PreparedStatementComponent() {
@Override
public String getStatementComponent() {
StringBuilder sb = new StringBuilder(values.size() * 3);
// maximum "in" statement in Oracle takes 1000 values
appendInCondition(sb, fieldName, values.size(), 1000);
return sb.toString();
}
@Override
public int setParameters(PreparedStatement stmt, int startIndex) throws SQLException {
for (String value : values) {
if (binary) {
try {
stmt.setBytes(startIndex++, value.getBytes("UTF-8"));
} catch (UnsupportedEncodingException ex) {
LOG.error("UTF-8 not supported??", ex);
throw new DocumentStoreException(ex);
}
} else {
stmt.setString(startIndex++, value);
}
}
return startIndex;
}
};
}
use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.
the class RDBDocumentStoreJDBC method determineServerTimeDifferenceMillis.
public long determineServerTimeDifferenceMillis(Connection connection) {
String sql = this.dbInfo.getCurrentTimeStampInSecondsSyntax();
if (sql.isEmpty()) {
LOG.debug("{}: unsupported database, skipping DB server time check", this.dbInfo.toString());
return 0;
} else {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = connection.prepareStatement(sql);
long start = System.currentTimeMillis();
rs = stmt.executeQuery();
if (rs.next()) {
long roundtrip = System.currentTimeMillis() - start;
long serverTimeSec = rs.getInt(1);
long roundedTimeSec = ((start + roundtrip / 2) + 500) / 1000;
long resultSec = roundedTimeSec - serverTimeSec;
String message = String.format("instance timestamp: %d, DB timestamp: %d, difference: %d", roundedTimeSec, serverTimeSec, resultSec);
if (Math.abs(resultSec) >= 2) {
LOG.info(message);
} else {
LOG.debug(message);
}
return resultSec * 1000;
} else {
throw new DocumentStoreException("failed to determine server timestamp");
}
} catch (Exception ex) {
LOG.error("Trying to determine time difference to server", ex);
throw new DocumentStoreException(ex);
} finally {
closeResultSet(rs);
closeStatement(stmt);
}
}
}
use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.
the class RDBDocumentStoreJDBC method delete.
public int delete(Connection connection, RDBTableMetaData tmd, Map<String, Map<Key, Condition>> toDelete) throws SQLException, DocumentStoreException {
String or = "";
StringBuilder whereClause = new StringBuilder();
for (Entry<String, Map<Key, Condition>> entry : toDelete.entrySet()) {
whereClause.append(or);
or = " or ";
whereClause.append("ID=?");
for (Entry<Key, Condition> c : entry.getValue().entrySet()) {
if (!c.getKey().getName().equals(MODIFIED)) {
throw new DocumentStoreException("Unsupported condition: " + c);
}
whereClause.append(" and MODIFIED");
if (c.getValue().type == Condition.Type.EQUALS && c.getValue().value instanceof Long) {
whereClause.append("=?");
} else if (c.getValue().type == Condition.Type.EXISTS) {
whereClause.append(" is not null");
} else {
throw new DocumentStoreException("Unsupported condition: " + c);
}
}
}
PreparedStatement stmt = connection.prepareStatement("delete from " + tmd.getName() + " where " + whereClause);
try {
int i = 1;
for (Entry<String, Map<Key, Condition>> entry : toDelete.entrySet()) {
setIdInStatement(tmd, stmt, i++, entry.getKey());
for (Entry<Key, Condition> c : entry.getValue().entrySet()) {
if (c.getValue().type == Condition.Type.EQUALS) {
stmt.setLong(i++, (Long) c.getValue().value);
}
}
}
return stmt.executeUpdate();
} finally {
stmt.close();
}
}
use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.
the class RDBDocumentStoreJDBC method query.
@Nonnull
public List<RDBRow> query(Connection connection, RDBTableMetaData tmd, String minId, String maxId, List<String> excludeKeyPatterns, List<QueryCondition> conditions, int limit) throws SQLException {
long start = System.currentTimeMillis();
List<RDBRow> result = new ArrayList<RDBRow>();
long dataTotal = 0, bdataTotal = 0;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = prepareQuery(connection, tmd, "ID, MODIFIED, MODCOUNT, CMODCOUNT, HASBINARY, DELETEDONCE, DATA, BDATA", minId, maxId, excludeKeyPatterns, conditions, limit, "ID");
rs = stmt.executeQuery();
while (rs.next() && result.size() < limit) {
String id = getIdFromRS(tmd, rs, 1);
if ((minId != null && id.compareTo(minId) < 0) || (maxId != null && id.compareTo(maxId) > 0)) {
throw new DocumentStoreException("unexpected query result: '" + minId + "' < '" + id + "' < '" + maxId + "' - broken DB collation?");
}
long modified = readLongFromResultSet(rs, 2);
long modcount = readLongFromResultSet(rs, 3);
long cmodcount = readLongFromResultSet(rs, 4);
Long hasBinary = readLongOrNullFromResultSet(rs, 5);
Boolean deletedOnce = readBooleanOrNullFromResultSet(rs, 6);
String data = rs.getString(7);
byte[] bdata = rs.getBytes(8);
result.add(new RDBRow(id, hasBinary, deletedOnce, modified, modcount, cmodcount, data, bdata));
dataTotal += data.length();
bdataTotal += bdata == null ? 0 : bdata.length;
}
} finally {
closeStatement(stmt);
closeResultSet(rs);
}
long elapsed = System.currentTimeMillis() - start;
if ((this.queryHitsLimit != 0 && result.size() > this.queryHitsLimit) || (this.queryTimeLimit != 0 && elapsed > this.queryTimeLimit)) {
String params = String.format("params minid '%s' maxid '%s' excludeKeyPatterns %s conditions %s limit %d.", minId, maxId, excludeKeyPatterns, conditions, limit);
String resultRange = "";
if (result.size() > 0) {
resultRange = String.format(" Result range: '%s'...'%s'.", result.get(0).getId(), result.get(result.size() - 1).getId());
}
String postfix = String.format(" Read %d chars from DATA and %d bytes from BDATA. Check calling method.", dataTotal, bdataTotal);
if (this.queryHitsLimit != 0 && result.size() > this.queryHitsLimit) {
String message = String.format("Potentially excessive query on %s with %d hits (limited to %d, configured QUERYHITSLIMIT %d), elapsed time %dms, %s%s%s", tmd.getName(), result.size(), limit, this.queryHitsLimit, elapsed, params, resultRange, postfix);
LOG.info(message, new Exception("call stack"));
}
if (this.queryTimeLimit != 0 && elapsed > this.queryTimeLimit) {
String message = String.format("Long running query on %s with %d hits (limited to %d), elapsed time %dms (configured QUERYTIMELIMIT %d), %s%s%s", tmd.getName(), result.size(), limit, elapsed, this.queryTimeLimit, params, resultRange, postfix);
LOG.info(message, new Exception("call stack"));
}
}
return result;
}
use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.
the class MongoDocumentStore method find.
@SuppressWarnings("unchecked")
private <T extends Document> T find(final Collection<T> collection, final String key, boolean preferCached, final int maxCacheAge) {
if (collection != Collection.NODES) {
return findUncachedWithRetry(collection, key, DocumentReadPreference.PRIMARY, 2);
}
NodeDocument doc;
if (maxCacheAge > 0 || preferCached) {
// first try without lock
doc = nodesCache.getIfPresent(key);
if (doc != null) {
if (preferCached || getTime() - doc.getCreated() < maxCacheAge) {
stats.doneFindCached(collection, key);
if (doc == NodeDocument.NULL) {
return null;
}
return (T) doc;
}
}
}
Throwable t;
try {
Lock lock = nodeLocks.acquire(key);
try {
if (maxCacheAge > 0 || preferCached) {
// try again some other thread may have populated
// the cache by now
doc = nodesCache.getIfPresent(key);
if (doc != null) {
if (preferCached || getTime() - doc.getCreated() < maxCacheAge) {
stats.doneFindCached(collection, key);
if (doc == NodeDocument.NULL) {
return null;
}
return (T) doc;
}
}
}
final NodeDocument d = (NodeDocument) findUncachedWithRetry(collection, key, getReadPreference(maxCacheAge), 2);
invalidateCache(collection, key);
doc = nodesCache.get(key, new Callable<NodeDocument>() {
@Override
public NodeDocument call() throws Exception {
return d == null ? NodeDocument.NULL : d;
}
});
} finally {
lock.unlock();
}
if (doc == NodeDocument.NULL) {
return null;
} else {
return (T) doc;
}
} catch (UncheckedExecutionException e) {
t = e.getCause();
} catch (ExecutionException e) {
t = e.getCause();
} catch (RuntimeException e) {
t = e;
}
throw new DocumentStoreException("Failed to load document with " + key, t);
}
Aggregations