github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/metastore/metastore_utils.go (about)

     1  package metastore
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/url"
     7  
     8  	"github.com/treeverse/lakefs/pkg/gateway/path"
     9  )
    10  
    11  const symlinkInputFormat = "org.apache.hadoop.hive.ql.io.SymlinkTextInputFormat"
    12  
    13  var ErrInvalidLocation = errors.New("got empty schema or host while parsing location url, location should be schema://host/path")
    14  
    15  func ReplaceBranchName(location, branch string) (string, error) {
    16  	u, err := url.Parse(location)
    17  	if err != nil {
    18  		return "", err
    19  	}
    20  	if u.Scheme == "" || u.Host == "" {
    21  		return "", ErrInvalidLocation
    22  	}
    23  	p, err := path.ResolvePath(u.Path)
    24  	if err != nil {
    25  		return "", err
    26  	}
    27  
    28  	return u.Scheme + "://" + u.Host + "/" + branch + "/" + p.Path, nil
    29  }
    30  
    31  func ReplaceExternalToLakeFSImported(location, repo, branch string) (string, error) {
    32  	u, err := url.Parse(location)
    33  	if err != nil {
    34  		return "", err
    35  	}
    36  	if u.Scheme == "" || u.Host == "" {
    37  		return "", fmt.Errorf("%w: %s", ErrInvalidLocation, location)
    38  	}
    39  	return u.Scheme + "://" + repo + "/" + branch + "/" + u.Path, nil
    40  }
    41  
    42  func GetSymlinkLocation(location, locationPrefix string) (string, error) {
    43  	u, err := url.Parse(location)
    44  	if err != nil {
    45  		return "", err
    46  	}
    47  	if u.Scheme == "" || u.Host == "" {
    48  		return "", ErrInvalidLocation
    49  	}
    50  	p, err := path.ResolvePath(u.Path)
    51  	if err != nil {
    52  		return "", err
    53  	}
    54  	return locationPrefix + "/" + u.Host + "/" + p.Ref + "/" + p.Path, nil
    55  }
    56  
    57  func ExtractRepoAndBranch(metastoreLocationURI string) (string, string, error) {
    58  	u, err := url.Parse(metastoreLocationURI)
    59  	if err != nil {
    60  		return "", "", err
    61  	}
    62  	if u.Scheme == "" || u.Host == "" {
    63  		return "", "", ErrInvalidLocation
    64  	}
    65  	p, err := path.ResolvePath(u.Path)
    66  	if err != nil {
    67  		return "", "", err
    68  	}
    69  
    70  	return u.Host, p.Ref, nil
    71  }