github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/utils/remotesrv/cscache.go (about)

     1  // Copyright 2019 Dolthub, 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"context"
    19  	"path/filepath"
    20  	"sync"
    21  
    22  	"github.com/dolthub/dolt/go/libraries/doltcore/remotesrv"
    23  	"github.com/dolthub/dolt/go/libraries/utils/filesys"
    24  	"github.com/dolthub/dolt/go/store/nbs"
    25  )
    26  
    27  const (
    28  	defaultMemTableSize = 128 * 1024 * 1024
    29  )
    30  
    31  type LocalCSCache struct {
    32  	mu  *sync.Mutex
    33  	dbs map[string]remotesrv.RemoteSrvStore
    34  
    35  	fs filesys.Filesys
    36  }
    37  
    38  func NewLocalCSCache(filesys filesys.Filesys) *LocalCSCache {
    39  	return &LocalCSCache{
    40  		&sync.Mutex{},
    41  		make(map[string]remotesrv.RemoteSrvStore),
    42  		filesys,
    43  	}
    44  }
    45  
    46  func (cache *LocalCSCache) Get(ctx context.Context, repopath, nbfVerStr string) (remotesrv.RemoteSrvStore, error) {
    47  	cache.mu.Lock()
    48  	defer cache.mu.Unlock()
    49  
    50  	id := filepath.FromSlash(repopath)
    51  
    52  	if cs, ok := cache.dbs[id]; ok {
    53  		return cs, nil
    54  	}
    55  
    56  	err := cache.fs.MkDirs(id)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	path, err := cache.fs.Abs(id)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	newCS, err := nbs.NewLocalStore(ctx, nbfVerStr, path, defaultMemTableSize, nbs.NewUnlimitedMemQuotaProvider())
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	cache.dbs[id] = newCS
    71  
    72  	return newCS, nil
    73  }
    74  
    75  type SingletonCSCache struct {
    76  	s remotesrv.RemoteSrvStore
    77  }
    78  
    79  func (cache SingletonCSCache) Get(_ context.Context, path, nbfVerStr string) (remotesrv.RemoteSrvStore, error) {
    80  	return cache.s, nil
    81  }