Skip to content

Commit

Permalink
Added 'GetAll' method for BoltDB
Browse files Browse the repository at this point in the history
  • Loading branch information
danesparza committed Mar 11, 2016
1 parent 57b6f7e commit 7599131
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions cmd/cc-boltinit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ func main() {
ct3, _ := db.Get(query)
fmt.Printf("Found config item: %s value: %s \n", ct3.Name, ct3.Value)

// Get all config items:
cis, _ := db.GetAll("Formbuilder")
for _, configItem := range cis {
fmt.Printf("%s - %s:%s\n", configItem.Application, configItem.Name, configItem.Value)
}
}
43 changes: 40 additions & 3 deletions datastores/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ func (store BoltDB) Get(configItem ConfigItem) (ConfigItem, error) {
// Our return item:
retval := ConfigItem{}

// Need to decide on appropriate nesting structure for BoltDB...
// Perhaps {appname} or {} -> Configitems?
// ^ this means 'global'
// TODO: Get global first, then get application, then get application + machine
// Get the key based on the application
err = db.View(func(tx *bolt.Tx) error {
// Get the item from the bucket with the app name
b := tx.Bucket([]byte(configItem.Application))
Expand All @@ -55,6 +54,44 @@ func (store BoltDB) Get(configItem ConfigItem) (ConfigItem, error) {
return retval, err
}

func (store BoltDB) GetAll(application string) ([]ConfigItem, error) {
// Open the database:
db, err := bolt.Open(store.Database, 0600, nil)
checkErr(err)
defer db.Close()

// Our return items:
retval := []ConfigItem{}

// TODO: Get global first, then get application, then get application + machine
// Get the key based on the application
err = db.View(func(tx *bolt.Tx) error {

// Get the items from the bucket with the app name
b := tx.Bucket([]byte(application))

c := b.Cursor()

for k, v := c.First(); k != nil; k, v = c.Next() {

// Unmarshal data into our config item
ci := ConfigItem{}
if err := json.Unmarshal(v, &ci); err != nil {
return err
}

// Add the item to our list of config items
retval = append(retval, ci)
}

return nil
})

checkErr(err)

return retval, err
}

func (store BoltDB) Set(configItem ConfigItem) error {

// Open the database:
Expand Down

0 comments on commit 7599131

Please sign in to comment.