br0xen/boltbrowser

Bucket names as byte-encoded integers produces unreadable UI

utdrmac opened this issue · 1 comments

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

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.