Search in sources :

Example 1 with PartitionAlreadyExistsException

use of io.crate.exceptions.PartitionAlreadyExistsException in project crate by crate.

the class RestoreSnapshotAnalyzer method analyze.

public RestoreSnapshotAnalyzedStatement analyze(RestoreSnapshot node, Analysis analysis) {
    List<String> nameParts = node.name().getParts();
    Preconditions.checkArgument(nameParts.size() == 2, "Snapshot name not supported, only <repository>.<snapshot> works.");
    String repositoryName = nameParts.get(0);
    repositoryService.failIfRepositoryDoesNotExist(repositoryName);
    // validate and extract settings
    Settings settings = GenericPropertiesConverter.settingsFromProperties(node.properties(), analysis.parameterContext(), SETTINGS).build();
    if (node.tableList().isPresent()) {
        List<Table> tableList = node.tableList().get();
        Set<RestoreSnapshotAnalyzedStatement.RestoreTableInfo> restoreTables = new HashSet<>(tableList.size());
        for (Table table : tableList) {
            TableIdent tableIdent = TableIdent.of(table, analysis.sessionContext().defaultSchema());
            boolean tableExists = schemas.tableExists(tableIdent);
            if (tableExists) {
                if (table.partitionProperties().isEmpty()) {
                    throw new TableAlreadyExistsException(tableIdent);
                }
                TableInfo tableInfo = schemas.getTableInfo(tableIdent);
                if (!(tableInfo instanceof DocTableInfo)) {
                    throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot restore snapshot of tables in schema '%s'", tableInfo.ident().schema()));
                }
                DocTableInfo docTableInfo = ((DocTableInfo) tableInfo);
                PartitionName partitionName = PartitionPropertiesAnalyzer.toPartitionName(tableIdent, docTableInfo, table.partitionProperties(), analysis.parameterContext().parameters());
                if (docTableInfo.partitions().contains(partitionName)) {
                    throw new PartitionAlreadyExistsException(partitionName);
                }
                restoreTables.add(new RestoreSnapshotAnalyzedStatement.RestoreTableInfo(tableIdent, partitionName));
            } else {
                if (table.partitionProperties().isEmpty()) {
                    restoreTables.add(new RestoreSnapshotAnalyzedStatement.RestoreTableInfo(tableIdent, null));
                } else {
                    PartitionName partitionName = PartitionPropertiesAnalyzer.toPartitionName(tableIdent, null, table.partitionProperties(), analysis.parameterContext().parameters());
                    restoreTables.add(new RestoreSnapshotAnalyzedStatement.RestoreTableInfo(tableIdent, partitionName));
                }
            }
        }
        return RestoreSnapshotAnalyzedStatement.forTables(nameParts.get(1), repositoryName, settings, ImmutableList.copyOf(restoreTables));
    } else {
        return RestoreSnapshotAnalyzedStatement.all(nameParts.get(1), repositoryName, settings);
    }
}
Also used : TableAlreadyExistsException(io.crate.exceptions.TableAlreadyExistsException) DocTableInfo(io.crate.metadata.doc.DocTableInfo) Table(io.crate.sql.tree.Table) TableIdent(io.crate.metadata.TableIdent) PartitionName(io.crate.metadata.PartitionName) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) Settings(org.elasticsearch.common.settings.Settings) PartitionAlreadyExistsException(io.crate.exceptions.PartitionAlreadyExistsException) HashSet(java.util.HashSet)

Aggregations

PartitionAlreadyExistsException (io.crate.exceptions.PartitionAlreadyExistsException)1 TableAlreadyExistsException (io.crate.exceptions.TableAlreadyExistsException)1 PartitionName (io.crate.metadata.PartitionName)1 TableIdent (io.crate.metadata.TableIdent)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1 TableInfo (io.crate.metadata.table.TableInfo)1 Table (io.crate.sql.tree.Table)1 HashSet (java.util.HashSet)1 Settings (org.elasticsearch.common.settings.Settings)1