<commit_before>/**
 * Copyright 2015 <NAME>
 *
 * Licensed under the BSD-3 License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://opensource.org/licenses/BSD-3-Clause
 *
 * 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.schema;

import java.util.Set;

import com.google.common.base.Preconditions;
import com.palantir.atlasdb.keyvalue.api.KeyValueService;
import com.palantir.atlasdb.table.description.IndexDefinition;
import com.palantir.atlasdb.table.description.IndexDefinition.IndexType;
import com.palantir.atlasdb.table.description.Schemas;
import com.palantir.atlasdb.table.description.TableDefinition;

public class SimpleSchemaUpdaterImpl implements SimpleSchemaUpdater {
    private final KeyValueService kvs;
    private final Namespace namespace;

    private SimpleSchemaUpdaterImpl(KeyValueService kvs, Namespace namespace) {
        this.kvs = kvs;
        this.namespace = namespace;
    }

    public static SimpleSchemaUpdater create(KeyValueService kvs, Namespace namespace) {
        return new SimpleSchemaUpdaterImpl(kvs, namespace);
    }

    @Override
    public void addTable(String tableName, TableDefinition definition) {
        updateTableMetadata(tableName, definition);
    }

    @Override
    public void deleteTable(String tableName) {
        deleteTableMetadata(tableName);
    }

    @Override
    public void addIndex(String indexName, IndexDefinition definition) {
        updateIndexMetadata(indexName, definition);
    }

    @Override
    public void deleteIndex(String indexName) {
        deleteIndexMetadata(indexName);
    }

    @Override
    public void updateTableMetadata(String tableName, TableDefinition definition) {
        String fullTableName = getFullTableName(tableName);
        Schemas.createTable(kvs, fullTableName, definition);
    }

    @Override
    public void updateIndexMetadata(String indexName, IndexDefinition definition) {
        String trueIndexName = getFullTableName(Schemas.appendIndexSuffix(indexName, definition));
        Schemas.createIndex(kvs, trueIndexName, definition);
    }

    private void deleteTableMetadata(String tableName) {
        String fullTableName = getFullTableName(tableName);
        kvs.dropTable(fullTableName);
    }

    private void deleteIndexMetadata(final String indexName) {
        Set<String> tableNames = kvs.getAllTableNames();
        for(IndexType type : IndexType.values()) {
            String trueIndexName = getFullTableName(indexName + type.getIndexSuffix());
            // This should only happen once - enforced by Schema.validateIndex()
            if (tableNames.contains(trueIndexName)) {
                kvs.dropTable(trueIndexName);
                return;
            }
        }
    }

    @Override
    public boolean tableExists(String tableName) {
        String fullTableName = getFullTableName(tableName);
        return kvs.getAllTableNames().contains(fullTableName);
    }

    @Override
    public void initializeSchema(final AtlasSchema schema) {
        Preconditions.checkArgument(schema.getNamespace().equals(namespace));
        Schemas.createTablesAndIndexes(schema.getLatestSchema(), kvs);
    }

    @Override
    public void resetTableMetadata(AtlasSchema schema, String tableName) {
        Schemas.createTable(schema.getLatestSchema(), kvs, tableName);
    }

    @Override
    public void resetIndexMetadata(AtlasSchema schema, String indexName) {
        Schemas.createIndex(schema.getLatestSchema(), kvs, indexName);
    }

    private String getFullTableName(String tableName) {<commit_msg>better message on namespace mismatch<commit_after>/**
 * Copyright 2015 <NAME>
 *
 * Licensed under the BSD-3 License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://opensource.org/licenses/BSD-3-Clause
 *
 * 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.schema;

import java.util.Set;

import com.google.common.base.Preconditions;
import com.palantir.atlasdb.keyvalue.api.KeyValueService;
import com.palantir.atlasdb.table.description.IndexDefinition;
import com.palantir.atlasdb.table.description.IndexDefinition.IndexType;
import com.palantir.atlasdb.table.description.Schemas;
import com.palantir.atlasdb.table.description.TableDefinition;

public class SimpleSchemaUpdaterImpl implements SimpleSchemaUpdater {
    private final KeyValueService kvs;
    private final Namespace namespace;

    private SimpleSchemaUpdaterImpl(KeyValueService kvs, Namespace namespace) {
        this.kvs = kvs;
        this.namespace = namespace;
    }

    public static SimpleSchemaUpdater create(KeyValueService kvs, Namespace namespace) {
        return new SimpleSchemaUpdaterImpl(kvs, namespace);
    }

    @Override
    public void addTable(String tableName, TableDefinition definition) {
        updateTableMetadata(tableName, definition);
    }

    @Override
    public void deleteTable(String tableName) {
        deleteTableMetadata(tableName);
    }

    @Override
    public void addIndex(String indexName, IndexDefinition definition) {
        updateIndexMetadata(indexName, definition);
    }

    @Override
    public void deleteIndex(String indexName) {
        deleteIndexMetadata(indexName);
    }

    @Override
    public void updateTableMetadata(String tableName, TableDefinition definition) {
        String fullTableName = getFullTableName(tableName);
        Schemas.createTable(kvs, fullTableName, definition);
    }

    @Override
    public void updateIndexMetadata(String indexName, IndexDefinition definition) {
        String trueIndexName = getFullTableName(Schemas.appendIndexSuffix(indexName, definition));
        Schemas.createIndex(kvs, trueIndexName, definition);
    }

    private void deleteTableMetadata(String tableName) {
        String fullTableName = getFullTableName(tableName);
        kvs.dropTable(fullTableName);
    }

    private void deleteIndexMetadata(final String indexName) {
        Set<String> tableNames = kvs.getAllTableNames();
        for(IndexType type : IndexType.values()) {
            String trueIndexName = getFullTableName(indexName + type.getIndexSuffix());
            // This should only happen once - enforced by Schema.validateIndex()
            if (tableNames.contains(trueIndexName)) {
                kvs.dropTable(trueIndexName);
                return;
            }
        }
    }

    @Override
    public boolean tableExists(String tableName) {
        String fullTableName = getFullTableName(tableName);
        return kvs.getAllTableNames().contains(fullTableName);
    }

    @Override
    public void initializeSchema(final AtlasSchema schema) {
        Preconditions.checkArgument(schema.getNamespace().equals(namespace), "Refusing to initialize namespace " + schema.getNamespace() + ", which doesn't match given Atlas namespace of " + namespace + ".");
        Schemas.createTablesAndIndexes(schema.getLatestSchema(), kvs);
    }

    @Override
    public void resetTableMetadata(AtlasSchema schema, String tableName) {
        Schemas.createTable(schema.getLatestSchema(), kvs, tableName);
    }

    @Override
    public void resetIndexMetadata(AtlasSchema schema, String indexName) {
        Schemas.createIndex(schema.getLatestSchema(), kvs, indexName);
    }

    private String getFullTableName(String tableName) {
{'fraction_non_alphanumeric': 0.07968672302337146, 'fraction_numerical': 0.0014917951268025858, 'mean_word_length': 3.6610660486674393, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5632468', 'n_tokens_mistral': 2126, 'n_tokens_neox': 2085, 'n_words': 545}