From b873121f10d1ee7ab42be52640a23b26523fcd28 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Mon, 14 Mar 2016 15:25:09 -0400 Subject: [PATCH] Open db only when needed Former-commit-id: df0d9f6391b8490cd61bb1c49ec8fb8331873156 [formerly 73f2ff826d1711d937619618c6075dca99b3c7a8] [formerly 72a58bdb3ac3708539dbd28ea5248776ab741567 [formerly 9675611275fc51ba1e74ddba2653a51487b589d6 [formerly b767dd035200cac24ce2c69ec2e8e48c0d0e9c8c]]] Former-commit-id: 616ef7e0ec57636152e1e338ec8e8b4c29dab262 [formerly df0a13da1e51f144a12dfc51dbed1f42b3546912] Former-commit-id: 6c3f76f8a4f4cfd361b2a87fe0a09c717db94422 Former-commit-id: 5721604187b5af70ecf63793aa2a7ed1cb6938d2 --- db.go | 8 ++++++++ main.go | 8 ++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/db.go b/db.go index 0460467..5b1c213 100644 --- a/db.go +++ b/db.go @@ -43,6 +43,8 @@ type WikiData struct { } func hasPassword(title string) (bool, error) { + Open(RuntimeArgs.DatabaseLocation) + defer Close() title = strings.ToLower(title) if !open { 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) { + Open(RuntimeArgs.DatabaseLocation) + defer Close() title = strings.ToLower(title) var vi []versionsInfo 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 { + Open(RuntimeArgs.DatabaseLocation) + defer Close() title = strings.ToLower(title) if !open { 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 { + Open(RuntimeArgs.DatabaseLocation) + defer Close() if !open { return fmt.Errorf("db must be opened before saving") } diff --git a/main.go b/main.go index 8afbb38..f9a128e 100644 --- a/main.go +++ b/main.go @@ -73,10 +73,9 @@ Options:`) } RuntimeArgs.SourcePath = cwd - Open(RuntimeArgs.DatabaseLocation) - defer Close() - // create programdata bucket + Open(RuntimeArgs.DatabaseLocation) + err := db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucketIfNotExists([]byte("programdata")) if err != nil { @@ -87,7 +86,8 @@ Options:`) if err != nil { panic(err) } - + Close() + // Default page aboutFile, _ := ioutil.ReadFile(path.Join(RuntimeArgs.SourcePath, "templates/aboutpage.md")) p := WikiData{"help", "", []string{}, []string{}, false}