Search in sources :

Example 1 with Table

use of com.yahoo.elide.datastores.aggregation.metadata.models.Table in project elide by yahoo.

the class AggregationDataStore method validateModelExpressionChecks.

/**
 * Validates The security Check expression type for both Table and all its fields.
 * Table Security Check Condition - User Checks and Filter Expression Checks
 * Field Security Check Condition - User Checks
 * @param dictionary - Entity Dictionary
 * @param clz - Model Type.
 */
private void validateModelExpressionChecks(EntityDictionary dictionary, Type<?> clz) {
    PermissionExpressionVisitor visitor = new PermissionExpressionVisitor();
    ParseTree parseTree = dictionary.getPermissionsForClass(clz, ReadPermission.class);
    if (parseTree != null) {
        validateExpression(dictionary, visitor.visit(parseTree), (checkClass) -> UserCheck.class.isAssignableFrom(checkClass) || FilterExpressionCheck.class.isAssignableFrom(checkClass), "Table Can only have User Check and Filter Expression Check." + "Operation Checks Not allowed. given - %s");
    }
    dictionary.getAllExposedFields(clz).stream().map(field -> dictionary.getPermissionsForField(clz, field, ReadPermission.class)).filter(Objects::nonNull).forEach(tree -> validateExpression(dictionary, visitor.visit(tree), (checkClass) -> UserCheck.class.isAssignableFrom(checkClass), "Fields Can only have User checks. Given - %s"));
}
Also used : ColumnMeta(com.yahoo.elide.datastores.aggregation.annotation.ColumnMeta) Arrays(java.util.Arrays) ArgumentType(com.yahoo.elide.core.dictionary.ArgumentType) Join(com.yahoo.elide.datastores.aggregation.annotation.Join) AccessibleObject(com.yahoo.elide.core.type.AccessibleObject) TableMeta(com.yahoo.elide.datastores.aggregation.annotation.TableMeta) UserCheck(com.yahoo.elide.core.security.checks.UserCheck) PermissionExecutor(com.yahoo.elide.core.security.PermissionExecutor) Function(java.util.function.Function) ClassType(com.yahoo.elide.core.type.ClassType) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) ToString(lombok.ToString) ParseTree(org.antlr.v4.runtime.tree.ParseTree) FilterExpressionCheck(com.yahoo.elide.core.security.checks.FilterExpressionCheck) RequestScope(com.yahoo.elide.core.RequestScope) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Check(com.yahoo.elide.core.security.checks.Check) Cache(com.yahoo.elide.datastores.aggregation.cache.Cache) FromSubquery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery) PermissionExpressionVisitor(com.yahoo.elide.modelconfig.validator.PermissionExpressionVisitor) NonNull(lombok.NonNull) Predicate(java.util.function.Predicate) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) ArgumentDefinition(com.yahoo.elide.datastores.aggregation.metadata.models.ArgumentDefinition) Set(java.util.Set) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) AggregationStorePermissionExecutor(com.yahoo.elide.core.security.executors.AggregationStorePermissionExecutor) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Objects(java.util.Objects) List(java.util.List) ReadPermission(com.yahoo.elide.annotation.ReadPermission) Builder(lombok.Builder) DataStore(com.yahoo.elide.core.datastore.DataStore) Type(com.yahoo.elide.core.type.Type) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension) Annotation(java.lang.annotation.Annotation) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) QueryLogger(com.yahoo.elide.datastores.aggregation.core.QueryLogger) PermissionExpressionVisitor(com.yahoo.elide.modelconfig.validator.PermissionExpressionVisitor) ReadPermission(com.yahoo.elide.annotation.ReadPermission) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 2 with Table

use of com.yahoo.elide.datastores.aggregation.metadata.models.Table in project elide by yahoo.

the class AggregationDataStoreTransaction method buildQuery.

@VisibleForTesting
Query buildQuery(EntityProjection entityProjection, RequestScope scope) {
    Table table = metaDataStore.getTable(scope.getDictionary().getJsonAliasFor(entityProjection.getType()), scope.getApiVersion());
    String bypassCacheStr = scope.getRequestHeaderByName("bypasscache");
    Boolean bypassCache = "true".equals(bypassCacheStr);
    EntityProjectionTranslator translator = new EntityProjectionTranslator(queryEngine, table, entityProjection, scope, bypassCache);
    Query query = translator.getQuery();
    Query modifiedQuery = addTableFilterArguments(table, query, scope.getDictionary());
    modifiedQuery = addColumnFilterArguments(table, modifiedQuery, scope.getDictionary());
    return modifiedQuery;
}
Also used : Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Query(com.yahoo.elide.datastores.aggregation.query.Query) ToString(lombok.ToString) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with Table

