Search in sources :

Example 1 with Range

use of io.cdap.cdap.spi.data.table.field.Range in project cdap by caskdata.

the class PostgreSqlStructuredTable method getCountStatement.

private String getCountStatement(Collection<Range> ranges) {
    StringBuilder statement = new StringBuilder("SELECT COUNT(*) FROM ").append(tableSchema.getTableId().getName());
    boolean whereAdded = false;
    for (Range range : ranges) {
        fieldValidator.validatePrimaryKeys(range.getBegin(), true);
        fieldValidator.validatePrimaryKeys(range.getEnd(), true);
        if (!range.getBegin().isEmpty() || !range.getEnd().isEmpty()) {
            if (!whereAdded) {
                // first WHERE condition
                statement.append(" WHERE ");
                whereAdded = true;
            } else {
                // subsequent WHERE conditions
                statement.append(" OR ");
            }
            appendRange(statement, range);
        }
    }
    return statement.toString();
}
Also used : Range(io.cdap.cdap.spi.data.table.field.Range)

Example 2 with Range

use of io.cdap.cdap.spi.data.table.field.Range in project cdap by caskdata.

the class ArtifactStore method addPluginsInRangeToMap.

private void addPluginsInRangeToMap(final NamespaceId namespace, List<Id.Artifact> parentArtifacts, Iterator<StructuredRow> iterator, SortedMap<ArtifactDescriptor, PluginClass> plugins, @Nullable Predicate<io.cdap.cdap.proto.id.ArtifactId> range, int limit) {
    // if predicate is null,
    // filter out plugins whose artifacts are not in the system namespace and not in this namespace
    range = range != null ? range : input -> NamespaceId.SYSTEM.equals(input.getParent()) || input.getParent().equals(namespace);
    while (iterator.hasNext()) {
        StructuredRow row = iterator.next();
        ImmutablePair<ArtifactDescriptor, PluginData> pluginPair = getPlugin(row, range);
        if (pluginPair == null) {
            continue;
        }
        PluginData pluginData = pluginPair.getSecond();
        // filter out plugins that don't extend this version of the parent artifact
        for (Id.Artifact parentArtifactId : parentArtifacts) {
            if (pluginData.isUsableBy(parentArtifactId.toEntityId()) && isAllowed(pluginData.pluginClass)) {
                plugins.put(pluginPair.getFirst(), pluginData.pluginClass);
                break;
            }
        }
        if (limit < plugins.size()) {
            plugins.remove(plugins.lastKey());
        }
    }
}
Also used : Arrays(java.util.Arrays) ImmutablePair(io.cdap.cdap.common.utils.ImmutablePair) TransactionRunners(io.cdap.cdap.spi.data.transaction.TransactionRunners) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Spliterators(java.util.Spliterators) Inject(com.google.inject.Inject) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ArtifactClasses(io.cdap.cdap.api.artifact.ArtifactClasses) Fields(io.cdap.cdap.spi.data.table.field.Fields) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) ArtifactSortOrder(io.cdap.cdap.proto.artifact.ArtifactSortOrder) Map(java.util.Map) Field(io.cdap.cdap.spi.data.table.field.Field) URI(java.net.URI) Collector(java.util.stream.Collector) StoreDefinition(io.cdap.cdap.store.StoreDefinition) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) ApplicationClass(io.cdap.cdap.api.artifact.ApplicationClass) StructuredTableContext(io.cdap.cdap.spi.data.StructuredTableContext) Collectors(java.util.stream.Collectors) PluginClass(io.cdap.cdap.api.plugin.PluginClass) BinaryOperator(java.util.function.BinaryOperator) Id(io.cdap.cdap.common.id.Id) List(java.util.List) TransactionRunner(io.cdap.cdap.spi.data.transaction.TransactionRunner) Optional(java.util.Optional) Constants(io.cdap.cdap.common.conf.Constants) ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) ArtifactAlreadyExistsException(io.cdap.cdap.common.ArtifactAlreadyExistsException) SortedMap(java.util.SortedMap) SchemaTypeAdapter(io.cdap.cdap.internal.io.SchemaTypeAdapter) TransactionException(io.cdap.cdap.spi.data.transaction.TransactionException) Location(org.apache.twill.filesystem.Location) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Locations(io.cdap.cdap.common.io.Locations) StreamSupport(java.util.stream.StreamSupport) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) Files(java.nio.file.Files) Throwables(com.google.common.base.Throwables) Impersonator(io.cdap.cdap.security.impersonation.Impersonator) IOException(java.io.IOException) LocationFactory(org.apache.twill.filesystem.LocationFactory) Maps(com.google.common.collect.Maps) Schema(io.cdap.cdap.api.data.schema.Schema) CloseableIterator(io.cdap.cdap.api.dataset.lib.CloseableIterator) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) File(java.io.File) MinMaxPriorityQueue(com.google.common.collect.MinMaxPriorityQueue) EntityImpersonator(io.cdap.cdap.security.impersonation.EntityImpersonator) AbstractMap(java.util.AbstractMap) Requirements(io.cdap.cdap.api.plugin.Requirements) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) TreeMap(java.util.TreeMap) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) Range(io.cdap.cdap.spi.data.table.field.Range) NamespacePathLocator(io.cdap.cdap.common.namespace.NamespacePathLocator) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Collections(java.util.Collections) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId)

Example 3 with Range

use of io.cdap.cdap.spi.data.table.field.Range in project cdap by caskdata.

the class ArtifactStore method getPluginClasses.

