Search in sources :

Example 21 with MetacatRequestContext

use of com.netflix.metacat.common.MetacatRequestContext in project metacat by Netflix.

the class TableServiceImpl method rename.

/**
 * {@inheritDoc}
 */
@Override
public void rename(final QualifiedName oldName, final QualifiedName newName, final boolean isMView) {
    validate(oldName);
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    final ConnectorTableService service = connectorManager.getTableService(oldName);
    final TableDto oldTable = get(oldName, GetTableServiceParameters.builder().includeInfo(true).disableOnReadMetadataIntercetor(false).includeDefinitionMetadata(true).includeDataMetadata(true).build()).orElseThrow(() -> new TableNotFoundException(oldName));
    if (oldTable != null) {
        // Ignore if the operation is not supported, so that we can at least go ahead and save the user metadata
        eventBus.postSync(new MetacatRenameTablePreEvent(oldName, metacatRequestContext, this, newName));
        try {
            log.info("Renaming {} {} to {}", isMView ? "view" : "table", oldName, newName);
            final ConnectorRequestContext connectorRequestContext = converterUtil.toConnectorContext(metacatRequestContext);
            service.rename(connectorRequestContext, oldName, newName);
        } catch (UnsupportedOperationException ignored) {
        }
        userMetadataService.renameDefinitionMetadataKey(oldName, newName);
        tagService.rename(oldName, newName.getTableName());
        final TableDto dto = get(newName, GetTableServiceParameters.builder().includeInfo(true).disableOnReadMetadataIntercetor(false).includeDefinitionMetadata(true).includeDataMetadata(true).build()).orElseThrow(() -> new IllegalStateException("should exist"));
        eventBus.postAsync(new MetacatRenameTablePostEvent(oldName, metacatRequestContext, this, oldTable, dto, isMView));
    }
}
Also used : TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) ConnectorTableService(com.netflix.metacat.common.server.connectors.ConnectorTableService) MetacatRenameTablePreEvent(com.netflix.metacat.common.server.events.MetacatRenameTablePreEvent) TableDto(com.netflix.metacat.common.dto.TableDto) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext) MetacatRenameTablePostEvent(com.netflix.metacat.common.server.events.MetacatRenameTablePostEvent)

Example 22 with MetacatRequestContext

use of com.netflix.metacat.common.MetacatRequestContext in project metacat by Netflix.

the class TableServiceImpl method deleteAndReturn.

/**
 * {@inheritDoc}
 */
@Override
public TableDto deleteAndReturn(final QualifiedName name, final boolean isMView) {
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    eventBus.postSync(new MetacatDeleteTablePreEvent(name, metacatRequestContext, this));
    validate(name);
    final ConnectorTableService service = connectorManager.getTableService(name);
    final Optional<TableDto> oTable = get(name, GetTableServiceParameters.builder().includeInfo(true).disableOnReadMetadataIntercetor(false).includeDefinitionMetadata(true).includeDataMetadata(true).build());
    if (oTable.isPresent()) {
        log.info("Drop table {}", name);
        final ConnectorRequestContext connectorRequestContext = converterUtil.toConnectorContext(metacatRequestContext);
        service.delete(connectorRequestContext, name);
    }
    final TableDto tableDto = oTable.orElseGet(() -> {
        // If the table doesn't exist construct a blank copy we can use to delete the definitionMetadata and tags
        final TableDto t = new TableDto();
        t.setName(name);
        return t;
    });
    if (canDeleteMetadata(name)) {
        // Delete the metadata.  Type doesn't matter since we discard the result
        log.info("Deleting user metadata for table {}", name);
        userMetadataService.deleteMetadata(metacatRequestContext.getUserName(), Lists.newArrayList(tableDto));
        log.info("Deleting tags for table {}", name);
        tagService.delete(name, false);
    } else {
        if (config.canSoftDeleteDataMetadata() && tableDto.isDataExternal()) {
            userMetadataService.softDeleteDataMetadata(metacatRequestContext.getUserName(), Lists.newArrayList(tableDto.getDataUri()));
        }
    }
    eventBus.postAsync(new MetacatDeleteTablePostEvent(name, metacatRequestContext, this, tableDto, isMView));
    return tableDto;
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) ConnectorTableService(com.netflix.metacat.common.server.connectors.ConnectorTableService) MetacatDeleteTablePostEvent(com.netflix.metacat.common.server.events.MetacatDeleteTablePostEvent) TableDto(com.netflix.metacat.common.dto.TableDto) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext) MetacatDeleteTablePreEvent(com.netflix.metacat.common.server.events.MetacatDeleteTablePreEvent)

