elasticstream/source/elastic/read.go

36 lines
583 B
Go
Raw Normal View History

2024-10-07 15:25:13 +05:30
package elastic
import (
"context"
2024-10-07 16:26:41 +05:30
"fmt"
"elasticstream/opencdc"
2024-10-07 15:25:13 +05:30
)
2024-10-07 16:26:41 +05:30
func (c *Client) Read(context.Context) (*opencdc.Data, error) {
2024-10-07 15:25:13 +05:30
data, ok := <-c.ch
if !ok {
return nil, fmt.Errorf("error reading data from channel")
}
index := data.Header.Index
if !ok {
return nil, fmt.Errorf("error index not found in data header")
}
offset, ok := c.offsets[index]
if !ok {
return nil, fmt.Errorf("error offset index not found")
}
c.offsets[index] = offset + 1
err := setOffset(c.db, index, c.offsets[index])
if err != nil {
return nil, err
}
return &data, nil
}