All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arangodb.async.internal.ArangoDatabaseAsyncImpl Maven / Gradle / Ivy

There is a newer version: 7.13.0
Show newest version
/*
 * DISCLAIMER
 *
 * Copyright 2016 ArangoDB GmbH, Cologne, Germany
 *
 * Licensed 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.
 *
 * Copyright holder is ArangoDB GmbH, Cologne, Germany
 */

package com.arangodb.async.internal;

import com.arangodb.ArangoDBException;
import com.arangodb.DbName;
import com.arangodb.async.*;
import com.arangodb.entity.*;
import com.arangodb.entity.arangosearch.analyzer.SearchAnalyzer;
import com.arangodb.internal.ArangoCursorExecute;
import com.arangodb.internal.ArangoErrors;
import com.arangodb.internal.InternalArangoDatabase;
import com.arangodb.internal.cursor.entity.InternalCursorEntity;
import com.arangodb.internal.net.HostHandle;
import com.arangodb.internal.util.DocumentUtil;
import com.arangodb.model.*;
import com.arangodb.model.arangosearch.AnalyzerDeleteOptions;
import com.arangodb.model.arangosearch.ArangoSearchCreateOptions;
import com.arangodb.model.arangosearch.SearchAliasCreateOptions;
import com.arangodb.internal.InternalRequest;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;

import static com.arangodb.internal.serde.SerdeUtils.constructListType;

/**
 * @author Mark Vollmary
 * @author Michele Rastelli
 */
