Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.opensharing.asset.table.delta.DeltaKernel;
import io.opensharing.asset.table.delta.DeltaTableMetadataReader;
import io.opensharing.asset.table.delta.DeltaTableQueryReader;
import io.opensharing.asset.table.delta.SourceTableException;
import io.opensharing.auth.AuthContext;
import io.opensharing.auth.UserContext;
import io.opensharing.catalog.AssetLocation;
Expand All @@ -33,6 +34,7 @@
import java.util.Locale;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -124,8 +126,12 @@ public long version(
String startingTimestamp) {
ShareEntity share = protocolShares.requireGranted(recipient, shareName);
Instant timestamp = parseTimestamp("startingTimestamp", startingTimestamp);
return kernel.getVersion(
findTable(share, schemaAlias, tableAlias).catalogTable(), timestamp, owner(share));
ResolvedAsset table = findTable(share, schemaAlias, tableAlias).catalogTable();
return readingTable(
share,
schemaAlias,
tableAlias,
() -> kernel.getVersion(table, timestamp, owner(share)));
}

/**
Expand All @@ -142,9 +148,15 @@ public DeltaTableMetadataReader.Result metadata(
String timestamp,
String capabilities) {
ShareEntity share = protocolShares.requireGranted(recipient, shareName);
TableRequestChecks.requireAtMostOne("version,timestamp", version, timestamp);
TableRequestChecks.requireNotNegative("version", version);
Instant asOf = parseTimestamp("timestamp", timestamp);
ResolvedAsset table = findTable(share, schemaAlias, tableAlias).catalogTable();
return metadataReader.read(table, version, asOf, owner(share), capabilities);
return readingTable(
share,
schemaAlias,
tableAlias,
() -> metadataReader.read(table, version, asOf, owner(share), capabilities));
}

/**
Expand Down Expand Up @@ -202,12 +214,17 @@ public void query(
DeltaTableQueryReader.ResponseOptions options,
DeltaTableQueryReader.ResponseSink sink) {
ShareEntity share = protocolShares.requireGranted(recipient, shareName);
QueryTableRequest body = request == null ? QueryTableRequest.EMPTY : request;
requireValid(body);
ResolvedAsset catalogTable =
requireUrlAccess(
findTable(share, schemaAlias, tableAlias).catalogTable(), schemaAlias, tableAlias);
QueryTableRequest body = request == null ? QueryTableRequest.EMPTY : request;
if (body.startingVersion() != null || body.endingVersion() != null) {
queryChanges(body, catalogTable, share, options, sink);
readingTable(
share,
schemaAlias,
tableAlias,
() -> queryChanges(body, catalogTable, share, options, sink));
return;
}
boolean historical = body.version() != null || body.timestamp() != null;
Expand All @@ -223,17 +240,23 @@ public void query(
if (body.predicateHints() != null && !body.predicateHints().isEmpty()) {
log.debug("Ignoring deprecated predicateHints {}", body.predicateHints());
}
queryReader.read(
catalogTable,
owner(share),
options,
sink,
body.version(),
parseTimestamp("timestamp", body.timestamp()),
body.refreshToken(),
includeRefreshToken,
body.jsonPredicateHints(),
limitHint(body));
Instant timestamp = parseTimestamp("timestamp", body.timestamp());
readingTable(
share,
schemaAlias,
tableAlias,
() ->
queryReader.read(
catalogTable,
owner(share),
options,
sink,
body.version(),
timestamp,
body.refreshToken(),
includeRefreshToken,
body.jsonPredicateHints(),
limitHint(body)));
}

/**
Expand All @@ -251,20 +274,80 @@ public void changes(
DeltaTableQueryReader.ResponseOptions options,
DeltaTableQueryReader.ResponseSink sink) {
ShareEntity share = protocolShares.requireGranted(recipient, shareName);
requireValid(request);
ResolvedAsset catalogTable =
requireUrlAccess(
findTable(share, schemaAlias, tableAlias).catalogTable(), schemaAlias, tableAlias);
queryReader.readChangeDataFeed(
catalogTable,
owner(share),
options,
sink,
request.startingVersion(),
parseTimestamp("startingTimestamp", request.startingTimestamp()),
request.endingVersion(),
parseTimestamp("endingTimestamp", request.endingTimestamp()),
Boolean.TRUE.equals(request.includeHistoricalMetadata()),
Boolean.TRUE.equals(request.includeHistoricalProtocol()));
Instant startingTimestamp = parseTimestamp("startingTimestamp", request.startingTimestamp());
Instant endingTimestamp = parseTimestamp("endingTimestamp", request.endingTimestamp());
readingTable(
share,
schemaAlias,
tableAlias,
() ->
queryReader.readChangeDataFeed(
catalogTable,
owner(share),
options,
sink,
request.startingVersion(),
startingTimestamp,
request.endingVersion(),
endingTimestamp,
Boolean.TRUE.equals(request.includeHistoricalMetadata()),
Boolean.TRUE.equals(request.includeHistoricalProtocol())));
}

/** The version parameters of a snapshot or data change query, in the order they are checked.*/
private static void requireValid(QueryTableRequest body) {
TableRequestChecks.requireNotNegative("table version", body.version());
TableRequestChecks.requireNotNegative("startingVersion", body.startingVersion());
TableRequestChecks.requireNotNegative("endingVersion", body.endingVersion());
TableRequestChecks.requireAtMostOne(
"version,timestamp,startingVersion",
body.version(),
body.timestamp(),
body.startingVersion());
TableRequestChecks.requireOrderedVersions(body.startingVersion(), body.endingVersion());
}

