LanceTableAccess.java

// 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.doris.datasource.lance.metadata;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * Access parameters resolved for one read; credentials are not versioned dataset metadata.
 *
 * <p>Every dataset is read by its URI and version, by the FE and by the BE alike: Lance resolves
 * a version from the {@code _versions/} directory of the dataset itself. For a namespace-managed
 * dataset ({@code DescribeTableResponse.managed_versioning = true}) the namespace decides which
 * versions exist and which is the newest, so the FE asks it before choosing the version to read.
 */
public final class LanceTableAccess {
    private final String datasetUri;
    private final Map<String, String> storageOptions;
    private final List<String> namespaceTableId;
    private final String branch;

    /** A dataset whose versions live in its own {@code _versions/} directory. */
    public LanceTableAccess(String datasetUri, Map<String, String> storageOptions) {
        this(datasetUri, storageOptions, null, null);
    }

    private LanceTableAccess(String datasetUri, Map<String, String> storageOptions,
            List<String> namespaceTableId, String branch) {
        this.datasetUri = Objects.requireNonNull(datasetUri, "datasetUri");
        this.storageOptions = Collections.unmodifiableMap(new HashMap<>(storageOptions));
        this.namespaceTableId = namespaceTableId == null
                ? null : Collections.unmodifiableList(new ArrayList<>(namespaceTableId));
        this.branch = branch;
    }

    /**
     * The same table on one of its branches. A branch is a separate manifest chain with its own
     * root directory, {@code <table>/tree/<branch>}; a reader that opens by URI, such as the BE,
     * addresses the branch by that root. The storage options and namespace
     * identity are unchanged.
     */
    public LanceTableAccess onBranch(String branchName, String branchUri) {
        return new LanceTableAccess(Objects.requireNonNull(branchUri, "branchUri"), storageOptions,
                namespaceTableId, Objects.requireNonNull(branchName, "branchName"));
    }

    /** The branch this access addresses, if not the main chain. */
    public Optional<String> getBranch() {
        return Optional.ofNullable(branch);
    }

    /** A dataset whose versions are recorded by the namespace that owns {@code namespaceTableId}. */
    public static LanceTableAccess managedByNamespace(String datasetUri, Map<String, String> storageOptions,
            List<String> namespaceTableId) {
        return new LanceTableAccess(datasetUri, storageOptions,
                Objects.requireNonNull(namespaceTableId, "namespaceTableId"), null);
    }

    public String getDatasetUri() {
        return datasetUri;
    }

    /** The options both the FE and the BE open the dataset with. */
    public Map<String, String> getStorageOptions() {
        return storageOptions;
    }

    /** Whether the namespace, rather than the dataset directory, records which versions exist. */
    public boolean isManagedVersioning() {
        return namespaceTableId != null;
    }

    /** The namespace table identifier of a managed dataset; null otherwise. */
    public List<String> getNamespaceTableId() {
        return namespaceTableId;
    }
}