use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.
the class DraftStore method doSort.
/**
* Helper method to apply the sorting onto a list of rows. Sorting needs to take place at the store-level so it can
* leverage the StructuredRow to enable sorting on any field.
*
* @param rows list of {@link StructuredRow} to be sorted
* @param sortRequest {@link SortRequest} describing the sort to be performed
* @return a sorted list of {@link StructuredRow}
*/
private List<StructuredRow> doSort(List<StructuredRow> rows, @Nullable SortRequest sortRequest) {
if (sortRequest == null) {
return rows;
}
String sortField = sortRequest.getFieldName();
FieldType field = TABLE_SPEC.getFieldTypes().stream().filter(f -> f.getName().equals(sortField)).findFirst().orElse(null);
if (field == null) {
throw new IllegalArgumentException(String.format("Invalid value '%s' for sortBy. This field does not exist in the Drafts table.", sortField));
}
FieldType.Type fieldType = field.getType();
Comparator<StructuredRow> comparator;
switch(fieldType) {
case STRING:
comparator = Comparator.<StructuredRow, String>comparing(o -> o.getString(sortField));
break;
case INTEGER:
comparator = Comparator.<StructuredRow, Integer>comparing(o -> o.getInteger(sortField));
break;
case LONG:
comparator = Comparator.<StructuredRow, Long>comparing(o -> o.getLong(sortField));
break;
case FLOAT:
comparator = Comparator.<StructuredRow, Float>comparing(o -> o.getFloat(sortField));
break;
case DOUBLE:
comparator = Comparator.<StructuredRow, Double>comparing(o -> o.getDouble(sortField));
break;
case BYTES:
comparator = Comparator.comparing(o -> o.getBytes(sortField), Bytes.BYTES_COMPARATOR);
break;
default:
throw new UnsupportedOperationException(String.format("Cannot sort field '%s' because type '%s' is not supported.", sortField, fieldType.toString()));
}
if (sortRequest.getOrder() != SortRequest.SortOrder.ASC) {
comparator = comparator.reversed();
}
rows.sort(comparator);
return rows;
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.
the class DraftStore method getDraft.
/**
* Fetch a given draft if it exists
*
* @param id {@link DraftId} that is used to uniquely identify a draft
* @return an {@link Optional<Draft>} representing the requested draft
* @throws TableNotFoundException if the draft store table is not found
* @throws InvalidFieldException if the fields in the {@link DraftId} object do not match the fields in the
* StructuredTable
*/
public Optional<Draft> getDraft(DraftId id) throws TableNotFoundException, InvalidFieldException {
return TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = context.getTable(TABLE_ID);
Optional<StructuredRow> row = table.read(getKey(id));
return row.map(this::fromRow);
}, TableNotFoundException.class, InvalidFieldException.class);
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.
the class OAuthStore method getProvider.
/**
* Get an OAuth provider.
*
* @param name name of {@link OAuthProvider} to read
* @throws OAuthStoreException if the read fails
*/
public Optional<OAuthProvider> getProvider(String name) throws OAuthStoreException {
OAuthClientCredentials clientCreds;
try {
String clientCredsJson = new String(secureStore.get(NamespaceId.SYSTEM.getNamespace(), getClientCredsKey(name)).get(), StandardCharsets.UTF_8);
clientCreds = GSON.fromJson(clientCredsJson, OAuthClientCredentials.class);
} catch (IOException e) {
throw new OAuthStoreException("Failed to read from OAuth provider secure storage", e);
} catch (Exception e) {
return Optional.empty();
}
try {
return TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = context.getTable(TABLE_ID);
Optional<StructuredRow> row = table.read(getKey(name));
return row.map(structuredRow -> fromRow(structuredRow, clientCreds));
}, TableNotFoundException.class, InvalidFieldException.class);
} catch (TableNotFoundException e) {
throw new OAuthStoreException("OAuth provider table not found", e);
} catch (InvalidFieldException e) {
throw new OAuthStoreException("OAuth provider object fields do not match table", e);
}
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.
the class UsageTable method getPrograms.
/**
* Returns programs using dataset.
* @param datasetInstanceId dataset
* @return programs using datasetInstanceId
*/
public Set<ProgramId> getPrograms(DatasetId datasetInstanceId) throws IOException {
Set<ProgramId> programs = new HashSet<>();
Field<?> index = Fields.stringField(StoreDefinition.UsageStore.INDEX_FIELD, GSON.toJson(new DatasetKey(datasetInstanceId)));
try (CloseableIterator<StructuredRow> iterator = table.scan(index)) {
while (iterator.hasNext()) {
StructuredRow row = iterator.next();
programs.add(new ProgramId(row.getString(StoreDefinition.UsageStore.NAMESPACE_FIELD), row.getString(StoreDefinition.UsageStore.APPLICATION_FIELD), row.getString(StoreDefinition.UsageStore.PROGRAM_TYPE_FIELD), row.getString(StoreDefinition.UsageStore.PROGRAM_FIELD)));
}
}
return programs;
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.
the class FieldLineageTable method getSourceFields.
private Set<String> getSourceFields(EndPoint endPoint, long start, long end) throws IOException {
Set<Long> checksums = getChecksumsWithProgramRunsInRange(OUTGOING_DIRECTION_MARKER, endPoint, start, end).keySet();
Set<String> fields = new HashSet<>();
for (long checksum : checksums) {
List<Field<?>> prefix = getSummaryPrefix(checksum, OUTGOING_DIRECTION_MARKER, endPoint);
try (CloseableIterator<StructuredRow> iterator = getSummaryFieldsTable().scan(Range.singleton(prefix), Integer.MAX_VALUE)) {
while (iterator.hasNext()) {
StructuredRow row = iterator.next();
fields.add(row.getString(StoreDefinition.FieldLineageStore.ENDPOINT_FIELD));
}
}
}
return fields;
}
Aggregations