Example 23 with MetacatRequestContext

use of com.netflix.metacat.common.MetacatRequestContext in project metacat by Netflix.

the class TableServiceImpl method get.

/**
 * {@inheritDoc}
 */
@Override
public Optional<TableDto> get(final QualifiedName name, final GetTableServiceParameters getTableServiceParameters) {
    validate(name);
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    final ConnectorRequestContext connectorRequestContext = converterUtil.toConnectorContext(metacatRequestContext);
    final ConnectorTableService service = connectorManager.getTableService(name);
    final TableDto tableInternal;
    final TableDto table;
    try {
        tableInternal = converterUtil.toTableDto(service.get(connectorRequestContext, name));
    } catch (NotFoundException ignored) {
        return Optional.empty();
    }
    if (getTableServiceParameters.isIncludeInfo()) {
        table = tableInternal;
    } else {
        table = new TableDto();
        table.setName(name);
    }
    if (getTableServiceParameters.isIncludeDefinitionMetadata()) {
        final Optional<ObjectNode> definitionMetadata = (getTableServiceParameters.isDisableOnReadMetadataIntercetor()) ? userMetadataService.getDefinitionMetadata(name) : userMetadataService.getDefinitionMetadataWithInterceptor(name, GetMetadataInterceptorParameters.builder().hasMetadata(tableInternal).build());
        definitionMetadata.ifPresent(table::setDefinitionMetadata);
    }
    if (getTableServiceParameters.isIncludeDataMetadata()) {
        TableDto dto = table;
        if (!getTableServiceParameters.isIncludeInfo()) {
            try {
                dto = converterUtil.toTableDto(service.get(connectorRequestContext, name));
            } catch (NotFoundException ignored) {
            }
        }
        if (dto != null && dto.getSerde() != null) {
            final Optional<ObjectNode> dataMetadata = userMetadataService.getDataMetadata(dto.getSerde().getUri());
            dataMetadata.ifPresent(table::setDataMetadata);
        }
    }
    return Optional.of(table);
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) ConnectorTableService(com.netflix.metacat.common.server.connectors.ConnectorTableService) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) NotFoundException(com.netflix.metacat.common.server.connectors.exception.NotFoundException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext) TableDto(com.netflix.metacat.common.dto.TableDto)

Example 24 with MetacatRequestContext

use of com.netflix.metacat.common.MetacatRequestContext in project metacat by Netflix.

the class TableServiceImpl method exists.

/**
 * {@inheritDoc}
 */
@Override
public boolean exists(final QualifiedName name) {
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    final ConnectorTableService service = connectorManager.getTableService(name);
    final ConnectorRequestContext connectorRequestContext = converterUtil.toConnectorContext(metacatRequestContext);
    return service.exists(connectorRequestContext, name);
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) ConnectorTableService(com.netflix.metacat.common.server.connectors.ConnectorTableService) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext)

Example 25 with MetacatRequestContext

use of com.netflix.metacat.common.MetacatRequestContext in project metacat by Netflix.

the class MetacatContextManager method getContext.

/**
 * Returns the current thread context.
 *
 * @return context
 */
public static MetacatRequestContext getContext() {
    MetacatRequestContext result = context.get();
    if (result == null) {
        result = new MetacatRequestContext();
        setContext(result);
    }
    return result;
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext)

Aggregations

MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)50 QualifiedName (com.netflix.metacat.common.QualifiedName)22 TableDto (com.netflix.metacat.common.dto.TableDto)20 ConnectorRequestContext (com.netflix.metacat.common.server.connectors.ConnectorRequestContext)20 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)16 List (java.util.List)12 PartitionDto (com.netflix.metacat.common.dto.PartitionDto)11 ConnectorTableService (com.netflix.metacat.common.server.connectors.ConnectorTableService)11 ConnectorPartitionService (com.netflix.metacat.common.server.connectors.ConnectorPartitionService)10 Collectors (java.util.stream.Collectors)10 Slf4j (lombok.extern.slf4j.Slf4j)10 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 Lists (com.google.common.collect.Lists)8 DatabaseDto (com.netflix.metacat.common.dto.DatabaseDto)8 GetPartitionsRequestDto (com.netflix.metacat.common.dto.GetPartitionsRequestDto)8 NotFoundException (com.netflix.metacat.common.server.connectors.exception.NotFoundException)8 Metrics (com.netflix.metacat.common.server.monitoring.Metrics)8 UserMetadataService (com.netflix.metacat.common.server.usermetadata.UserMetadataService)8 MetacatContextManager (com.netflix.metacat.common.server.util.MetacatContextManager)8 Registry (com.netflix.spectator.api.Registry)8