[#316] locode/boltdb: Add option to enable read-only mode

Add ReadOnly function that returns Option that enables read-only mode in DB.
RO mode can be used by processes that won't modify the DB in order to not
acquire write flock.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-02-09 18:18:29 +03:00 committed by Leonard Lyubich
parent 04b06ba539
commit 0d2440649a
2 changed files with 13 additions and 0 deletions

View file

@ -13,6 +13,8 @@ import (
// Open opens underlying BoltDB instance. // Open opens underlying BoltDB instance.
// //
// Timeout of BoltDB opening is 3s (only for Linux or Darwin). // Timeout of BoltDB opening is 3s (only for Linux or Darwin).
//
// Opens BoltDB in read-only mode if DB is read-only.
func (db *DB) Open() error { func (db *DB) Open() error {
// copy-paste from metabase: // copy-paste from metabase:
// consider universal Open/Close for BoltDB wrappers // consider universal Open/Close for BoltDB wrappers
@ -98,6 +100,7 @@ func recordFromValue(data []byte) (*locodedb.Record, error) {
// The records are stored in internal binary JSON format. // The records are stored in internal binary JSON format.
// //
// Must not be called before successful Open call. // Must not be called before successful Open call.
// Must not be called in read-only mode: behavior is undefined.
func (db *DB) Put(key locodedb.Key, rec locodedb.Record) error { func (db *DB) Put(key locodedb.Key, rec locodedb.Record) error {
return db.bolt.Update(func(tx *bbolt.Tx) error { return db.bolt.Update(func(tx *bbolt.Tx) error {
countryKey, err := countryBucketKey(key.CountryCode()) countryKey, err := countryBucketKey(key.CountryCode())

View file

@ -24,3 +24,13 @@ func defaultOpts() *options {
}, },
} }
} }
// ReadOnly enables read-only mode of the DB.
//
// Do not call DB.Put method on instances with
// this option: the behavior is undefined.
func ReadOnly() Option {
return func(o *options) {
o.boltOpts.ReadOnly = true
}
}