code.vegaprotocol.io/vega@v0.79.0/core/snapshot/tree/options.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 tree 17 18 import ( 19 "errors" 20 "fmt" 21 22 metadatadb "code.vegaprotocol.io/vega/core/snapshot/databases/metadata" 23 snapshotdb "code.vegaprotocol.io/vega/core/snapshot/databases/snapshot" 24 "code.vegaprotocol.io/vega/paths" 25 ) 26 27 var ( 28 ErrDatabasesAreAlreadyInitialized = errors.New("the databases are already initialized") 29 ErrMinimumNumberOfSnapshotsToKeepIs1 = errors.New("the minimum number of snapshots to keep is 1") 30 ) 31 32 type Options func(t *Tree) error 33 34 func WithMaxNumberOfSnapshotsToKeep(max uint64) Options { 35 return func(t *Tree) error { 36 if max < 1 { 37 return ErrMinimumNumberOfSnapshotsToKeepIs1 38 } 39 t.maxNumberOfSnapshotsToKeep = max 40 return nil 41 } 42 } 43 44 func StartingAtBlockHeight(blockHeight uint64) Options { 45 return func(t *Tree) error { 46 t.blockHeightToStartFrom = blockHeight 47 return nil 48 } 49 } 50 51 func WithLevelDBDatabase(vegaPaths paths.Paths) Options { 52 return func(t *Tree) error { 53 if t.snapshotDB != nil || t.metadataDB != nil { 54 return ErrDatabasesAreAlreadyInitialized 55 } 56 57 snapshotsDB, err := snapshotdb.NewLevelDBDatabase(vegaPaths) 58 if err != nil { 59 return fmt.Errorf("could not initialize snapshot database: %w", err) 60 } 61 t.snapshotDB = snapshotsDB 62 63 metadataDB, err := metadatadb.NewLevelDBDatabase(vegaPaths) 64 if err != nil { 65 return fmt.Errorf("could not initialize metadata database: %w", err) 66 } 67 t.metadataDB = metadataDB 68 69 return nil 70 } 71 } 72 73 func WithInMemoryDatabase() Options { 74 return func(t *Tree) error { 75 t.snapshotDB = snapshotdb.NewInMemoryDatabase() 76 t.metadataDB = metadatadb.NewInMemoryDatabase() 77 return nil 78 } 79 }