Skip to content

Commit

Permalink
(BIDS-2550) add column filter to ClearByPrefix misc command
Browse files Browse the repository at this point in the history
  • Loading branch information
remoterami committed Jan 8, 2024
1 parent 1750b56 commit a4c537a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cmd/misc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func main() {
case "debug-blocks":
err = debugBlocks()
case "clear-bigtable":
clearBigtable(opts.Table, opts.Family, opts.Key, opts.DryRun, bt)
clearBigtable(opts.Table, opts.Family, opts.Columns, opts.Key, opts.DryRun, bt)
case "index-old-eth1-blocks":
indexOldEth1Blocks(opts.StartBlock, opts.EndBlock, opts.BatchSize, opts.DataConcurrency, opts.Transformers, bt, erigonClient)
case "update-aggregation-bits":
Expand Down Expand Up @@ -923,7 +923,7 @@ func compareRewards(dayStart uint64, dayEnd uint64, validator uint64, bt *db.Big

}

func clearBigtable(table string, family string, key string, dryRun bool, bt *db.Bigtable) {
func clearBigtable(table string, family string, columns string, key string, dryRun bool, bt *db.Bigtable) {

if !dryRun {
confirmation := utils.CmdPrompt(fmt.Sprintf("Are you sure you want to delete all big table entries starting with [%v] for family [%v]?", key, family))
Expand All @@ -946,7 +946,7 @@ func clearBigtable(table string, family string, key string, dryRun bool, bt *db.
// if err != nil {
// logrus.Fatal(err)
// }
err := bt.ClearByPrefix(table, family, key, dryRun)
err := bt.ClearByPrefix(table, family, columns, key, dryRun)

if err != nil {
logrus.Fatalf("error deleting from bigtable: %v", err)
Expand Down
25 changes: 22 additions & 3 deletions db/bigtable_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"eth2-exporter/utils"
"fmt"
"sort"
"strings"
"time"

gcp_bigtable "cloud.google.com/go/bigtable"
Expand Down Expand Up @@ -74,7 +75,7 @@ func (bigtable *Bigtable) WriteBulk(mutations *types.BulkMutations, table *gcp_b
return nil
}

func (bigtable *Bigtable) ClearByPrefix(table string, family, prefix string, dryRun bool) error {
func (bigtable *Bigtable) ClearByPrefix(table string, family, columns, prefix string, dryRun bool) error {
if family == "" || prefix == "" {
return fmt.Errorf("please provide family [%v] and prefix [%v]", family, prefix)
}
Expand Down Expand Up @@ -106,6 +107,18 @@ func (bigtable *Bigtable) ClearByPrefix(table string, family, prefix string, dry

mutsDelete := types.NewBulkMutations(MAX_BATCH_MUTATIONS)

var filter gcp_bigtable.Filter
columnsSlice := strings.Split(columns, ",")
if len(columnsSlice) > 1 {
columnNames := make([]gcp_bigtable.Filter, len(columnsSlice))
for i, f := range columnsSlice {
columnNames[i] = gcp_bigtable.ColumnFilter(f)
}
filter = gcp_bigtable.InterleaveFilters(columnNames...)
} else {
filter = gcp_bigtable.ColumnFilter(columnsSlice[0])
}

keysCount := 0
err := btTable.ReadRows(context.Background(), rowRange, func(row gcp_bigtable.Row) bool {

Expand All @@ -126,7 +139,13 @@ func (bigtable *Bigtable) ClearByPrefix(table string, family, prefix string, dry
}

mutDelete := gcp_bigtable.NewMutation()
mutDelete.DeleteRow()
if columns == "" {
mutDelete.DeleteRow()
} else {
for _, f := range columnsSlice {
mutDelete.DeleteCellsInColumn(family, f)
}
}
mutsDelete.Keys = append(mutsDelete.Keys, row_.Row)
mutsDelete.Muts = append(mutsDelete.Muts, mutDelete)
keysCount++
Expand All @@ -146,7 +165,7 @@ func (bigtable *Bigtable) ClearByPrefix(table string, family, prefix string, dry
mutsDelete = types.NewBulkMutations(MAX_BATCH_MUTATIONS)
}
return true
})
}, gcp_bigtable.RowFilter(filter))
if err != nil {
return err
}
Expand Down

0 comments on commit a4c537a

Please sign in to comment.