featurebase/ctl/rbf_pages.go
Seebs cf97a0dcb8 overhaul: switch over to using QueryContext
We switch everything to use QueryContext/QueryRead/etc instead
of Qcx/Tx.

We drop the short_txkey subpackage (it's now handled by either
keys or querycontext).

We drop all the dbshard stuff, and all the tx/txfactory stuff.

We remove all the things that related to the old "Block" concept,
which was mostly used by the anti-entropy code, but had one
fragmentary usage left in the ImportRoaringOverwrite case of
ImportRoaring. That's replaced by using a rewriter that deletes
all bits (not just bits in specific columns) from an existing
thing, but writes in new bits. Actually we could probably do that
better with a custom "eradicate-rewriter" that doesn't try to
be clever, and just eliminates things.

This includes a number of minor bug fixes that were
exposed by getting the testing to work. For example:
* When checking whether an operation "requires write", we
  now consider a Delete a kind of a Write, because it is.
* Several tests were relying on the fact that writes through
  Qcx were being committed whether or not the Qcx was ever
  told to finish. With QueryContext, you actually have to
  reach a Commit() or the writes don't happen (except for
  special cases in Delete).
* Replaced a lot of panics with t.Fatalf in tests.

There's also some minor staticcheck fixes, like deleting the
unused "db" member of a boltdb transaction wrapper.
2023-01-11 12:57:56 -06:00

133 lines
3.3 KiB
Go

// Copyright 2021 Molecula Corp. All rights reserved.
package ctl
import (
"context"
"fmt"
"io"
"os"
"github.com/molecula/featurebase/v3/logger"
"github.com/molecula/featurebase/v3/rbf"
)
// 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 ", rbf.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 ", rbf.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 ", rbf.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
}