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

com.datastax.stargate.sdk.rest.KeyspaceClient Maven / Gradle / Ivy

There is a newer version: 2.3.7
Show newest version
/*
 * Copyright DataStax, Inc.
 *
 * 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.datastax.stargate.sdk.rest;


import static com.datastax.stargate.sdk.utils.JsonUtils.marshall;
import static com.datastax.stargate.sdk.utils.JsonUtils.unmarshallType;

import java.net.HttpURLConnection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.datastax.stargate.sdk.StargateClientNode;
import com.datastax.stargate.sdk.StargateHttpClient;
import com.datastax.stargate.sdk.core.ApiResponse;
import com.datastax.stargate.sdk.core.ApiResponseHttp;
import com.datastax.stargate.sdk.core.DataCenter;
import com.datastax.stargate.sdk.rest.domain.CreateTable;
import com.datastax.stargate.sdk.rest.domain.Keyspace;
import com.datastax.stargate.sdk.rest.domain.TableDefinition;
import com.datastax.stargate.sdk.rest.domain.TypeDefinition;
import com.datastax.stargate.sdk.utils.Assert;
import com.fasterxml.jackson.core.type.TypeReference;

/**
 * Client for API resource /v2/namespaces.
 *
 * @author Cedrick LUNVEN (@clunven)
 */
public class KeyspaceClient {
    
    /** URL parts. */
    public static final String PATH_KEYSPACES   = "/v2/keyspaces";
    
    /** URL parts. */
    public static final String PATH_TABLES      = "/tables";
    
    /** URL parts. */
    public static final String PATH_TYPES       = "/types";
    
    /** Marshalling {@link TypeReference}. */
    private static final TypeReference> RESPONSE_KEYSPACE = 
            new TypeReference>(){};
    
    /** Marshalling {@link TypeReference}. */
    private static final TypeReference>> RESPONSE_TABLE_DEFINITIONS = 
            new TypeReference>>(){};
            
    /** Marshalling {@link TypeReference}. */
    private static final TypeReference>> RESPONSE_TYPE_DEFINITIONS = 
            new TypeReference>>(){};
            
    
    /** Reference for the resources. */
    private ApiDataClient apiData = null;
    
    /** Get Topology of the nodes. */
    private final StargateHttpClient stargateHttpClient;
    
    /** Namespace. */
    private String keyspace;
    
    /** Hold a reference to client to keep singletons.*/
    private Map  tablesClient = new HashMap<>();
    
    /** Hold a reference to client to keep singletons.*/
    private Map  typesClient = new HashMap<>();
    
    /**
     * Full constructor.
     * 
     * @param apiData ApiDataClient
     * @param keyspace String
     */
    public KeyspaceClient(ApiDataClient apiData, String keyspace) {
        this.apiData            = apiData;
        this.keyspace           = keyspace;
        this.stargateHttpClient = apiData.stargateHttpClient;
        Assert.hasLength(keyspace, "keyspace");
    }
    
    // ---------------------------------
    // ----        CRUD             ----
    // ---------------------------------
     
    /**
     * Find a namespace and its metadata based on its id.
     * 
     * @see Reference Documentation
     * 
     * @return Keyspace
     */
    public Optional find() {
        ApiResponseHttp res = stargateHttpClient.GET(keyspaceSchemaResource);
        if (HttpURLConnection.HTTP_NOT_FOUND == res.getCode()) {
            return Optional.empty();
        } else {
            return Optional.of(unmarshallType(res.getBody(), RESPONSE_KEYSPACE).getData());
        }
    }
    
    /**
     * Check it the keyspace exist.
     * 
     * @return boolean
     */
    public boolean exist() {
        return find().isPresent();
    }
    
    /**
     * Create a keyspace providing the replications per Datacenter.
     * 
     * @see Reference Documentation
     * 
     * @param datacenters DataCenter
     */
    public void create(DataCenter... datacenters) {
        Assert.notNull(datacenters, "datacenters");
        Assert.isTrue(datacenters.length > 0, "DataCenters are required");
        stargateHttpClient.POST(
                apiData.keyspacesSchemaResource, 
                marshall(new Keyspace(keyspace, Arrays.asList(datacenters))));
    }
    
