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);
}
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);
}
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);
}
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);
}
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);
}
}
Aggregations