github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/dbfactory/file.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  	"os"
    21  	"path/filepath"
    22  
    23  	"github.com/dolthub/dolt/go/libraries/utils/filesys"
    24  	"github.com/dolthub/dolt/go/store/datas"
    25  	"github.com/dolthub/dolt/go/store/nbs"
    26  	"github.com/dolthub/dolt/go/store/types"
    27  )
    28  
    29  const (
    30  	// DoltDir defines the directory used to hold the dolt repo data within the filesys
    31  	DoltDir = ".dolt"
    32  
    33  	// DataDir is the directory internal to the DoltDir which holds the noms files.
    34  	DataDir = "noms"
    35  )
    36  
    37  // DoltDataDir is the directory where noms files will be stored
    38  var DoltDataDir = filepath.Join(DoltDir, DataDir)
    39  
    40  // FileFactory is a DBFactory implementation for creating local filesys backed databases
    41  type FileFactory struct {
    42  }
    43  
    44  // CreateDB creates an local filesys backed database
    45  func (fact FileFactory) CreateDB(ctx context.Context, nbf *types.NomsBinFormat, urlObj *url.URL, params map[string]string) (datas.Database, error) {
    46  	path, err := url.PathUnescape(urlObj.Path)
    47  
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	path = filepath.FromSlash(path)
    53  	path = urlObj.Host + path
    54  
    55  	info, err := os.Stat(path)
    56  
    57  	if err != nil {
    58  		return nil, err
    59  	} else if !info.IsDir() {
    60  		return nil, filesys.ErrIsFile
    61  	}
    62  
    63  	st, err := nbs.NewLocalStore(ctx, nbf.VersionString(), path, defaultMemTableSize)
    64  
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	return datas.NewDatabase(nbs.NewNBSMetricWrapper(st)), nil
    70  }