github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/dbfactory/gs.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 dbfactory
    16  
    17  import (
    18  	"context"
    19  	"net/url"
    20  	"path/filepath"
    21  
    22  	"cloud.google.com/go/storage"
    23  
    24  	"github.com/dolthub/dolt/go/store/blobstore"
    25  	"github.com/dolthub/dolt/go/store/datas"
    26  	"github.com/dolthub/dolt/go/store/nbs"
    27  	"github.com/dolthub/dolt/go/store/prolly/tree"
    28  	"github.com/dolthub/dolt/go/store/types"
    29  )
    30  
    31  // GSFactory is a DBFactory implementation for creating GCS backed databases
    32  type GSFactory struct {
    33  }
    34  
    35  func (fact GSFactory) PrepareDB(ctx context.Context, nbf *types.NomsBinFormat, urlObj *url.URL, params map[string]interface{}) error {
    36  	// nothing to prepare
    37  	return nil
    38  }
    39  
    40  // CreateDB creates an GCS backed database
    41  func (fact GSFactory) CreateDB(ctx context.Context, nbf *types.NomsBinFormat, urlObj *url.URL, params map[string]interface{}) (datas.Database, types.ValueReadWriter, tree.NodeStore, error) {
    42  	var db datas.Database
    43  	gcs, err := storage.NewClient(ctx)
    44  
    45  	if err != nil {
    46  		return nil, nil, nil, err
    47  	}
    48  
    49  	bs := blobstore.NewGCSBlobstore(gcs, urlObj.Host, urlObj.Path)
    50  	q := nbs.NewUnlimitedMemQuotaProvider()
    51  	gcsStore, err := nbs.NewBSStore(ctx, nbf.VersionString(), bs, defaultMemTableSize, q)
    52  
    53  	if err != nil {
    54  		return nil, nil, nil, err
    55  	}
    56  
    57  	vrw := types.NewValueStore(gcsStore)
    58  	ns := tree.NewNodeStore(gcsStore)
    59  	db = datas.NewTypesDatabase(vrw, ns)
    60  
    61  	return db, vrw, ns, nil
    62  }
    63  
    64  // LocalBSFactory is a DBFactory implementation for creating a local filesystem blobstore backed databases for testing
    65  type LocalBSFactory struct {
    66  }
    67  
    68  func (fact LocalBSFactory) PrepareDB(ctx context.Context, nbf *types.NomsBinFormat, urlObj *url.URL, params map[string]interface{}) error {
    69  	// nothing to prepare
    70  	return nil
    71  }
    72  
    73  // CreateDB creates a local filesystem blobstore backed database
    74  func (fact LocalBSFactory) CreateDB(ctx context.Context, nbf *types.NomsBinFormat, urlObj *url.URL, params map[string]interface{}) (datas.Database, types.ValueReadWriter, tree.NodeStore, error) {
    75  	var db datas.Database
    76  	absPath, err := filepath.Abs(filepath.Join(urlObj.Host, urlObj.Path))
    77  
    78  	if err != nil {
    79  		return nil, nil, nil, err
    80  	}
    81  
    82  	bs := blobstore.NewLocalBlobstore(absPath)
    83  	q := nbs.NewUnlimitedMemQuotaProvider()
    84  	bsStore, err := nbs.NewBSStore(ctx, nbf.VersionString(), bs, defaultMemTableSize, q)
    85  
    86  	if err != nil {
    87  		return nil, nil, nil, err
    88  	}
    89  
    90  	vrw := types.NewValueStore(bsStore)
    91  	ns := tree.NewNodeStore(bsStore)
    92  	db = datas.NewTypesDatabase(vrw, ns)
    93  
    94  	return db, vrw, ns, err
    95  }