Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get all items of a Bucket #263

Open
sh0umik opened this issue Nov 6, 2019 · 1 comment
Open

Get all items of a Bucket #263

sh0umik opened this issue Nov 6, 2019 · 1 comment

Comments

@sh0umik
Copy link

sh0umik commented Nov 6, 2019

When I use Set and Get it requires a bucket name.

db.Set("logs", time.Now(), "I'm eating my breakfast man")
db.Get("logs", someObjectId, &log)

But how can I get all the items in the particular bucket logs ? How to iterate over a bucket elements ?

Proposing Something like

var allLogs []Logs
err := db.GetAll("bucketname", &allLogs)
@waveletlet
Copy link

This is similar to #209, but I can't figure out how to do it with the .Each() method mentioned in that discussion (or a db.All()) with a very simple key:value store (string:bool).

The best I've figured out so far is to do a manual Bolt transaction. I only want the keys in my case:

// create db, do some 'db.Set("urls", urlString, true)'
var urls []string
db.Bolt.View(func(tx *bolt.Tx) error {    
  b := tx.Bucket([]byte("urls"))    
  c := b.Cursor()                                         
  for k, v := c.First(); k != nil; k, v = c.Next() {    
    fmt.Printf("key=%s, value=%s\n", k, v)
    urls = append(urls, string(k))  //convert to string because Bolt holds it as []byte
  }    
  return nil    
}) 

Is there any more direct Storm way of doing that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants