Search in sources :

Example 26 with StructuredTable

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

the class DraftStore method deleteDraft.

/**
 * Delete the given draft. This is a no-op if the draft does not exist
 *
 * @param id {@link DraftId} that is used to uniquely identify a draft
 * @throws TableNotFoundException if the draft store table is not found
 * @throws InvalidFieldException if the fields in the {@link Draft} object do not match the fields in the
 *   StructuredTable
 */
public void deleteDraft(DraftId id) throws TableNotFoundException, InvalidFieldException {
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = context.getTable(TABLE_ID);
        table.delete(getKey(id));
    }, TableNotFoundException.class, InvalidFieldException.class);
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable)

Example 27 with StructuredTable

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

the class DraftStore method getDraft.

/**
 * Fetch a given draft if it exists
 *
 * @param id {@link DraftId} that is used to uniquely identify a draft
 * @return an {@link Optional<Draft>} representing the requested draft
 * @throws TableNotFoundException if the draft store table is not found
 * @throws InvalidFieldException if the fields in the {@link DraftId} object do not match the fields in the
 *   StructuredTable
 */
public Optional<Draft> getDraft(DraftId id) throws TableNotFoundException, InvalidFieldException {
    return TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = context.getTable(TABLE_ID);
        Optional<StructuredRow> row = table.read(getKey(id));
        return row.map(this::fromRow);
    }, TableNotFoundException.class, InvalidFieldException.class);
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow)

Example 28 with StructuredTable

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

the class DraftStore method writeDraft.

/**
 * Create/update the given draft
 *
 * @param id {@link DraftId} that is used to uniquely identify a draft
 * @param request {@link DraftStoreRequest} that contains the rest of the draft data
 * @throws TableNotFoundException if the draft store table is not found
 * @throws InvalidFieldException if the fields in the {@link Draft} or {@link DraftStoreRequest} objects do not
 *   match the fields in the StructuredTable
 */
public <T extends ETLConfig> void writeDraft(DraftId id, DraftStoreRequest<T> request) throws TableNotFoundException, InvalidFieldException {
    Optional<Draft> existing = getDraft(id);
    long now = System.currentTimeMillis();
    long createTime = existing.map(Draft::getCreatedTimeMillis).orElse(now);
    Draft draft = new Draft(request.getConfig(), request.getName(), request.getDescription(), request.getArtifact(), id.getId(), createTime, now);
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = context.getTable(TABLE_ID);
        table.upsert(getRow(id, draft));
    }, TableNotFoundException.class, InvalidFieldException.class);
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable)

Example 29 with StructuredTable

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

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 30 with StructuredTable

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

the class ArtifactStore method deleteMeta.

private void deleteMeta(StructuredTableContext context, Id.Artifact artifactId, ArtifactData oldMeta) throws IOException {
    // delete old artifact data
    StructuredTable artifactTable = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
    ArtifactCell artifactCell = new ArtifactCell(artifactId);
    artifactTable.delete(artifactCell.keys);
    // delete old appclass metadata
    StructuredTable appClassTable = getTable(context, StoreDefinition.ArtifactStore.APP_DATA_TABLE);
    for (ApplicationClass appClass : oldMeta.meta.getClasses().getApps()) {
        AppClassKey appClassKey = new AppClassKey(artifactId.getNamespace().toEntityId(), appClass.getClassName());
        deleteRangeFromTable(appClassTable, Range.singleton(appClassKey.keys));
    }
    // delete old plugins, we loop twice to only access to one table at a time to prevent deadlock
    StructuredTable pluginDataTable = getTable(context, StoreDefinition.ArtifactStore.PLUGIN_DATA_TABLE);
    for (PluginClass pluginClass : oldMeta.meta.getClasses().getPlugins()) {
        // delete metadata for each artifact this plugin extends
        for (ArtifactRange artifactRange : oldMeta.meta.getUsableBy()) {
            // these four fields are prefixes of the plugin table primary keys
            PluginKeyPrefix pluginKey = new PluginKeyPrefix(artifactRange.getNamespace(), artifactRange.getName(), pluginClass.getType(), pluginClass.getName());
            pluginDataTable.delete(concatFields(pluginKey.keys, artifactCell.keys));
        }
    }
    // Delete the universal plugin row
    StructuredTable uniPluginTable = getTable(context, StoreDefinition.ArtifactStore.UNIV_PLUGIN_DATA_TABLE);
    for (PluginClass pluginClass : oldMeta.meta.getClasses().getPlugins()) {
        if (oldMeta.meta.getUsableBy().isEmpty()) {
            UniversalPluginKeyPrefix pluginKey = new UniversalPluginKeyPrefix(artifactId.getNamespace().getId(), pluginClass.getType(), pluginClass.getName());
            uniPluginTable.delete(concatFields(pluginKey.keys, artifactCell.keys));
        }
    }
    // delete the old jar file
    try {
        new EntityImpersonator(artifactId.toEntityId(), impersonator).impersonate(() -> {
            Locations.getLocationFromAbsolutePath(locationFactory, oldMeta.getLocationPath()).delete();
            return null;
        });
    } catch (IOException ioe) {
        throw ioe;
    } catch (Exception e) {
        // this should not happen
        throw Throwables.propagate(e);
    }
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) EntityImpersonator(io.cdap.cdap.security.impersonation.EntityImpersonator) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) ApplicationClass(io.cdap.cdap.api.artifact.ApplicationClass) IOException(java.io.IOException) PluginClass(io.cdap.cdap.api.plugin.PluginClass) ArtifactAlreadyExistsException(io.cdap.cdap.common.ArtifactAlreadyExistsException) TransactionException(io.cdap.cdap.spi.data.transaction.TransactionException) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) IOException(java.io.IOException) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException)

Aggregations

StructuredTable (io.cdap.cdap.spi.data.StructuredTable)118 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)60 Field (io.cdap.cdap.spi.data.table.field.Field)44 ArrayList (java.util.ArrayList)34 IOException (java.io.IOException)22 Range (io.cdap.cdap.spi.data.table.field.Range)18 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)14 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)12 HashMap (java.util.HashMap)12 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)10 Map (java.util.Map)10 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)8 PluginNotExistsException (io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException)8 List (java.util.List)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 Gson (com.google.gson.Gson)6 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)6 PluginClass (io.cdap.cdap.api.plugin.PluginClass)6 ArtifactAlreadyExistsException (io.cdap.cdap.common.ArtifactAlreadyExistsException)6 Connection (io.cdap.cdap.etl.proto.connection.Connection)6