github.com/theQRL/go-zond@v0.1.1/core/rawdb/ancient_utils.go (about) 1 // Copyright 2022 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package rawdb 18 19 import ( 20 "fmt" 21 22 "github.com/theQRL/go-zond/common" 23 "github.com/theQRL/go-zond/zonddb" 24 ) 25 26 type tableSize struct { 27 name string 28 size common.StorageSize 29 } 30 31 // freezerInfo contains the basic information of the freezer. 32 type freezerInfo struct { 33 name string // The identifier of freezer 34 head uint64 // The number of last stored item in the freezer 35 tail uint64 // The number of first stored item in the freezer 36 sizes []tableSize // The storage size per table 37 } 38 39 // count returns the number of stored items in the freezer. 40 func (info *freezerInfo) count() uint64 { 41 return info.head - info.tail + 1 42 } 43 44 // size returns the storage size of the entire freezer. 45 func (info *freezerInfo) size() common.StorageSize { 46 var total common.StorageSize 47 for _, table := range info.sizes { 48 total += table.size 49 } 50 return total 51 } 52 53 func inspect(name string, order map[string]bool, reader zonddb.AncientReader) (freezerInfo, error) { 54 info := freezerInfo{name: name} 55 for t := range order { 56 size, err := reader.AncientSize(t) 57 if err != nil { 58 return freezerInfo{}, err 59 } 60 info.sizes = append(info.sizes, tableSize{name: t, size: common.StorageSize(size)}) 61 } 62 // Retrieve the number of last stored item 63 ancients, err := reader.Ancients() 64 if err != nil { 65 return freezerInfo{}, err 66 } 67 info.head = ancients - 1 68 69 // Retrieve the number of first stored item 70 tail, err := reader.Tail() 71 if err != nil { 72 return freezerInfo{}, err 73 } 74 info.tail = tail 75 return info, nil 76 } 77 78 // inspectFreezers inspects all freezers registered in the system. 79 func inspectFreezers(db zonddb.Database) ([]freezerInfo, error) { 80 var infos []freezerInfo 81 for _, freezer := range freezers { 82 switch freezer { 83 case chainFreezerName: 84 info, err := inspect(chainFreezerName, chainFreezerNoSnappy, db) 85 if err != nil { 86 return nil, err 87 } 88 infos = append(infos, info) 89 90 case stateFreezerName: 91 if ReadStateScheme(db) != PathScheme { 92 continue 93 } 94 datadir, err := db.AncientDatadir() 95 if err != nil { 96 return nil, err 97 } 98 f, err := NewStateFreezer(datadir, true) 99 if err != nil { 100 return nil, err 101 } 102 defer f.Close() 103 104 info, err := inspect(stateFreezerName, stateFreezerNoSnappy, f) 105 if err != nil { 106 return nil, err 107 } 108 infos = append(infos, info) 109 110 default: 111 return nil, fmt.Errorf("unknown freezer, supported ones: %v", freezers) 112 } 113 } 114 return infos, nil 115 } 116 117 // InspectFreezerTable dumps out the index of a specific freezer table. The passed 118 // ancient indicates the path of root ancient directory where the chain freezer can 119 // be opened. Start and end specify the range for dumping out indexes. 120 // Note this function can only be used for debugging purposes. 121 func InspectFreezerTable(ancient string, freezerName string, tableName string, start, end int64) error { 122 var ( 123 path string 124 tables map[string]bool 125 ) 126 switch freezerName { 127 case chainFreezerName: 128 path, tables = resolveChainFreezerDir(ancient), chainFreezerNoSnappy 129 default: 130 return fmt.Errorf("unknown freezer, supported ones: %v", freezers) 131 } 132 noSnappy, exist := tables[tableName] 133 if !exist { 134 var names []string 135 for name := range tables { 136 names = append(names, name) 137 } 138 return fmt.Errorf("unknown table, supported ones: %v", names) 139 } 140 table, err := newFreezerTable(path, tableName, noSnappy, true) 141 if err != nil { 142 return err 143 } 144 table.dumpIndexStdout(start, end) 145 return nil 146 }