github.com/ledgerwatch/erigon-lib@v1.0.0/kv/kvcfg/accessors_config.go (about)

     1  /*
     2     Copyright 2021 Erigon contributors
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package kvcfg
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/ledgerwatch/erigon-lib/kv"
    23  )
    24  
    25  type ConfigKey []byte
    26  
    27  var (
    28  	HistoryV3 = ConfigKey("history.v3")
    29  )
    30  
    31  func (k ConfigKey) Enabled(tx kv.Tx) (bool, error) { return kv.GetBool(tx, kv.DatabaseInfo, k) }
    32  func (k ConfigKey) FromDB(db kv.RoDB) (enabled bool) {
    33  	if err := db.View(context.Background(), func(tx kv.Tx) error {
    34  		var err error
    35  		enabled, err = k.Enabled(tx)
    36  		if err != nil {
    37  			return err
    38  		}
    39  		return nil
    40  	}); err != nil {
    41  		panic(err)
    42  	}
    43  	return enabled
    44  }
    45  func (k ConfigKey) WriteOnce(tx kv.RwTx, v bool) (bool, error) {
    46  	_, enabled, err := kv.EnsureNotChangedBool(tx, kv.DatabaseInfo, k, v)
    47  	return enabled, err
    48  }
    49  func (k ConfigKey) EnsureNotChanged(tx kv.RwTx, value bool) (ok, enabled bool, err error) {
    50  	return kv.EnsureNotChangedBool(tx, kv.DatabaseInfo, k, value)
    51  }
    52  func (k ConfigKey) ForceWrite(tx kv.RwTx, enabled bool) error {
    53  	if enabled {
    54  		if err := tx.Put(kv.DatabaseInfo, k, []byte{1}); err != nil {
    55  			return err
    56  		}
    57  	} else {
    58  		if err := tx.Put(kv.DatabaseInfo, k, []byte{0}); err != nil {
    59  			return err
    60  		}
    61  	}
    62  	return nil
    63  }