github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/dbfactory/factory.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  	"fmt"
    20  	"net/url"
    21  	"strings"
    22  
    23  	"github.com/dolthub/dolt/go/libraries/utils/earl"
    24  	"github.com/dolthub/dolt/go/store/datas"
    25  	"github.com/dolthub/dolt/go/store/prolly/tree"
    26  	"github.com/dolthub/dolt/go/store/types"
    27  )
    28  
    29  const (
    30  	// AWSScheme
    31  	AWSScheme = "aws"
    32  
    33  	// GSScheme
    34  	GSScheme = "gs"
    35  
    36  	// OCIScheme
    37  	OCIScheme = "oci"
    38  
    39  	// FileScheme
    40  	FileScheme = "file"
    41  
    42  	// MemScheme
    43  	MemScheme = "mem"
    44  
    45  	// HTTPSScheme
    46  	HTTPSScheme = "https"
    47  
    48  	// HTTPScheme
    49  	HTTPScheme = "http"
    50  
    51  	// LocalFS Blobstore Scheme
    52  	LocalBSScheme = "localbs"
    53  
    54  	OSSScheme = "oss"
    55  
    56  	defaultScheme       = HTTPSScheme
    57  	defaultMemTableSize = 256 * 1024 * 1024
    58  )
    59  
    60  // DBFactory is an interface for creating concrete datas.Database instances from different backing stores
    61  type DBFactory interface {
    62  	// CreateDB returns the database located at the URL given and its associated data access interfaces
    63  	CreateDB(ctx context.Context, nbf *types.NomsBinFormat, u *url.URL, params map[string]interface{}) (datas.Database, types.ValueReadWriter, tree.NodeStore, error)
    64  	// PrepareDB does any necessary setup work for a new database to be created at the URL given, e.g. to receive a push.
    65  	// Not all factories support this operation.
    66  	PrepareDB(ctx context.Context, nbf *types.NomsBinFormat, u *url.URL, params map[string]interface{}) error
    67  }
    68  
    69  // DBFactories is a map from url scheme name to DBFactory.  Additional factories can be added to the DBFactories map
    70  // from external packages.
    71  var DBFactories = map[string]DBFactory{
    72  	AWSScheme:     AWSFactory{},
    73  	OSSScheme:     OSSFactory{},
    74  	GSScheme:      GSFactory{},
    75  	OCIScheme:     OCIFactory{},
    76  	FileScheme:    FileFactory{},
    77  	MemScheme:     MemFactory{},
    78  	LocalBSScheme: LocalBSFactory{},
    79  	HTTPScheme:    NewDoltRemoteFactory(true),
    80  	HTTPSScheme:   NewDoltRemoteFactory(false),
    81  }
    82  
    83  // CreateDB creates a database based on the supplied urlStr, and creation params.  The DBFactory used for creation is
    84  // determined by the scheme of the url.  Naked urls will use https by default.
    85  func CreateDB(ctx context.Context, nbf *types.NomsBinFormat, urlStr string, params map[string]interface{}) (datas.Database, types.ValueReadWriter, tree.NodeStore, error) {
    86  	urlObj, err := earl.Parse(urlStr)
    87  
    88  	if err != nil {
    89  		return nil, nil, nil, err
    90  	}
    91  
    92  	scheme := urlObj.Scheme
    93  	if len(scheme) == 0 {
    94  		scheme = defaultScheme
    95  	}
    96  
    97  	if fact, ok := DBFactories[strings.ToLower(scheme)]; ok {
    98  		return fact.CreateDB(ctx, nbf, urlObj, params)
    99  	}
   100  
   101  	return nil, nil, nil, fmt.Errorf("unknown url scheme: '%s'", urlObj.Scheme)
   102  }
   103  
   104  // PrepareDB does the necessary work to create a database at the URL given, e.g. to ready a new remote for pushing. Not
   105  // all URL schemes can support this operation. The DBFactory used for preparing the DB is determined by the scheme of
   106  // the url. Naked urls will use https by default.
   107  func PrepareDB(ctx context.Context, nbf *types.NomsBinFormat, urlStr string, params map[string]interface{}) error {
   108  	url, err := earl.Parse(urlStr)
   109  	if err != nil {
   110  		return err
   111  	}
   112  
   113  	scheme := url.Scheme
   114  	if len(scheme) == 0 {
   115  		scheme = defaultScheme
   116  	}
   117  
   118  	if fact, ok := DBFactories[strings.ToLower(scheme)]; ok {
   119  		return fact.PrepareDB(ctx, nbf, url, params)
   120  	}
   121  
   122  	return fmt.Errorf("unknown url scheme: '%s'", url.Scheme)
   123  }