2017-03-27 09:00:28 +00:00
|
|
|
package common
|
2017-02-15 14:43:19 +00:00
|
|
|
|
2017-03-27 09:00:28 +00:00
|
|
|
import "database/sql"
|
2017-02-15 14:43:19 +00:00
|
|
|
|
|
|
|
const partitionOffsetsSchema = `
|
|
|
|
-- The offsets that the server has processed up to.
|
|
|
|
CREATE TABLE IF NOT EXISTS partition_offsets (
|
|
|
|
-- The name of the topic.
|
|
|
|
topic TEXT NOT NULL,
|
|
|
|
-- The 32-bit partition ID
|
|
|
|
partition INTEGER NOT NULL,
|
|
|
|
-- The 64-bit offset.
|
|
|
|
partition_offset BIGINT NOT NULL,
|
|
|
|
CONSTRAINT topic_partition_unique UNIQUE (topic, partition)
|
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
const selectPartitionOffsetsSQL = "" +
|
|
|
|
"SELECT partition, partition_offset FROM partition_offsets WHERE topic = $1"
|
|
|
|
|
|
|
|
const upsertPartitionOffsetsSQL = "" +
|
|
|
|
"INSERT INTO partition_offsets (topic, partition, partition_offset) VALUES ($1, $2, $3)" +
|
|
|
|
" ON CONFLICT ON CONSTRAINT topic_partition_unique" +
|
|
|
|
" DO UPDATE SET partition_offset = $3"
|
|
|
|
|
2017-03-27 09:00:28 +00:00
|
|
|
// PartitionOffsetStatements represents a set of statements that can be run on a partition_offsets table.
|
|
|
|
type PartitionOffsetStatements struct {
|
2017-02-15 14:43:19 +00:00
|
|
|
selectPartitionOffsetsStmt *sql.Stmt
|
|
|
|
upsertPartitionOffsetStmt *sql.Stmt
|
|
|
|
}
|
|
|
|
|
2017-03-27 09:00:28 +00:00
|
|
|
// Prepare converts the raw SQL statements into prepared statements.
|
|
|
|
func (s *PartitionOffsetStatements) Prepare(db *sql.DB) (err error) {
|
2017-02-15 14:43:19 +00:00
|
|
|
_, err = db.Exec(partitionOffsetsSchema)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-03-27 09:00:28 +00:00
|
|
|
if s.selectPartitionOffsetsStmt, err = db.Prepare(selectPartitionOffsetsSQL); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.upsertPartitionOffsetStmt, err = db.Prepare(upsertPartitionOffsetsSQL); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
2017-02-15 14:43:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-27 09:00:28 +00:00
|
|
|
// SelectPartitionOffsets returns all the partition offsets for the given topic.
|
|
|
|
func (s *PartitionOffsetStatements) SelectPartitionOffsets(topic string) ([]PartitionOffset, error) {
|
2017-02-15 14:43:19 +00:00
|
|
|
rows, err := s.selectPartitionOffsetsStmt.Query(topic)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2017-03-27 09:00:28 +00:00
|
|
|
var results []PartitionOffset
|
2017-02-15 14:43:19 +00:00
|
|
|
for rows.Next() {
|
2017-03-27 09:00:28 +00:00
|
|
|
var offset PartitionOffset
|
2017-02-15 14:43:19 +00:00
|
|
|
if err := rows.Scan(&offset.Partition, &offset.Offset); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-29 13:05:43 +00:00
|
|
|
results = append(results, offset)
|
2017-02-15 14:43:19 +00:00
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2017-03-27 09:00:28 +00:00
|
|
|
// UpsertPartitionOffset updates or inserts the partition offset for the given topic.
|
|
|
|
func (s *PartitionOffsetStatements) UpsertPartitionOffset(topic string, partition int32, offset int64) error {
|
2017-02-15 14:43:19 +00:00
|
|
|
_, err := s.upsertPartitionOffsetStmt.Exec(topic, partition, offset)
|
|
|
|
return err
|
|
|
|
}
|