br0xen / boltbrowser

A CLI Browser for BoltDB Files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Read-Only mode?

coreyog opened this issue · comments

I have a trivial testing app:

package main

import (
	"fmt"
	"strconv"

	"github.com/boltdb/bolt"
)

func main() {
	db, err := bolt.Open("my.db", 0600, nil)
	if err != nil {
		panic(err)
	}
	defer db.Close()

	err = db.Update(func(tx *bolt.Tx) (err error) {
		bucket, err := tx.CreateBucketIfNotExists([]byte("Test"))
		if err != nil {
			return err
		}
		raw := bucket.Get([]byte("num"))
		num := 0
		if len(raw) != 0 {
			num, err = strconv.Atoi(string(raw))
			if err != nil {
				return err
			}
		}
		num++
		fmt.Println(num)
		bucket.Put([]byte("num"), []byte(strconv.Itoa(num)))
		return nil
	})
	if err != nil {
		panic(err)
	}
}

Every time you run it, it reads a number, increments it, and puts it back.

If I have boltbrowser connected to my.db and I run my app then my app sits and waits until boltbrowser releases it's connection. I know bolt only allows 1 write transaction at a time. It'd be nice if boltbrowser had a read only flag so viewing a DB that's currently under use won't halt all interactions with the database.

I imagine if another process is changing the DB then there's no way to know for sure if a value in a bucket didn't change immediately after being accessed and shown on the screen. Personally I'm ok with that.

It looks like all you'd have to do is prevent p/P, b/B, D, e, and r key presses if an "-ro" flag is present. Maybe just show a popup saying "Cannot do that in Read Only mode."

Nevermind, after a little more research I see that Bolt does not allow multiple processes to access the DB file at the same time. Nothing you can do about that.