Skip to content

Commit

Permalink
refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
max-hoffman committed May 20, 2024
1 parent 51caf2a commit 1bde799
Show file tree
Hide file tree
Showing 17 changed files with 284 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,18 @@ func getTableWriter(ctx *sql.Context, engine *gms.Engine, tableName, databaseNam

ds := dsess.DSessFromSess(ctx.Session)
setter := ds.SetWorkingRoot
tableWriter, err := writeSession.GetTableWriter(ctx, doltdb.TableName{Name: tableName}, databaseName, setter)

table, _, err := engine.Analyzer.Catalog.Table(ctx, databaseName, tableName)
if err != nil {
return nil, nil, err
}

dTab, err := table.(*sqle.WritableDoltTable).DoltTable.DoltTable(ctx)
if err != nil {
return nil, nil, err
}

tableWriter, err := writeSession.GetTableWriter(ctx, dTab, doltdb.TableName{Name: tableName}, databaseName, setter)
if err != nil {
return nil, nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,9 @@ func (db Database) GetTriggers(ctx *sql.Context) ([]sql.TriggerDefinition, error
}

key, err := doltdb.NewDataCacheKey(root)
if err != nil {
return nil, err
}

ds := dsess.DSessFromSess(ctx.Session)
dbState, _, err := ds.LookupDbState(ctx, db.RevisionQualifiedName())
Expand Down
49 changes: 8 additions & 41 deletions go/libraries/doltcore/sqle/dsess/session.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/cmd/dolt/cli"
"strconv"
"strings"
"sync"
Expand All @@ -27,7 +28,6 @@ import (
sqltypes "github.com/dolthub/go-mysql-server/sql/types"
"github.com/shopspring/decimal"

"github.com/dolthub/dolt/go/cmd/dolt/cli"
"github.com/dolthub/dolt/go/libraries/doltcore/branch_control"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/env"
Expand Down Expand Up @@ -61,7 +61,7 @@ type DoltSession struct {
statsProv sql.StatsProvider
mu *sync.Mutex
fs filesys.Filesys
writeSessProv writeSessFunc
writeSessProv WriteSessFunc

// If non-nil, this will be returned from ValidateSession.
// Used by sqle/cluster to put a session into a terminal err state.
Expand All @@ -74,7 +74,7 @@ var _ sql.TransactionSession = (*DoltSession)(nil)
var _ branch_control.Context = (*DoltSession)(nil)

// DefaultSession creates a DoltSession with default values
func DefaultSession(pro DoltDatabaseProvider, sessFunc writeSessFunc) *DoltSession {
func DefaultSession(pro DoltDatabaseProvider, sessFunc WriteSessFunc) *DoltSession {
return &DoltSession{
Session: sql.NewBaseSession(),
username: "",
Expand All @@ -91,16 +91,14 @@ func DefaultSession(pro DoltDatabaseProvider, sessFunc writeSessFunc) *DoltSessi
}
}

type writeSessFunc func(nbf *types.NomsBinFormat, ws *doltdb.WorkingSet, aiTracker globalstate.AutoIncrementTracker, opts editor.Options) WriteSession

// NewDoltSession creates a DoltSession object from a standard sql.Session and 0 or more Database objects.
func NewDoltSession(
sqlSess *sql.BaseSession,
pro DoltDatabaseProvider,
conf config.ReadWriteConfig,
branchController *branch_control.Controller,
statsProvider sql.StatsProvider,
writeSessProv writeSessFunc,
writeSessProv WriteSessFunc,
) (*DoltSession, error) {
username := conf.GetStringOrDefault(config.UserNameKey, "")
email := conf.GetStringOrDefault(config.UserEmailKey, "")
Expand Down Expand Up @@ -1748,38 +1746,7 @@ func DefaultHead(baseName string, db SqlDatabase) (string, error) {
return head, nil
}

// WriteSession encapsulates writes made within a SQL session.
// It's responsible for creating and managing the lifecycle of TableWriter's.
type WriteSession interface {
// GetTableWriter creates a TableWriter and adds it to the WriteSession.
GetTableWriter(ctx *sql.Context, tableName doltdb.TableName, db string, setter SessionRootSetter) (TableWriter, error)

// SetWorkingSet modifies the state of the WriteSession. The WorkingSetRef of |ws| must match the existing Ref.
SetWorkingSet(ctx *sql.Context, ws *doltdb.WorkingSet) error

// GetOptions returns the editor.Options for this session.
GetOptions() editor.Options

// SetOptions sets the editor.Options for this session.
SetOptions(opts editor.Options)

WriteSessionFlusher
}

type TableWriter interface {
sql.TableEditor
sql.ForeignKeyEditor
sql.AutoIncrementSetter
}

// SessionRootSetter sets the root value for the session.
type SessionRootSetter func(ctx *sql.Context, dbName string, root doltdb.RootValue) error

// WriteSessionFlusher is responsible for flushing any pending edits to the session
type WriteSessionFlusher interface {
// Flush flushes the pending writes in the session.
Flush(ctx *sql.Context) (*doltdb.WorkingSet, error)
// FlushWithAutoIncrementOverrides flushes the pending writes in the session, overriding the auto increment values
// for any tables provided in the map
FlushWithAutoIncrementOverrides(ctx *sql.Context, increment bool, autoIncrements map[string]uint64) (*doltdb.WorkingSet, error)
}
// WriteSessFunc is a constructor that session builders use to
// create fresh table editors.
// The indirection avoids a writer/dsess package import cycle.
type WriteSessFunc func(nbf *types.NomsBinFormat, ws *doltdb.WorkingSet, aiTracker globalstate.AutoIncrementTracker, opts editor.Options) WriteSession
33 changes: 1 addition & 32 deletions go/libraries/doltcore/sqle/dsess/session_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ import (
"github.com/dolthub/go-mysql-server/sql"

"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
"github.com/dolthub/dolt/go/store/val"
)

// SessionCache caches various pieces of expensive to compute information to speed up future lookups in the session.
type SessionCache struct {
// indexes is keyed by table schema
// indexes is keyed by table schema hash
indexes map[doltdb.DataCacheKey]map[string][]sql.Index
// tables is keyed by table root value
tables map[doltdb.DataCacheKey]map[TableCacheKey]sql.Table
Expand All @@ -42,32 +40,6 @@ type SessionCache struct {
mu sync.RWMutex
}

// WriterState caches expensive objects required for writing rows.
// All objects in writerState are valid as long as a table schema
// is the same.
type WriterState struct {
DoltSchema schema.Schema
PkSchema sql.PrimaryKeySchema
PriIndex IndexState
SecIndexes []IndexState
AutoIncCol schema.Column
}

// IndexState caches objects required for writing specific indexes.
// The objects are valid as long as the index's schema is the same.
type IndexState struct {
Name string
Schema schema.Schema
ValMapping val.OrdinalMapping
KeyMapping val.OrdinalMapping
PkMapping val.OrdinalMapping
IsFullText bool
IsUnique bool
IsSpatial bool
PrefixLengths []uint16
Count int
}

// DatabaseCache stores databases and their initial states, offloading the compute / IO involved in resolving a
// database name to a particular database. This is safe only because the database objects themselves don't have any
// handles to data or state, but always defer to the session. Keys in the secondary map are revision specifier strings
Expand Down Expand Up @@ -216,9 +188,6 @@ func (c *SessionCache) GetCachedTable(key doltdb.DataCacheKey, tableName TableCa
func (c *SessionCache) GetCachedWriterState(key doltdb.DataCacheKey) (*WriterState, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
if c.writers == nil {
return nil, false
}
schemaState, ok := c.writers[key]
return schemaState, ok
}
Expand Down
85 changes: 85 additions & 0 deletions go/libraries/doltcore/sqle/dsess/table_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dsess

import (
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
"github.com/dolthub/dolt/go/libraries/doltcore/table/editor"
"github.com/dolthub/dolt/go/store/val"
"github.com/dolthub/go-mysql-server/sql"
)

// WriteSession encapsulates writes made within a SQL session.
// It's responsible for creating and managing the lifecycle of TableWriter's.
type WriteSession interface {
// GetTableWriter creates a TableWriter and adds it to the WriteSession.
GetTableWriter(ctx *sql.Context, t *doltdb.Table, table doltdb.TableName, db string, setter SessionRootSetter) (TableWriter, error)

// SetWorkingSet modifies the state of the WriteSession. The WorkingSetRef of |ws| must match the existing Ref.
SetWorkingSet(ctx *sql.Context, ws *doltdb.WorkingSet) error

// GetOptions returns the editor.Options for this session.
GetOptions() editor.Options

// SetOptions sets the editor.Options for this session.
SetOptions(opts editor.Options)

WriteSessionFlusher
}

type TableWriter interface {
sql.TableEditor
sql.ForeignKeyEditor
sql.AutoIncrementSetter
}

// SessionRootSetter sets the root value for the session.
type SessionRootSetter func(ctx *sql.Context, dbName string, root doltdb.RootValue) error

// WriteSessionFlusher is responsible for flushing any pending edits to the session
type WriteSessionFlusher interface {
// Flush flushes the pending writes in the session.
Flush(ctx *sql.Context) (*doltdb.WorkingSet, error)
// FlushWithAutoIncrementOverrides flushes the pending writes in the session, overriding the auto increment values
// for any tables provided in the map
FlushWithAutoIncrementOverrides(ctx *sql.Context, increment bool, autoIncrements map[string]uint64) (*doltdb.WorkingSet, error)
}

// WriterState caches expensive objects required for writing rows.
// All objects in writerState are valid as long as a table schema
// is the same.
type WriterState struct {
DoltSchema schema.Schema
PkSchema sql.PrimaryKeySchema
PriIndex IndexState
SecIndexes []IndexState
AutoIncCol schema.Column
}

// IndexState caches objects required for writing specific indexes.
// The objects are valid as long as the index's schema is the same.
type IndexState struct {
Name string
Schema schema.Schema
ValMapping val.OrdinalMapping
KeyMapping val.OrdinalMapping
PkMapping val.OrdinalMapping
IsFullText bool
IsUnique bool
IsSpatial bool
PrefixLengths []uint16
Count int
}
8 changes: 6 additions & 2 deletions go/libraries/doltcore/sqle/dtables/docs_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package dtables

import (
"fmt"

"github.com/dolthub/go-mysql-server/sql"
sqlTypes "github.com/dolthub/go-mysql-server/sql/types"
"github.com/dolthub/vitess/go/sqltypes"
Expand Down Expand Up @@ -258,7 +257,12 @@ func (iw *docsWriter) StatementBegin(ctx *sql.Context) {
}

if ws := dbState.WriteSession(); ws != nil {
tableWriter, err := ws.GetTableWriter(ctx, doltdb.TableName{Name: doltdb.DocTableName}, dbName, dSess.SetWorkingRoot)
table, _, err := roots.Working.GetTable(ctx, doltdb.TableName{Name: doltdb.DocTableName})
if err != nil {
iw.errDuringStatementBegin = err
return
}
tableWriter, err := ws.GetTableWriter(ctx, table, doltdb.TableName{Name: doltdb.DocTableName}, dbName, dSess.SetWorkingRoot)
if err != nil {
iw.errDuringStatementBegin = err
return
Expand Down
8 changes: 6 additions & 2 deletions go/libraries/doltcore/sqle/dtables/ignore_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package dtables

import (
"fmt"

"github.com/dolthub/go-mysql-server/sql"
sqlTypes "github.com/dolthub/go-mysql-server/sql/types"

Expand Down Expand Up @@ -274,7 +273,12 @@ func (iw *ignoreWriter) StatementBegin(ctx *sql.Context) {
}

if ws := dbState.WriteSession(); ws != nil {
tableWriter, err := ws.GetTableWriter(ctx, doltdb.TableName{Name: doltdb.IgnoreTableName}, dbName, dSess.SetWorkingRoot)
table, _, err := roots.Working.GetTable(ctx, doltdb.TableName{Name: doltdb.IgnoreTableName})
if err != nil {
iw.errDuringStatementBegin = err
return
}
tableWriter, err := ws.GetTableWriter(ctx, table, doltdb.TableName{Name: doltdb.IgnoreTableName}, dbName, dSess.SetWorkingRoot)
if err != nil {
iw.errDuringStatementBegin = err
return
Expand Down
11 changes: 8 additions & 3 deletions go/libraries/doltcore/sqle/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,12 @@ func (t *WritableDoltTable) getTableEditor(ctx *sql.Context) (ed dsess.TableWrit
}

setter := ds.SetWorkingRoot
ed, err = writeSession.GetTableWriter(ctx, doltdb.TableName{Name: t.tableName, Schema: t.db.schemaName}, t.db.RevisionQualifiedName(), setter)
dTab, err := t.DoltTable.DoltTable(ctx)
if err != nil {
return nil, err
}

ed, err = writeSession.GetTableWriter(ctx, dTab, doltdb.TableName{Name: t.tableName}, t.db.RevisionQualifiedName(), setter)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1712,7 +1717,7 @@ func (t *AlterableDoltTable) RewriteInserter(
opts.ForeignKeyChecksDisabled = true
writeSession := writer.NewWriteSession(dt.Format(), newWs, ait, opts)

ed, err := writeSession.GetTableWriter(ctx, doltdb.TableName{Name: t.Name(), Schema: t.db.schemaName}, t.db.RevisionQualifiedName(), sess.SetWorkingRoot)
ed, err := writeSession.GetTableWriter(ctx, dt, doltdb.TableName{Name: t.Name(), Schema: t.db.schemaName}, t.db.RevisionQualifiedName(), sess.SetWorkingRoot)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1761,7 +1766,7 @@ func fullTextRewriteEditor(
opts.ForeignKeyChecksDisabled = true
writeSession := writer.NewWriteSession(dt.Format(), newWs, ait, opts)

parentEditor, err := writeSession.GetTableWriter(ctx, doltdb.TableName{Name: t.Name(), Schema: t.db.schemaName}, t.db.RevisionQualifiedName(), sess.SetWorkingRoot)
parentEditor, err := writeSession.GetTableWriter(ctx, dt, doltdb.TableName{Name: t.Name(), Schema: t.db.schemaName}, t.db.RevisionQualifiedName(), sess.SetWorkingRoot)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions go/libraries/doltcore/sqle/temp_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func NewTempTable(
opts: opts,
}

tempTable.ed, err = writeSession.GetTableWriter(ctx, doltdb.TableName{Name: name}, db, setTempTableRoot(tempTable))
tempTable.ed, err = writeSession.GetTableWriter(ctx, tbl, doltdb.TableName{Name: name}, db, setTempTableRoot(tempTable))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -166,7 +166,7 @@ func setTempTableRoot(t *TempTable) func(ctx *sql.Context, dbName string, newRoo
}

writeSession := writer.NewWriteSession(newTable.Format(), newWs, ait, t.opts)
t.ed, err = writeSession.GetTableWriter(ctx, doltdb.TableName{Name: t.tableName}, t.dbName, setTempTableRoot(t))
t.ed, err = writeSession.GetTableWriter(ctx, t.table, doltdb.TableName{Name: t.tableName}, t.dbName, setTempTableRoot(t))
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions go/libraries/doltcore/sqle/testutil.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/libraries/utils/config"
"io"
"strings"

Expand All @@ -35,7 +36,6 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlutil"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/writer"
"github.com/dolthub/dolt/go/libraries/doltcore/table/editor"
config2 "github.com/dolthub/dolt/go/libraries/utils/config"
"github.com/dolthub/dolt/go/libraries/utils/filesys"
"github.com/dolthub/dolt/go/store/prolly/tree"
"github.com/dolthub/dolt/go/store/types"
Expand Down Expand Up @@ -112,7 +112,7 @@ func ExecuteSql(dEnv *env.DoltEnv, root doltdb.RootValue, statements string) (do
}

func NewTestSQLCtxWithProvider(ctx context.Context, pro dsess.DoltDatabaseProvider, statsPro sql.StatsProvider) *sql.Context {
s, err := dsess.NewDoltSession(sql.NewBaseSession(), pro, config2.NewMapConfig(make(map[string]string)), branch_control.CreateDefaultController(ctx), statsPro, writer.NewWriteSession)
s, err := dsess.NewDoltSession(sql.NewBaseSession(), pro, config.NewMapConfig(make(map[string]string)), branch_control.CreateDefaultController(ctx), statsPro, writer.NewWriteSession)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -401,8 +401,8 @@ func CreateTestEnvWithName(envName string) *env.DoltEnv {
dEnv := env.Load(context.Background(), homeDirFunc, fs, doltdb.InMemDoltDB+envName, "test")
cfg, _ := dEnv.Config.GetConfig(env.GlobalConfig)
cfg.SetStrings(map[string]string{
config2.UserNameKey: name,
config2.UserEmailKey: email,
config.UserNameKey: name,
config.UserEmailKey: email,
})

err := dEnv.InitRepo(context.Background(), types.Format_Default, name, email, env.DefaultInitBranch)
Expand Down
Loading

0 comments on commit 1bde799

Please sign in to comment.