/**
 * Get all plugin classes of the given type that extend the given parent artifact.
 * Results are returned as a map from plugin artifact to plugins in that artifact.
 *
 * @param namespace the namespace to search for plugins. The system namespace is always included
 * @param parentArtifactId the id of the artifact to find plugins for
 * @param type the type of plugin to look for or {@code null} for matching any type
 * @return an unmodifiable map of plugin artifact to plugin classes for all plugin classes accessible by the
 *         given artifact. The map will never be null. If there are no plugin classes, an empty map will be returned.
 * @throws ArtifactNotFoundException if the artifact to find plugins for does not exist
 * @throws IOException if there was an exception reading metadata from the metastore
 */
public SortedMap<ArtifactDescriptor, Set<PluginClass>> getPluginClasses(NamespaceId namespace, Id.Artifact parentArtifactId, @Nullable String type) throws ArtifactNotFoundException, IOException {
    return TransactionRunners.run(transactionRunner, context -> {
        StructuredTable artifactDataTable = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
        SortedMap<ArtifactDescriptor, Set<PluginClass>> plugins = getPluginsInArtifact(artifactDataTable, parentArtifactId, input -> (type == null || type.equals(input.getType())) && isAllowed(input));
        // Scan plugins
        StructuredTable pluginTable = getTable(context, StoreDefinition.ArtifactStore.PLUGIN_DATA_TABLE);
        try (CloseableIterator<StructuredRow> iterator = pluginTable.scan(createPluginScanRange(parentArtifactId, type), Integer.MAX_VALUE)) {
            while (iterator.hasNext()) {
                StructuredRow row = iterator.next();
                addPluginToMap(namespace, parentArtifactId, plugins, row);
            }
        }
        // Scan universal plugins
        StructuredTable uniPluginTable = getTable(context, StoreDefinition.ArtifactStore.UNIV_PLUGIN_DATA_TABLE);
        List<Range> ranges = Arrays.asList(createUniversalPluginScanRange(namespace.getNamespace(), type), createUniversalPluginScanRange(NamespaceId.SYSTEM.getNamespace(), type));
        for (Range range : ranges) {
            try (CloseableIterator<StructuredRow> iterator = uniPluginTable.scan(range, Integer.MAX_VALUE)) {
                while (iterator.hasNext()) {
                    StructuredRow row = iterator.next();
                    addPluginToMap(namespace, parentArtifactId, plugins, row);
                }
            }
        }
        return Collections.unmodifiableSortedMap(plugins);
    }, ArtifactNotFoundException.class, IOException.class);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) Range(io.cdap.cdap.spi.data.table.field.Range)

Example 4 with Range

use of io.cdap.cdap.spi.data.table.field.Range in project cdap by caskdata.

the class AppMetadataStore method scanActiveRuns.

/**
 * Scans active runs, starting from the given cursor.
 *
 * @param cursor the cursor to start the scan. A cursor can be obtained
 *               from the call to the given {@link BiConsumer} for some previous scan, or use
 *               {@link Cursor#EMPTY} to start a scan at the beginning.
 * @param limit maximum number of records to scan
 * @param consumer a {@link BiConsumer} to consume the scan result
 * @throws IOException if failed to query the storage
 */
public void scanActiveRuns(Cursor cursor, int limit, BiConsumer<Cursor, RunRecordDetail> consumer) throws IOException {
    Collection<Field<?>> begin = cursor.fields;
    if (begin.isEmpty()) {
        begin = getRunRecordStatusPrefix(TYPE_RUN_RECORD_ACTIVE);
    }
    Range range = Range.create(begin, cursor.bound, getRunRecordStatusPrefix(TYPE_RUN_RECORD_ACTIVE), Range.Bound.INCLUSIVE);
    StructuredTable table = getRunRecordsTable();
    try (CloseableIterator<StructuredRow> iterator = table.scan(range, limit)) {
        while (iterator.hasNext()) {
            StructuredRow row = iterator.next();
            consumer.accept(new Cursor(row.getPrimaryKeys(), Range.Bound.EXCLUSIVE), deserializeRunRecordMeta(row));
        }
    }
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Range(io.cdap.cdap.spi.data.table.field.Range)

Example 5 with Range

use of io.cdap.cdap.spi.data.table.field.Range in project cdap by caskdata.

the class ConfigTable method list.

public List<Config> list(String namespace, String type) throws IOException {
    List<Field<?>> scanStart = new ArrayList<>(2);
    scanStart.add(Fields.stringField(StoreDefinition.ConfigStore.NAMESPACE_FIELD, namespace));
    scanStart.add(Fields.stringField(StoreDefinition.ConfigStore.TYPE_FIELD, type));
    Range range = Range.singleton(scanStart);
    try (CloseableIterator<StructuredRow> iter = table.scan(range, Integer.MAX_VALUE)) {
        List<Config> result = new ArrayList<>();
        while (iter.hasNext()) {
            StructuredRow row = iter.next();
            result.add(fromRow(row));
        }
        return result;
    }
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) ArrayList(java.util.ArrayList) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Range(io.cdap.cdap.spi.data.table.field.Range)

Aggregations

Range (io.cdap.cdap.spi.data.table.field.Range)23 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)14 ArrayList (java.util.ArrayList)14 Field (io.cdap.cdap.spi.data.table.field.Field)12 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)9 HashSet (java.util.HashSet)6 LinkedHashSet (java.util.LinkedHashSet)6 Set (java.util.Set)6 LinkedHashMap (java.util.LinkedHashMap)5 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)4 IOException (java.io.IOException)4 Collection (java.util.Collection)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)3 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Nullable (javax.annotation.Nullable)3 ImmutableList (com.google.common.collect.ImmutableList)2 AbstractCloseableIterator (io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator)2