github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/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/types"
    28  )
    29  
    30  // GSFactory is a DBFactory implementation for creating GCS backed databases
    31  type GSFactory struct {
    32  }
    33  
    34  // CreateDB creates an GCS backed database
    35  func (fact GSFactory) CreateDB(ctx context.Context, nbf *types.NomsBinFormat, urlObj *url.URL, params map[string]string) (datas.Database, error) {
    36  	var db datas.Database
    37  	gcs, err := storage.NewClient(ctx)
    38  
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	bs := blobstore.NewGCSBlobstore(gcs, urlObj.Host, urlObj.Path)
    44  	gcsStore, err := nbs.NewBSStore(ctx, nbf.VersionString(), bs, defaultMemTableSize)
    45  
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	db = datas.NewDatabase(gcsStore)
    51  
    52  	return db, err
    53  }
    54  
    55  // LocalBSFactory is a DBFactory implementation for creating a local filesystem blobstore backed databases for testing
    56  type LocalBSFactory struct {
    57  }
    58  
    59  // CreateDB creates a local filesystem blobstore backed database
    60  func (fact LocalBSFactory) CreateDB(ctx context.Context, nbf *types.NomsBinFormat, urlObj *url.URL, params map[string]string) (datas.Database, error) {
    61  	var db datas.Database
    62  	absPath, err := filepath.Abs(filepath.Join(urlObj.Host, urlObj.Path))
    63  
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	bs := blobstore.NewLocalBlobstore(absPath)
    69  	bsStore, err := nbs.NewBSStore(ctx, nbf.VersionString(), bs, defaultMemTableSize)
    70  
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	db = datas.NewDatabase(bsStore)
    76  
    77  	return db, err
    78  }