public class ArangoDatabaseAsyncImpl extends InternalArangoDatabase
        implements ArangoDatabaseAsync {

    ArangoDatabaseAsyncImpl(final ArangoDBAsyncImpl arangoDB, final DbName name) {
        super(arangoDB, name);
    }

    @Override
    public CompletableFuture getVersion() {
        return executor.execute(getVersionRequest(), ArangoDBVersion.class);
    }

    @Override
    public CompletableFuture getEngine() {
        return executor.execute(getEngineRequest(), ArangoDBEngine.class);
    }

    @Override
    public CompletableFuture exists() {
        return getInfo()
                .handle((result, ex) -> {
                    if (result != null) {
                        return true;
                    }

                    if (ex instanceof CompletionException && ex.getCause() instanceof ArangoDBException) {
                        ArangoDBException e = (ArangoDBException) ex.getCause();
                        if (ArangoErrors.ERROR_ARANGO_DATABASE_NOT_FOUND.equals(e.getErrorNum())) {
                            return false;
                        }
                    }

                    throw new CompletionException(ex);
                });
    }

    @Override
    public CompletableFuture> getAccessibleDatabases() {
        return executor.execute(getAccessibleDatabasesRequest(), getDatabaseResponseDeserializer());
    }

    @Override
    public ArangoCollectionAsync collection(final String name) {
        return new ArangoCollectionAsyncImpl(this, name);
    }

    @Override
    public CompletableFuture createCollection(final String name) {
        return executor.execute(createCollectionRequest(name, new CollectionCreateOptions()), CollectionEntity.class);
    }

    @Override
    public CompletableFuture createCollection(
            final String name,
            final CollectionCreateOptions options) {
        return executor.execute(createCollectionRequest(name, options), CollectionEntity.class);
    }

    @Override
    public CompletableFuture> getCollections() {
        return executor.execute(getCollectionsRequest(new CollectionsReadOptions()),
                getCollectionsResponseDeserializer());
    }

    @Override
    public CompletableFuture> getCollections(final CollectionsReadOptions options) {
        return executor.execute(getCollectionsRequest(options), getCollectionsResponseDeserializer());
    }

    @Override
    public CompletableFuture getIndex(final String id) {
        DocumentUtil.validateIndexId(id);
        final String[] split = id.split("/");
        return collection(split[0]).getIndex(split[1]);
    }

    @Override
    public CompletableFuture deleteIndex(final String id) {
        DocumentUtil.validateIndexId(id);
        final String[] split = id.split("/");
        return collection(split[0]).deleteIndex(split[1]);
    }

    @Override
    public CompletableFuture create() {
        return arango().createDatabase(dbName());
    }

    @Override
    public CompletableFuture drop() {
        return executor.execute(dropRequest(), createDropResponseDeserializer());
    }

    @Override
    public CompletableFuture grantAccess(final String user, final Permissions permissions) {
        return executor.execute(grantAccessRequest(user, permissions), Void.class);
    }

    @Override
    public CompletableFuture grantAccess(final String user) {
        return executor.execute(grantAccessRequest(user, Permissions.RW), Void.class);
    }

    @Override
    public CompletableFuture revokeAccess(final String user) {
        return executor.execute(grantAccessRequest(user, Permissions.NONE), Void.class);
    }

    @Override
    public CompletableFuture resetAccess(final String user) {
        return executor.execute(resetAccessRequest(user), Void.class);
    }

    @Override
    public CompletableFuture grantDefaultCollectionAccess(final String user, final Permissions permissions) {
        return executor.execute(updateUserDefaultCollectionAccessRequest(user, permissions), Void.class);
    }

    @Override
    public CompletableFuture getPermissions(final String user) {
        return executor.execute(getPermissionsRequest(user), getPermissionsResponseDeserialzer());
    }

    @Override
    public  CompletableFuture> query(
            final String query,
            final Map bindVars,
            final AqlQueryOptions options,
            final Class type) {
        final InternalRequest request = queryRequest(query, bindVars, options);
        final HostHandle hostHandle = new HostHandle();
        final CompletableFuture execution = executor.execute(request,
                InternalCursorEntity.class, hostHandle);
        return execution.thenApply(result -> createCursor(result, type, options, hostHandle));
    }

    @Override
    public  CompletableFuture> query(
            final String query,
            final AqlQueryOptions options,
            final Class type) {
        return query(query, null, options, type);
    }

    @Override
    public  CompletableFuture> query(
            final String query,
            final Map bindVars,
            final Class type) {
        return query(query, bindVars, null, type);
    }

    @Override
    public  CompletableFuture> query(final String query, final Class type) {
        return query(query, null, null, type);
    }

    @Override
    public  CompletableFuture> cursor(final String cursorId, final Class type) {
        final HostHandle hostHandle = new HostHandle();
        final CompletableFuture execution = executor.execute(queryNextRequest(cursorId, null,
                null), InternalCursorEntity.class, hostHandle);
        return execution.thenApply(result -> createCursor(result, type, null, hostHandle));
    }

    private  ArangoCursorAsync createCursor(
            final InternalCursorEntity result,
            final Class type,
            final AqlQueryOptions options,
            final HostHandle hostHandle) {
        return new ArangoCursorAsyncImpl<>(this, new ArangoCursorExecute() {
            @Override
            public InternalCursorEntity next(final String id, Map meta) {
                final CompletableFuture result = executor.execute(queryNextRequest(id, options,
                                meta),
                        InternalCursorEntity.class, hostHandle);
                try {
                    return result.get();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw new ArangoDBException(e);
                } catch (ExecutionException e) {
                    throw new ArangoDBException(e);
                }
            }

            @Override
            public void close(final String id, Map meta) {
                try {
                    executor.execute(queryCloseRequest(id, options, meta), Void.class, hostHandle).get();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw new ArangoDBException(e);
                } catch (ExecutionException e) {
                    throw new ArangoDBException(e);
                }
            }
        }, type, result);
    }

    @Override
    public CompletableFuture explainQuery(
            final String query,
            final Map bindVars,
            final AqlQueryExplainOptions options) {
        return executor.execute(explainQueryRequest(query, bindVars, options), AqlExecutionExplainEntity.class);
    }

    @Override
    public CompletableFuture parseQuery(final String query) {
        return executor.execute(parseQueryRequest(query), AqlParseEntity.class);
    }

    @Override
    public CompletableFuture clearQueryCache() {
        return executor.execute(clearQueryCacheRequest(), Void.class);
    }

    @Override
    public CompletableFuture getQueryCacheProperties() {
        return executor.execute(getQueryCachePropertiesRequest(), QueryCachePropertiesEntity.class);
    }

    @Override
    public CompletableFuture setQueryCacheProperties(
            final QueryCachePropertiesEntity properties) {
        return executor.execute(setQueryCachePropertiesRequest(properties), QueryCachePropertiesEntity.class);
    }

    @Override
    public CompletableFuture getQueryTrackingProperties() {
        return executor.execute(getQueryTrackingPropertiesRequest(), QueryTrackingPropertiesEntity.class);
    }

    @Override
    public CompletableFuture setQueryTrackingProperties(
            final QueryTrackingPropertiesEntity properties) {
        return executor.execute(setQueryTrackingPropertiesRequest(properties), QueryTrackingPropertiesEntity.class);
    }

    @Override
    public CompletableFuture> getCurrentlyRunningQueries() {
        return executor.execute(getCurrentlyRunningQueriesRequest(),
                constructListType(QueryEntity.class));
    }

    @Override
    public CompletableFuture> getSlowQueries() {
        return executor.execute(getSlowQueriesRequest(),
                constructListType(QueryEntity.class));
    }

    @Override
    public CompletableFuture clearSlowQueries() {
        return executor.execute(clearSlowQueriesRequest(), Void.class);
    }

    @Override
    public CompletableFuture killQuery(final String id) {
        return executor.execute(killQueryRequest(id), Void.class);
    }

    @Override
    public CompletableFuture createAqlFunction(
            final String name,
            final String code,
            final AqlFunctionCreateOptions options) {
        return executor.execute(createAqlFunctionRequest(name, code, options), Void.class);

    }

    @Override
    public CompletableFuture deleteAqlFunction(final String name, final AqlFunctionDeleteOptions options) {
        return executor.execute(deleteAqlFunctionRequest(name, options), deleteAqlFunctionResponseDeserializer());
    }

    @Override
    public CompletableFuture> getAqlFunctions(final AqlFunctionGetOptions options) {
        return executor.execute(getAqlFunctionsRequest(options), getAqlFunctionsResponseDeserializer());
    }

    @Override
    public ArangoGraphAsync graph(final String name) {
        return new ArangoGraphAsyncImpl(this, name);
    }

    @Override
    public CompletableFuture createGraph(
            final String name,
            final Collection edgeDefinitions) {
        return executor.execute(createGraphRequest(name, edgeDefinitions, new GraphCreateOptions()),
                createGraphResponseDeserializer());
    }

    @Override
    public CompletableFuture createGraph(
            final String name,
            final Collection edgeDefinitions,
            final GraphCreateOptions options) {
        return executor.execute(createGraphRequest(name, edgeDefinitions, options), createGraphResponseDeserializer());
    }

    @Override
    public CompletableFuture> getGraphs() {
        return executor.execute(getGraphsRequest(), getGraphsResponseDeserializer());
    }

    @Override
    public  CompletableFuture transaction(
            final String action,
            final Class type,
            final TransactionOptions options) {
        return executor.execute(transactionRequest(action, options), transactionResponseDeserializer(type));
    }

    @Override
    public CompletableFuture beginStreamTransaction(StreamTransactionOptions options) {
        return executor.execute(beginStreamTransactionRequest(options), streamTransactionResponseDeserializer());
    }

    @Override
    public CompletableFuture abortStreamTransaction(String id) {
        return executor.execute(abortStreamTransactionRequest(id), streamTransactionResponseDeserializer());
    }

    @Override
    public CompletableFuture getStreamTransaction(String id) {
        return executor.execute(getStreamTransactionRequest(id), streamTransactionResponseDeserializer());
    }

    @Override
    public CompletableFuture> getStreamTransactions() {
        return executor.execute(getStreamTransactionsRequest(), transactionsResponseDeserializer());
    }

    @Override
    public CompletableFuture commitStreamTransaction(String id) {
        return executor.execute(commitStreamTransactionRequest(id), streamTransactionResponseDeserializer());
    }

    @Override
    public CompletableFuture getInfo() {
        return executor.execute(getInfoRequest(), getInfoResponseDeserializer());
    }

    @Override
    public  CompletableFuture getDocument(final String id, final Class type) {
        DocumentUtil.validateDocumentId(id);
        final String[] split = id.split("/");
        return collection(split[0]).getDocument(split[1], type);
    }

    @Override
    public  CompletableFuture getDocument(final String id, final Class type,
                                                final DocumentReadOptions options) {
        DocumentUtil.validateDocumentId(id);
        final String[] split = id.split("/");
        return collection(split[0]).getDocument(split[1], type, options);
    }

    @Override
    public CompletableFuture reloadRouting() {
        return executor.execute(reloadRoutingRequest(), Void.class);
    }

    @Override
    public ArangoRouteAsync route(final String... path) {
        return new ArangoRouteAsyncImpl(this, String.join("/", path), Collections.emptyMap());
    }

    @Override
    public CompletableFuture> getViews() {
        return executor.execute(getViewsRequest(), getViewsResponseDeserializer());
    }

    @Override
    public ArangoViewAsync view(final String name) {
        return new ArangoViewAsyncImpl(this, name);
    }

    @Override
    public ArangoSearchAsync arangoSearch(final String name) {
        return new ArangoSearchAsyncImpl(this, name);
    }

    @Override
    public SearchAliasAsync searchAlias(String name) {
        return new SearchAliasAsyncImpl(this, name);
    }

    @Override
    public CompletableFuture createView(final String name, final ViewType type) {
        return executor.execute(createViewRequest(name, type), ViewEntity.class);
    }

    @Override
    public CompletableFuture createArangoSearch(final String name,
                                                            final ArangoSearchCreateOptions options) {
        return executor.execute(createArangoSearchRequest(name, options), ViewEntity.class);
    }

    @Override
    public CompletableFuture createSearchAlias(String name, SearchAliasCreateOptions options) {
        return executor.execute(createSearchAliasRequest(name, options), ViewEntity.class);
    }

    @Override
    public CompletableFuture createSearchAnalyzer(SearchAnalyzer analyzer) {
        return executor.execute(createAnalyzerRequest(analyzer), SearchAnalyzer.class);
    }

    @Override
    public CompletableFuture getSearchAnalyzer(String name) {
        return executor.execute(getAnalyzerRequest(name), SearchAnalyzer.class);
    }

    @Override
    public CompletableFuture> getSearchAnalyzers() {
        return executor.execute(getAnalyzersRequest(), getSearchAnalyzersResponseDeserializer());
    }

    @Override
    public CompletableFuture deleteSearchAnalyzer(String name) {
        return deleteSearchAnalyzer(name, null);
    }

    @Override
    public CompletableFuture deleteSearchAnalyzer(String name, AnalyzerDeleteOptions options) {
        return executor.execute(deleteAnalyzerRequest(name, options), Void.class);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy