@SuppressWarnings("unchecked") @Override public T readResult(Record results, int offset) { return (T)results.getValue(offset); }
private static NavigableMap<byte[], SortedMap<byte[], Value>> breakUpValuesByRow(Result<? extends Record> records) { NavigableMap<byte[], SortedMap<byte[], Value>> ret = Maps.newTreeMap(UnsignedBytes.lexicographicalComparator()); for (Record record : records) { byte[] row = record.getValue(A_ROW_NAME); SortedMap<byte[], Value> colMap = ret.computeIfAbsent(row, rowName -> Maps.newTreeMap(UnsignedBytes.lexicographicalComparator())); colMap.put(record.getValue(A_COL_NAME), Value.create(record.getValue(A_VALUE), record.getValue(A_TIMESTAMP))); } return ret; }
private static NavigableMap<byte[], SortedMap<byte[], Set<Long>>> breakUpTimestampsByRow(Result<? extends Record> records) { NavigableMap<byte[], SortedMap<byte[], Set<Long>>> ret = Maps.newTreeMap(UnsignedBytes.lexicographicalComparator()); for (Record record : records) { byte[] row = record.getValue(A_ROW_NAME); byte[] col = record.getValue(A_COL_NAME); SortedMap<byte[], Set<Long>> colMap = ret.computeIfAbsent(row, rowName -> Maps.newTreeMap(UnsignedBytes.lexicographicalComparator())); Set<Long> tsSet = colMap.computeIfAbsent(col, ts -> Sets.newHashSet()); tsSet.add(record.getValue(A_TIMESTAMP)); } return ret; }
@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 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; }); }
private <K> void copyValueIntoRecord(T outputRecord, Record inputRecord, Field<K> field, int idx) { outputRecord.setValue(field, inputRecord.getValue(idx, field.getConverter())); }
@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); }
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 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; }
@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 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 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; }
@SuppressWarnings("unchecked") @Override public P get(R target) throws Exception { return (P) target.getValue(index); }
@SuppressWarnings("unchecked") @Override public P get(R target) throws Exception { return (P) target.getValue(index); }
static Endpoint endpoint(Record a) { String serviceName = a.getValue(ZIPKIN_ANNOTATIONS.ENDPOINT_SERVICE_NAME); if (serviceName == null) { return null; } Short port = a.getValue(ZIPKIN_ANNOTATIONS.ENDPOINT_PORT); return port != null ? Endpoint.create(serviceName, a.getValue(ZIPKIN_ANNOTATIONS.ENDPOINT_IPV4), port.intValue()) : Endpoint.create(serviceName, a.getValue(ZIPKIN_ANNOTATIONS.ENDPOINT_IPV4)); }
@Override @Deprecated public final Object getValue(int index, int fieldIndex, Object defaultValue) { return get(index).getValue(fieldIndex, defaultValue); }
@Override @Deprecated public final Object getValue(int index, String fieldName, Object defaultValue) { return get(index).getValue(fieldName, defaultValue); }
/** * Type-safely copy a value from one record to another */ static final <T> void setValue(Record target, Field<T> targetField, Record source, Field<?> sourceField) { setValue(target, targetField, source.getValue(sourceField)); }
@Override public final Object fetchOne(String fieldName) { R record = fetchOne(); return record == null ? null : record.getValue(fieldName); }