This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
/
rbf_pages.go
144 lines (124 loc) · 3.49 KB
/
rbf_pages.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
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
// SPDX-License-Identifier: Apache-2.0
package ctl
import (
"context"
"fmt"
"io"
"os"
"github.com/featurebasedb/featurebase/v3/logger"
"github.com/featurebasedb/featurebase/v3/rbf"
"github.com/featurebasedb/featurebase/v3/txkey"
)
// RBFPagesCommand represents a command for printing a list of RBF page metadata.
type RBFPagesCommand struct {
// Filepath to the RBF database.
Path string
// Print the b-tree key with each row.
WithTree bool
// Standard input/output
stdout io.Writer
logDest logger.Logger
}
// NewRBFPagesCommand returns a new instance of RBFPagesCommand.
func NewRBFPagesCommand(logdest logger.Logger) *RBFPagesCommand {
return &RBFPagesCommand{
stdout: os.Stdout,
logDest: logdest,
}
}
// Run executes the export.
func (cmd *RBFPagesCommand) Run(ctx context.Context) error {
// Open database.
db := rbf.NewDB(cmd.Path, nil)
if err := db.Open(); err != nil {
return err
}
defer db.Close()
// Execute with a transaction.
tx, err := db.Begin(false)
if err != nil {
return err
}
defer tx.Rollback()
// Iterate over each page and grab info.
infos, err := tx.PageInfos()
if err != nil {
fmt.Fprintln(cmd.stdout, "ERRORS:")
switch err := err.(type) {
case rbf.ErrorList:
for i := range err {
fmt.Fprintln(cmd.stdout, err[i])
}
default:
fmt.Fprintln(cmd.stdout, err)
}
fmt.Fprintln(cmd.stdout, "")
}
// Write header.
fmt.Fprint(cmd.stdout, "ID ")
fmt.Fprint(cmd.stdout, "TYPE ")
if cmd.WithTree {
fmt.Fprint(cmd.stdout, "TREE ")
}
fmt.Fprintln(cmd.stdout, "EXTRA")
fmt.Fprint(cmd.stdout, "======== ")
fmt.Fprint(cmd.stdout, "========== ")
if cmd.WithTree {
fmt.Fprint(cmd.stdout, "============================== ")
}
fmt.Fprintln(cmd.stdout, "====================")
// Print one line for each page.
for pgno, info := range infos {
fmt.Fprintf(cmd.stdout, "%-8d ", pgno)
switch info := info.(type) {
case *rbf.MetaPageInfo:
fmt.Fprintf(cmd.stdout, "%-10s ", "meta")
if cmd.WithTree {
fmt.Fprintf(cmd.stdout, "%-30q ", "")
}
fmt.Fprintf(cmd.stdout, "pageN=%d,walid=%d,rootrec=%d,freelist=%d\n", info.PageN, info.WALID, info.RootRecordPageNo, info.FreelistPageNo)
case *rbf.RootRecordPageInfo:
fmt.Fprintf(cmd.stdout, "%-10s ", "rootrec")
if cmd.WithTree {
fmt.Fprintf(cmd.stdout, "%-30q ", "")
}
fmt.Fprintf(cmd.stdout, "next=%d\n", info.Next)
case *rbf.LeafPageInfo:
fmt.Fprintf(cmd.stdout, "%-10s ", "leaf")
if cmd.WithTree {
fmt.Fprintf(cmd.stdout, "%-30q ", prefixToString(info.Tree))
}
fmt.Fprintf(cmd.stdout, "flags=x%x,celln=%d\n", info.Flags, info.CellN)
case *rbf.BranchPageInfo:
fmt.Fprintf(cmd.stdout, "%-10s ", "branch")
if cmd.WithTree {
fmt.Fprintf(cmd.stdout, "%-30q ", prefixToString(info.Tree))
}
fmt.Fprintf(cmd.stdout, "flags=x%x,celln=%d\n", info.Flags, info.CellN)
case *rbf.BitmapPageInfo:
fmt.Fprintf(cmd.stdout, "%-10s ", "bitmap")
if cmd.WithTree {
fmt.Fprintf(cmd.stdout, "%-30q ", prefixToString(info.Tree))
}
fmt.Fprintf(cmd.stdout, "-\n")
case *rbf.FreePageInfo:
fmt.Fprintf(cmd.stdout, "%-10s ", "free")
if cmd.WithTree {
fmt.Fprintf(cmd.stdout, "%-30q ", "")
}
fmt.Fprintf(cmd.stdout, "-\n")
default:
fmt.Fprintf(cmd.stdout, "unknown [%T]\n", info)
}
}
return nil
}
func prefixToString(s string) (ret string) {
defer func() {
if err := recover(); err != nil {
ret = s
}
}()
return txkey.PrefixToString([]byte(s))
}