Skip to content

Commit

Permalink
Make resolve return Object instead of Table
Browse files Browse the repository at this point in the history
  • Loading branch information
devinrsmith committed Oct 26, 2021
1 parent 33620a8 commit b01288f
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import io.deephaven.grpc_api.console.ConsoleServiceGrpcImpl;
import io.deephaven.grpc_api.log.LogInit;
import io.deephaven.grpc_api.session.SessionService;
import io.deephaven.grpc_api.uri.UriResolver;
import io.deephaven.grpc_api.uri.UriResolvers;
import io.deephaven.internal.log.LoggerFactory;
import io.deephaven.io.logger.Logger;
import io.deephaven.grpc_api.uri.TableResolver;
import io.deephaven.grpc_api.uri.TableResolversInstance;
import io.deephaven.grpc_api.uri.TableResolvers;
import io.deephaven.grpc_api.uri.UriResolversInstance;
import io.deephaven.util.process.ProcessEnvironment;
import io.deephaven.util.process.ShutdownManager;
import io.grpc.Server;
Expand Down Expand Up @@ -60,7 +60,7 @@ public static void start(DeephavenApiServer server, SessionService sessionServic
private final ConsoleServiceGrpcImpl consoleService;
private final ApplicationInjector applicationInjector;
private final ApplicationServiceGrpcImpl applicationService;
private final TableResolvers tableResolvers;
private final UriResolvers tableResolvers;

@Inject
public DeephavenApiServer(
Expand All @@ -70,7 +70,7 @@ public DeephavenApiServer(
final ConsoleServiceGrpcImpl consoleService,
final ApplicationInjector applicationInjector,
final ApplicationServiceGrpcImpl applicationService,
final TableResolvers tableResolvers) {
final UriResolvers tableResolvers) {
this.server = server;
this.ltm = ltm;
this.logInit = logInit;
Expand Down Expand Up @@ -107,10 +107,10 @@ public void start() throws IOException, ClassNotFoundException {
applicationInjector.run();

{
for (TableResolver resolver : tableResolvers.resolvers()) {
for (UriResolver resolver : tableResolvers.resolvers()) {
log.info().append("Found table resolver ").append(resolver.getClass().toString()).endl();
}
TableResolversInstance.init(tableResolvers);
UriResolversInstance.init(tableResolvers);
}

log.info().append("Starting server...").endl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.deephaven.appmode.ApplicationState;
import io.deephaven.appmode.Field;
import io.deephaven.db.tables.Table;
import io.deephaven.grpc_api.appmode.ApplicationStates;
import io.deephaven.uri.ApplicationUri;
import io.deephaven.uri.DeephavenUri;
Expand All @@ -21,10 +20,10 @@
*
* @see ApplicationUri application URI format
*/
public final class ApplicationResolver implements TableResolver {
public final class ApplicationResolver implements UriResolver {

public static ApplicationResolver get() {
return TableResolversInstance.get().find(ApplicationResolver.class).get();
return UriResolversInstance.get().find(ApplicationResolver.class).get();
}

private final ApplicationStates states;
Expand All @@ -45,31 +44,17 @@ public boolean isResolvable(URI uri) {
}

@Override
public Table resolve(URI uri) {
return resolve(ApplicationUri.of(uri));
public Object resolve(URI uri) {
final Field<Object> field = resolve(ApplicationUri.of(uri));
return field == null ? null : field.value();
}

public Table resolve(ApplicationUri uri) {
public Field<Object> resolve(ApplicationUri uri) {
return resolve(uri.applicationId(), uri.fieldName());
}

public Table resolve(String applicationId, String fieldName) {
public Field<Object> resolve(String applicationId, String fieldName) {
final ApplicationState app = states.getApplicationState(applicationId).orElse(null);
if (app == null) {
return null;
}
final Field<Object> field = app.getField(fieldName);
if (field == null) {
return null;
}
return asTable(field.value(), applicationId, fieldName);
}

private Table asTable(Object value, String context, String fieldName) {
if (value == null || value instanceof Table) {
return (Table) value;
}
throw new IllegalArgumentException(
String.format("Field '%s' in '%s' is not a Table, is %s", fieldName, context, value.getClass()));
return app == null ? null : app.getField(fieldName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
import java.util.concurrent.ScheduledExecutorService;

/**
* The barrage table resolver is able to resolve {@link RemoteUri remote URIs} into tables.
* The barrage table resolver is able to resolve {@link RemoteUri remote URIs} into {@link Table tables}.
*
* <p>
* For more advanced use cases, see {@link BarrageSession}.
*
* @see RemoteUri remote URI format
*/
@Singleton
public final class BarrageTableResolver implements TableResolver {
public final class BarrageTableResolver implements UriResolver {

/**
* The default options, which uses {@link BarrageSubscriptionOptions#useDeephavenNulls()}.
Expand All @@ -53,7 +53,7 @@ public final class BarrageTableResolver implements TableResolver {
new HashSet<>(Arrays.asList(DeephavenUri.TLS_SCHEME, DeephavenUri.PLAINTEXT_SCHEME)));

public static BarrageTableResolver get() {
return TableResolversInstance.get().find(BarrageTableResolver.class).get();
return UriResolversInstance.get().find(BarrageTableResolver.class).get();
}

private final BarrageSessionFactoryBuilder builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@

/**
* The csv table resolver is able to resolve CSV for schemes {@code csv+http}, {@code http+csv}, {@code csv+https},
* {@code https+csv}, {@code csv+file}, {@code file+csv}, and {@code csv}.
* {@code https+csv}, {@code csv+file}, {@code file+csv}, and {@code csv} into {@link Table tables}.
*
* <p>
* For example, {@code csv+https://media.githubusercontent.com/media/deephaven/examples/main/Iris/csv/iris.csv}.
*
* <p>
* For more advanced use cases, see {@link CsvHelpers}.
*/
public final class CsvTableResolver implements TableResolver {
public final class CsvTableResolver implements UriResolver {

private static final Set<String> SCHEMES = Collections.unmodifiableSet(new HashSet<>(
Arrays.asList("csv+http", "http+csv", "csv+https", "https+csv", "csv+file", "file+csv", "csv")));

public static CsvTableResolver get() {
return UriResolversInstance.get().find(CsvTableResolver.class).get();
}

@Inject
public CsvTableResolver() {}

Expand All @@ -44,12 +48,16 @@ public boolean isResolvable(URI uri) {
@Override
public Table resolve(URI uri) {
try {
return CsvHelpers.readCsv(csvString(uri));
return read(uri);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public Table read(URI uri) throws IOException {
return CsvHelpers.readCsv(csvString(uri));
}

private static String csvString(URI uri) {
final String scheme = uri.getScheme();
switch (scheme) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
import java.util.Set;

/**
* The parquet table resolver is able to resolve local parquet files or directories for the scheme {@value #SCHEME}.
* The parquet table resolver is able to resolve local parquet files, or directories for the scheme {@value #SCHEME}, into {@link Table tables}.
*
* <p>
* For example, {@code parquet:///data/my-file.parquet} or {@code parquet:///data/my-dir}.
*
* <p>
* For more advanced use cases, see {@link ParquetTools}.
*/
public final class ParquetTableResolver implements TableResolver {
public final class ParquetTableResolver implements UriResolver {

/**
* The parquet scheme, {@code parquet}.
Expand All @@ -31,6 +31,10 @@ public static boolean isWellFormed(URI uri) {
return SCHEME.equals(uri.getScheme()) && UriHelper.isLocalPath(uri);
}

public static ParquetTableResolver get() {
return UriResolversInstance.get().find(ParquetTableResolver.class).get();
}

@Inject
public ParquetTableResolver() {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.deephaven.grpc_api.uri;

import io.deephaven.db.tables.Table;
import io.deephaven.grpc_api.console.GlobalSessionProvider;
import io.deephaven.uri.DeephavenUri;
import io.deephaven.uri.QueryScopeUri;
Expand All @@ -19,10 +18,10 @@
*
* @see QueryScopeUri query scope URI format
*/
public final class QueryScopeResolver implements TableResolver {
public final class QueryScopeResolver implements UriResolver {

public static QueryScopeResolver get() {
return TableResolversInstance.get().find(QueryScopeResolver.class).get();
return UriResolversInstance.get().find(QueryScopeResolver.class).get();
}

private final GlobalSessionProvider globalSessionProvider;
Expand All @@ -43,24 +42,15 @@ public boolean isResolvable(URI uri) {
}

@Override
public Table resolve(URI uri) {
public Object resolve(URI uri) {
return resolve(QueryScopeUri.of(uri));
}

public Table resolve(QueryScopeUri uri) {
public Object resolve(QueryScopeUri uri) {
return resolve(uri.variableName());
}

public Table resolve(String variableName) {
final Object variable = globalSessionProvider.getGlobalSession().getVariable(variableName, null);
return asTable(variable, "global query scope", variableName);
}

private Table asTable(Object value, String context, String fieldName) {
if (value == null || value instanceof Table) {
return (Table) value;
}
throw new IllegalArgumentException(
String.format("Field '%s' in '%s' is not a Table, is %s", fieldName, context, value.getClass()));
public Object resolve(String variableName) {
return globalSessionProvider.getGlobalSession().getVariable(variableName, null);
}
}
12 changes: 6 additions & 6 deletions grpc-api/src/main/java/io/deephaven/grpc_api/uri/UriModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io.deephaven.grpc_api.barrage.BarrageClientModule;

/**
* Installs the {@link TableResolver table resolvers}. See each specific resolver for more information.
* Installs the {@link UriResolver table resolvers}. See each specific resolver for more information.
*
* @see BarrageTableResolver
* @see QueryScopeResolver
Expand All @@ -19,21 +19,21 @@ public interface UriModule {

@Binds
@IntoSet
TableResolver bindQueryScopeResolver(QueryScopeResolver resolver);
UriResolver bindQueryScopeResolver(QueryScopeResolver resolver);

@Binds
@IntoSet
TableResolver bindApplicationResolver(ApplicationResolver resolver);
UriResolver bindApplicationResolver(ApplicationResolver resolver);

@Binds
@IntoSet
TableResolver bindsBarrageTableResolver(BarrageTableResolver resolver);
UriResolver bindsBarrageTableResolver(BarrageTableResolver resolver);

@Binds
@IntoSet
TableResolver bindCsvResolver(CsvTableResolver resolver);
UriResolver bindCsvResolver(CsvTableResolver resolver);

@Binds
@IntoSet
TableResolver bindParquetResolver(ParquetTableResolver resolver);
UriResolver bindParquetResolver(ParquetTableResolver resolver);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package io.deephaven.grpc_api.uri;

import io.deephaven.db.tables.Table;

import java.net.URI;
import java.util.Set;

/**
* A table resolver resolves {@link URI URIs} into {@link Table tables}.
* A URI resolver resolves {@link URI URIs} into {@link Object objects}.
*/
public interface TableResolver {
public interface UriResolver {

/**
* The supported schemes.
Expand All @@ -26,11 +24,11 @@ public interface TableResolver {
boolean isResolvable(URI uri);

/**
* Resolve {@code uri} into a table.
* Resolve {@code uri} into an object.
*
* @param uri the URI
* @return the table
* @return the object
* @throws InterruptedException if the current thread is interrupted
*/
Table resolve(URI uri) throws InterruptedException;
Object resolve(URI uri) throws InterruptedException;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.deephaven.grpc_api.uri;

import io.deephaven.db.tables.Table;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.net.URI;
Expand All @@ -16,37 +14,37 @@
import java.util.stream.Collectors;

@Singleton
public final class TableResolvers {
public final class UriResolvers {

private final Set<TableResolver> resolvers;
private final Map<String, Set<TableResolver>> map;
private final Set<UriResolver> resolvers;
private final Map<String, Set<UriResolver>> map;

@Inject
public TableResolvers(Set<TableResolver> resolvers) {
public UriResolvers(Set<UriResolver> resolvers) {
this.resolvers = Objects.requireNonNull(resolvers);
map = new HashMap<>();
for (TableResolver resolver : resolvers) {
for (UriResolver resolver : resolvers) {
for (String scheme : resolver.schemes()) {
final Set<TableResolver> set = map.computeIfAbsent(scheme, s -> new HashSet<>());
final Set<UriResolver> set = map.computeIfAbsent(scheme, s -> new HashSet<>());
set.add(resolver);
}
}
}

public Set<TableResolver> resolvers() {
public Set<UriResolver> resolvers() {
return resolvers;
}

public <T extends TableResolver> Optional<T> find(Class<T> clazz) {
public <T extends UriResolver> Optional<T> find(Class<T> clazz) {
return resolvers()
.stream()
.filter(t -> clazz.equals(t.getClass()))
.map(clazz::cast)
.findFirst();
}

public TableResolver resolver(URI uri) {
final List<TableResolver> resolvers = map.getOrDefault(uri.getScheme(), Collections.emptySet())
public UriResolver resolver(URI uri) {
final List<UriResolver> resolvers = map.getOrDefault(uri.getScheme(), Collections.emptySet())
.stream()
.filter(t -> t.isResolvable(uri))
.collect(Collectors.toList());
Expand All @@ -55,7 +53,7 @@ public TableResolver resolver(URI uri) {
String.format("Unable to find resolver for uri '%s'", uri));
} else if (resolvers.size() > 1) {
final String classes = resolvers.stream()
.map(TableResolver::getClass)
.map(UriResolver::getClass)
.map(Class::toString)
.collect(Collectors.joining(",", "[", "]"));
throw new UnsupportedOperationException(
Expand All @@ -64,7 +62,7 @@ public TableResolver resolver(URI uri) {
return resolvers.get(0);
}

public Table resolve(URI uri) throws InterruptedException {
public Object resolve(URI uri) throws InterruptedException {
return resolver(uri).resolve(uri);
}
}
Loading

0 comments on commit b01288f

Please sign in to comment.