code.vegaprotocol.io/vega@v0.79.0/datanode/config/encoding/fileloadingmode.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package encoding
    17  
    18  import (
    19  	"errors"
    20  
    21  	"github.com/dgraph-io/badger/v2/options"
    22  )
    23  
    24  var (
    25  	// ErrCouldNotMarshalFLM is to be used when marshalling failed.
    26  	ErrCouldNotMarshalFLM = errors.New("could not marshal FileLoadingMode value to string")
    27  	// ErrCouldNotUnmarshalFLM is to be used when unmarshalling failed.
    28  	ErrCouldNotUnmarshalFLM = errors.New("could not unmarshal FileLoadingMode value from string")
    29  )
    30  
    31  // FileLoadingMode is for storing a badger.FileLoadingMode as a string in a config file.
    32  type FileLoadingMode struct {
    33  	options.FileLoadingMode
    34  }
    35  
    36  // Get returns the underlying FileLoadingMode.
    37  func (m *FileLoadingMode) Get() options.FileLoadingMode {
    38  	return m.FileLoadingMode
    39  }
    40  
    41  // UnmarshalText maps a string to a FileLoadingMode enum value.
    42  func (m *FileLoadingMode) UnmarshalText(text []byte) error {
    43  	switch string(text) {
    44  	case "FileIO":
    45  		m.FileLoadingMode = options.FileIO
    46  	case "LoadToRAM":
    47  		m.FileLoadingMode = options.LoadToRAM
    48  	case "MemoryMap":
    49  		m.FileLoadingMode = options.MemoryMap
    50  	default:
    51  		return ErrCouldNotUnmarshalFLM
    52  	}
    53  	return nil
    54  }
    55  
    56  // MarshalText maps a FileLoadingMode enum value to a string.
    57  func (m FileLoadingMode) MarshalText() ([]byte, error) {
    58  	var t string
    59  	switch m.FileLoadingMode {
    60  	case options.FileIO:
    61  		t = "FileIO"
    62  	case options.LoadToRAM:
    63  		t = "LoadToRAM"
    64  	case options.MemoryMap:
    65  		t = "MemoryMap"
    66  	default:
    67  		return []byte{}, ErrCouldNotMarshalFLM
    68  	}
    69  	return []byte(t), nil
    70  }