-
Notifications
You must be signed in to change notification settings - Fork 19
/
in_memory.go
88 lines (73 loc) · 2.17 KB
/
in_memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package metadata
import (
"sort"
tmtypes "github.com/cometbft/cometbft/abci/types"
)
type InMemoryDatabase struct {
store map[int64]*tmtypes.Snapshot
}
func (d *InMemoryDatabase) Save(version int64, state *tmtypes.Snapshot) error {
d.store[version] = state
return nil
}
func (d *InMemoryDatabase) Load(version int64) (*tmtypes.Snapshot, error) {
s, ok := d.store[version]
if !ok {
return nil, noMetadataForSnapshotVersion(version)
}
return s, nil
}
func (d *InMemoryDatabase) Close() error {
return nil
}
func (d *InMemoryDatabase) Clear() error {
d.store = map[int64]*tmtypes.Snapshot{}
return nil
}
func (d *InMemoryDatabase) IsEmpty() bool {
return len(d.store) == 0
}
func (d *InMemoryDatabase) FindVersionByBlockHeight(blockHeight uint64) (int64, error) {
for version, snapshot := range d.store {
if snapshot.Height == blockHeight {
return version, nil
}
}
return -1, nil
}
func (d *InMemoryDatabase) Delete(version int64) error {
delete(d.store, version)
return nil
}
func (d *InMemoryDatabase) DeleteRange(fromVersion, toVersion int64) error {
versions := make([]int, 0, len(d.store))
for version := range d.store {
versions = append(versions, int(version))
}
sort.Ints(versions)
for _, version := range versions {
if version >= int(fromVersion) && version < int(toVersion) {
delete(d.store, int64(version))
}
}
return nil
}
func NewInMemoryDatabase() *InMemoryDatabase {
return &InMemoryDatabase{
store: map[int64]*tmtypes.Snapshot{},
}
}