use of com.yahoo.elide.datastores.aggregation.metadata.models.Table in project elide by yahoo.

the class AggregationDataStoreTransaction method loadObjects.

@Override
public <T> DataStoreIterable<T> loadObjects(EntityProjection entityProjection, RequestScope scope) {
    QueryResult result = null;
    QueryResponse response = null;
    String cacheKey = null;
    try {
        // Convert multivalued map to map.
        Map<String, String> headers = scope.getRequestHeaders().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, (entry) -> entry.getValue().stream().collect(Collectors.joining(" "))));
        queryLogger.acceptQuery(scope.getRequestId(), scope.getUser(), headers, scope.getApiVersion(), scope.getQueryParams(), scope.getPath());
        Query query = buildQuery(entityProjection, scope);
        Table table = (Table) query.getSource();
        if (cache != null && !query.isBypassingCache()) {
            String tableVersion = queryEngine.getTableVersion(table, queryEngineTransaction);
            tableVersion = tableVersion == null ? "" : tableVersion;
            cacheKey = tableVersion + ';' + QueryKeyExtractor.extractKey(query);
            result = cache.get(cacheKey);
        }
        boolean isCached = result != null;
        List<String> queryText = queryEngine.explain(query);
        queryLogger.processQuery(scope.getRequestId(), query, queryText, isCached);
        if (result == null) {
            result = queryEngine.executeQuery(query, queryEngineTransaction);
            if (cacheKey != null) {
                // The query result needs to be streamed into an in memory list before caching.
                // TODO - add a cap to how many records can be streamed back.  If this is exceeded, abort caching
                // and return the results.
                QueryResult cacheableResult = QueryResult.builder().data(Lists.newArrayList(result.getData().iterator())).pageTotals(result.getPageTotals()).build();
                cache.put(cacheKey, cacheableResult);
                result = cacheableResult;
            }
        }
        if (entityProjection.getPagination() != null && entityProjection.getPagination().returnPageTotals()) {
            entityProjection.getPagination().setPageTotals(result.getPageTotals());
        }
        response = new QueryResponse(HttpStatus.SC_OK, result.getData(), null);
        return new DataStoreIterableBuilder(result.getData()).build();
    } catch (HttpStatusException e) {
        response = new QueryResponse(e.getStatus(), null, e.getMessage());
        throw e;
    } catch (Exception e) {
        response = new QueryResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, null, e.getMessage());
        throw e;
    } finally {
        queryLogger.completeQuery(scope.getRequestId(), response);
    }
}
Also used : HttpStatus(com.yahoo.elide.core.exceptions.HttpStatus) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) QueryResponse(com.yahoo.elide.datastores.aggregation.core.QueryResponse) HashMap(java.util.HashMap) Argument(com.yahoo.elide.core.request.Argument) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) Map(java.util.Map) MatchesTemplateVisitor(com.yahoo.elide.datastores.aggregation.filter.visitor.MatchesTemplateVisitor) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) ToString(lombok.ToString) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) RequestScope(com.yahoo.elide.core.RequestScope) RequiresFilter(com.yahoo.elide.datastores.aggregation.metadata.models.RequiresFilter) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) Cache(com.yahoo.elide.datastores.aggregation.cache.Cache) Lists(org.apache.commons.compress.utils.Lists) EntityProjection(com.yahoo.elide.core.request.EntityProjection) QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Query(com.yahoo.elide.datastores.aggregation.query.Query) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) QueryKeyExtractor(com.yahoo.elide.datastores.aggregation.cache.QueryKeyExtractor) Type(com.yahoo.elide.core.type.Type) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) QueryLogger(com.yahoo.elide.datastores.aggregation.core.QueryLogger) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Query(com.yahoo.elide.datastores.aggregation.query.Query) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) ToString(lombok.ToString) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) IOException(java.io.IOException) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) QueryResponse(com.yahoo.elide.datastores.aggregation.core.QueryResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Table

use of com.yahoo.elide.datastores.aggregation.metadata.models.Table in project elide by yahoo.

the class MetaDataStoreTest method testHiddenFields.

@Test
public void testHiddenFields() {
    Table playerStats = dataStore.getTable(ClassType.of(PlayerStats.class));
    Dimension country = playerStats.getDimension("country");
    Dimension playerRank = playerStats.getDimension("playerRank");
    Metric highScore = playerStats.getMetric("highScore");
    Metric hiddenHighScore = playerStats.getMetric("hiddenHighScore");
    TimeDimension recordedDate = playerStats.getTimeDimension("recordedDate");
    TimeDimension hiddenRecordedDate = playerStats.getTimeDimension("hiddenRecordedDate");
    assertTrue(country.isHidden());
    assertFalse(playerRank.isHidden());
    assertTrue(hiddenHighScore.isHidden());
    assertFalse(highScore.isHidden());
    assertTrue(hiddenRecordedDate.isHidden());
    assertFalse(recordedDate.isHidden());
    assertTrue(playerStats.getColumns().contains(highScore));
    assertTrue(playerStats.getColumns().contains(recordedDate));
    assertTrue(playerStats.getColumns().contains(playerRank));
    assertFalse(playerStats.getColumns().contains(country));
    assertFalse(playerStats.getColumns().contains(hiddenHighScore));
    assertFalse(playerStats.getColumns().contains(hiddenRecordedDate));
    assertTrue(playerStats.getAllColumns().contains(highScore));
    assertTrue(playerStats.getAllColumns().contains(recordedDate));
    assertTrue(playerStats.getAllColumns().contains(playerRank));
    assertTrue(playerStats.getAllColumns().contains(country));
    assertTrue(playerStats.getAllColumns().contains(hiddenHighScore));
    assertTrue(playerStats.getAllColumns().contains(hiddenRecordedDate));
    assertFalse(playerStats.getDimensions().contains(country));
    assertFalse(playerStats.getMetrics().contains(hiddenHighScore));
    assertFalse(playerStats.getTimeDimensions().contains(hiddenRecordedDate));
    assertTrue(playerStats.getMetrics().contains(highScore));
    assertTrue(playerStats.getDimensions().contains(playerRank));
    assertTrue(playerStats.getTimeDimensions().contains(recordedDate));
    assertTrue(playerStats.getAllDimensions().contains(country));
    assertTrue(playerStats.getAllMetrics().contains(hiddenHighScore));
    assertTrue(playerStats.getAllTimeDimensions().contains(hiddenRecordedDate));
    assertTrue(playerStats.getAllMetrics().contains(highScore));
    assertTrue(playerStats.getAllDimensions().contains(playerRank));
    assertTrue(playerStats.getAllTimeDimensions().contains(recordedDate));
}
Also used : Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Metric(com.yahoo.elide.datastores.aggregation.metadata.models.Metric) PlayerStats(example.PlayerStats) Dimension(com.yahoo.elide.datastores.aggregation.metadata.models.Dimension) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension) Test(org.junit.jupiter.api.Test)

