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

org.dinky.shaded.paimon.catalog.FileSystemCatalog Maven / Gradle / Ivy

The 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.dinky.shaded.paimon.catalog;

import org.dinky.shaded.paimon.fs.FileIO;
import org.dinky.shaded.paimon.fs.FileStatus;
import org.dinky.shaded.paimon.fs.Path;
import org.dinky.shaded.paimon.options.Options;
import org.dinky.shaded.paimon.schema.Schema;
import org.dinky.shaded.paimon.schema.SchemaChange;
import org.dinky.shaded.paimon.schema.SchemaManager;
import org.dinky.shaded.paimon.schema.TableSchema;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;

import static org.dinky.shaded.paimon.catalog.FileSystemCatalogOptions.CASE_SENSITIVE;

/** A catalog implementation for {@link FileIO}. */
public class FileSystemCatalog extends AbstractCatalog {

    private final Path warehouse;

    public FileSystemCatalog(FileIO fileIO, Path warehouse) {
        super(fileIO);
        this.warehouse = warehouse;
    }

    public FileSystemCatalog(FileIO fileIO, Path warehouse, Options options) {
        super(fileIO, options);
        this.warehouse = warehouse;
    }

    @Override
    public Optional lockFactory() {
        return Optional.empty();
    }

    @Override
    public List listDatabases() {
        List databases = new ArrayList<>();
        for (FileStatus status : uncheck(() -> fileIO.listStatus(warehouse))) {
            Path path = status.getPath();
            if (status.isDir() && isDatabase(path)) {
                databases.add(database(path));
            }
        }
        return databases;
    }

    @Override
    protected boolean databaseExistsImpl(String databaseName) {
        return uncheck(() -> fileIO.exists(newDatabasePath(databaseName)));
    }

    @Override
    protected void createDatabaseImpl(String name) {
        uncheck(() -> fileIO.mkdirs(newDatabasePath(name)));
    }

    @Override
    protected void dropDatabaseImpl(String name) {
        uncheck(() -> fileIO.delete(newDatabasePath(name), true));
    }

    @Override
    protected List listTablesImpl(String databaseName) {
        List tables = new ArrayList<>();
        for (FileStatus status : uncheck(() -> fileIO.listStatus(newDatabasePath(databaseName)))) {
            if (status.isDir() && tableExists(status.getPath())) {
                tables.add(status.getPath().getName());
            }
        }
        return tables;
    }

    @Override
    public boolean tableExists(Identifier identifier) {
        if (isSystemTable(identifier)) {
            return super.tableExists(identifier);
        }

        return tableExists(getDataTableLocation(identifier));
    }

    private boolean tableExists(Path tablePath) {
        return new SchemaManager(fileIO, tablePath).listAllIds().size() > 0;
    }

    @Override
    public TableSchema getDataTableSchema(Identifier identifier) throws TableNotExistException {
        Path path = getDataTableLocation(identifier);
        return new SchemaManager(fileIO, path)
                .latest()
                .orElseThrow(() -> new TableNotExistException(identifier));
    }

    @Override
    protected void dropTableImpl(Identifier identifier) {
        Path path = getDataTableLocation(identifier);
        uncheck(() -> fileIO.delete(path, true));
    }

    @Override
    public void createTableImpl(Identifier identifier, Schema schema) {
        Path path = getDataTableLocation(identifier);
        uncheck(() -> new SchemaManager(fileIO, path).createTable(schema));
    }

    @Override
    public void renameTableImpl(Identifier fromTable, Identifier toTable) {
        Path fromPath = getDataTableLocation(fromTable);
        Path toPath = getDataTableLocation(toTable);
        uncheck(() -> fileIO.rename(fromPath, toPath));
    }

    @Override
    protected void alterTableImpl(Identifier identifier, List changes)
            throws TableNotExistException, ColumnAlreadyExistException, ColumnNotExistException {
        new SchemaManager(fileIO, getDataTableLocation(identifier)).commitChanges(changes);
    }

    private static  T uncheck(Callable callable) {
        try {
            return callable.call();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static boolean isDatabase(Path path) {
        return path.getName().endsWith(DB_SUFFIX);
    }

    private static String database(Path path) {
        String name = path.getName();
        return name.substring(0, name.length() - DB_SUFFIX.length());
    }

    @Override
    public void close() throws Exception {}

    @Override
    public String warehouse() {
        return warehouse.toString();
    }

    @Override
    public boolean caseSensitive() {
        return catalogOptions.get(CASE_SENSITIVE);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy