elasticstream/source/elastic/read.go

36 lines
583 B
Go

package elastic
import (
"context"
"fmt"
"elasticstream/opencdc"
)
func (c *Client) Read(context.Context) (*opencdc.Data, error) {
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
}