Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: sheet lru structure (#12271)

Danny Xu982521fe8332
diff --git a/backend/component/sheet/sheet.go b/backend/component/sheet/sheet.go
index 7f2ac0e095..af2f51a388 100644
--- a/backend/component/sheet/sheet.go
+++ b/backend/component/sheet/sheet.go
@@ -5,6 +5,11 @@ import (
"io"
"log/slog"
"strings"
+ "sync"
+ "time"
+
+ lru "github.com/hashicorp/golang-lru/v2/expirable"
+ "github.com/zeebo/xxh3"
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/plugin/db/mssql"
@@ -17,13 +22,17 @@ import (
// Manager is the coordinator for all sheets and SQL statements.
type Manager struct {
- store *store.Store
+ sync.Mutex
+
+ store *store.Store
+ astCache *lru.LRU[uint64, *Result]
}
// NewManager creates a new sheet manager.
func NewManager(store *store.Store) *Manager {
return &Manager{
- store: store,
+ store: store,
+ astCache: lru.NewLRU[uint64, *Result](8, nil, 3*time.Minute),
}
}
@@ -136,3 +145,31 @@ func getSheetCommandsForMSSQL(statement string) []*storepb.SheetCommand {
}
return sheetCommands
}
+
+type Result struct {
+ sync.Mutex
+ ast any
+ err error
+}
+
+func (sm *Manager) GetAST(_ storepb.Engine, statement string) (any, error) {
+ var result *Result
+ hashKey := xxh3.HashString(statement)
+ sm.Lock()
+ if v, ok := sm.astCache.Get(hashKey); ok {
+ result = v
+ } else {
+ result = &Result{}
+ sm.astCache.Add(hashKey, result)
+ }
+ sm.Unlock()
+
+ result.Lock()
+ defer result.Unlock()
+ if result.ast != nil {
+ return result.ast, result.err
+ }
+ // TODO(d): do something.
+ result.ast = 1
+ return result.ast, result.err
+}
diff --git a/go.mod b/go.mod
index 0afa86286d..14673f4fb6 100644
--- a/go.mod
+++ b/go.mod
@@ -78,6 +78,7 @@ require (
github.com/vjeantet/ldapserver v1.0.1
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8
github.com/xuri/excelize/v2 v2.8.1
+ github.com/zeebo/xxh3 v1.0.2
go.mongodb.org/mongo-driver v1.15.0
go.uber.org/multierr v1.11.0
golang.org/x/crypto v0.23.0
@@ -175,7 +176,6 @@ require (
github.com/vcaesar/cedar v0.20.1 // indirect
github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53 // indirect
github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05 // indirect
- github.com/zeebo/xxh3 v1.0.2 // indirect
go.etcd.io/etcd/api/v3 v3.5.10 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.10 // indirect
go.etcd.io/etcd/client/v3 v3.5.10 // indirect