    /**
     * Create a keyspace.
     * 
     * @see Reference Documentation
     * 
     * @param replicas int
     */
    public void createSimple(int replicas) {
        Assert.isTrue(replicas>0, "Replica number should be bigger than 0");
        stargateHttpClient.POST(
                apiData.keyspacesSchemaResource, 
                marshall(new Keyspace(keyspace, replicas)));
    }
    
    /**
     * Delete a keyspace.
     * 
     * @see Reference Documentation
     */
    public void delete() {
        stargateHttpClient.DELETE(keyspaceSchemaResource);
    }
    
    /**
     * List tablenames in keyspace.
     *
     * @see Reference Documentation
     * 
     * @return TableDefinition
     */
    public Stream tables() {
        return unmarshallType(
                stargateHttpClient.GET(tablesSchemaResource).getBody(), 
                RESPONSE_TABLE_DEFINITIONS)
                .getData().stream();
    }
    
    /**
     * Sample collector as MAP of stream.
     * 
     * @return
     *      map
     */
    public Map tablesAsMap() {
        return tables().collect(
                Collectors.toMap(TableDefinition::getName, Function.identity()));
    }
    
    /**
     * List types in the keyspace.
     * 
     * @see Reference Documentation
     *
     * @return
     *      list of types.
     */
    public Stream types() {
        return unmarshallType(
                stargateHttpClient.GET(typesSchemaResource).getBody(), 
                RESPONSE_TYPE_DEFINITIONS)
                .getData().stream();
    }
        
    /**
     * Map to list only table names.
     * 
     * @return Stream
     */
    public Stream tableNames() {
        return tables().map(TableDefinition::getName);
    }
    
    /**
     * Map to list only types names.
     * 
     * @return Stream
     */
    public Stream typeNames() {
        return types().map(TypeDefinition::getName);
    }
    
    // ---------------------------------
    // ----    Sub Resources        ----
    // ---------------------------------
    
    /**
     * Move to the Table client
     * 
     * @param tableName String
     * @return TableClient
     */
    public TableClient table(String tableName) {
        Assert.hasLength(tableName, "tableName");
        if (!tablesClient.containsKey(tableName)) {
            tablesClient.put(tableName, new TableClient(stargateHttpClient, this, tableName));
        }
        return tablesClient.get(tableName);
    }
    
    /**
     * Move to the Type client
     * 
     * @param typeName String
     * @return TypeClient
     */
    public TypeClient type(String typeName) {
        Assert.hasLength(typeName, "typeName");
        if (!typesClient.containsKey(typeName)) {
            typesClient.put(typeName, new TypeClient(stargateHttpClient, this, typeName));
        }
        return typesClient.get(typeName);
    }
    
    /**
     * Syntax sugar more easier to understand in a fluent API.
     * 
     * @see Reference Documentation
     * 
     * @param tableName tableName
     * @param ct CreateTable
     */
    public void createTable(String tableName, CreateTable ct) {
        table(tableName).create(ct);
    }
    
    // ---------------------------------
    // ----       Resources         ----
    // ---------------------------------
    
    /**
     * Getter accessor for attribute 'keyspace'.
     *
     * @return current value of 'keyspace'
     */
    public String getKeyspace() {
        return keyspace;
    }

    /**
     * /v2/keyspaces/{keyspace}
     */
    public Function keyspaceResource = 
             (node) -> apiData.keyspacesResource.apply(node) + "/" + keyspace;
    
    /**
     * /v2/schemas/keyspaces/{keyspace}
     */
    public Function keyspaceSchemaResource = 
            (node) -> apiData.keyspacesSchemaResource.apply(node) + "/" + keyspace;
   
    /**
     * /v2/keyspaces/{keyspace}/tables
     */
    public Function tablesResource = 
            (node) -> keyspaceResource.apply(node) + PATH_TABLES;

    /**
     * /v2/schemas/keyspaces/{keyspace}/tables
     */
    public Function tablesSchemaResource = 
            (node) -> keyspaceSchemaResource.apply(node) + PATH_TABLES;
    
    /**
     * /v2/keyspaces/{keyspace}/types
     */
    public Function typesResource = 
            (node) -> keyspaceResource.apply(node) + PATH_TYPES;
                       
    /** 
     * /v2/schemas/keyspaces/{keyspace}/types 
     */
    public Function typesSchemaResource = 
            (node) -> keyspaceSchemaResource.apply(node) + PATH_TYPES;
  
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy