github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/dsess/globalstate.go (about) 1 // Copyright 2023 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 dsess 16 17 import ( 18 "context" 19 "sync" 20 21 "github.com/dolthub/go-mysql-server/sql" 22 23 "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" 24 "github.com/dolthub/dolt/go/libraries/doltcore/ref" 25 "github.com/dolthub/dolt/go/libraries/doltcore/sqle/globalstate" 26 ) 27 28 func NewGlobalStateStoreForDb(ctx context.Context, dbName string, db *doltdb.DoltDB) (GlobalStateImpl, error) { 29 branches, err := db.GetBranches(ctx) 30 if err != nil { 31 return GlobalStateImpl{}, err 32 } 33 34 remotes, err := db.GetRemoteRefs(ctx) 35 if err != nil { 36 return GlobalStateImpl{}, err 37 } 38 39 rootRefs := make([]ref.DoltRef, 0, len(branches)+len(remotes)) 40 rootRefs = append(rootRefs, branches...) 41 rootRefs = append(rootRefs, remotes...) 42 43 var roots []doltdb.Rootish 44 for _, b := range rootRefs { 45 switch b.GetType() { 46 case ref.BranchRefType: 47 wsRef, err := ref.WorkingSetRefForHead(b) 48 if err != nil { 49 return GlobalStateImpl{}, err 50 } 51 52 ws, err := db.ResolveWorkingSet(ctx, wsRef) 53 if err == doltdb.ErrWorkingSetNotFound { 54 // use the branch head if there isn't a working set for it 55 cm, err := db.ResolveCommitRef(ctx, b) 56 if err != nil { 57 return GlobalStateImpl{}, err 58 } 59 roots = append(roots, cm) 60 } else if err != nil { 61 return GlobalStateImpl{}, err 62 } else { 63 roots = append(roots, ws) 64 } 65 case ref.RemoteRefType: 66 cm, err := db.ResolveCommitRef(ctx, b) 67 if err != nil { 68 return GlobalStateImpl{}, err 69 } 70 roots = append(roots, cm) 71 } 72 } 73 74 tracker, err := NewAutoIncrementTracker(ctx, dbName, roots...) 75 if err != nil { 76 return GlobalStateImpl{}, err 77 } 78 79 return GlobalStateImpl{ 80 aiTracker: tracker, 81 mu: &sync.Mutex{}, 82 }, nil 83 } 84 85 type GlobalStateImpl struct { 86 aiTracker globalstate.AutoIncrementTracker 87 mu *sync.Mutex 88 } 89 90 var _ globalstate.GlobalState = GlobalStateImpl{} 91 92 func (g GlobalStateImpl) AutoIncrementTracker(ctx *sql.Context) (globalstate.AutoIncrementTracker, error) { 93 return g.aiTracker, nil 94 }