Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 48 additions & 18 deletions go/libraries/doltcore/doltdb/doltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"time"

"github.com/dolthub/go-mysql-server/sql"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/sirupsen/logrus"

"github.com/dolthub/dolt/go/libraries/doltcore/dbfactory"
Expand All @@ -49,18 +50,19 @@ func init() {
types.CreateEditAccForMapEdits = edits.NewAsyncSortedEditsWithDefaults
}

// WORKING and STAGED identifiers refer to the working and staged roots in special circumstances where
// we expect to resolve a commit spec, but need working or staged
const (
// Working and Staged identifiers refer to the working and staged roots in special circumstances where
// we expect to resolve a commit spec, but need working or staged
Working = "WORKING"
Staged = "STAGED"
)

const (
CreationBranch = "create"

// 1GB.
defaultTargetFileSize = 1 << 30

// Keep most recent 10000 materialized commits
commitCacheSize = 10000
)

var ErrMissingDoltDataDir = errors.New("missing dolt data directory")
Expand All @@ -87,17 +89,29 @@ type DoltDB struct {
// parent directory as the database name. For non-filesystem based databases, the database name will not
// currently be populated.
databaseName string

// Keep a LRU Cache of materialized commits to speed up future commit resolutions
commitCache *lru.Cache[hash.Hash, *OptionalCommit]
}

// DoltDBFromCS creates a DoltDB from a noms chunks.ChunkStore
func DoltDBFromCS(cs chunks.ChunkStore, databaseName string) *DoltDB {
func DoltDBFromCS(cs chunks.ChunkStore, databaseName string) (*DoltDB, error) {
vrw := types.NewValueStore(cs)
ns := tree.NewNodeStore(cs)
db := datas.NewTypesDatabase(vrw, ns)

ret := &DoltDB{db: hooksDatabase{Database: db}, vrw: vrw, ns: ns, databaseName: databaseName}
commitCache, err := lru.New[hash.Hash, *OptionalCommit](commitCacheSize)
if err != nil {
return nil, err
}
ret := &DoltDB{
db: hooksDatabase{Database: db},
vrw: vrw,
ns: ns,
databaseName: databaseName,
commitCache: commitCache,
}
ret.db.db = ret
return ret
return ret, nil
}

// GetDatabaseName returns the name of the database.
Expand Down Expand Up @@ -154,7 +168,18 @@ func LoadDoltDBWithParams(ctx context.Context, nbf *types.NomsBinFormat, urlStr
return nil, err
}

ret := &DoltDB{db: hooksDatabase{Database: db}, vrw: vrw, ns: ns, databaseName: name}
commitCache, err := lru.New[hash.Hash, *OptionalCommit](commitCacheSize)
if err != nil {
return nil, err
}

ret := &DoltDB{
db: hooksDatabase{Database: db},
vrw: vrw,
ns: ns,
databaseName: name,
commitCache: commitCache,
}
ret.db.db = ret
return ret, nil
}
Expand Down Expand Up @@ -476,19 +501,24 @@ func (ddb *DoltDB) Resolve(ctx context.Context, cs *CommitSpec, cwb ref.DoltRef)

// ResolveHash takes a hash and returns an OptionalCommit directly.
// This assumes no ancestor spec resolution and no current working branch (cwb) needed.
func (ddb *DoltDB) ResolveHash(ctx context.Context, hash hash.Hash) (*OptionalCommit, error) {
commitValue, err := datas.LoadCommitAddr(ctx, ddb.vrw, hash)
if err != nil {
return nil, err
}
if commitValue.IsGhost() {
return &OptionalCommit{nil, hash}, nil
func (ddb *DoltDB) ResolveHash(ctx context.Context, h hash.Hash) (*OptionalCommit, error) {
if oc, ok := ddb.commitCache.Get(h); ok {
return oc, nil
}
commit, err := NewCommit(ctx, ddb.vrw, ddb.ns, commitValue)
commitValue, err := datas.LoadCommitAddr(ctx, ddb.vrw, h)
if err != nil {
return nil, err
}
return &OptionalCommit{commit, hash}, nil
oc := &OptionalCommit{Addr: h}
if !commitValue.IsGhost() {
commit, err := NewCommit(ctx, ddb.vrw, ddb.ns, commitValue)
if err != nil {
return nil, err
}
oc.Commit = commit
}
ddb.commitCache.Add(h, oc)
return oc, nil
}

// BootstrapShallowResolve is a special case of Resolve that is used to resolve a commit prior to pulling it's history
Expand Down
7 changes: 5 additions & 2 deletions go/libraries/doltcore/env/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ func NewMemoryDbData(ctx context.Context, cfg config.ReadableConfig) (DbData[con
func NewMemoryDoltDB(ctx context.Context, initBranch string) (*doltdb.DoltDB, error) {
ts := &chunks.TestStorage{}
cs := ts.NewViewWithDefaultFormat()
ddb := doltdb.DoltDBFromCS(cs, "")
ddb, err := doltdb.DoltDBFromCS(cs, "")
if err != nil {
return nil, err
}

m := "memory"
branchRef := ref.NewBranchRef(initBranch)
err := ddb.WriteEmptyRepoWithCommitTimeAndDefaultBranch(ctx, m, m, datas.CommitterDate(), branchRef)
err = ddb.WriteEmptyRepoWithCommitTimeAndDefaultBranch(ctx, m, m, datas.CommitterDate(), branchRef)
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion go/libraries/doltcore/migrate/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,10 @@ func initTestMigrationDB(ctx context.Context) (*doltdb.DoltDB, error) {
if err != nil {
return nil, err
}
return doltdb.DoltDBFromCS(cs, ""), nil

ddb, err := doltdb.DoltDBFromCS(cs, "")
if err != nil {
return nil, err
}
return ddb, nil
}
Loading