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

org.apache.cassandra.schema.ReplicationParams Maven / Gradle / Ivy

Go to download

The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together Dynamo's fully distributed design and Bigtable's ColumnFamily-based data model.

There is a newer version: 5.0-rc1
Show newest version
/*
 * 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.cassandra.schema;

import java.util.HashMap;
import java.util.Map;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableMap;

import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.cql3.CqlBuilder;
import org.apache.cassandra.locator.*;
import org.apache.cassandra.service.StorageService;

public final class ReplicationParams
{
    public static final String CLASS = "class";

    public final Class klass;
    public final ImmutableMap options;

    private ReplicationParams(Class klass, Map options)
    {
        this.klass = klass;
        this.options = ImmutableMap.copyOf(options);
    }

    static ReplicationParams local()
    {
        return new ReplicationParams(LocalStrategy.class, ImmutableMap.of());
    }

    static ReplicationParams simple(int replicationFactor)
    {
        return new ReplicationParams(SimpleStrategy.class, ImmutableMap.of("replication_factor", Integer.toString(replicationFactor)));
    }

    static ReplicationParams simple(String replicationFactor)
    {
        return new ReplicationParams(SimpleStrategy.class, ImmutableMap.of("replication_factor", replicationFactor));
    }

    static ReplicationParams nts(Object... args)
    {
        assert args.length % 2 == 0;

        Map options = new HashMap<>();
        for (int i = 0; i < args.length; i += 2)
        {
            options.put((String) args[i], args[i + 1].toString());
        }

        return new ReplicationParams(NetworkTopologyStrategy.class, options);
    }

    public void validate(String name)
    {
        // Attempt to instantiate the ARS, which will throw a ConfigurationException if the options aren't valid.
        TokenMetadata tmd = StorageService.instance.getTokenMetadata();
        IEndpointSnitch eps = DatabaseDescriptor.getEndpointSnitch();
        AbstractReplicationStrategy.validateReplicationStrategy(name, klass, tmd, eps, options);
    }

    public static ReplicationParams fromMap(Map map) {
        return fromMapWithDefaults(map, new HashMap<>());
    }

    public static ReplicationParams fromMapWithDefaults(Map map, Map defaults)
    {
        Map options = new HashMap<>(map);
        String className = options.remove(CLASS);

        Class klass = AbstractReplicationStrategy.getClass(className);
        AbstractReplicationStrategy.prepareReplicationStrategyOptions(klass, options, defaults);

        return new ReplicationParams(klass, options);
    }

    public Map asMap()
    {
        Map map = new HashMap<>(options);
        map.put(CLASS, klass.getName());
        return map;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
            return true;

        if (!(o instanceof ReplicationParams))
            return false;

        ReplicationParams r = (ReplicationParams) o;

        return klass.equals(r.klass) && options.equals(r.options);
    }

    @Override
    public int hashCode()
    {
        return Objects.hashCode(klass, options);
    }

    @Override
    public String toString()
    {
        MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(this);
        helper.add(CLASS, klass.getName());
        for (Map.Entry entry : options.entrySet())
            helper.add(entry.getKey(), entry.getValue());
        return helper.toString();
    }

    public void appendCqlTo(CqlBuilder builder)
    {
        String classname = "org.apache.cassandra.locator".equals(klass.getPackage().getName()) ? klass.getSimpleName()
                                                                                               : klass.getName();
        builder.append("{'class': ")
               .appendWithSingleQuotes(classname);

        options.forEach((k, v) -> {
            builder.append(", ")
                   .appendWithSingleQuotes(k)
                   .append(": ")
                   .appendWithSingleQuotes(v);
        });

        builder.append('}');
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy