Search in sources :

Example 1 with SchemaInfo

use of io.crate.metadata.table.SchemaInfo in project crate by crate.

the class CreateSnapshotAnalyzer method analyze.

public CreateSnapshotAnalyzedStatement analyze(CreateSnapshot node, Analysis analysis) {
    Optional<QualifiedName> repositoryName = node.name().getPrefix();
    // validate repository
    Preconditions.checkArgument(repositoryName.isPresent(), "Snapshot must be specified by \"<repository_name>\".\"<snapshot_name>\"");
    Preconditions.checkArgument(repositoryName.get().getParts().size() == 1, String.format(Locale.ENGLISH, "Invalid repository name '%s'", repositoryName.get()));
    repositoryService.failIfRepositoryDoesNotExist(repositoryName.get().toString());
    // snapshot existence in repo is validated upon execution
    String snapshotName = node.name().getSuffix();
    SnapshotId snapshotId = new SnapshotId(repositoryName.get().toString(), snapshotName);
    // validate and extract settings
    Settings settings = GenericPropertiesConverter.settingsFromProperties(node.properties(), analysis.parameterContext(), SETTINGS).build();
    boolean ignoreUnavailable = settings.getAsBoolean(IGNORE_UNAVAILABLE.name(), IGNORE_UNAVAILABLE.defaultValue());
    // iterate tables
    if (node.tableList().isPresent()) {
        List<Table> tableList = node.tableList().get();
        Set<String> snapshotIndices = new HashSet<>(tableList.size());
        for (Table table : tableList) {
            TableInfo tableInfo;
            try {
                tableInfo = schemas.getTableInfo(TableIdent.of(table, analysis.sessionContext().defaultSchema()));
            } catch (ResourceUnknownException e) {
                if (ignoreUnavailable) {
                    LOGGER.info("ignoring: {}", e.getMessage());
                    continue;
                } else {
                    throw e;
                }
            }
            if (!(tableInfo instanceof DocTableInfo)) {
                throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot create snapshot of tables in schema '%s'", tableInfo.ident().schema()));
            }
            Operation.blockedRaiseException(tableInfo, Operation.READ);
            DocTableInfo docTableInfo = (DocTableInfo) tableInfo;
            if (table.partitionProperties().isEmpty()) {
                snapshotIndices.addAll(Arrays.asList(docTableInfo.concreteIndices()));
            } else {
                PartitionName partitionName = PartitionPropertiesAnalyzer.toPartitionName(docTableInfo, table.partitionProperties(), analysis.parameterContext().parameters());
                if (!docTableInfo.partitions().contains(partitionName)) {
                    if (!ignoreUnavailable) {
                        throw new PartitionUnknownException(tableInfo.ident().fqn(), partitionName.ident());
                    } else {
                        LOGGER.info("ignoring unknown partition of table '{}' with ident '{}'", partitionName.tableIdent(), partitionName.ident());
                    }
                } else {
                    snapshotIndices.add(partitionName.asIndexName());
                }
            }
        }
        /**
             * For now, we always (in case there are indices to restore) include the globalMetaData,
             * not only if one of the tables in the table list is partitioned.
             * Previously we only included it in snapshots of full partitioned tables.
             * However, to make restoring of shapshots of single partitions work
             * we also need to include the global metadata (index templates).
             */
        return CreateSnapshotAnalyzedStatement.forTables(snapshotId, settings, ImmutableList.copyOf(snapshotIndices), !snapshotIndices.isEmpty());
    } else {
        for (SchemaInfo schemaInfo : schemas) {
            for (TableInfo tableInfo : schemaInfo) {
                // only check for user generated tables
                if (tableInfo instanceof DocTableInfo) {
                    Operation.blockedRaiseException(tableInfo, Operation.READ);
                }
            }
        }
        return CreateSnapshotAnalyzedStatement.all(snapshotId, settings);
    }
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) Table(io.crate.sql.tree.Table) QualifiedName(io.crate.sql.tree.QualifiedName) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException) PartitionName(io.crate.metadata.PartitionName) SnapshotId(org.elasticsearch.cluster.metadata.SnapshotId) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) Settings(org.elasticsearch.common.settings.Settings) SchemaInfo(io.crate.metadata.table.SchemaInfo)

Example 2 with SchemaInfo

use of io.crate.metadata.table.SchemaInfo in project crate by crate.

the class DocIndexMetaDataTest method getDocIndexMetaDataFromStatement.

