package scan import ( "context" "io" "strings" "archer/internal/row" "archer/internal/sqlutil" ) // DateAScanner uses an index that starts with (shard_column, key...) and // performs keyset pagination over it. NULL rows are scanned in a separate // first phase because row-constructor comparisons do not advance past NULL. type DateAScanner struct { o *Options phase string // null | value cursor row.Key done bool } func NewDateA(o Options) *DateAScanner { s := &DateAScanner{o: &o, phase: "null"} if o.Resume != nil { switch o.Resume.Phase { case "null": s.phase = "null" s.cursor = o.Resume.Key case "value": s.phase = "value" s.cursor = o.Resume.Key } } return s } func (s *DateAScanner) Next(ctx context.Context) (*row.Chunk, error) { for { if s.done { return nil, io.EOF } var ( c *row.Chunk exhausted bool err error ) if s.phase == "null" { c, exhausted, err = s.queryNull(ctx) } else { c, exhausted, err = s.queryValue(ctx) } if err != nil { return nil, err } if c.Len() == 0 { if s.phase == "null" { s.phase = "value" s.cursor = nil continue } s.done = true return nil, io.EOF } if s.phase == "null" { if exhausted { s.phase = "value" s.cursor = nil } else { s.cursor = c.LastKey } } else { s.cursor = s.o.compositeCursor(c.Rows[len(c.Rows)-1]) c.LastKey = s.cursor if exhausted { s.done = true } } return c, nil } } func (s *DateAScanner) queryNull(ctx context.Context) (*row.Chunk, bool, error) { var b strings.Builder b.WriteString(s.o.selectSQL()) b.WriteString(" AND ") b.WriteString(sqlutil.Ident(s.o.Shard.Column)) b.WriteString(" IS NULL") 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 s.run(ctx, b.String(), args) } func (s *DateAScanner) queryValue(ctx context.Context) (*row.Chunk, bool, error) { var b strings.Builder b.WriteString(s.o.selectSQL()) b.WriteString(" AND ") b.WriteString(sqlutil.Ident(s.o.Shard.Column)) b.WriteString(" IS NOT NULL") cols := append([]string{s.o.Shard.Column}, s.o.KeyCols...) var args []any if len(s.cursor) > 0 { b.WriteString(" AND ") b.WriteString(sqlutil.GreaterThan(cols)) args = append(args, s.cursor.Args()...) } b.WriteString(" ORDER BY ") b.WriteString(sqlutil.Columns(cols)) b.WriteString(" LIMIT ?") args = append(args, s.o.ChunkSize) c, exhausted, err := s.run(ctx, b.String(), args) if err != nil { return nil, false, err } if c.Len() > 0 { c.LastKey = s.o.compositeCursor(c.Rows[len(c.Rows)-1]) } return c, exhausted, nil } func (s *DateAScanner) run(ctx context.Context, q string, args []any) (*row.Chunk, bool, error) { rows, err := s.o.DB.QueryContext(ctx, q, args...) if err != nil { return nil, false, err } defer rows.Close() c, exhausted, err := s.o.readChunk(rows, s.o.ChunkSize) if err != nil { return nil, false, err } if err := s.o.fillBuckets(c); err != nil { return nil, false, err } return c, exhausted, nil } func (s *DateAScanner) ResumeState() CursorState { return CursorState{Phase: s.phase, Key: s.cursor} } func (s *DateAScanner) Close() error { return nil }