From 0d2440649a356dc596789191794962098ca544bb Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 9 Feb 2021 18:18:29 +0300 Subject: [PATCH] [#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 --- pkg/util/locode/db/boltdb/calls.go | 3 +++ pkg/util/locode/db/boltdb/opts.go | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/pkg/util/locode/db/boltdb/calls.go b/pkg/util/locode/db/boltdb/calls.go index 2c10aa0de..f260da8cd 100644 --- a/pkg/util/locode/db/boltdb/calls.go +++ b/pkg/util/locode/db/boltdb/calls.go @@ -13,6 +13,8 @@ import ( // Open opens underlying BoltDB instance. // // 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 { // copy-paste from metabase: // 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. // // 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 { return db.bolt.Update(func(tx *bbolt.Tx) error { countryKey, err := countryBucketKey(key.CountryCode()) diff --git a/pkg/util/locode/db/boltdb/opts.go b/pkg/util/locode/db/boltdb/opts.go index 072655011..62b132596 100644 --- a/pkg/util/locode/db/boltdb/opts.go +++ b/pkg/util/locode/db/boltdb/opts.go @@ -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 + } +}