Skip to content

Commit

Permalink
if repo not found in badger cache, check boltdb. if found in boltdb, …
Browse files Browse the repository at this point in the history
…set in badger and return
  • Loading branch information
shawnps committed Feb 10, 2020
1 parent ec94215 commit f3452d8
Showing 1 changed file with 56 additions and 8 deletions.
64 changes: 56 additions & 8 deletions handlers/checks.go
Expand Up @@ -2,10 +2,12 @@ package handlers

import (
"encoding/json"
"errors"
"fmt"
"log"
"time"

"github.com/boltdb/bolt"
"github.com/dgraph-io/badger"
"github.com/dustin/go-humanize"
"github.com/gojp/goreportcard/check"
Expand Down Expand Up @@ -33,20 +35,66 @@ func getFromCache(db *badger.DB, repo string) (checksResp, error) {
return err
}

if item == nil {
return notFoundError{repo}
if item != nil {
log.Printf("repo %q found in badger cache", repo)
err = item.Value(func(val []byte) error {
err = json.Unmarshal(val, &resp)
if err != nil {
return fmt.Errorf("failed to parse JSON for %q in cache", repo)
}

return nil
})
}

err = item.Value(func(val []byte) error {
err = json.Unmarshal(val, &resp)
if item == nil {
log.Printf("Repo %q not found in badger cache. Checking boltdb...", repo)
// try getting from boltdb and saving to badger.
boltDB, err := bolt.Open("goreportcard.db", 0600, &bolt.Options{Timeout: 3 * time.Second})
if err != nil {
return fmt.Errorf("failed to parse JSON for %q in cache", repo)
return fmt.Errorf("failed to open bolt database during GET: %v", err)
}
defer boltDB.Close()

err = boltDB.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("repos"))
if b == nil {
return errors.New("No repo bucket")
}
cached := b.Get([]byte(repo))
if cached == nil {
log.Println("Repo not found in boltdb")
return notFoundError{repo}
}

err = json.Unmarshal(cached, &resp)
if err != nil {
return fmt.Errorf("failed to parse JSON for %q in cache", repo)
}

return db.Update(func(txn *badger.Txn) error {
log.Printf("Repo %q found in boltdb. Saving to badger cache...", repo)

// save repo to cache
err = txn.Set([]byte(RepoPrefix+repo), cached)
if err != nil {
return err
}

return updateMetadata(txn, resp, repo, false)
})
})

return nil
})
if err != nil {
return err
}

return err
if resp.Repo == "" {
return notFoundError{repo}
}
}

return nil
})

if err != nil {
Expand Down

0 comments on commit f3452d8

Please sign in to comment.