elasticstream/source/elastic/read.go

38 lines
678 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"
2024-10-07 16:49:36 +05:30
"log"
2024-10-07 16:26:41 +05:30
"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 16:49:36 +05:30
log.Println(">>>> elastic.Read()")
defer log.Println("<<<< elastic.Read()")
if c == nil || c.ch == nil {
return nil, fmt.Errorf("error source not opened for reading")
}
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
return &data, nil
}