1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Open db only when needed

This commit is contained in:
Zack Scholl 2016-03-14 15:25:09 -04:00
parent 5b423cf3a0
commit b767dd0352
2 changed files with 12 additions and 4 deletions

8
db.go
View File

@ -43,6 +43,8 @@ type WikiData struct {
} }
func hasPassword(title string) (bool, error) { func hasPassword(title string) (bool, error) {
Open(RuntimeArgs.DatabaseLocation)
defer Close()
title = strings.ToLower(title) title = strings.ToLower(title)
if !open { if !open {
return false, fmt.Errorf("db must be opened before loading") return false, fmt.Errorf("db must be opened before loading")
@ -79,6 +81,8 @@ func hasPassword(title string) (bool, error) {
} }
func getCurrentText(title string, version int) (string, []versionsInfo, bool, time.Duration, bool) { func getCurrentText(title string, version int) (string, []versionsInfo, bool, time.Duration, bool) {
Open(RuntimeArgs.DatabaseLocation)
defer Close()
title = strings.ToLower(title) title = strings.ToLower(title)
var vi []versionsInfo var vi []versionsInfo
totalTime := time.Now().Sub(time.Now()) totalTime := time.Now().Sub(time.Now())
@ -122,6 +126,8 @@ func getCurrentText(title string, version int) (string, []versionsInfo, bool, ti
} }
func (p *WikiData) load(title string) error { func (p *WikiData) load(title string) error {
Open(RuntimeArgs.DatabaseLocation)
defer Close()
title = strings.ToLower(title) title = strings.ToLower(title)
if !open { if !open {
return fmt.Errorf("db must be opened before loading!") return fmt.Errorf("db must be opened before loading!")
@ -156,6 +162,8 @@ func (p *WikiData) load(title string) error {
} }
func (p *WikiData) save(newText string) error { func (p *WikiData) save(newText string) error {
Open(RuntimeArgs.DatabaseLocation)
defer Close()
if !open { if !open {
return fmt.Errorf("db must be opened before saving") return fmt.Errorf("db must be opened before saving")
} }

View File

@ -73,10 +73,9 @@ Options:`)
} }
RuntimeArgs.SourcePath = cwd RuntimeArgs.SourcePath = cwd
Open(RuntimeArgs.DatabaseLocation)
defer Close()
// create programdata bucket // create programdata bucket
Open(RuntimeArgs.DatabaseLocation)
err := db.Update(func(tx *bolt.Tx) error { err := db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("programdata")) _, err := tx.CreateBucketIfNotExists([]byte("programdata"))
if err != nil { if err != nil {
@ -87,6 +86,7 @@ Options:`)
if err != nil { if err != nil {
panic(err) panic(err)
} }
Close()
// Default page // Default page
aboutFile, _ := ioutil.ReadFile(path.Join(RuntimeArgs.SourcePath, "templates/aboutpage.md")) aboutFile, _ := ioutil.ReadFile(path.Join(RuntimeArgs.SourcePath, "templates/aboutpage.md"))