package storeimport (bolt)varlogger = logutil.GetLogger("[store] ")varinitDB = map[string](func(*bolt.Tx) error){}// DBStore is the permanent storage backend for elvish. It is not thread-safe.// In particular, the store may be closed while another goroutine is still// accessing the To prevent bad things from happening, every time the// main goroutine spawns a new goroutine to operate on the store, it should call// Waits.Add(1) in the main goroutine before spawning another goroutine, and// call Waits.Done() in the spawned goroutine after the operation is finished.typeDBStoreinterface {StoreWaits() *sync.WaitGroupClose() error}typedbStorestruct {db *bolt.DB// Waits is used for registering outstanding operations on thewaitssync.WaitGroup}func ( string) (*bolt.DB, error) { , := bolt.Open(, 0644, &bolt.Options{Timeout: 1 * time.Second, })return , }// NewStore creates a new Store from the given file.func ( string) (DBStore, error) { , := dbWithDefaultOptions()if != nil {returnnil, }returnNewStoreFromDB()}// NewStoreFromDB creates a new Store from a bolt DB.func ( *bolt.DB) (DBStore, error) {logger.Println("initializing store")deferlogger.Println("initialized store") := &dbStore{db: ,waits: sync.WaitGroup{}, } := .Update(func( *bolt.Tx) error {for , := rangeinitDB { := ()if != nil {returnfmt.Errorf("failed to %s: %v", , ) } }returnnil })return , }// Waits returns a WaitGroup used to register outstanding storage requests when// making calls asynchronously.func ( *dbStore) () *sync.WaitGroup {return &.waits}// Close waits for all outstanding operations to finish, and closes the// database.func ( *dbStore) () error {if == nil || .db == nil {returnnil } .waits.Wait()return .db.Close()}
The pages are generated with Goldsv0.2.8-preview. (GOOS=darwin GOARCH=arm64)