zippoxer / bow

Bow - Minimal embedded database powered by Badger
MIT License
223 stars 15 forks source link

Cannot open read-only #6

Closed cbalano closed 5 years ago

cbalano commented 5 years ago

When I am trying to open the database in read-only mode, like this: dir := /bow opts := badger.DefaultOptions opts.ReadOnly = true opts.Logger = nil db, err := bow.Open(dir, bow.SetBadgerOptions(opts))

I see:

error: No sets or deletes are allowed in a read-only transaction

From badger docs:

// ErrReadOnlyTxn is returned if an update function is called on a read-only transaction. ErrReadOnlyTxn = errors.New("No sets or deletes are allowed in a read-only transaction")

I update nothing; or am I ?

zippoxer commented 5 years ago

This must be some update call bow makes when it's initializing. I'll be checking this later today, and hopefully commit a fix and let you know.

cbalano commented 5 years ago

Alright, sounds good; please let me know.

zippoxer commented 5 years ago

Okay, I think I found the issue -- when you're Open-ing a brand new database that hasn't been opened before, bow tries to create and store it's meta structure (containing information about buckets) even if it's opened with ReadOnly.

This same problem happens when you access a bucket that doesn't exist -- bow will try to create it regardless of the ReadOnly restriction.

Thanks for reporting this issue, I will fix it and let you know.

cbalano commented 5 years ago

Excellent. Read-only is a big deal if you access the database from a different process. Inside the same process, one can delegate all Bow operations to a dedicated goroutine and thus avoid dealing with DB locks.

zippoxer commented 5 years ago

Update: I've set up a read-only test, and I'm fixing stuff until it passes. Will let you know.

zippoxer commented 5 years ago

@cbalano

Commit 6703512ae74d252b8ffe01e5fcaeb8bbf926b2fe resolves this issue.

You can now Open with SetReadOnly(true) and you should be able to read freely, but not write.

Please let me know how it works out for you 😄

cbalano commented 5 years ago

Read-only confirmed as working now. Thank you, good sir!

cbalano commented 5 years ago

When I attempt to open read-only in a directory that does not exist, I get a panic, rather than an error. Is that the intended behavior?

zippoxer commented 5 years ago

@cbalano This behaviour is inherited from BadgerDB. Would you prefer to get an error when opening the database instead? Maybe something like ErrNoDatabase?

cbalano commented 5 years ago

Well, it's a nicety, not a must.