
rapture.kernel.StructuredApiImpl Maven / Gradle / Ivy
/**
* The MIT License (MIT)
*
* Copyright (c) 2011-2016 Incapture Technologies LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package rapture.kernel;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;
import com.google.common.collect.ImmutableList;
import rapture.common.ForeignKey;
import rapture.common.StoredProcedureParams;
import rapture.common.StoredProcedureResponse;
import rapture.common.CallingContext;
import rapture.common.RaptureURI;
import rapture.common.Scheme;
import rapture.common.StructuredRepoConfig;
import rapture.common.StructuredRepoConfigStorage;
import rapture.common.TableIndex;
import rapture.common.api.StructuredApi;
import rapture.common.exception.RaptureExceptionFactory;
import rapture.repo.StructuredRepo;
import rapture.structured.DefaultValidator;
import rapture.structured.Validator;
import javax.annotation.Nullable;
public class StructuredApiImpl extends KernelBase implements StructuredApi {
private Validator validator;
public StructuredApiImpl(Kernel raptureKernel) {
super(raptureKernel);
validator = new DefaultValidator();
}
@Override
public void createStructuredRepo(CallingContext context, String repoURI, String config) {
checkParameter("URI", repoURI);
checkParameter("Config", config);
RaptureURI internalURI = new RaptureURI(repoURI, Scheme.STRUCTURED);
String authority = internalURI.getAuthority();
if ((authority == null) || authority.isEmpty()) {
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, apiMessageCatalog.getMessage("NoAuthority")); //$NON-NLS-1$
}
if (internalURI.hasDocPath()) {
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, apiMessageCatalog.getMessage("NoDocPath", repoURI)); //$NON-NLS-1$
}
// TODO write a config validator
if (structuredRepoExists(context, repoURI)) {
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, apiMessageCatalog.getMessage("Exists", internalURI.toShortString())); //$NON-NLS-1$
}
StructuredRepoConfig structured = new StructuredRepoConfig();
structured.setAuthority(authority);
structured.setConfig(config);
StructuredRepoConfigStorage.add(structured, context.getUser(), "Create Structured repository");
}
@Override
public void deleteStructuredRepo(CallingContext context, String repoURI) {
RaptureURI uri = new RaptureURI(repoURI, Scheme.STRUCTURED);
if (uri.hasDocPath()) {
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, apiMessageCatalog.getMessage("NoDocPath", repoURI)); //$NON-NLS-1$
}
getRepoOrFail(uri.getAuthority()).drop();
removeRepoFromCache(uri.getAuthority());
StructuredRepoConfigStorage.deleteByAddress(uri, context.getUser(), "Delete structured repo");
}
@Override
public Boolean structuredRepoExists(CallingContext context, String repoURI) {
RaptureURI uri = new RaptureURI(repoURI, Scheme.STRUCTURED);
if (uri.hasDocPath()) {
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, apiMessageCatalog.getMessage("NoDocPath", repoURI)); //$NON-NLS-1$
}
return getRepoFromCache(uri.getAuthority()) != null;
}
@Override
public StructuredRepoConfig getStructuredRepoConfig(CallingContext ctx, String uriStr) {
RaptureURI uri = new RaptureURI(uriStr, Scheme.STRUCTURED);
if (uri.hasDocPath()) {
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST,
"Structured repository URI " + uriStr + " can't have a doc path: '" + uri.getDocPath() + "'");
}
return StructuredRepoConfigStorage.readByAddress(uri);
}
@Override
public List getStructuredRepoConfigs(CallingContext context) {
return StructuredRepoConfigStorage.readAll();
}
private StructuredRepo getRepoOrFail(String authority) {
StructuredRepo repo = getRepoFromCache(authority);
if (repo == null) {
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, apiMessageCatalog.getMessage("NoSuchRepo", Scheme.STRUCTURED+"://"+authority)); //$NON-NLS-1$
} else {
return repo;
}
}
private StructuredRepo getRepoFromCache(String authority) {
return Kernel.getRepoCacheManager().getStructuredRepo(authority);
}
private void removeRepoFromCache(String authority) {
Kernel.getRepoCacheManager().removeRepo(Scheme.STRUCTURED.toString(), authority);
}
@Override
public void createTableUsingSql(CallingContext context, String schema, String rawSql) {
StructuredRepo repo = getRepoOrFail(schema);
registerWithTxManager(context, repo);
try {
repo.createTableUsingSql(context, rawSql);
} catch (Exception e) {
TransactionManager.transactionFailed(getTxId(context));
throw RaptureExceptionFactory.create(e.getMessage(), e.getCause());
}
}
@Override
public void createTable(CallingContext context, String tableUri, Map columns) {
RaptureURI uri = new RaptureURI(tableUri, Scheme.STRUCTURED);
// We do not allow tables with dots in them. Standard SQL does not allow this and we won't either.
// The complexity involved to go back and forth and text-parse is not worth it.
validateTable(uri.getDocPath());
validateColumns(columns.keySet());
StructuredRepo repo = getRepoOrFail(uri.getAuthority());
registerWithTxManager(context, repo);
try {
repo.createTable(uri.getDocPath(), columns);
} catch (Exception e) {
TransactionManager.transactionFailed(getTxId(context));
throw RaptureExceptionFactory.create(e.getMessage(), e.getCause());
}
}
@Override
public void dropTable(CallingContext context, String tableUri) {
RaptureURI uri = new RaptureURI(tableUri, Scheme.STRUCTURED);
StructuredRepo repo = getRepoOrFail(uri.getAuthority());
registerWithTxManager(context, repo);
try {
repo.dropTable(uri.getDocPath());
} catch (Exception e) {
TransactionManager.transactionFailed(getTxId(context));
throw RaptureExceptionFactory.create(e.getMessage(), e.getCause());
}
}
@Override
public Boolean tableExists(CallingContext context, String tableUri) {
RaptureURI uri = new RaptureURI(tableUri, Scheme.STRUCTURED);
return getRepoOrFail(uri.getAuthority()).tableExists(uri.getDocPath());
}
@Override
public List getSchemas(CallingContext context) {
List configs = getStructuredRepoConfigs(context);
return Lists.transform(configs, new Function() {
@Nullable
@Override
public String apply(StructuredRepoConfig structuredRepoConfig) {
return structuredRepoConfig.getAuthority();
}
});
}
@Override
public List getTables(CallingContext context, String repoURI) {
RaptureURI uri = new RaptureURI(repoURI, Scheme.STRUCTURED);
if (uri.hasDocPath()) {
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, apiMessageCatalog.getMessage("NoDocPath", repoURI)); //$NON-NLS-1$
}
return getRepoOrFail(uri.getAuthority()).getTables();
}
@Override
public Map describeTable(CallingContext context, String tableUri) {
RaptureURI uri = new RaptureURI(tableUri, Scheme.STRUCTURED);
return getRepoOrFail(uri.getAuthority()).describeTable(uri.getDocPath()).getRows();
}
@Override
public void addTableColumns(CallingContext context, String tableUri, Map columns) {
RaptureURI uri = new RaptureURI(tableUri, Scheme.STRUCTURED);
validateColumns(columns.keySet());
StructuredRepo repo = getRepoOrFail(uri.getAuthority());
registerWithTxManager(context, repo);
try {
repo.addTableColumns(uri.getDocPath(), columns);
} catch (Exception e) {
TransactionManager.transactionFailed(getTxId(context));
throw RaptureExceptionFactory.create(e.getMessage(), e.getCause());
}
}
@Override
public void deleteTableColumns(CallingContext context, String tableUri, List columnNames) {
RaptureURI uri = new RaptureURI(tableUri, Scheme.STRUCTURED);
StructuredRepo repo = getRepoOrFail(uri.getAuthority());
registerWithTxManager(context, repo);
try {
repo.deleteTableColumns(uri.getDocPath(), columnNames);
} catch (Exception e) {
TransactionManager.transactionFailed(getTxId(context));
throw RaptureExceptionFactory.create(e.getMessage(), e.getCause());
}
}
@Override
public void updateTableColumns(CallingContext context, String tableUri, Map columns) {
RaptureURI uri = new RaptureURI(tableUri, Scheme.STRUCTURED);
validateColumns(columns.keySet());
StructuredRepo repo = getRepoOrFail(uri.getAuthority());
registerWithTxManager(context, repo);
try {
repo.updateTableColumns(uri.getDocPath(), columns);
} catch (Exception e) {
TransactionManager.transactionFailed(getTxId(context));
throw RaptureExceptionFactory.create(e.getMessage(), e.getCause());
}
}
@Override
public void renameTableColumns(CallingContext context, String tableUri, Map columnNames) {
RaptureURI uri = new RaptureURI(tableUri, Scheme.STRUCTURED);
StructuredRepo repo = getRepoOrFail(uri.getAuthority());
registerWithTxManager(context, repo);
try {
repo.renameTableColumns(uri.getDocPath(), columnNames);
} catch (Exception e) {
TransactionManager.transactionFailed(getTxId(context));
throw RaptureExceptionFactory.create(e.getMessage(), e.getCause());
}
}
@Override
public void createIndex(CallingContext context, String tableUri, String indexName, List columnNames) {
RaptureURI uri = new RaptureURI(tableUri, Scheme.STRUCTURED);
StructuredRepo repo = getRepoOrFail(uri.getAuthority());
registerWithTxManager(context, repo);
try {
repo.createIndex(uri.getDocPath(), indexName, columnNames);
} catch (Exception e) {
TransactionManager.transactionFailed(getTxId(context));
throw RaptureExceptionFactory.create(e.getMessage(), e.getCause());
}
}
@Override
public void dropIndex(CallingContext context, String tableUri, String indexName) {
RaptureURI uri = new RaptureURI(tableUri, Scheme.STRUCTURED);
StructuredRepo repo = getRepoOrFail(uri.getAuthority());
registerWithTxManager(context, repo);
try {
repo.dropIndex(indexName);
} catch (Exception e) {
TransactionManager.transactionFailed(getTxId(context));
throw RaptureExceptionFactory.create(e.getMessage(), e.getCause());
}
}
@Override
public List getIndexes(CallingContext context, String tableUri) {
RaptureURI uri = new RaptureURI(tableUri, Scheme.STRUCTURED);
StructuredRepo repo = getRepoOrFail(uri.getAuthority());
return repo.getIndexes(uri.getDocPath());
}
@Override
public String getPrimaryKey(CallingContext context, String tableUri) {
RaptureURI uri = new RaptureURI(tableUri, Scheme.STRUCTURED);
StructuredRepo repo = getRepoOrFail(uri.getAuthority());
return repo.getPrimaryKey(uri.getDocPath());
}
@Override
public List getForeignKeys(CallingContext context, String tableUri) {
RaptureURI uri = new RaptureURI(tableUri, Scheme.STRUCTURED);
StructuredRepo repo = getRepoOrFail(uri.getAuthority());
return repo.getForeignKeys(uri.getDocPath());
}
@Override
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy