Search in sources :

Example 96 with StructuredRow

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

the class FileMetadataCleanerTest method deleteAllMetaEntries.

private boolean deleteAllMetaEntries(TransactionRunner transactionRunner) {
    final List<DeletedEntry> deletedEntries = new ArrayList<>();
    try {
        TransactionRunners.run(transactionRunner, context -> {
            StructuredTable table = context.getTable(StoreDefinition.LogFileMetaStore.LOG_FILE_META);
            try (CloseableIterator<StructuredRow> iter = table.scan(Range.all(), Integer.MAX_VALUE)) {
                while (iter.hasNext()) {
                    StructuredRow row = iter.next();
                    String loggingContext = row.getString(StoreDefinition.LogFileMetaStore.LOGGING_CONTEXT_FIELD);
                    long eventTime = row.getLong(StoreDefinition.LogFileMetaStore.EVENT_TIME_FIELD);
                    long creationTime = row.getLong(StoreDefinition.LogFileMetaStore.CREATION_TIME_FIELD);
                    table.delete(Arrays.asList(Fields.stringField(StoreDefinition.LogFileMetaStore.LOGGING_CONTEXT_FIELD, loggingContext), Fields.longField(StoreDefinition.LogFileMetaStore.EVENT_TIME_FIELD, eventTime), Fields.longField(StoreDefinition.LogFileMetaStore.CREATION_TIME_FIELD, creationTime)));
                    deletedEntries.add(new DeletedEntry(loggingContext, eventTime, creationTime));
                }
            }
        }, IOException.class);
    } catch (IOException e) {
        return false;
    }
    return true;
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) ArrayList(java.util.ArrayList) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) IOException(java.io.IOException)

Example 97 with StructuredRow

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

the class ArtifactStore method getArtifacts.

/**
 * Get information about all versions of the given artifact.
 *
 * @param namespace the namespace to get artifacts from
 * @param artifactName the name of the artifact to get
 * @param limit the limit number of the result
 * @param order the order of the result
 * @return unmodifiable list of information about all versions of the given artifact
 * @throws ArtifactNotFoundException if no version of the given artifact exists
 * @throws IOException if there was an exception reading the artifact information from the metastore
 */
public List<ArtifactDetail> getArtifacts(NamespaceId namespace, String artifactName, int limit, ArtifactSortOrder order) throws ArtifactNotFoundException, IOException {
    return TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
        Collection<Field<?>> keys = Arrays.asList(Fields.stringField(StoreDefinition.ArtifactStore.ARTIFACT_NAMESPACE_FIELD, namespace.getNamespace()), Fields.stringField(StoreDefinition.ArtifactStore.ARTIFACT_NAME_FIELD, artifactName));
        try (CloseableIterator<StructuredRow> iterator = table.scan(Range.singleton(keys), Integer.MAX_VALUE)) {
            List<ArtifactDetail> artifacts = getArtifacts(iterator, limit, order, null);
            if (artifacts.isEmpty()) {
                throw new ArtifactNotFoundException(namespace, artifactName);
            }
            return artifacts;
        }
    }, ArtifactNotFoundException.class, IOException.class);
}
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) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException)

Example 98 with StructuredRow

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

the class ArtifactStore method updateArtifactProperties.

/**
 * Update artifact properties using an update function. Functions will receive an immutable map.
 *
 * @param artifactId the id of the artifact to add
 * @param updateFunction the function used to update existing properties
 * @throws ArtifactNotFoundException if the artifact does not exist
 * @throws IOException if there was an exception writing the properties to the metastore
 */
public void updateArtifactProperties(Id.Artifact artifactId, Function<Map<String, String>, Map<String, String>> updateFunction) throws ArtifactNotFoundException, IOException {
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable artifactDataTable = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
        ArtifactCell artifactCell = new ArtifactCell(artifactId);
        Optional<StructuredRow> optional = artifactDataTable.read(artifactCell.keys);
        if (!optional.isPresent()) {
            throw new ArtifactNotFoundException(artifactId.toEntityId());
        }
        ArtifactData old = GSON.fromJson(optional.get().getString(StoreDefinition.ArtifactStore.ARTIFACT_DATA_FIELD), ArtifactData.class);
        ArtifactMeta updatedMeta = new ArtifactMeta(old.meta.getClasses(), old.meta.getUsableBy(), updateFunction.apply(old.meta.getProperties()));
        ArtifactData updatedData = new ArtifactData(Locations.getLocationFromAbsolutePath(locationFactory, old.getLocationPath()), updatedMeta);
        // write artifact metadata
        List<Field<?>> fields = ImmutableList.<Field<?>>builder().addAll(artifactCell.keys).add(Fields.stringField(StoreDefinition.ArtifactStore.ARTIFACT_DATA_FIELD, GSON.toJson(updatedData))).build();
        artifactDataTable.upsert(fields);
    }, ArtifactNotFoundException.class, IOException.class);
}
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) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException)

Example 99 with StructuredRow

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

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);
}
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) 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) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) Range(io.cdap.cdap.spi.data.table.field.Range) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 100 with StructuredRow

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

the class ArtifactStore method getArtifact.

/**
 * Get information about the given artifact.
 *
 * @param artifactId the artifact to get
 * @return information about the artifact
 * @throws ArtifactNotFoundException if the given artifact does not exist
 * @throws IOException if there was an exception reading the artifact information from the metastore
 */
public ArtifactDetail getArtifact(final Id.Artifact artifactId) throws ArtifactNotFoundException, IOException {
    ArtifactData artifactData = TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
        ArtifactCell artifactCell = new ArtifactCell(artifactId);
        Optional<StructuredRow> row = table.read(artifactCell.keys);
        if (!row.isPresent()) {
            throw new ArtifactNotFoundException(artifactId.toEntityId());
        }
        return GSON.fromJson(row.get().getString(StoreDefinition.ArtifactStore.ARTIFACT_DATA_FIELD), ArtifactData.class);
    }, IOException.class, ArtifactNotFoundException.class);
    try {
        Location artifactLocation = impersonator.doAs(artifactId.getNamespace().toEntityId(), () -> Locations.getLocationFromAbsolutePath(locationFactory, artifactData.getLocationPath()));
        ArtifactMeta artifactMeta = filterPlugins(artifactData.meta);
        return new ArtifactDetail(new ArtifactDescriptor(artifactId.getNamespace().getId(), artifactId.toArtifactId(), artifactLocation), artifactMeta);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) 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) Location(org.apache.twill.filesystem.Location)

Aggregations

StructuredRow (io.cdap.cdap.spi.data.StructuredRow)142 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)68 Field (io.cdap.cdap.spi.data.table.field.Field)66 ArrayList (java.util.ArrayList)54 Range (io.cdap.cdap.spi.data.table.field.Range)36 HashSet (java.util.HashSet)28 IOException (java.io.IOException)22 HashMap (java.util.HashMap)22 LinkedHashSet (java.util.LinkedHashSet)22 List (java.util.List)20 LinkedHashMap (java.util.LinkedHashMap)18 Map (java.util.Map)18 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)16 Collection (java.util.Collection)16 Set (java.util.Set)16 Nullable (javax.annotation.Nullable)16 ImmutableList (com.google.common.collect.ImmutableList)14 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)14 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)14 Fields (io.cdap.cdap.spi.data.table.field.Fields)14