// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package reader

import (
	"context"

	gmysql "github.com/go-mysql-org/go-mysql/mysql"
	"github.com/go-mysql-org/go-mysql/replication"
)

// Reader is a binlog event reader, it may read binlog events from a TCP stream, binlog files or any other in-memory buffer.
// One reader should read binlog events either through position mode or GTID mode.
type Reader interface {
	Streamer

	// StartSyncByPos prepares the reader for reading binlog from the specified position.
	StartSyncByPos(pos gmysql.Position) error

	// StartSyncByGTID prepares the reader for reading binlog from the specified GTID set.
	StartSyncByGTID(gSet gmysql.GTIDSet) error

	// Close closes the reader and release the resource.
	// Close will be blocked if `GetEvent` has not returned.
	Close() error

	// Status returns the status of the reader.
	Status() interface{}
}

// Streamer provides the ability to get binlog event from remote server or local file.
type Streamer interface {
	// GetEvent returns binlog event
	GetEvent(ctx context.Context) (*replication.BinlogEvent, error)
}
