blob: 962b0d5a4f3ac324fa5ea4e47fa0bca15ed5c7e9 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.asterix.bad.lang.statement;
import org.apache.asterix.algebra.extension.ExtensionStatement;
import org.apache.asterix.app.translator.QueryTranslator;
import org.apache.asterix.bad.extension.BADLangExtension;
import org.apache.asterix.bad.metadata.Broker;
import org.apache.asterix.common.exceptions.CompilationException;
import org.apache.asterix.common.metadata.DataverseName;
import org.apache.asterix.lang.common.struct.Identifier;
import org.apache.asterix.lang.common.visitor.base.ILangVisitor;
import org.apache.asterix.metadata.MetadataManager;
import org.apache.asterix.metadata.MetadataTransactionContext;
import org.apache.asterix.metadata.declared.MetadataProvider;
import org.apache.asterix.translator.IRequestParameters;
import org.apache.asterix.translator.IStatementExecutor;
import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
import org.apache.hyracks.api.client.IHyracksClientConnection;
import org.apache.hyracks.api.exceptions.HyracksDataException;
public class BrokerDropStatement extends ExtensionStatement {
private final DataverseName dataverseName;
private final Identifier brokerName;
private boolean ifExists;
public BrokerDropStatement(DataverseName dataverseName, Identifier brokerName, boolean ifExists) {
this.brokerName = brokerName;
this.dataverseName = dataverseName;
this.ifExists = ifExists;
}
public boolean getIfExists() {
return ifExists;
}
public DataverseName getDataverseName() {
return dataverseName;
}
public Identifier getBrokerName() {
return brokerName;
}
@Override
public byte getCategory() {
return Category.DDL;
}
@Override
public <R, T> R accept(ILangVisitor<R, T> visitor, T arg) throws CompilationException {
return null;
}
@Override
public String getName() {
return BrokerDropStatement.class.getName();
}
@Override
public void handle(IHyracksClientConnection hcc, IStatementExecutor statementExecutor,
IRequestParameters requestParameters, MetadataProvider metadataProvider, int resultSetId)
throws HyracksDataException, AlgebricksException {
//TODO: dont drop a broker that's being used
DataverseName dataverse = statementExecutor.getActiveDataverseName(dataverseName);
MetadataTransactionContext mdTxnCtx = null;
try {
mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
metadataProvider.setMetadataTxnContext(mdTxnCtx);
Broker broker = BADLangExtension.getBroker(mdTxnCtx, dataverse, brokerName.getValue());
if (broker == null) {
throw new AlgebricksException("A broker with this name " + brokerName + " doesn't exist.");
}
MetadataManager.INSTANCE.deleteEntity(mdTxnCtx, broker);
MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
} catch (Exception e) {
QueryTranslator.abort(e, e, mdTxnCtx);
throw HyracksDataException.create(e);
} finally {
metadataProvider.getLocks().unlock();
}
}
}