2024-10-03 12:40:46 +05:30
|
|
|
package elasticstream
|
|
|
|
|
|
|
|
import (
|
2024-10-03 21:45:17 +05:30
|
|
|
"fmt"
|
2024-10-07 09:50:00 +05:30
|
|
|
"log"
|
2024-10-03 21:45:17 +05:30
|
|
|
|
2024-10-07 09:50:00 +05:30
|
|
|
"github.com/boltdb/bolt"
|
2024-10-03 12:40:46 +05:30
|
|
|
"github.com/elastic/go-elasticsearch/v8"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2024-10-07 09:50:00 +05:30
|
|
|
es *elasticsearch.Client
|
|
|
|
config *Config
|
|
|
|
ch chan Data
|
|
|
|
db *bolt.DB
|
|
|
|
offsets map[string]int
|
2024-10-03 12:40:46 +05:30
|
|
|
}
|
|
|
|
|
2024-10-03 12:57:23 +05:30
|
|
|
func NewClient(config *Config) (*Client, error) {
|
|
|
|
client := &Client{
|
2024-10-07 09:50:00 +05:30
|
|
|
config: config,
|
|
|
|
offsets: make(map[string]int),
|
2024-10-03 12:57:23 +05:30
|
|
|
}
|
2024-10-03 21:34:57 +05:30
|
|
|
return client, nil
|
|
|
|
}
|
2024-10-03 12:57:23 +05:30
|
|
|
|
2024-10-03 21:34:57 +05:30
|
|
|
func (c *Client) Open() error {
|
|
|
|
// Open a connection with ElasticSearch
|
2024-10-03 12:57:23 +05:30
|
|
|
cfg := elasticsearch.Config{
|
|
|
|
Addresses: []string{
|
2024-10-03 21:34:57 +05:30
|
|
|
c.config.Host,
|
2024-10-03 12:57:23 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-10-03 21:34:57 +05:30
|
|
|
var err error
|
|
|
|
c.es, err = elasticsearch.NewClient(cfg)
|
2024-10-03 12:40:46 +05:30
|
|
|
if err != nil {
|
2024-10-03 21:34:57 +05:30
|
|
|
return err
|
2024-10-03 12:40:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// create a buffer channel
|
2024-10-07 09:50:00 +05:30
|
|
|
c.ch = make(chan Data, 25)
|
|
|
|
|
|
|
|
// open bolt db
|
|
|
|
c.db, err = bolt.Open(c.config.DBPath, 0666, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// defer db.Close()
|
2024-10-03 12:40:46 +05:30
|
|
|
|
2024-10-03 14:04:17 +05:30
|
|
|
for _, index := range c.config.Indexes {
|
2024-10-07 09:50:00 +05:30
|
|
|
offset, err := getOffset(c.db, index)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("getOffset(%s) err:%s\n", index, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c.offsets[index] = offset
|
|
|
|
|
|
|
|
NewWorker(c, index, offset, c.config.BatchSize)
|
2024-10-03 12:40:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-03 21:45:17 +05:30
|
|
|
func (c *Client) Read() (*Data, error) {
|
|
|
|
|
|
|
|
data, ok := <-c.ch
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("error reading data from channel")
|
|
|
|
}
|
2024-10-07 09:50:00 +05:30
|
|
|
|
|
|
|
index, ok := 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
|
|
|
|
}
|
|
|
|
|
2024-10-03 21:45:17 +05:30
|
|
|
return &data, nil
|
2024-10-03 12:40:46 +05:30
|
|
|
}
|
|
|
|
|
2024-10-03 21:34:57 +05:30
|
|
|
// func (c *Client) Ack(ctx context.Context, position int) error {
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
|
2024-10-03 12:40:46 +05:30
|
|
|
// close the client
|
|
|
|
func (c *Client) Teardown() error {
|
|
|
|
return nil
|
|
|
|
}
|