Skip to content

Commit

Permalink
Use DBLoader when loading Dolt environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicktobey committed Feb 11, 2025
1 parent cff431b commit f506927
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
13 changes: 7 additions & 6 deletions go/cmd/dolt/dolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func runMain() int {
args = nil

// This is the dEnv passed to sub-commands, and is used to create the multi-repo environment.
dEnv := env.LoadWithoutDB(ctx, env.GetCurrentUserHomeDir, cfg.dataDirFS, doltdb.LocalDirDoltDB, doltversion.Version)
dEnv := env.Load(ctx, env.GetCurrentUserHomeDir, cfg.dataDirFS, doltdb.LocalDirDoltDB, doltversion.Version)

if dEnv.CfgLoadErr != nil {
cli.PrintErrln(color.RedString("Failed to load the global config. %v", dEnv.CfgLoadErr))
Expand Down Expand Up @@ -678,11 +678,8 @@ If you're interested in running this command against a remote host, hit us up on
}

var lookForServer bool
if targetEnv.DoltDB(ctx) != nil && targetEnv.IsAccessModeReadOnly(ctx) {
// If the loaded target environment has a doltDB and we do not
// have access to it, we look for a server.
lookForServer = true
} else if targetEnv.DoltDB(ctx) == nil {

if !targetEnv.Exists() {
// If the loaded environment itself does not have a doltDB, we
// may want to look for a server. We do so if all of the
// repositories in our MultiEnv are ReadOnly. This includes the
Expand All @@ -695,6 +692,10 @@ If you're interested in running this command against a remote host, hit us up on
return !allReposAreReadOnly, nil
})
lookForServer = allReposAreReadOnly
} else if targetEnv.IsAccessModeReadOnly(ctx) {
// If the loaded target environment has a doltDB and we do not
// have access to it, we look for a server.
lookForServer = true
}
if lookForServer {
localCreds, err := sqlserver.FindAndLoadLocalCreds(targetEnv.FS)
Expand Down
15 changes: 12 additions & 3 deletions go/libraries/doltcore/env/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"github.com/dolthub/dolt/go/store/chunks"
"os"
"path/filepath"
"strings"
Expand All @@ -37,7 +38,6 @@ import (
"github.com/dolthub/dolt/go/libraries/utils/concurrentmap"
"github.com/dolthub/dolt/go/libraries/utils/config"
"github.com/dolthub/dolt/go/libraries/utils/filesys"
"github.com/dolthub/dolt/go/store/chunks"
"github.com/dolthub/dolt/go/store/datas"
"github.com/dolthub/dolt/go/store/hash"
"github.com/dolthub/dolt/go/store/types"
Expand Down Expand Up @@ -88,6 +88,7 @@ type DoltEnv struct {
RepoState *RepoState
RSLoadErr error

dbLoader dbfactory.DBLoader
loadDBOnce sync.Once
doltDB *doltdb.DoltDB
DBLoadError error
Expand Down Expand Up @@ -203,13 +204,14 @@ func LoadWithoutDB(_ context.Context, hdp HomeDirProvider, fs filesys.Filesys, u
// Load loads the DoltEnv for the .dolt directory determined by resolving the specified urlStr with the specified Filesys.
func Load(ctx context.Context, hdp HomeDirProvider, fs filesys.Filesys, urlStr string, version string) *DoltEnv {
dEnv := LoadWithoutDB(ctx, hdp, fs, urlStr, version)
LoadDoltDB(ctx, dEnv.FS, dEnv.urlStr, dEnv)
dEnv.dbLoader, dEnv.DBLoadError = doltdb.GetDBLoader(ctx, types.Format_Default, urlStr, fs)

return dEnv
}

func LoadDoltDB(ctx context.Context, fs filesys.Filesys, urlStr string, dEnv *DoltEnv) {
dEnv.loadDBOnce.Do(func() {
ddb, dbLoadErr := doltdb.LoadDoltDB(ctx, types.Format_Default, urlStr, fs)
ddb, dbLoadErr := doltdb.LoadDB(ctx, dEnv.dbLoader, urlStr)
dEnv.doltDB = ddb
dEnv.DBLoadError = dbLoadErr
dEnv.urlStr = urlStr
Expand Down Expand Up @@ -1269,6 +1271,13 @@ func (dEnv *DoltEnv) BulkDbEaFactory(ctx context.Context) editor.DbEaFactory {
return editor.NewBulkImportTEAFactory(dEnv.DoltDB(ctx).ValueReadWriter(), tmpDir)
}

func (dEnv *DoltEnv) Exists() bool {
return dEnv.dbLoader != nil || dEnv.doltDB != nil
}

func (dEnv *DoltEnv) IsAccessModeReadOnly(ctx context.Context) bool {
if dEnv.dbLoader != nil {
return dEnv.dbLoader.IsAccessModeReadOnly()
}
return dEnv.DoltDB(ctx).AccessMode() == chunks.ExclusiveAccessMode_ReadOnly
}
9 changes: 7 additions & 2 deletions go/libraries/doltcore/env/multi_repo_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func MultiEnvForDirectory(
version = dEnv.Version
}

newEnv := LoadWithoutDB(ctx, GetCurrentUserHomeDir, newFs, doltdb.LocalDirDoltDB, version)
newEnv := Load(ctx, GetCurrentUserHomeDir, newFs, doltdb.LocalDirDoltDB, version)
if newEnv.Valid() {
envSet[dbfactory.DirToDBName(dir)] = newEnv
} else {
Expand Down Expand Up @@ -246,7 +246,12 @@ func (mrEnv *MultiRepoEnv) ReloadDBs(
}
}
}
mrEnv.envs = enforceSingleFormat(ctx, mrEnv.envs)
// We can skip the format check if there's only one environment.
// This is vital because it allows us to defer loading the database if there's only one.
// TODO: Can we determine the format without loading the database?
if len(mrEnv.envs) > 1 {
mrEnv.envs = enforceSingleFormat(ctx, mrEnv.envs)
}
}

func (mrEnv *MultiRepoEnv) FileSystem() filesys.Filesys {
Expand Down
3 changes: 1 addition & 2 deletions go/store/nbs/file_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ func MaybeMigrateFileManifest(ctx context.Context, dir string) (bool, error) {
}

// getFileManifest makes a new file manifest.
func getFileManifest(ctx context.Context, dir string, mode updateMode) (m manifest, err error) {
lock := fslock.New(filepath.Join(dir, lockFileName))
func getFileManifest(ctx context.Context, lock *fslock.Lock, dir string, mode updateMode) (m manifest, err error) {
m = fileManifest{dir: dir, mode: mode, lock: lock}

var f *os.File
Expand Down

0 comments on commit f506927

Please sign in to comment.