private static String getIdFromRS(RDBTableMetaData tmd, ResultSet rs, int idx) throws SQLException { if (tmd.isIdBinary()) { try { return new String(rs.getBytes(idx), "UTF-8"); } catch (UnsupportedEncodingException ex) { LOG.error("UTF-8 not supported", ex); throw asDocumentStoreException(ex, "UTF-8 not supported"); } } else { return rs.getString(idx); } }
private static String getIdFromRS(RDBTableMetaData tmd, ResultSet rs, int idx) throws SQLException { if (tmd.isIdBinary()) { try { return new String(rs.getBytes(idx), "UTF-8"); } catch (UnsupportedEncodingException ex) { LOG.error("UTF-8 not supported", ex); throw asDocumentStoreException(ex, "UTF-8 not supported"); } } else { return rs.getString(idx); } }
/** * Creates a {@linkplain RDBDocumentStore} instance using the provided * {@link DataSource}, {@link DocumentNodeStoreBuilder}, and {@link RDBOptions}. */ public RDBDocumentStore(DataSource ds, DocumentNodeStoreBuilder<?> builder, RDBOptions options) { try { initialize(ds, builder, options); } catch (Exception ex) { throw asDocumentStoreException(ex, "initializing RDB document store"); } }
/** * Creates a {@linkplain RDBDocumentStore} instance using the provided * {@link DataSource}, {@link DocumentNodeStoreBuilder}, and {@link RDBOptions}. */ public RDBDocumentStore(DataSource ds, DocumentNodeStoreBuilder<?> builder, RDBOptions options) { try { initialize(ds, builder, options); } catch (Exception ex) { throw asDocumentStoreException(ex, "initializing RDB document store"); } }
private <T extends Document> DocumentStoreException handleException(String message, Exception ex, Collection<T> collection, java.util.Collection<String> ids) { if (collection == Collection.NODES) { for (String id : ids) { invalidateCache(collection, id, false); } } return asDocumentStoreException(ex, message); }
private <T extends Document> DocumentStoreException handleException(String message, Exception ex, Collection<T> collection, java.util.Collection<String> ids) { if (collection == Collection.NODES) { for (String id : ids) { invalidateCache(collection, id, false); } } return asDocumentStoreException(ex, message); }
public static byte[] asBytes(@NotNull String data) { byte[] bytes; try { bytes = data.getBytes("UTF-8"); } catch (UnsupportedEncodingException ex) { LOG.error("UTF-8 not supported??", ex); throw asDocumentStoreException(ex, "UTF-8 not supported??"); } if (NOGZIP) { return bytes; } else { ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length / 2); try (GZIPOutputStream gos = asGZIPOutputStream(bos, Deflater.BEST_SPEED)) { gos.write(bytes); gos.close(); byte[] compressedBytes = bos.toByteArray(); if (LOG.isTraceEnabled()) { long ratio = (100L * compressedBytes.length) / bytes.length; LOG.trace("Gzipped {} bytes to {} ({}%)", bytes.length, compressedBytes.length, ratio); } return compressedBytes; } catch (IOException ex) { LOG.error("Error while gzipping contents", ex); throw asDocumentStoreException(ex, "Error while gzipping contents"); } } }
public static byte[] asBytes(@NotNull String data) { byte[] bytes; try { bytes = data.getBytes("UTF-8"); } catch (UnsupportedEncodingException ex) { LOG.error("UTF-8 not supported??", ex); throw asDocumentStoreException(ex, "UTF-8 not supported??"); } if (NOGZIP) { return bytes; } else { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length()); GZIPOutputStream gos = new GZIPOutputStream(bos) { { // TODO: make this configurable this.def.setLevel(Deflater.BEST_SPEED); } }; gos.write(bytes); gos.close(); return bos.toByteArray(); } catch (IOException ex) { LOG.error("Error while gzipping contents", ex); throw asDocumentStoreException(ex, "Error while gzipping contents"); } } }
@Override public int setParameters(PreparedStatement stmt, int startIndex) throws SQLException { for (String value : values) { try { if (binary) { stmt.setBytes(startIndex++, UTF8Encoder.encodeAsByteArray(value)); } else { if (!UTF8Encoder.canEncode(value)) { throw new IOException("can not encode as UTF-8"); } stmt.setString(startIndex++, value); } } catch (IOException ex) { LOG.warn("Invalid ID: " + value, ex); throw asDocumentStoreException(ex, "Invalid ID: " + value); } } return startIndex; } };
@Override public int setParameters(PreparedStatement stmt, int startIndex) throws SQLException { for (String value : values) { try { if (binary) { stmt.setBytes(startIndex++, UTF8Encoder.encodeAsByteArray(value)); } else { if (!UTF8Encoder.canEncode(value)) { throw new IOException("can not encode as UTF-8"); } stmt.setString(startIndex++, value); } } catch (IOException ex) { LOG.warn("Invalid ID: " + value, ex); throw asDocumentStoreException(ex, "Invalid ID: " + value); } } return startIndex; } };
private static void setIdInStatement(RDBTableMetaData tmd, PreparedStatement stmt, int idx, String id) throws SQLException { try { if (tmd.isIdBinary()) { stmt.setBytes(idx, UTF8Encoder.encodeAsByteArray(id)); } else { if (!UTF8Encoder.canEncode(id)) { throw new IOException("can not encode as UTF-8"); } stmt.setString(idx, id); } } catch (IOException ex) { LOG.warn("Invalid ID: " + id, ex); throw asDocumentStoreException(ex, "Invalid ID: " + id); } }
private static void setIdInStatement(RDBTableMetaData tmd, PreparedStatement stmt, int idx, String id) throws SQLException { try { if (tmd.isIdBinary()) { stmt.setBytes(idx, UTF8Encoder.encodeAsByteArray(id)); } else { if (!UTF8Encoder.canEncode(id)) { throw new IOException("can not encode as UTF-8"); } stmt.setString(idx, id); } } catch (IOException ex) { LOG.warn("Invalid ID: " + id, ex); throw asDocumentStoreException(ex, "Invalid ID: " + id); } }
@Override public long determineServerTimeDifferenceMillis() { Connection connection = null; try { connection = this.ch.getROConnection(); long result = this.db.determineServerTimeDifferenceMillis(connection); connection.commit(); return result; } catch (SQLException ex) { LOG.error("Trying to determine time difference to server", ex); throw asDocumentStoreException(ex, "Trying to determine time difference to server"); } finally { this.ch.closeConnection(connection); } }
@Override public long determineServerTimeDifferenceMillis() { Connection connection = null; try { connection = this.ch.getROConnection(); long result = this.db.determineServerTimeDifferenceMillis(connection); connection.commit(); return result; } catch (SQLException ex) { LOG.error("Trying to determine time difference to server", ex); throw asDocumentStoreException(ex, "Trying to determine time difference to server"); } finally { this.ch.closeConnection(connection); } }
private <T extends Document> Map<String, T> readDocumentsUncached(Collection<T> collection, Set<String> keys) { Map<String, T> result = new HashMap<String, T>(); Connection connection = null; RDBTableMetaData tmd = getTable(collection); try { connection = this.ch.getROConnection(); List<RDBRow> rows = db.read(connection, tmd, keys); int size = rows.size(); for (int i = 0; i < size; i++) { RDBRow row = rows.set(i, null); T document = convertFromDBObject(collection, row); result.put(document.getId(), document); } connection.commit(); } catch (Exception ex) { throw asDocumentStoreException(ex, "trying to read: " + keys); } finally { this.ch.closeConnection(connection); } return result; }
private <T extends Document> Map<String, T> readDocumentsUncached(Collection<T> collection, Set<String> keys) { Map<String, T> result = new HashMap<String, T>(); Connection connection = null; RDBTableMetaData tmd = getTable(collection); try { connection = this.ch.getROConnection(); List<RDBRow> rows = db.read(connection, tmd, keys); int size = rows.size(); for (int i = 0; i < size; i++) { RDBRow row = rows.set(i, null); T document = convertFromDBObject(collection, row); result.put(document.getId(), document); } connection.commit(); } catch (Exception ex) { throw asDocumentStoreException(ex, "trying to read: " + keys); } finally { this.ch.closeConnection(connection); } return result; }
private <T extends Document> long internalGetAggregate(final Collection<T> collection, final String aggregrate, String field, String fromKey, String toKey, final List<String> excludeKeyPatterns, final List<QueryCondition> conditions) { final RDBTableMetaData tmd = getTable(collection); for (QueryCondition cond : conditions) { if (!INDEXEDPROPERTIES.contains(cond.getPropertyName())) { String message = "indexed property " + cond.getPropertyName() + " not supported, query was '" + cond + "'; supported properties are " + INDEXEDPROPERTIES; LOG.info(message); throw new DocumentStoreException(message); } } final String from = collection == Collection.NODES && NodeDocument.MIN_ID_VALUE.equals(fromKey) ? null : fromKey; final String to = collection == Collection.NODES && NodeDocument.MAX_ID_VALUE.equals(toKey) ? null : toKey; Connection connection = null; try { connection = ch.getROConnection(); long result = db.getLong(connection, tmd, aggregrate, field, from, to, excludeKeyPatterns, conditions); connection.commit(); return result; } catch (SQLException ex) { LOG.error("SQL exception on query", ex); throw asDocumentStoreException(ex, "SQL exception on query"); } finally { this.ch.closeConnection(connection); } }
private <T extends Document> long internalGetAggregate(final Collection<T> collection, final String aggregrate, String field, String fromKey, String toKey, final List<String> excludeKeyPatterns, final List<QueryCondition> conditions) { final RDBTableMetaData tmd = getTable(collection); for (QueryCondition cond : conditions) { if (!INDEXEDPROPERTIES.contains(cond.getPropertyName())) { String message = "indexed property " + cond.getPropertyName() + " not supported, query was '" + cond + "'; supported properties are " + INDEXEDPROPERTIES; LOG.info(message); throw new DocumentStoreException(message); } } final String from = collection == Collection.NODES && NodeDocument.MIN_ID_VALUE.equals(fromKey) ? null : fromKey; final String to = collection == Collection.NODES && NodeDocument.MAX_ID_VALUE.equals(toKey) ? null : toKey; Connection connection = null; try { connection = ch.getROConnection(); long result = db.getLong(connection, tmd, aggregrate, field, from, to, excludeKeyPatterns, conditions); connection.commit(); return result; } catch (SQLException ex) { LOG.error("SQL exception on query", ex); throw asDocumentStoreException(ex, "SQL exception on query"); } finally { this.ch.closeConnection(connection); } }
private <T extends Document> int deleteWithCondition(Collection<T> collection, List<QueryCondition> conditions) { int numDeleted = 0; RDBTableMetaData tmd = getTable(collection); Stopwatch watch = startWatch(); Connection connection = null; try { connection = this.ch.getRWConnection(); numDeleted = db.deleteWithCondition(connection, tmd, conditions); connection.commit(); } catch (Exception ex) { this.ch.rollbackConnection(connection); throw asDocumentStoreException(ex, "deleting " + collection + ": " + conditions); } finally { this.ch.closeConnection(connection); stats.doneRemove(watch.elapsed(TimeUnit.NANOSECONDS), collection, numDeleted); } return numDeleted; }
private <T extends Document> int deleteWithCondition(Collection<T> collection, List<QueryCondition> conditions) { int numDeleted = 0; RDBTableMetaData tmd = getTable(collection); Stopwatch watch = startWatch(); Connection connection = null; try { connection = this.ch.getRWConnection(); numDeleted = db.deleteWithCondition(connection, tmd, conditions); connection.commit(); } catch (Exception ex) { this.ch.rollbackConnection(connection); throw asDocumentStoreException(ex, "deleting " + collection + ": " + conditions); } finally { this.ch.closeConnection(connection); stats.doneRemove(watch.elapsed(TimeUnit.NANOSECONDS), collection, numDeleted); } return numDeleted; }