private DocIndexMetaData getDocIndexMetaDataFromStatement(String stmt) throws IOException {
    Statement statement = SqlParser.createStatement(stmt);
    ClusterService clusterService = new NoopClusterService();
    final TransportPutIndexTemplateAction transportPutIndexTemplateAction = mock(TransportPutIndexTemplateAction.class);
    Provider<TransportPutIndexTemplateAction> indexTemplateActionProvider = new Provider<TransportPutIndexTemplateAction>() {

        @Override
        public TransportPutIndexTemplateAction get() {
            return transportPutIndexTemplateAction;
        }
    };
    DocTableInfoFactory docTableInfoFactory = new InternalDocTableInfoFactory(functions, new IndexNameExpressionResolver(Settings.EMPTY), indexTemplateActionProvider, executorService);
    DocSchemaInfo docSchemaInfo = new DocSchemaInfo(Schemas.DEFAULT_SCHEMA_NAME, clusterService, docTableInfoFactory);
    CreateTableStatementAnalyzer analyzer = new CreateTableStatementAnalyzer(new Schemas(Settings.EMPTY, ImmutableMap.<String, SchemaInfo>of("doc", docSchemaInfo), clusterService, new DocSchemaInfoFactory(docTableInfoFactory)), new FulltextAnalyzerResolver(clusterService, new IndicesAnalysisService(Settings.EMPTY)), functions, new NumberOfShards(clusterService));
    Analysis analysis = new Analysis(SessionContext.SYSTEM_SESSION, ParameterContext.EMPTY, ParamTypeHints.EMPTY);
    CreateTableAnalyzedStatement analyzedStatement = analyzer.analyze((CreateTable) statement, analysis.parameterContext(), analysis.sessionContext());
    Settings.Builder settingsBuilder = Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).put("index.version.created", org.elasticsearch.Version.CURRENT).put(analyzedStatement.tableParameter().settings());
    IndexMetaData indexMetaData = IndexMetaData.builder(analyzedStatement.tableIdent().name()).settings(settingsBuilder).putMapping(new MappingMetaData(Constants.DEFAULT_MAPPING_TYPE, analyzedStatement.mapping())).build();
    return newMeta(indexMetaData, analyzedStatement.tableIdent().name());
}
Also used : IndicesAnalysisService(org.elasticsearch.indices.analysis.IndicesAnalysisService) Statement(io.crate.sql.tree.Statement) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) Provider(org.elasticsearch.common.inject.Provider) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) NoopClusterService(org.elasticsearch.test.cluster.NoopClusterService) ClusterService(org.elasticsearch.cluster.ClusterService) TransportPutIndexTemplateAction(org.elasticsearch.action.admin.indices.template.put.TransportPutIndexTemplateAction) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) NoopClusterService(org.elasticsearch.test.cluster.NoopClusterService) Settings(org.elasticsearch.common.settings.Settings) SchemaInfo(io.crate.metadata.table.SchemaInfo)

Example 3 with SchemaInfo

use of io.crate.metadata.table.SchemaInfo in project crate by crate.

the class Schemas method getTableInfo.

/**
 * @param ident the table ident to get a TableInfo for
 * @return an instance of TableInfo for the given ident, guaranteed to be not null
 * @throws io.crate.exceptions.SchemaUnknownException if schema given in <code>ident</code>
 *                                                    does not exist
 * @throws RelationUnknown  if table given in <code>ident</code> does
 *                                                    not exist in the given schema
 */
public <T extends TableInfo> T getTableInfo(RelationName ident) {
    SchemaInfo schemaInfo = getSchemaInfo(ident);
    TableInfo info = schemaInfo.getTableInfo(ident.name());
    if (info == null) {
        throw new RelationUnknown(ident);
    }
    return (T) info;
}
Also used : RelationUnknown(io.crate.exceptions.RelationUnknown) TableInfo(io.crate.metadata.table.TableInfo) BlobSchemaInfo(io.crate.metadata.blob.BlobSchemaInfo) PgCatalogSchemaInfo(io.crate.metadata.pgcatalog.PgCatalogSchemaInfo) SysSchemaInfo(io.crate.metadata.sys.SysSchemaInfo) SchemaInfo(io.crate.metadata.table.SchemaInfo) InformationSchemaInfo(io.crate.metadata.information.InformationSchemaInfo)

Example 4 with SchemaInfo

use of io.crate.metadata.table.SchemaInfo in project crate by crate.

the class Schemas method resolveView.

/**
 * @throws RelationUnknown if the view cannot be resolved against the search path.
 */
