make the region filtering an util func

This commit is contained in:
lion 2025-09-04 14:58:53 +08:00
parent cdd2a2fc85
commit 980c9cfdd4
2 changed files with 23 additions and 24 deletions

View File

@ -58,7 +58,6 @@ import (
"log/slog"
"math"
"os"
"strings"
"time"
)
@ -156,28 +155,6 @@ func (m *Maker) initDbHeader() error {
return nil
}
func (m *Maker) getFilteredRegion(region string) (string, error) {
if len(m.fields) == 0 {
return region, nil
}
fs := strings.Split(region, "|")
var sb []string
for _, idx := range m.fields {
if idx < 0 {
return "", fmt.Errorf("negative filter index %d", idx)
}
if idx >= len(fs) {
return "", fmt.Errorf("field index %d exceeded the max length of %d", idx, len(fs))
}
sb = append(sb, fs[idx])
}
return strings.Join(sb, "|"), nil
}
func (m *Maker) loadSegments() error {
slog.Info("try to load the segments ... ")
var last *Segment = nil
@ -197,7 +174,7 @@ func (m *Maker) loadSegments() error {
}
// apply the field filter
region, err := m.getFilteredRegion(seg.Region)
region, err := RegionFiltering(seg.Region, m.fields)
if err != nil {
return err
}

View File

@ -199,3 +199,25 @@ func CheckSegments(segList []*Segment) error {
return nil
}
func RegionFiltering(region string, fields []int) (string, error) {
if len(fields) == 0 {
return region, nil
}
fs := strings.Split(region, "|")
var sb []string
for _, idx := range fields {
if idx < 0 {
return "", fmt.Errorf("negative filter index %d", idx)
}
if idx >= len(fs) {
return "", fmt.Errorf("field index %d exceeded the max length of %d", idx, len(fs))
}
sb = append(sb, fs[idx])
}
return strings.Join(sb, "|"), nil
}