Search in sources :

Example 1 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.

the class SqlStructuredTableRegistry method registerSpecification.

@Override
public void registerSpecification(StructuredTableSpecification specification) throws TableAlreadyExistsException {
    initIfNeeded();
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable registry = context.getTable(REGISTRY);
        StructuredTableId tableId = specification.getTableId();
        Optional<StructuredRow> optional = registry.read(Collections.singleton(Fields.stringField(TABLE_NAME_FIELD, tableId.getName())));
        if (optional.isPresent()) {
            throw new TableAlreadyExistsException(tableId);
        }
        LOG.debug("Registering table specification {}", specification);
        registry.upsert(Arrays.asList(Fields.stringField(TABLE_NAME_FIELD, tableId.getName()), Fields.stringField(TABLE_SPEC_FIELD, GSON.toJson(specification))));
    }, TableAlreadyExistsException.class);
}
Also used : TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) StructuredRow(io.cdap.cdap.spi.data.StructuredRow)

Example 2 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.

the class SqlStructuredTableRegistry method getSpecification.

@Override
@Nullable
public StructuredTableSpecification getSpecification(StructuredTableId tableId) {
    initIfNeeded();
    return TransactionRunners.run(transactionRunner, context -> {
        StructuredTable registry = context.getTable(REGISTRY);
        Optional<StructuredRow> optional = registry.read(Collections.singleton(Fields.stringField(TABLE_NAME_FIELD, tableId.getName())));
        if (!optional.isPresent()) {
            return null;
        }
        String specString = optional.get().getString(TABLE_SPEC_FIELD);
        LOG.trace("Got specification {} from registry", specString);
        return GSON.fromJson(specString, StructuredTableSpecification.class);
    });
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Nullable(javax.annotation.Nullable)

Example 3 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.

the class ArtifactStore method getApplicationClasses.

/**
 * Get all application classes that belong to the specified namespace.
 * Results are returned as a sorted map from artifact to application classes in that artifact.
 * Map entries are sorted by the artifact.
 *
 * @param namespace the namespace from which to get application classes
 * @return an unmodifiable map of artifact to a list of all application classes in that artifact.
 *         The map will never be null. If there are no application classes, an empty map will be returned.
 */
public SortedMap<ArtifactDescriptor, List<ApplicationClass>> getApplicationClasses(NamespaceId namespace) {
    return TransactionRunners.run(transactionRunner, context -> {
        SortedMap<ArtifactDescriptor, List<ApplicationClass>> result = Maps.newTreeMap();
        StructuredTable table = getTable(context, StoreDefinition.ArtifactStore.APP_DATA_TABLE);
        Collection<Field<?>> keys = Collections.singleton(Fields.stringField(StoreDefinition.ArtifactStore.NAMESPACE_FIELD, namespace.getNamespace()));
        try (CloseableIterator<StructuredRow> iterator = table.scan(Range.singleton(keys), Integer.MAX_VALUE)) {
            while (iterator.hasNext()) {
                StructuredRow row = iterator.next();
                Map.Entry<ArtifactDescriptor, ApplicationClass> entry = extractApplicationClass(row);
                List<ApplicationClass> existingAppClasses = result.computeIfAbsent(entry.getKey(), k -> new ArrayList<>());
                existingAppClasses.add(entry.getValue());
            }
        }
        return Collections.unmodifiableSortedMap(result);
    });
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ApplicationClass(io.cdap.cdap.api.artifact.ApplicationClass) Field(io.cdap.cdap.spi.data.table.field.Field) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) SortedMap(java.util.SortedMap) AbstractMap(java.util.AbstractMap) TreeMap(java.util.TreeMap)

Example 4 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.

the class ArtifactStore method getApplicationClasses.

/**
 * Get all application classes that belong to the specified namespace of the specified classname.
 * Results are returned as a sorted map from artifact to application classes in that artifact.
 * Map entries are sorted by the artifact.
 *
 * @param namespace the namespace from which to get application classes
 * @param className the classname of application classes to get
 * @return an unmodifiable map of artifact the application classes in that artifact.
 *         The map will never be null. If there are no application classes, an empty map will be returned.
 */
public SortedMap<ArtifactDescriptor, ApplicationClass> getApplicationClasses(final NamespaceId namespace, final String className) {
    return TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = getTable(context, StoreDefinition.ArtifactStore.APP_DATA_TABLE);
        SortedMap<ArtifactDescriptor, ApplicationClass> result = Maps.newTreeMap();
        Collection<Field<?>> keys = ImmutableList.of(Fields.stringField(StoreDefinition.ArtifactStore.NAMESPACE_FIELD, namespace.getNamespace()), Fields.stringField(StoreDefinition.ArtifactStore.CLASS_NAME_FIELD, className));
        try (CloseableIterator<StructuredRow> iterator = table.scan(Range.singleton(keys), Integer.MAX_VALUE)) {
            while (iterator.hasNext()) {
                StructuredRow row = iterator.next();
                Map.Entry<ArtifactDescriptor, ApplicationClass> entry = extractApplicationClass(row);
                result.put(entry.getKey(), entry.getValue());
            }
        }
        return Collections.unmodifiableSortedMap(result);
    });
}
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) ApplicationClass(io.cdap.cdap.api.artifact.ApplicationClass) Map(java.util.Map) SortedMap(java.util.SortedMap) AbstractMap(java.util.AbstractMap) TreeMap(java.util.TreeMap)

Example 5 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable 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)

Aggregations

StructuredTable (io.cdap.cdap.spi.data.StructuredTable)59 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)30 Field (io.cdap.cdap.spi.data.table.field.Field)22 ArrayList (java.util.ArrayList)17 IOException (java.io.IOException)11 Range (io.cdap.cdap.spi.data.table.field.Range)9 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)7 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)6 HashMap (java.util.HashMap)6 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)5 Map (java.util.Map)5 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)4 PluginNotExistsException (io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException)4 List (java.util.List)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 Gson (com.google.gson.Gson)3 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)3 PluginClass (io.cdap.cdap.api.plugin.PluginClass)3 ArtifactAlreadyExistsException (io.cdap.cdap.common.ArtifactAlreadyExistsException)3 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)3