-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
216 lines (184 loc) · 5.56 KB
/
util.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
// SPDX-License-Identifier: Apache-2.0
package rbf
import (
"fmt"
"strings"
txkey "github.com/gernest/rbf/short_txkey"
"github.com/gernest/rbf/vprint"
)
// we don't currently use dumpAllPages but it's tricky enough to get right
// that it's probably worth keeping as a debugging tool.
var _ = (*Tx).dumpAllPages
func (tx *Tx) dumpAllPages(showLeaves bool) error {
infos, err := tx.PageInfos()
if err != nil {
return err
}
// Write header.
fmt.Printf("Pgno ")
fmt.Printf("TYPE ")
spc := strings.Repeat(" ", 29)
fmt.Printf("TREE " + spc)
fmt.Printf("EXTRA\n")
fmt.Printf("======== ")
fmt.Printf("========== ")
fmt.Printf("============================== " + spc)
fmt.Printf("====================\n")
// Print one line for each page.
for pgno, info := range infos {
switch info := info.(type) {
case *MetaPageInfo:
fmt.Printf("Pgno:%-8d ", pgno)
fmt.Printf("%-10s ", "meta")
fmt.Printf("%-54s ", "")
fmt.Printf("pageN=%d,walid=%d,rootrec=%d,freelist=%d\n", info.PageN, info.WALID, info.RootRecordPageNo, info.FreelistPageNo)
case *RootRecordPageInfo:
fmt.Printf("Pgno:%-8d ", pgno)
fmt.Printf("%-10s ", "rootrec")
fmt.Printf("%-54s ", "")
fmt.Printf("next=%d\n", info.Next)
page, _, err := tx.readPage(uint32(pgno))
vprint.PanicOn(err)
rootRecords, err := readRootRecords(page)
vprint.PanicOn(err)
for k, rr := range rootRecords {
fmt.Printf(" [%02v] Name:'%v' pgno:%v\n", k, prefixToString(rr.Name), rr.Pgno)
}
case *LeafPageInfo:
if !showLeaves {
continue
}
fmt.Printf("Pgno:%-8d ", pgno)
fmt.Printf("%-10s ", "leaf")
fmt.Printf("%-54q ", prefixToString(info.Tree))
fmt.Printf("flags=x%x,celln=%d\n", info.Flags, info.CellN)
page, _, err := tx.readPage(uint32(pgno))
vprint.PanicOn(err)
var leafCells [PageSize / 8]leafCell
cells := readLeafCells(page, leafCells[:])
for k, cell := range cells {
fmt.Printf(" [%02v] : (container)Key:%v Type:%v BitN:%v len(Data):%v\n", k, cell.Key, cell.Type, cell.BitN, len(cell.Data))
}
case *BranchPageInfo:
fmt.Printf("Pgno:%-8d ", pgno)
fmt.Printf("%-10s ", "branch")
fmt.Printf("%-54q ", prefixToString(info.Tree))
fmt.Printf("flags=x%x,celln=%d\n", info.Flags, info.CellN)
page, _, err := tx.readPage(uint32(pgno))
vprint.PanicOn(err)
cells := readBranchCells(page)
for i, cell := range cells {
fmt.Printf(" [%02v] : (ChildPages's smallest) Key:%05v -> (Child) pgno:%v\n", i, cell.LeftKey, cell.ChildPgno)
}
case *BitmapPageInfo:
fmt.Printf("Pgno:%-8d ", pgno)
fmt.Printf("%-10s ", "bitmap")
fmt.Printf("%-54q ", prefixToString(info.Tree))
fmt.Printf("-\n")
case *FreePageInfo:
fmt.Printf("Pgno:%-8d ", pgno)
fmt.Printf("%-10s ", "free")
fmt.Printf("%-54s ", "")
fmt.Printf("-\n")
case nil:
fmt.Printf("Pgno:%-8d ", pgno)
fmt.Printf("%-10s ", "<nil> problem, corrupt page set")
fmt.Printf("%-54s ", "")
fmt.Printf("-\n")
default:
panic(fmt.Sprintf("unexpected page info type %T at pgno %v", info, pgno))
}
}
return nil
}
func (tx *Tx) dumpPages(pgnos []uint32) error {
// Fetch the page.
pages, err := tx.Pages(pgnos)
if err != nil {
return err
}
for _, page := range pages {
switch page := page.(type) {
case *MetaPage:
printMetaPage(page)
case *RootRecordPage:
printRootRecordPage(page)
case *LeafPage:
printLeafPage(page)
case *BranchPage:
printBranchPage(page)
case *BitmapPage:
printBitmapPage(page)
case *FreePage:
printFreePage(page)
default:
return fmt.Errorf("unexpected page type %T", page)
}
fmt.Printf("\n")
}
return nil
}
var _ = (&Tx{}).dumpPages
func printMetaPage(page *MetaPage) {
fmt.Printf("Pgno: %d\n", page.Pgno)
fmt.Printf("Type: meta\n")
fmt.Printf("PageN: %d\n", page.PageN)
fmt.Printf("WALID: %d\n", page.WALID)
fmt.Printf("Root Record Pgno: %d\n", page.RootRecordPageNo)
fmt.Printf("Freelist Pgno: %d\n", page.FreelistPageNo)
}
func printRootRecordPage(page *RootRecordPage) {
fmt.Printf("Pgno: %d\n", page.Pgno)
fmt.Printf("Type: root record\n")
fmt.Printf("Next: %d\n", page.Next)
fmt.Printf("Records: n=%d\n", len(page.Records))
for i, rec := range page.Records {
fmt.Printf("[%d]: name=%q pgno=%d\n", i, rec.Name, rec.Pgno)
}
}
func printLeafPage(page *LeafPage) {
fmt.Printf("Pgno: %d\n", page.Pgno)
fmt.Printf("Type: leaf\n")
fmt.Printf("Cells: n=%d\n", len(page.Cells))
for i, cell := range page.Cells {
if cell.Type == ContainerTypeBitmapPtr {
fmt.Printf("[%d]: ckey=%d type=%s pgno=%d\n", i, cell.Key, cell.Type, cell.Pgno)
} else {
fmt.Printf("[%d]: ckey=%d type=%s values=%v\n", i, cell.Key, cell.Type, cell.Values)
}
}
}
func printBranchPage(page *BranchPage) {
fmt.Printf("Pgno: %d\n", page.Pgno)
fmt.Printf("Type: branch\n")
fmt.Printf("Cells: n=%d\n", len(page.Cells))
for i, cell := range page.Cells {
fmt.Printf("[%d]: ckey=%d flags=%d pgno=%d\n", i, cell.Key, cell.Flags, cell.Pgno)
}
}
func printBitmapPage(page *BitmapPage) {
fmt.Printf("Pgno: %d\n", page.Pgno)
fmt.Printf("Type: bitmap\n")
fmt.Printf("Values: %v\n", page.Values)
}
func printFreePage(page *FreePage) {
fmt.Printf("Pgno: %d\n", page.Pgno)
fmt.Printf("Type: free\n")
}
func prefixToString(s string) (ret string) {
defer func() {
if err := recover(); err != nil {
ret = s
}
}()
return txkey.PrefixToString([]byte(s))
}
///////////////// happy linter
var _ = printMetaPage
var _ = printRootRecordPage
var _ = printLeafPage
var _ = printBranchPage
var _ = printBitmapPage
var _ = printFreePage
var _ = prefixToString