public Tuple<ViewMetadata, RelationName> resolveView(QualifiedName ident, SearchPath searchPath) {
    ViewsMetadata views = clusterService.state().metadata().custom(ViewsMetadata.TYPE);
    ViewMetadata view = null;
    RelationName viewRelationName = null;
    String identSchema = schemaName(ident);
    String viewName = relationName(ident);
    if (views != null) {
        if (identSchema == null) {
            for (String pathSchema : searchPath) {
                SchemaInfo schemaInfo = schemas.get(pathSchema);
                if (schemaInfo != null) {
                    viewRelationName = new RelationName(pathSchema, viewName);
                    view = views.getView(viewRelationName);
                    if (view != null) {
                        break;
                    }
                }
            }
        } else {
            viewRelationName = new RelationName(identSchema, viewName);
            view = views.getView(viewRelationName);
        }
    }
    if (view == null) {
        throw new RelationUnknown(viewName);
    }
    return Tuple.tuple(view, viewRelationName);
}
Also used : RelationUnknown(io.crate.exceptions.RelationUnknown) ViewsMetadata(io.crate.metadata.view.ViewsMetadata) ViewMetadata(io.crate.metadata.view.ViewMetadata) BlobSchemaInfo(io.crate.metadata.blob.BlobSchemaInfo) PgCatalogSchemaInfo(io.crate.metadata.pgcatalog.PgCatalogSchemaInfo) SysSchemaInfo(io.crate.metadata.sys.SysSchemaInfo) SchemaInfo(io.crate.metadata.table.SchemaInfo) InformationSchemaInfo(io.crate.metadata.information.InformationSchemaInfo)

Example 5 with SchemaInfo

use of io.crate.metadata.table.SchemaInfo in project crate by crate.

the class Schemas method clusterChanged.

@Override
public void clusterChanged(ClusterChangedEvent event) {
    if (!event.metadataChanged()) {
        return;
    }
    Set<String> newCurrentSchemas = getNewCurrentSchemas(event.state().metadata());
    synchronized (schemas) {
        Set<String> nonBuiltInSchemas = Sets.difference(schemas.keySet(), builtInSchemas.keySet());
        Set<String> deleted = Sets.difference(nonBuiltInSchemas, newCurrentSchemas);
        Set<String> added = Sets.difference(newCurrentSchemas, schemas.keySet());
        for (String deletedSchema : deleted) {
            try {
                schemas.remove(deletedSchema).close();
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
        for (String addedSchema : added) {
            schemas.put(addedSchema, getCustomSchemaInfo(addedSchema));
        }
        // update all existing schemas
        for (SchemaInfo schemaInfo : this) {
            schemaInfo.update(event);
        }
    }
}
Also used : SchemaUnknownException(io.crate.exceptions.SchemaUnknownException) BlobSchemaInfo(io.crate.metadata.blob.BlobSchemaInfo) PgCatalogSchemaInfo(io.crate.metadata.pgcatalog.PgCatalogSchemaInfo) SysSchemaInfo(io.crate.metadata.sys.SysSchemaInfo) SchemaInfo(io.crate.metadata.table.SchemaInfo) InformationSchemaInfo(io.crate.metadata.information.InformationSchemaInfo)

Aggregations

SchemaInfo (io.crate.metadata.table.SchemaInfo)16 BlobSchemaInfo (io.crate.metadata.blob.BlobSchemaInfo)10 InformationSchemaInfo (io.crate.metadata.information.InformationSchemaInfo)10 SysSchemaInfo (io.crate.metadata.sys.SysSchemaInfo)10 TableInfo (io.crate.metadata.table.TableInfo)10 PgCatalogSchemaInfo (io.crate.metadata.pgcatalog.PgCatalogSchemaInfo)7 DocTableInfo (io.crate.metadata.doc.DocTableInfo)6 RelationUnknown (io.crate.exceptions.RelationUnknown)3 SchemaUnknownException (io.crate.exceptions.SchemaUnknownException)3 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)2 Row (io.crate.data.Row)2 PartitionUnknownException (io.crate.exceptions.PartitionUnknownException)2 ResourceUnknownException (io.crate.exceptions.ResourceUnknownException)2 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)2 PartitionName (io.crate.metadata.PartitionName)2 RelationName (io.crate.metadata.RelationName)2 Schemas (io.crate.metadata.Schemas)2 Table (io.crate.sql.tree.Table)2 Settings (org.elasticsearch.common.settings.Settings)2 Test (org.junit.Test)2