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

com.palantir.atlasdb.keyvalue.api.TableReference Maven / Gradle / Ivy

There is a newer version: 0.1152.0
Show newest version
/*
 * (c) Copyright 2018 Palantir Technologies Inc. All rights reserved.
 *
 * 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.palantir.atlasdb.keyvalue.api;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.benmanes.caffeine.cache.Interner;
import com.palantir.atlasdb.util.Measurable;
import com.palantir.logsafe.UnsafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
import java.util.Locale;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;

public final class TableReference implements Measurable {
    private static final Interner tableNames = Interner.newWeakInterner();

    private final Namespace namespace;
    private final String tableName;

    /**
     * Creates a table reference based on fullTableName.
     * fullTableName is assumed to be of the format namespace.tableName, and must contain a dot.
     */
    public static TableReference createFromFullyQualifiedName(String fullTableName) {
        int index = fullTableName.indexOf('.');
        if (index <= 0) {
            throw new SafeIllegalArgumentException(
                    "Table name is not a fully qualified table name.", UnsafeArg.of("tableName", fullTableName));
        }
        return create(
                Namespace.create(fullTableName.substring(0, index), Namespace.UNCHECKED_NAME),
                fullTableName.substring(index + 1));
    }

    public static TableReference create(Namespace namespace, String tableName) {
        return new TableReference(namespace, tableName);
    }

    /**
     * Creates a table reference with an empty namespace, based on tablename.
     * This should only be used when creating a TableReference for a system table.
     */
    public static TableReference createWithEmptyNamespace(String tableName) {
        return new TableReference(Namespace.EMPTY_NAMESPACE, tableName);
    }

    public static TableReference createLowerCased(TableReference table) {
        String name = table.namespace.getName().toLowerCase(Locale.ROOT);
        Namespace namespace = name.isEmpty() ? Namespace.EMPTY_NAMESPACE : Namespace.create(name);
        return create(namespace, table.tableName.toLowerCase(Locale.ROOT));
    }

    /**
     * @deprecated please use createFromFullyQualifiedName, if fullTableName includes the namespace,
     * or createWithEmptyNamespace, if you're passing in a system table name.
     */
    @Deprecated
    @SuppressWarnings("InlineMeSuggester")
    public static TableReference createUnsafe(String fullTableName) {
        return fullTableName.indexOf('.') < 0
                ? createWithEmptyNamespace(fullTableName)
                : createFromFullyQualifiedName(fullTableName);
    }

    @JsonCreator
    private TableReference(
            @JsonProperty("namespace") Namespace namespace, @JsonProperty("tablename") String tableName) {
        this.namespace = namespace;
        this.tableName = tableNames.intern(tableName);
    }

    /**
     * @deprecated uses {@link TableReference#createUnsafe}, which is itself deprecated.
     */
    @Deprecated
    public static TableReference fromInternalTableName(String tableName) {
        if (tableName.startsWith("_")) {
            return createWithEmptyNamespace(tableName);
        }
        return createUnsafe(tableName.replaceFirst("__", "."));
    }

    public Namespace getNamespace() {
        return namespace;
    }

    /**
     * @deprecated Please use {@link TableReference#getTableName()}, which is consistent with broader AtlasDB
     *  naming conventions.
     */
    @JsonIgnore
    @Deprecated
    public String getTablename() {
        return getTableName();
    }

    @JsonProperty("tablename") // Backwards compatibility - DO NOT CHANGE THIS WITHOUT A MIGRATION!
    public String getTableName() {
        return tableName;
    }

    @JsonIgnore
    public String getQualifiedName() {
        return namespace.isEmptyNamespace() || namespace.getName().equals("met")
                ? tableName
                : namespace.getName() + "." + tableName;
    }

    public static boolean isFullyQualifiedName(String tableName) {
        return tableName.contains(".");
    }

    @JsonIgnore
    public boolean isFullyQualifiedName() {
        return getQualifiedName().contains(".");
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        TableReference that = (TableReference) obj;
        return Objects.equals(namespace, that.namespace) && Objects.equals(tableName, that.tableName);
    }

    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(namespace);
        result = 31 * result + Objects.hashCode(tableName);
        return result;
    }

    @Override
    public String toString() {
        return getQualifiedName();
    }

    @Override
    public long sizeInBytes() {
        return stringSizeInBytes(tableName) + stringSizeInBytes(namespace.getName()) + Character.BYTES;
    }

    private static long stringSizeInBytes(String string) {
        return (long) Character.BYTES * string.length();
    }

    public static TableReference fromString(String tableReferenceAsString) {
        int dotCount = StringUtils.countMatches(tableReferenceAsString, ".");
        if (dotCount == 0) {
            return TableReference.createWithEmptyNamespace(tableReferenceAsString);
        } else if (dotCount == 1) {
            return TableReference.createFromFullyQualifiedName(tableReferenceAsString);
        } else {
            throw new IllegalArgumentException(tableReferenceAsString + " is not a valid table reference.");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy