Files
archer/internal/shard/naming.go
T
voidandClaude Sonnet 4.6 bc4a6b504f Initial commit: archer, a Go-native MySQL/MariaDB table archiver
Renamed from arcdb: module path, cmd/archer, internal/archererr,
docs, build scripts and ARCHER_* environment variables.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-11-06 22:12:17 +08:00

64 lines
1.2 KiB
Go

package shard
import (
"fmt"
"path/filepath"
"strings"
"archer/internal/row"
)
// FinalName builds the sealed shard filename:
// <seq:06d>.<lo>-<hi>.<format>[.<comp>]
func FinalName(seq int64, lo, hi, format, compExt string) string {
return fmt.Sprintf("%06d.%s-%s.%s%s", seq, safeNamePart(lo), safeNamePart(hi), format, compExt)
}
// KeyFilePart renders a key tuple into a filename-safe fragment.
func KeyFilePart(k row.Key) string {
if len(k) == 0 {
return "NULL"
}
parts := make([]string, len(k))
for i, p := range k {
if p.Null {
parts[i] = "NULL"
} else {
parts[i] = p.Val
}
}
return strings.Join(parts, ",")
}
func safeNamePart(s string) string {
if s == "" {
return "_"
}
var b strings.Builder
for _, r := range s {
switch {
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9':
b.WriteRune(r)
case r == '.', r == '_', r == '-':
b.WriteRune(r)
default:
b.WriteByte('_')
}
if b.Len() >= 96 {
break
}
}
if b.Len() == 0 {
return "_"
}
return b.String()
}
// BucketDir returns the directory that holds shards for a bucket.
func BucketDir(root, bucket string) string {
if bucket == "" {
return root
}
return filepath.Join(root, "date="+safeNamePart(bucket))
}