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,74 @@
|
||||
package scan
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"archer/internal/row"
|
||||
"archer/internal/sqlutil"
|
||||
)
|
||||
|
||||
// PKScanner walks the table by its chosen unique key using
|
||||
// key > :last ORDER BY key LIMIT :chunk_size.
|
||||
type PKScanner struct {
|
||||
o *Options
|
||||
cursor row.Key
|
||||
done bool
|
||||
}
|
||||
|
||||
func NewPK(o Options) *PKScanner {
|
||||
s := &PKScanner{o: &o}
|
||||
if o.Resume != nil && (o.Resume.Phase == "" || o.Resume.Phase == "pk") {
|
||||
s.cursor = o.Resume.Key
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *PKScanner) Next(ctx context.Context) (*row.Chunk, error) {
|
||||
if s.done {
|
||||
return nil, io.EOF
|
||||
}
|
||||
q, args := s.query()
|
||||
rows, err := s.o.DB.QueryContext(ctx, q, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
chunk, exhausted, err := s.o.readChunk(rows, s.o.ChunkSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(chunk.Rows) == 0 {
|
||||
s.done = true
|
||||
return nil, io.EOF
|
||||
}
|
||||
if exhausted {
|
||||
s.done = true
|
||||
}
|
||||
s.cursor = chunk.LastKey
|
||||
return chunk, nil
|
||||
}
|
||||
|
||||
func (s *PKScanner) query() (string, []any) {
|
||||
var b strings.Builder
|
||||
b.WriteString(s.o.selectSQL())
|
||||
var args []any
|
||||
if len(s.cursor) > 0 {
|
||||
b.WriteString(" AND ")
|
||||
b.WriteString(sqlutil.GreaterThan(s.o.KeyCols))
|
||||
args = append(args, s.cursor.Args()...)
|
||||
}
|
||||
b.WriteString(" ORDER BY ")
|
||||
b.WriteString(sqlutil.Columns(s.o.KeyCols))
|
||||
b.WriteString(" LIMIT ?")
|
||||
args = append(args, s.o.ChunkSize)
|
||||
return b.String(), args
|
||||
}
|
||||
|
||||
func (s *PKScanner) Close() error { return nil }
|
||||
|
||||
func (s *PKScanner) ResumeState() CursorState {
|
||||
return CursorState{Phase: "pk", Key: s.cursor}
|
||||
}
|
||||
Reference in new issue
Block a user