Repository: dolthub/dolt
File: go/libraries/doltcore/doltdb/doltdb.go (Go)

Code before:

// LocalDirDoltDB stores the db in the current directory
var LocalDirDoltDB = "file://./" + dbfactory.DoltDataDir
var LocalDirStatsDB = "file://./" + dbfactory.DoltStatsDir

// InMemDoltDB stores the DoltDB db in memory and is primarily used for testing
var InMemDoltDB = "mem://"

var ErrNoRootValAtHash = errors.New("there is no dolt root value at that hash")
var ErrCannotDeleteLastBranch = errors.New("cannot delete the last branch")

// DoltDB wraps access to the underlying noms database and hides some of the details of the underlying storage.
type DoltDB struct {
	db  hooksDatabase
	vrw types.ValueReadWriter
	ns  tree.NodeStore

	// databaseName holds the name of the database for this DoltDB instance. Note that this name may not be
	// populated for all DoltDB instances. For filesystem based databases, the database name is determined
	// by looking through the filepath in reverse, finding the first .dolt directory, and then taking the
	// parent directory as the database name. For non-filesystem based databases, the database name will not
	// currently be populated.
	databaseName string

	mu          sync.Mutex // this is to guard the commitCache
	commitCache map[hash.Hash]*OptionalCommit
}

// DoltDBFromCS creates a DoltDB from a noms chunks.ChunkStore
func DoltDBFromCS(cs chunks.ChunkStore, databaseName string) *DoltDB {
	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:  make(map[hash.Hash]*OptionalCommit),
	}
	ret.db.db = ret
	return ret
}

// GetDatabaseName returns the name of the database.
// Note: This can return an empty string if the database name is not populated.
func (ddb *DoltDB) GetDatabaseName() string {
	return ddb.databaseName
}


Review comment (performance):
Changes look good, but feels like we shouldn't let this cache grow without bound. We already use the `hashicorp/golang-lru` package in Dolt, so that could be an easy way to plug in an LRU cache that we can put a max size on. 

Code after:

var ErrMissingDoltDataDir = errors.New("missing dolt data directory")

// LocalDirDoltDB stores the db in the current directory
var LocalDirDoltDB = "file://./" + dbfactory.DoltDataDir
var LocalDirStatsDB = "file://./" + dbfactory.DoltStatsDir

// InMemDoltDB stores the DoltDB db in memory and is primarily used for testing
var InMemDoltDB = "mem://"

var ErrNoRootValAtHash = errors.New("there is no dolt root value at that hash")
var ErrCannotDeleteLastBranch = errors.New("cannot delete the last branch")

// DoltDB wraps access to the underlying noms database and hides some of the details of the underlying storage.
type DoltDB struct {
	db  hooksDatabase
	vrw types.ValueReadWriter
	ns  tree.NodeStore

	// databaseName holds the name of the database for this DoltDB instance. Note that this name may not be
	// populated for all DoltDB instances. For filesystem based databases, the database name is determined
	// by looking through the filepath in reverse, finding the first .dolt directory, and then taking the
	// 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, error) {
	vrw := types.NewValueStore(cs)
	ns := tree.NewNodeStore(cs)
	db := datas.NewTypesDatabase(vrw, ns)
	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, nil
}

// GetDatabaseName returns the name of the database.