/** The range parameters of a Change Data Feed query, in the order they are checked. */
private static void requireValid(TableChangesRequest request) {
TableRequestChecks.requireOneBoundary(
"starting", request.startingVersion(), request.startingTimestamp());
TableRequestChecks.requireOneBoundary(
"ending", request.endingVersion(), request.endingTimestamp());
TableRequestChecks.requireStart(request.startingVersion(), request.startingTimestamp());
TableRequestChecks.requireNotNegative("startingVersion", request.startingVersion());
TableRequestChecks.requireNotNegative("endingVersion", request.endingVersion());
if (request.startingVersion() != null && request.endingVersion() != null) {
TableRequestChecks.requireOrderedRange(request.startingVersion(), request.endingVersion());
}
}

/**
* Reads the table, answering a failure of the source table itself in the table's name as the
* share gives it, since the recipient knows no other.
*/
private <T> T readingTable(
ShareEntity share, String schemaAlias, String tableAlias, Supplier<T> read) {
try {
return read.get();
} catch (SourceTableException failed) {
throw failed.forRecipient(share.getName() + "." + schemaAlias + "." + tableAlias);
}
}

private void readingTable(
ShareEntity share, String schemaAlias, String tableAlias, Runnable read) {
readingTable(
share,
schemaAlias,
tableAlias,
() -> {
read.run();
return null;
});
}

/** Returns {@code catalogTable} once it is known to support the {@code url} access mode. */
Expand Down Expand Up @@ -293,21 +376,10 @@ private void queryChanges(
if (startingVersion == null) {
throw ApiException.invalidParameter("endingVersion requires startingVersion");
}
if (body.version() != null || body.timestamp() != null) {
throw ApiException.invalidParameter(
"Only one of version, timestamp, and startingVersion can be set");
}
if (Boolean.TRUE.equals(body.includeRefreshToken()) || body.refreshToken() != null) {
throw ApiException.invalidParameter(
"includeRefreshToken and refreshToken cannot be used with startingVersion");
}
if (startingVersion < 0) {
throw ApiException.invalidParameter("startingVersion must be non-negative");
}
if (endingVersion != null && endingVersion < startingVersion) {
throw ApiException.invalidParameter(
"endingVersion must be greater than or equal to startingVersion");
}
queryReader.readChanges(
table,
owner(share),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package io.opensharing.asset.table;

import io.opensharing.http.ApiException;
import java.util.Arrays;
import java.util.Objects;

/**
* The checks and messages for a table request's version, timestamp and range parameters, which
* answer INVALID_PARAMETER_VALUE.
*/
public final class TableRequestChecks {

private TableRequestChecks() {}

/** {@code value} as a version, or null if absent. */
public static Long parseVersion(String name, String value) {
if (value == null) {
return null;
}
try {
return Long.parseLong(value.trim());
} catch (NumberFormatException invalid) {
throw ApiException.invalidParameter(name + " is not a valid number.");
}
}

public static void requireNotNegative(String name, Long version) {
if (version != null && version < 0) {
throw ApiException.invalidParameter(name + " cannot be negative.");
}
}

/** At most one of the parameters {@code names} lists can be set, as {@code values} say. */
public static void requireAtMostOne(String names, Object... values) {
if (Arrays.stream(values).filter(Objects::nonNull).count() > 1) {
throw ApiException.invalidParameter("Please only provide one of: " + names);
}
}

/** A query's starting version cannot be after its ending version. */
public static void requireOrderedVersions(Long startingVersion, Long endingVersion) {
if (startingVersion != null && endingVersion != null && endingVersion < startingVersion) {
throw ApiException.invalidParameter(
"startingVersion("
+ startingVersion
+ ") must be smaller than or equal to endingVersion("
+ endingVersion
+ ").");
}
}

/** A Change Data Feed read needs a starting version or timestamp. */
public static void requireStart(Object startingVersion, Object startingTimestamp) {
if (startingVersion == null && startingTimestamp == null) {
throw ApiException.invalidParameter(
"No startingVersion or startingTimestamp provided for CDF read. Set one of them on the"
+ " request's cdfOptions to bound the CDC range.");
}
}

/** A Change Data Feed range cannot end before it starts. */
public static void requireOrderedRange(long start, long end) {
if (end < start) {
throw ApiException.invalidParameter(
"CDF range from startingVersion "
+ start
+ " to endingVersion "
+ end
+ " was invalid. End cannot be before start.");
}
}

/** At most one of a Change Data Feed range's version and timestamp, for its {@code bound}. */
public static void requireOneBoundary(String bound, Object version, Object timestamp) {
if (version != null && timestamp != null) {
throw ApiException.invalidParameter(
"Multiple "
+ bound
+ " arguments provided for CDF read. Please provide one of either "
+ bound
+ "Timestamp or "
+ bound
+ "Version.");
}
}

/** The versions of a Change Data Feed range that the table has: not after {@code latest}. */
public static void requireStartAtMostLatest(long start, long latest) {
if (start > latest) {
throw ApiException.invalidParameter(
"Provided Start version("
+ start
+ ") for reading change data is invalid. Start version cannot be greater than the"
+ " latest version of the table("
+ latest
+ ").");
}
}

public static void requireEndAtMostLatest(long end, long latest) {
if (end > latest) {
throw ApiException.invalidParameter(
"Provided End version("
+ end
+ ") for reading change data is invalid. End version cannot be greater than the"
+ " latest version of the table("
+ latest
+ ").");
}
}
}
Loading
Loading