@Override @Nullable public PutBatch getNextBatch(Result<? extends Record> existingRecords) { Map<Cell, byte[]> existing = Maps.newHashMapWithExpectedSize(existingRecords.size()); for (Record record : existingRecords) { existing.put( Cell.create(record.getValue(JdbcConstants.A_ROW_NAME), record.getValue(JdbcConstants.A_COL_NAME)), record.getValue(JdbcConstants.A_VALUE)); } Map<Cell, byte[]> nextBatch = Maps.newHashMap(); for (Entry<Cell, byte[]> entry : data.entrySet()) { Cell cell = entry.getKey(); byte[] newValue = entry.getValue(); byte[] oldValue = existing.get(cell); if (oldValue == null) { nextBatch.put(cell, newValue); } else if (!Arrays.equals(oldValue, newValue)) { return null; } } return new SingleTimestampPutBatch(nextBatch, timestamp); }
private Set<TableReference> getAllTableNames(DSLContext ctx) { Result<? extends Record> records = ctx .select(TABLE_NAME) .from(METADATA_TABLE) .fetch(); Set<TableReference> tableRefs = Sets.newHashSetWithExpectedSize(records.size()); for (Record record : records) { tableRefs.add(TableReference.createUnsafe(record.getValue(TABLE_NAME))); } return tableRefs; }
@Override @Nullable public PutBatch getNextBatch(Result<? extends Record> existingRecords) { Map<CellTimestamp, byte[]> existing = Maps.newHashMapWithExpectedSize(existingRecords.size()); for (Record record : existingRecords) { existing.put( new CellTimestamp(record.getValue(JdbcConstants.A_ROW_NAME), record.getValue(JdbcConstants.A_COL_NAME), record.getValue(JdbcConstants.A_TIMESTAMP)), record.getValue(JdbcConstants.A_VALUE)); } Multimap<Cell, Value> nextBatch = ArrayListMultimap.create(); for (Entry<Cell, Value> entry : data.entries()) { Cell cell = entry.getKey(); Value newValue = entry.getValue(); byte[] oldValue = existing.get(new CellTimestamp(cell.getRowName(), cell.getColumnName(), newValue.getTimestamp())); if (oldValue == null) { nextBatch.put(cell, newValue); } else if (!Arrays.equals(oldValue, newValue.getContents())) { return null; } } return new MultiTimestampPutBatch(nextBatch); }
@Override public Map<Cell, Long> getLatestTimestamps(final TableReference tableRef, final Map<Cell, Long> timestampByCell) { if (timestampByCell.isEmpty()) { return ImmutableMap.of(); } Map<Cell, Long> toReturn = new HashMap<>(); for (List<Entry<Cell, Long>> partition : Iterables.partition(timestampByCell.entrySet(), batchSizeForReads)) { toReturn.putAll(run(ctx -> { Select<? extends Record> query = getLatestTimestampQueryManyTimestamps( ctx, tableRef, toRows(partition)); Result<? extends Record> records = query.fetch(); Map<Cell, Long> results = Maps.newHashMapWithExpectedSize(records.size()); for (Record record : records) { results.put( Cell.create(record.getValue(A_ROW_NAME), record.getValue(A_COL_NAME)), record.getValue(MAX_TIMESTAMP, Long.class)); } return results; })); } return toReturn; }
@Override public Map<TableReference, byte[]> getMetadataForTables() { return run(ctx -> { Result<? extends Record> records = ctx .select(TABLE_NAME, METADATA) .from(METADATA_TABLE) .fetch(); Map<TableReference, byte[]> metadata = Maps.newHashMapWithExpectedSize(records.size()); for (Record record : records) { metadata.put(TableReference.createUnsafe(record.getValue(TABLE_NAME)), record.getValue(METADATA)); } return metadata; }); }
private Map<Cell, Value> getRowsAllColumns(final TableReference tableRef, final Iterable<byte[]> rows, final long timestamp) { if (Iterables.isEmpty(rows)) { return ImmutableMap.of(); } return run(ctx -> { Select<? extends Record> query = getLatestTimestampQueryAllColumns( ctx, tableRef, ImmutableList.copyOf(rows), timestamp); Result<? extends Record> records = fetchValues(ctx, tableRef, query); Map<Cell, Value> results = Maps.newHashMapWithExpectedSize(records.size()); for (Record record : records) { results.put( Cell.create(record.getValue(A_ROW_NAME), record.getValue(A_COL_NAME)), Value.create(record.getValue(A_VALUE), record.getValue(A_TIMESTAMP))); } return results; }); }
@Override public Map<Cell, Value> get(final TableReference tableRef, final Map<Cell, Long> timestampByCell) { if (timestampByCell.isEmpty()) { return ImmutableMap.of(); } Map<Cell, Value> toReturn = new HashMap<>(); for (List<Entry<Cell, Long>> partition : Iterables.partition(timestampByCell.entrySet(), batchSizeForReads)) { toReturn.putAll(run(ctx -> { Select<? extends Record> query = getLatestTimestampQueryManyTimestamps( ctx, tableRef, toRows(partition)); Result<? extends Record> records = fetchValues(ctx, tableRef, query); Map<Cell, Value> results = Maps.newHashMapWithExpectedSize(records.size()); for (Record record : records) { results.put( Cell.create(record.getValue(A_ROW_NAME), record.getValue(A_COL_NAME)), Value.create(record.getValue(A_VALUE), record.getValue(A_TIMESTAMP))); } return results; })); } return toReturn; }
private Map<Cell, Value> getRowsSomeColumns(final TableReference tableRef, final Iterable<byte[]> rows, final ColumnSelection columnSelection, final long timestamp) { if (Iterables.isEmpty(rows)) { return ImmutableMap.of(); } return run(ctx -> { Select<? extends Record> query = getLatestTimestampQuerySomeColumns( ctx, tableRef, ImmutableList.copyOf(rows), columnSelection.getSelectedColumns(), timestamp); Result<? extends Record> records = fetchValues(ctx, tableRef, query); Map<Cell, Value> results = Maps.newHashMapWithExpectedSize(records.size()); for (Record record : records) { results.put( Cell.create(record.getValue(A_ROW_NAME), record.getValue(A_COL_NAME)), Value.create(record.getValue(A_VALUE), record.getValue(A_TIMESTAMP))); } return results; }); }
@Override public Multimap<Cell, Long> getAllTimestamps(final TableReference tableRef, final Set<Cell> cells, final long timestamp) throws InsufficientConsistencyException { if (cells.isEmpty()) { return ImmutableMultimap.of(); } Multimap<Cell, Long> toReturn = ArrayListMultimap.create(); for (List<Cell> partition : Iterables.partition(cells, batchSizeForReads)) { toReturn.putAll(run(ctx -> { Result<? extends Record> records = ctx .select(A_ROW_NAME, A_COL_NAME, A_TIMESTAMP) .from(atlasTable(tableRef).as(ATLAS_TABLE)) .join(values(ctx, toRows(Sets.newHashSet(partition)), TEMP_TABLE_1, ROW_NAME, COL_NAME)) .on(A_ROW_NAME.eq(T1_ROW_NAME) .and(A_COL_NAME.eq(T1_COL_NAME))) .where(A_TIMESTAMP.lessThan(timestamp)) .fetch(); Multimap<Cell, Long> results = ArrayListMultimap.create(records.size() / 4, 4); for (Record record : records) { results.put( Cell.create(record.getValue(A_ROW_NAME), record.getValue(A_COL_NAME)), record.getValue(A_TIMESTAMP)); } return results; })); } return toReturn; }
/** * Turn a {@link Result} into a list of {@link Row} */ static final List<Row> rows(Result<?> result) { List<Row> rows = new ArrayList<Row>(result.size()); for (Record record : result) rows.add(record.valuesRow()); return rows; }
private void checkInRange() throws SQLException { checkNotClosed(); if (index <= 0 || index > result.size()) throw new SQLException("ResultSet index is at an illegal position : " + index); }
private Record outParameters() throws SQLException { if (result == null || result.length == 0 || result[0].data == null || result[0].data.size() == 0) throw new SQLException("No OUT Parameters available"); return result[0].data.get(0); }
/** * Create a new {@link MockDataProvider} that always returns the same result * for all queries. */ public static final MockDataProvider of(Result<?> result) { return of(new MockResult(result.size(), result)); }
@Override public final R fetchNext() { Result<R> result = fetch(1); if (result.size() == 1) { return result.get(0); } return null; }
@SuppressWarnings("unchecked") @Override public final R[] fetchArray() { Result<R> r = fetch(); return r.toArray((R[]) Array.newInstance(getRecordType(), r.size())); }
@Override public void fetchEnd(ExecuteContext ctx) { super.fetchEnd(ctx); Pair<Long, Integer> stopWatchIntegerPair = timing.get(ctx.query().toString()); long duration = System.nanoTime() - stopWatchIntegerPair.getValue0(); timing.put(ctx.query().toString(), new Pair<>(duration, ctx.result().size())); }
@Override public boolean isUnique(UserPreference userPreference, long accountId) { return create().selectFrom(USER_PREFERENCE) .where(USER_PREFERENCE.NAME.eq(userPreference.getName()) .and(USER_PREFERENCE.ACCOUNT_ID.eq(accountId)).and(USER_PREFERENCE.REMOVED.isNull())).fetch().size() == 0; } }
@Override public boolean isSchedulerServiceEnabled(Long accountId) { return create() .select(SERVICE.ID) .from(SERVICE) .where(SERVICE.ACCOUNT_ID.equal(accountId) .and(SERVICE.SYSTEM.isTrue()) .and(SERVICE.REMOVED.isNull()) .and(SERVICE.STATE.notIn(INACTIVE_STATES)) .and(SERVICE.DATA.like("%io.rancher.container.agent_service.scheduling%"))).fetch().size() > 0; } }
@Override public void resultEnd(ExecuteContext ctx) { if (ctx.result() != null) { if (log.isTraceEnabled()) logMultiline("Fetched result", ctx.result().format(TXTFormat.DEFAULT.maxRows(500).maxColWidth(500)), Level.FINE); else if (log.isDebugEnabled()) logMultiline("Fetched result", ctx.result().format(TXTFormat.DEFAULT.maxRows(5).maxColWidth(50)), Level.FINE); if (log.isDebugEnabled()) log.debug("Fetched row(s)", ctx.result().size()); } }