More cleanup and bug fixes

This commit is contained in:
Zach Musgrave
2023-03-23 17:37:20 -07:00
parent a3cdb08846
commit 8c67f71ba9
7 changed files with 59 additions and 46 deletions
+5 -17
View File
@@ -96,7 +96,7 @@ func (r ReadOnlyDatabase) IsReadOnly() bool {
}
func (r ReadOnlyDatabase) InitialDBState(ctx *sql.Context, branch string) (dsess.InitialDbState, error) {
return GetInitialDBState(ctx, r, branch)
return initialDBState(ctx, r, branch)
}
// Revision implements dsess.RevisionDatabase
@@ -125,8 +125,9 @@ func NewDatabase(ctx context.Context, name string, dbData env.DbData, editOpts e
}, nil
}
// GetInitialDBState returns the InitialDbState for |db|.
func GetInitialDBState(ctx *sql.Context, db SqlDatabase, branch string) (dsess.InitialDbState, error) {
// initialDBState returns the InitialDbState for |db|. Other implementations of SqlDatabase outside this file should
// implement their own method for an initial db state and not rely on this method.
func initialDBState(ctx *sql.Context, db SqlDatabase, branch string) (dsess.InitialDbState, error) {
if len(db.Revision()) > 0 {
return initialStateForRevisionDb(ctx, db)
}
@@ -135,7 +136,7 @@ func GetInitialDBState(ctx *sql.Context, db SqlDatabase, branch string) (dsess.I
}
func (db Database) InitialDBState(ctx *sql.Context, branch string) (dsess.InitialDbState, error) {
return GetInitialDBState(ctx, db, branch)
return initialDBState(ctx, db, branch)
}
// Name returns the name of this database, set at creation time.
@@ -1332,19 +1333,6 @@ func (db Database) SetCollation(ctx *sql.Context, collation sql.CollationID) err
return db.SetRoot(ctx, newRoot)
}
// TODO: this is a hack to make user space DBs appear to the analyzer as full DBs with state etc., but the state is
// really skeletal. We need to reexamine the DB / session initialization to make this simpler -- most of these things
// aren't needed at initialization time and for most code paths.
func getInitialDBStateForUserSpaceDb(ctx context.Context, db SqlDatabase) (dsess.InitialDbState, error) {
return dsess.InitialDbState{
Db: db,
DbData: env.DbData{
Rsw: noopRepoStateWriter{},
},
ReadOnly: true,
}, nil
}
// noopRepoStateWriter is a minimal implementation of RepoStateWriter that does nothing
type noopRepoStateWriter struct{}
@@ -809,11 +809,6 @@ func (p DoltDatabaseProvider) databaseForRevision(ctx *sql.Context, revDB string
}
func initialDbState(ctx context.Context, db SqlDatabase, branch string) (dsess.InitialDbState, error) {
switch db := db.(type) {
case *UserSpaceDatabase, *SingleTableInfoDatabase:
return getInitialDBStateForUserSpaceDb(ctx, db)
}
rsr := db.DbData().Rsr
ddb := db.DbData().Ddb
@@ -24,11 +24,19 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/table/editor"
)
// InitialDbState is the initial state of a database, as returned by SessionDatabase.InitialDBState. It is used to
// establish the in memory state of the session for every new transaction.
type InitialDbState struct {
Db sql.Database
HeadCommit *doltdb.Commit
ReadOnly bool
// WorkingSet is the working set for this database. May be nil for databases tied to a detached root value, in which
// case HeadCommit must be set
WorkingSet *doltdb.WorkingSet
// The head commit for this database. May be nil for databases tied to a detached root value, in which case
// RootValue must be set.
HeadCommit *doltdb.Commit
// HeadRoot is the root value for databases without a HeadCommit. Nil for databases with a HeadCommit.
HeadRoot *doltdb.RootValue
ReadOnly bool
DbData env.DbData
ReadReplica *env.Remote
Remotes map[string]env.Remote
+27 -18
View File
@@ -23,6 +23,7 @@ import (
"sync"
"time"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/globalstate"
"github.com/dolthub/go-mysql-server/sql"
sqltypes "github.com/dolthub/go-mysql-server/sql/types"
goerrors "gopkg.in/src-d/go-errors.v1"
@@ -33,7 +34,6 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/env"
"github.com/dolthub/dolt/go/libraries/doltcore/env/actions"
"github.com/dolthub/dolt/go/libraries/doltcore/ref"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/globalstate"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/writer"
"github.com/dolthub/dolt/go/libraries/doltcore/table/editor"
"github.com/dolthub/dolt/go/libraries/utils/config"
@@ -1135,23 +1135,26 @@ func (d *DoltSession) addDB(ctx *sql.Context, db SessionDatabase) error {
sessionState.readOnly, sessionState.readReplica = dbState.ReadOnly, dbState.ReadReplica
// TODO: figure out how to cast this to dsqle.SqlDatabase without creating import cycles
// Or better yet, get rid of EditOptions from the database, it's a session setting
nbf := types.Format_Default
if sessionState.dbData.Ddb != nil {
nbf = sessionState.dbData.Ddb.Format()
}
editOpts := db.(interface{ EditOptions() editor.Options }).EditOptions()
stateProvider, ok := db.(globalstate.StateProvider)
if !ok {
return fmt.Errorf("database does not contain global state store")
}
sessionState.globalState = stateProvider.GetGlobalState()
// WorkingSet is nil in the case of a read only, detached head DB
if dbState.Err != nil {
sessionState.Err = dbState.Err
} else if dbState.WorkingSet != nil {
sessionState.WorkingSet = dbState.WorkingSet
// TODO: this is pretty clunky, there is a silly dependency between InitialDbState and globalstate.StateProvider
// that's hard to express with the current types
stateProvider, ok := db.(globalstate.StateProvider)
if !ok {
return fmt.Errorf("database does not contain global state store")
}
sessionState.globalState = stateProvider.GetGlobalState()
tracker, err := sessionState.globalState.GetAutoIncrementTracker(ctx)
if err != nil {
return err
@@ -1160,13 +1163,17 @@ func (d *DoltSession) addDB(ctx *sql.Context, db SessionDatabase) error {
if err = d.SetWorkingSet(ctx, db.Name(), dbState.WorkingSet); err != nil {
return err
}
} else {
} else if dbState.HeadCommit != nil {
// WorkingSet is nil in the case of a read only, detached head DB
headRoot, err := dbState.HeadCommit.GetRootValue(ctx)
if err != nil {
return err
}
sessionState.headRoot = headRoot
} else if dbState.HeadRoot != nil {
sessionState.headRoot = dbState.HeadRoot
} else {
return fmt.Errorf("invalid initial state for database %s", db.Name())
}
// This has to happen after SetRoot above, since it does a stale check before its work
@@ -1276,13 +1283,15 @@ func (d *DoltSession) setSessionVarsForDb(ctx *sql.Context, dbName string) error
return err
}
h, err = state.headCommit.HashOf()
if err != nil {
return err
}
err = d.Session.SetSessionVariable(ctx, HeadKey(dbName), h.String())
if err != nil {
return err
if state.headCommit != nil {
h, err = state.headCommit.HashOf()
if err != nil {
return err
}
err = d.Session.SetSessionVariable(ctx, HeadKey(dbName), h.String())
if err != nil {
return err
}
}
return nil
@@ -97,7 +97,7 @@ func (rrd ReadReplicaDatabase) ValidReplicaState(ctx *sql.Context) bool {
// This seems like a pointless override from the embedded Database implementation, but it's necessary to pass the
// correct pointer type to the session initializer.
func (rrd ReadReplicaDatabase) InitialDBState(ctx *sql.Context, branch string) (dsess.InitialDbState, error) {
return GetInitialDBState(ctx, rrd, branch)
return initialDBState(ctx, rrd, branch)
}
func (rrd ReadReplicaDatabase) PullFromRemote(ctx *sql.Context) error {
@@ -89,7 +89,13 @@ func (db *SingleTableInfoDatabase) Collation() sql.CollationID {
}
func (db *SingleTableInfoDatabase) InitialDBState(ctx *sql.Context, branch string) (dsess.InitialDbState, error) {
return getInitialDBStateForUserSpaceDb(ctx, db)
return dsess.InitialDbState{
Db: db,
ReadOnly: true,
DbData: env.DbData{
Rsw: noopRepoStateWriter{},
},
}, nil
}
// Partitions implements sql.Table.
@@ -321,4 +327,4 @@ func (db *SingleTableInfoDatabase) EditOptions() editor.Options {
func (db *SingleTableInfoDatabase) Revision() string {
return ""
}
}
@@ -77,7 +77,14 @@ func (db *UserSpaceDatabase) GetTableNames(ctx *sql.Context) ([]string, error) {
}
func (db *UserSpaceDatabase) InitialDBState(ctx *sql.Context, branch string) (dsess.InitialDbState, error) {
return getInitialDBStateForUserSpaceDb(ctx, db)
return dsess.InitialDbState{
Db: db,
ReadOnly: true,
HeadRoot: db.RootValue,
DbData: env.DbData{
Rsw: noopRepoStateWriter{},
},
}, nil
}
func (db *UserSpaceDatabase) GetRoot(*sql.Context) (*doltdb.RootValue, error) {