Search in sources :

Example 6 with DocumentStoreException

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;
        }
    };
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PreparedStatement(java.sql.PreparedStatement)

Example 7 with DocumentStoreException

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);
        }
    }
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) RDBJDBCTools.closeResultSet(org.apache.jackrabbit.oak.plugins.document.rdb.RDBJDBCTools.closeResultSet) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) BatchUpdateException(java.sql.BatchUpdateException) SQLException(java.sql.SQLException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 8 with DocumentStoreException

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();
    }
}
Also used : QueryCondition(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition) Condition(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map) Key(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Key)

Example 9 with DocumentStoreException

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;
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) ArrayList(java.util.ArrayList) RDBJDBCTools.closeResultSet(org.apache.jackrabbit.oak.plugins.document.rdb.RDBJDBCTools.closeResultSet) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) BatchUpdateException(java.sql.BatchUpdateException) SQLException(java.sql.SQLException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Nonnull(javax.annotation.Nonnull)

Example 10 with DocumentStoreException

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);
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable) Lock(java.util.concurrent.locks.Lock)

Aggregations

DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)30 IOException (java.io.IOException)11 SQLException (java.sql.SQLException)11 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 Stopwatch (com.google.common.base.Stopwatch)7 ExecutionException (java.util.concurrent.ExecutionException)6 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 ArrayList (java.util.ArrayList)5 Lock (java.util.concurrent.locks.Lock)5 CheckForNull (javax.annotation.CheckForNull)5 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)5 Nonnull (javax.annotation.Nonnull)4 ResultSet (java.sql.ResultSet)3 Map (java.util.Map)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)2 DBCollection (com.mongodb.DBCollection)2 QueryBuilder (com.mongodb.QueryBuilder)2 BatchUpdateException (java.sql.BatchUpdateException)2