code.vegaprotocol.io/vega@v0.79.0/core/snapshot/databases/metadata/in_memory.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 metadata
    17  
    18  import (
    19  	"sort"
    20  
    21  	tmtypes "github.com/cometbft/cometbft/abci/types"
    22  )
    23  
    24  type InMemoryDatabase struct {
    25  	store map[int64]*tmtypes.Snapshot
    26  }
    27  
    28  func (d *InMemoryDatabase) Save(version int64, state *tmtypes.Snapshot) error {
    29  	d.store[version] = state
    30  	return nil
    31  }
    32  
    33  func (d *InMemoryDatabase) Load(version int64) (*tmtypes.Snapshot, error) {
    34  	s, ok := d.store[version]
    35  	if !ok {
    36  		return nil, noMetadataForSnapshotVersion(version)
    37  	}
    38  	return s, nil
    39  }
    40  
    41  func (d *InMemoryDatabase) Close() error {
    42  	return nil
    43  }
    44  
    45  func (d *InMemoryDatabase) Clear() error {
    46  	d.store = map[int64]*tmtypes.Snapshot{}
    47  	return nil
    48  }
    49  
    50  func (d *InMemoryDatabase) IsEmpty() bool {
    51  	return len(d.store) == 0
    52  }
    53  
    54  func (d *InMemoryDatabase) FindVersionByBlockHeight(blockHeight uint64) (int64, error) {
    55  	for version, snapshot := range d.store {
    56  		if snapshot.Height == blockHeight {
    57  			return version, nil
    58  		}
    59  	}
    60  	return -1, nil
    61  }
    62  
    63  func (d *InMemoryDatabase) Delete(version int64) error {
    64  	delete(d.store, version)
    65  	return nil
    66  }
    67  
    68  func (d *InMemoryDatabase) DeleteRange(fromVersion, toVersion int64) error {
    69  	versions := make([]int, 0, len(d.store))
    70  	for version := range d.store {
    71  		versions = append(versions, int(version))
    72  	}
    73  	sort.Ints(versions)
    74  
    75  	for _, version := range versions {
    76  		if version >= int(fromVersion) && version < int(toVersion) {
    77  			delete(d.store, int64(version))
    78  		}
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  func NewInMemoryDatabase() *InMemoryDatabase {
    85  	return &InMemoryDatabase{
    86  		store: map[int64]*tmtypes.Snapshot{},
    87  	}
    88  }