Example 5 with Table

use of com.yahoo.elide.datastores.aggregation.metadata.models.Table in project elide by yahoo.

the class MetaDataStoreTest method testHiddenTable.

@Test
public void testHiddenTable() {
    Table player = dataStore.getTable(ClassType.of(Player.class));
    Table playerStats = dataStore.getTable(ClassType.of(PlayerStats.class));
    assertTrue(player.isHidden());
    assertFalse(playerStats.isHidden());
    Namespace namespace = dataStore.getNamespace(ClassType.of(Player.class));
    assertTrue(namespace.getTables().contains(playerStats));
    assertFalse(namespace.getTables().contains(player));
}
Also used : Player(example.Player) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) PlayerStats(example.PlayerStats) Namespace(com.yahoo.elide.datastores.aggregation.metadata.models.Namespace) Test(org.junit.jupiter.api.Test)

Aggregations

Table (com.yahoo.elide.datastores.aggregation.metadata.models.Table)10 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)6 Column (com.yahoo.elide.datastores.aggregation.metadata.models.Column)5 Type (com.yahoo.elide.core.type.Type)4 Namespace (com.yahoo.elide.datastores.aggregation.metadata.models.Namespace)4 TimeDimension (com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension)4 List (java.util.List)4 RequestScope (com.yahoo.elide.core.RequestScope)3 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)3 ValueType (com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType)3 ToString (lombok.ToString)3 Test (org.junit.jupiter.api.Test)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ReadPermission (com.yahoo.elide.annotation.ReadPermission)2 DataStore (com.yahoo.elide.core.datastore.DataStore)2 ArgumentType (com.yahoo.elide.core.dictionary.ArgumentType)2 Argument (com.yahoo.elide.core.request.Argument)2 PermissionExecutor (com.yahoo.elide.core.security.PermissionExecutor)2 Check (com.yahoo.elide.core.security.checks.Check)2 FilterExpressionCheck (com.yahoo.elide.core.security.checks.FilterExpressionCheck)2