github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/externalresource/internal/local/resource_desc.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package local
    15  
    16  import (
    17  	"context"
    18  	"path/filepath"
    19  
    20  	brStorage "github.com/pingcap/tidb/br/pkg/storage"
    21  	"github.com/pingcap/tiflow/engine/pkg/externalresource/internal"
    22  	resModel "github.com/pingcap/tiflow/engine/pkg/externalresource/model"
    23  )
    24  
    25  var _ internal.ResourceDescriptor = (*resourceDescriptor)(nil)
    26  
    27  // resourceDescriptor contains necessary data
    28  // to access a local file resource.
    29  type resourceDescriptor struct {
    30  	BasePath string
    31  	Ident    internal.ResourceIdent
    32  
    33  	storage brStorage.ExternalStorage
    34  }
    35  
    36  // AbsolutePath returns the absolute path of the given resource
    37  // in the local file system.
    38  func (d *resourceDescriptor) AbsolutePath() string {
    39  	encodedName := ResourceNameToFilePathName(d.Ident.Name)
    40  	return filepath.Join(d.BasePath, d.Ident.WorkerID, encodedName)
    41  }
    42  
    43  // ExternalStorage creates the storage object if one has not been created yet, and returns the
    44  // created storage object.
    45  func (d *resourceDescriptor) ExternalStorage(ctx context.Context) (brStorage.ExternalStorage, error) {
    46  	if d.storage == nil {
    47  		storage, err := newBrStorageForLocalFile(d.AbsolutePath())
    48  		if err != nil {
    49  			return nil, err
    50  		}
    51  		d.storage = storage
    52  	}
    53  	return d.storage, nil
    54  }
    55  
    56  // URI returns the URI of the local file resource.
    57  func (d *resourceDescriptor) URI() string {
    58  	return d.AbsolutePath()
    59  }
    60  
    61  // ID returns the resource ID of the local file resource.
    62  func (d *resourceDescriptor) ID() resModel.ResourceID {
    63  	return resModel.BuildResourceID(resModel.ResourceTypeLocalFile, d.Ident.Name)
    64  }
    65  
    66  // ResourceIdent returns the resource identity of the local file resource.
    67  func (d *resourceDescriptor) ResourceIdent() internal.ResourceIdent {
    68  	return d.Ident
    69  }