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

com.cognite.client.RawDatabases Maven / Gradle / Ivy

There is a newer version: 2.3.3
Show newest version
/*
 * Copyright (c) 2020 Cognite AS
 *
 * 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.
 */

package com.cognite.client;

import com.cognite.client.servicesV1.ConnectorServiceV1;
import com.cognite.client.servicesV1.ResponseItems;
import com.cognite.client.util.Partition;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.time.Instant;
import java.util.*;

/**
 * This class represents the Cognite raw databases api endpoint.
 *
 * It provides methods for interacting with the Raw service.
 */
@AutoValue
public abstract class RawDatabases extends ApiBase {

    private static Builder builder() {
        return new AutoValue_RawDatabases.Builder();
    }

    protected static final Logger LOG = LoggerFactory.getLogger(RawDatabases.class);

    /**
     * Constructs a new {@link RawDatabases} object using the provided client configuration.
     *
     * This method is intended for internal use--SDK clients should always use {@link CogniteClient}
     * as the entry point to this class.
     *
     * @param client The {@link CogniteClient} to use for configuration settings.
     * @return the assets api object.
     */
    public static RawDatabases of(CogniteClient client) {
        return RawDatabases.builder()
                .setClient(client)
                .build();
    }

    /**
     * Returns all database names.
     *
     * @return an {@link Iterator} to page through the db names.
     * @throws Exception
     */
    public Iterator> list() throws Exception {
        ConnectorServiceV1 connector = getClient().getConnectorService();
        ConnectorServiceV1.ResultFutureIterator futureIterator =
                connector.readRawDbNames(getClient().buildAuthConfig());

        return AdapterIterator.of(FanOutIterator.of(ImmutableList.of(futureIterator)), this::parseName);
    }

    /**
     * Creates Raw databases.
     *
     * @param databases The databases to create.
     * @return The created table names.
     * @throws Exception
     */
    public List create(List databases) throws Exception {
        String loggingPrefix = "create() - ";
        Instant startInstant = Instant.now();
        LOG.info(loggingPrefix + "Received {} databases to create.",
                databases.size());

        List deduplicated = new ArrayList<>(new HashSet<>(databases));

        ConnectorServiceV1 connector = getClient().getConnectorService();
        ConnectorServiceV1.ItemWriter createItemWriter = connector.writeRawDbNames();

        List> batches = Partition.ofSize(deduplicated, 100);
        for (List batch : batches) {
            List> items = new ArrayList<>();
            for (String table : batch) {
                items.add(ImmutableMap.of("name", table));
            }
            Request request = addAuthInfo(Request.create()
                    .withItems(items));
            ResponseItems response = createItemWriter.writeItems(request);
            if (!response.isSuccessful()) {
                throw new Exception(String.format(loggingPrefix + "Create database request failed: %s",
                        response.getResponseBodyAsString()));
            }
        }

        LOG.info(loggingPrefix + "Successfully created {} databases. Duration: {}.",
                databases.size(),
                Duration.between(startInstant, Instant.now()));

        return deduplicated;
    }

    /**
     * Deletes a set of Raw databases. The databases must be empty.
     *
     * @param databases The Raw database to delete.
     * @return The deleted databases.
     * @throws Exception
     */
    public List delete(List databases) throws Exception {
        return delete(databases, false);
    }

    /**
     * Deletes a set of Raw databases. Allows to recursively delete the databases' tables in the same operation.
     *
     * @param databases The Raw database to delete.
     * @param recursive Set to true to automatically delete the tables in the databases.
     * @return The deleted databases.
     * @throws Exception
     */
    public List delete(List databases, boolean recursive) throws Exception {
        String loggingPrefix = "delete() - ";
        Instant startInstant = Instant.now();
        LOG.info(loggingPrefix + "Received {} databases to delete.",
                databases.size());

        List deduplicated = new ArrayList<>(new HashSet<>(databases));

        ConnectorServiceV1 connector = getClient().getConnectorService();
        ConnectorServiceV1.ItemWriter deleteItemWriter = connector.deleteRawDbNames();

        List> batches = Partition.ofSize(deduplicated, 100);
        for (List batch : batches) {
            List> items = new ArrayList<>();
            for (String table : batch) {
                items.add(ImmutableMap.of("name", table));
            }
            Request request = addAuthInfo(Request.create()
                    .withItems(items)
                    .withRootParameter("recursive", recursive));
            ResponseItems response = deleteItemWriter.writeItems(request);
            if (!response.isSuccessful()) {
                throw new Exception(String.format(loggingPrefix + "Delete database request failed: %s",
                        response.getResponseBodyAsString()));
            }
        }

        LOG.info(loggingPrefix + "Successfully deleted {} databases. Duration: {}.",
                databases.size(),
                Duration.between(startInstant, Instant.now()));

        return deduplicated;
    }

    @AutoValue.Builder
    abstract static class Builder extends ApiBase.Builder {
        abstract RawDatabases build();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy