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>
This commit is contained in:
commit
bc4a6b504f
99 files changed
+12752
No files matched your search
@@ -0,0 +1,64 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"archer/internal/estimate"
|
||||
"archer/internal/progress"
|
||||
"archer/internal/sqlutil"
|
||||
)
|
||||
|
||||
// estimateTimeout bounds the one-off MIN/MAX query at startup.
|
||||
const estimateTimeout = 2 * time.Second
|
||||
|
||||
// estimateSpec decides which cheap position metric this run can expose.
|
||||
func (a *App) estimateSpec() estimate.Spec {
|
||||
if a.cfg.IsDateShard() && a.probe.shard != nil {
|
||||
return estimate.Spec{
|
||||
Kind: "day",
|
||||
Column: a.probe.shard.Column,
|
||||
ShardKind: a.probe.shard.Kind,
|
||||
Unit: a.probe.shard.Unit,
|
||||
Loc: a.loc,
|
||||
}
|
||||
}
|
||||
types := make([]string, len(a.probe.key.Columns))
|
||||
for i, name := range a.probe.key.Columns {
|
||||
c, _ := a.probe.table.Column(name)
|
||||
types[i] = c.DataType
|
||||
}
|
||||
col, ok := estimate.KeyColumn(a.probe.key.Columns, types)
|
||||
if !ok {
|
||||
return estimate.Spec{}
|
||||
}
|
||||
return estimate.Spec{Kind: "key", Column: col}
|
||||
}
|
||||
|
||||
// queryEstimate runs the bounded range query on a dedicated connection and
|
||||
// silently returns nil when the metric does not apply or the query fails.
|
||||
func (a *App) queryEstimate(ctx context.Context) *progress.Estimate {
|
||||
spec := a.estimateSpec()
|
||||
if spec.Kind == "" {
|
||||
return nil
|
||||
}
|
||||
conn, err := a.srcDB.Conn(ctx)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer conn.Close()
|
||||
query := func(qctx context.Context, column string) (sql.NullString, sql.NullString, error) {
|
||||
col := sqlutil.Ident(column)
|
||||
q := fmt.Sprintf("SELECT MIN(%s), MAX(%s) FROM %s WHERE (%s)",
|
||||
col, col, sqlutil.Qualified(a.srcCfg.DBName, a.cfg.Table), a.cfg.Where)
|
||||
if spec.Kind == "day" {
|
||||
q += " AND " + col + " IS NOT NULL"
|
||||
}
|
||||
var minRaw, maxRaw sql.NullString
|
||||
err := conn.QueryRowContext(qctx, q).Scan(&minRaw, &maxRaw)
|
||||
return minRaw, maxRaw, err
|
||||
}
|
||||
return estimate.Bounded(ctx, query, spec, estimateTimeout)
|
||||
}
|
||||
Reference in new issue
Block a user