github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/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/types"
    26  )
    27  
    28  const (
    29  	// AWSScheme
    30  	AWSScheme = "aws"
    31  
    32  	// GSScheme
    33  	GSScheme = "gs"
    34  
    35  	// FileScheme
    36  	FileScheme = "file"
    37  
    38  	// MemScheme
    39  	MemScheme = "mem"
    40  
    41  	// HTTPSScheme
    42  	HTTPSScheme = "https"
    43  
    44  	// HTTPScheme
    45  	HTTPScheme = "http"
    46  
    47  	// InMemBlobstore Scheme
    48  	LocalBSScheme = "localbs"
    49  
    50  	defaultScheme       = HTTPSScheme
    51  	defaultMemTableSize = 256 * 1024 * 1024
    52  )
    53  
    54  // DBFactory is an interface for creating concrete datas.Database instances which may have different backing stores.
    55  type DBFactory interface {
    56  	CreateDB(ctx context.Context, nbf *types.NomsBinFormat, urlObj *url.URL, params map[string]string) (datas.Database, error)
    57  }
    58  
    59  // DBFactories is a map from url scheme name to DBFactory.  Additional factories can be added to the DBFactories map
    60  // from external packages.
    61  var DBFactories = map[string]DBFactory{
    62  	AWSScheme:     AWSFactory{},
    63  	GSScheme:      GSFactory{},
    64  	FileScheme:    FileFactory{},
    65  	MemScheme:     MemFactory{},
    66  	LocalBSScheme: LocalBSFactory{},
    67  }
    68  
    69  // InitializeFactories initializes any factories that rely on a GRPCConnectionProvider (Namely http and https)
    70  func InitializeFactories(dp GRPCDialProvider) {
    71  	DBFactories[HTTPScheme] = NewDoltRemoteFactory(dp, true)
    72  	DBFactories[HTTPSScheme] = NewDoltRemoteFactory(dp, false)
    73  }
    74  
    75  // CreateDB creates a database based on the supplied urlStr, and creation params.  The DBFactory used for creation is
    76  // determined by the scheme of the url.  Naked urls will use https by default.
    77  func CreateDB(ctx context.Context, nbf *types.NomsBinFormat, urlStr string, params map[string]string) (datas.Database, error) {
    78  	urlObj, err := earl.Parse(urlStr)
    79  
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	scheme := urlObj.Scheme
    85  	if len(scheme) == 0 {
    86  		scheme = defaultScheme
    87  	}
    88  
    89  	if fact, ok := DBFactories[strings.ToLower(scheme)]; ok {
    90  		return fact.CreateDB(ctx, nbf, urlObj, params)
    91  	}
    92  
    93  	return nil, fmt.Errorf("unknown url scheme: '%s'", urlObj.Scheme)
    94  }