use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
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);
});
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
the class ArtifactStore method getArtifacts.
/**
* Get information about all artifacts in the given namespace. If there are no artifacts in the namespace,
* this will return an empty list. Note that existence of the namespace is not checked.
*
* @param namespace the namespace to get artifact information about
* @return unmodifiable list of artifact info about every artifact in the given namespace
* @throws IOException if there was an exception reading the artifact information from the metastore
*/
public List<ArtifactDetail> getArtifacts(final NamespaceId namespace) throws IOException {
return TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
List<ArtifactDetail> artifacts = Lists.newArrayList();
Range scanRange = createArtifactScanRange(namespace);
// TODO: CDAP-14636 add scan method without limit
try (CloseableIterator<StructuredRow> iterator = table.scan(scanRange, Integer.MAX_VALUE)) {
getArtifacts(iterator, Integer.MAX_VALUE, null, () -> artifacts);
}
return Collections.unmodifiableList(artifacts);
}, IOException.class);
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
the class ArtifactStore method clear.
/**
* Clear all data in the given namespace. Used only in unit tests.
*
* @param namespace the namespace to delete data in
* @throws IOException if there was some problem deleting the data
*/
@VisibleForTesting
void clear(final NamespaceId namespace) throws IOException {
final Id.Namespace namespaceId = Id.Namespace.fromEntityId(namespace);
namespacePathLocator.get(namespace).append(ARTIFACTS_PATH).delete(true);
TransactionRunners.run(transactionRunner, context -> {
// delete all rows about artifacts in the namespace
StructuredTable artifactDataTable = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
Range artifactScanRange = createArtifactScanRange(namespace);
deleteRangeFromTable(artifactDataTable, artifactScanRange);
// delete all rows about artifacts in the namespace and the plugins they have access to
StructuredTable pluginDataTable = getTable(context, StoreDefinition.ArtifactStore.PLUGIN_DATA_TABLE);
Collection<Field<?>> pluginKey = Collections.singleton(Fields.stringField(StoreDefinition.ArtifactStore.PARENT_NAMESPACE_FIELD, namespace.getNamespace()));
deleteRangeFromTable(pluginDataTable, Range.singleton(pluginKey));
// delete all rows about universal plugins
StructuredTable univPluginsDataTable = getTable(context, StoreDefinition.ArtifactStore.UNIV_PLUGIN_DATA_TABLE);
deleteRangeFromTable(univPluginsDataTable, createUniversalPluginScanRange(namespace.getNamespace(), null));
// delete app classes in this namespace
StructuredTable appClassTable = getTable(context, StoreDefinition.ArtifactStore.APP_DATA_TABLE);
deleteRangeFromTable(appClassTable, createAppClassRange(namespace));
// delete plugins in this namespace from system artifacts
// for example, if there was an artifact in this namespace that extends a system artifact
Collection<Field<?>> systemPluginKey = Collections.singleton(Fields.stringField(StoreDefinition.ArtifactStore.PARENT_NAMESPACE_FIELD, Id.Namespace.SYSTEM.getId()));
try (CloseableIterator<StructuredRow> iterator = pluginDataTable.scan(Range.singleton(systemPluginKey), Integer.MAX_VALUE)) {
while (iterator.hasNext()) {
StructuredRow row = iterator.next();
// if the plugin artifact is in the namespace we're deleting, delete this column.
if (namespaceId.getId().equals(row.getString(StoreDefinition.ArtifactStore.ARTIFACT_NAMESPACE_FIELD))) {
pluginDataTable.delete(concatFields(PluginKeyPrefix.fromRow(row), ArtifactCell.fromRow(row)));
}
}
}
}, IOException.class);
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
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());
}
}
}
use of io.cdap.cdap.spi.data.StructuredRow in project cdap by cdapio.
the class ArtifactStore method getPluginsInArtifact.
private SortedMap<ArtifactDescriptor, Set<PluginClass>> getPluginsInArtifact(StructuredTable artifactDataTable, Id.Artifact artifactId, Predicate<PluginClass> filter) throws ArtifactNotFoundException, IOException {
SortedMap<ArtifactDescriptor, Set<PluginClass>> result = new TreeMap<>();
// Make sure the artifact exists
ArtifactCell artifactCell = new ArtifactCell(artifactId);
Optional<StructuredRow> row = artifactDataTable.read(artifactCell.keys);
if (!row.isPresent()) {
throw new ArtifactNotFoundException(artifactId.toEntityId());
}
// include any plugin classes that are inside the artifact itself and is accepted by the filter
ArtifactData artifactData = GSON.fromJson(row.get().getString(StoreDefinition.ArtifactStore.ARTIFACT_DATA_FIELD), ArtifactData.class);
Set<PluginClass> plugins = artifactData.meta.getClasses().getPlugins().stream().filter(filter).collect(Collectors.toCollection(LinkedHashSet::new));
if (!plugins.isEmpty()) {
Location location = Locations.getLocationFromAbsolutePath(locationFactory, artifactData.getLocationPath());
ArtifactDescriptor descriptor = new ArtifactDescriptor(artifactId.getNamespace().getId(), artifactId.toArtifactId(), location);
result.put(descriptor, plugins);
}
return result;
}
Aggregations