br0xen / boltbrowser

A CLI Browser for BoltDB Files
GNU General Public License v3.0
631 stars 91 forks source link

Bucket names as byte-encoded integers produces unreadable UI #34

Closed utdrmac closed 5 years ago

utdrmac commented 5 years ago

The code below creates a series of buckets with integer-encoded names. See screenshot of what this results in. The bucket names are not decoded correctly on display. Integer-encoded keys, however, show up correctly.

package main

import (
    "encoding/binary"

    log "github.com/sirupsen/logrus"
    bolt "github.com/etcd-io/bbolt"
)

func main() {

    db, err := bolt.Open("rewards.db", 0600, nil)
    if err != nil {
        log.Fatal("Failed to init db:", err)
    }

    db.Update(func(tx *bolt.Tx) error {

        rBucket, err := tx.CreateBucketIfNotExists([]byte("rewards"))
        if err != nil {
            log.Fatal("Cannot create rewards bucket:", err)
        }

        for i := 1; i < 20; i++ {
            buk, err := rBucket.CreateBucketIfNotExists(inttob(i));
            if err != nil {
                log.Fatal("Cannot make bucket", i)
            }

            buk.Put(inttob(i*3), []byte("Hello"))
        }

        return nil
    })

    log.Print("Done")
}

func inttob(v int) []byte {
    return itob(int64(v))
}

// itob returns an 8-byte big endian representation of v.
func itob(v int64) []byte {
    b := make([]byte, 8)
    binary.BigEndian.PutUint64(b, uint64(v))
    return b
}

func btoi(b []byte) int64 {
    return int64(binary.BigEndian.Uint64(b))
}

Screen Shot 2019-10-12 at 7 46 37 PM

utdrmac commented 5 years ago

Just to note. That when you retrieve or cursor-scan the bucket names, decoding the bytes back into int64 works just fine within our main application. It only appears to be an "issue" when browsing using this tool.