Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit 885ea15

Browse files
authored
Adding very naive (dumb) ingest using v2 api (#1055)
Very rough implementation of the ingest endpoints with `v2` api, but I think we should merge this and continue working on such "sketchpad". It basically shows the way. --------- Signed-off-by: Przemysław Hejman <[email protected]>
1 parent 38c6b7f commit 885ea15

File tree

12 files changed

+2068
-7
lines changed

12 files changed

+2068
-7
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright Quesma, licensed under the Elastic License 2.0.
2+
// SPDX-License-Identifier: Elastic-2.0
3+
4+
package backend_connectors
5+
6+
import (
7+
"context"
8+
"database/sql"
9+
"github.com/ClickHouse/clickhouse-go/v2"
10+
11+
quesma_api "quesma_v2/core"
12+
)
13+
14+
type ClickHouseBackendConnector struct {
15+
Endpoint string
16+
connection *sql.DB
17+
}
18+
19+
type ClickHouseRows struct {
20+
rows *sql.Rows
21+
}
22+
23+
func (p *ClickHouseRows) Next() bool {
24+
return p.rows.Next()
25+
}
26+
27+
func (p *ClickHouseRows) Scan(dest ...interface{}) error {
28+
return p.rows.Scan(dest...)
29+
}
30+
31+
func (p *ClickHouseRows) Close() {
32+
err := p.rows.Close()
33+
if err != nil {
34+
panic(err)
35+
}
36+
}
37+
38+
func (p *ClickHouseRows) Err() error {
39+
return p.rows.Err()
40+
}
41+
42+
func (p *ClickHouseBackendConnector) GetId() quesma_api.BackendConnectorType {
43+
return quesma_api.ClickHouseSQLBackend
44+
}
45+
46+
func (p *ClickHouseBackendConnector) Open() error {
47+
conn, err := initDBConnection()
48+
if err != nil {
49+
return err
50+
}
51+
p.connection = conn
52+
return nil
53+
}
54+
55+
func (p *ClickHouseBackendConnector) Close() error {
56+
if p.connection == nil {
57+
return nil
58+
}
59+
return p.connection.Close()
60+
}
61+
62+
func (p *ClickHouseBackendConnector) Query(ctx context.Context, query string, args ...interface{}) (quesma_api.Rows, error) {
63+
rows, err := p.connection.QueryContext(ctx, query, args...)
64+
if err != nil {
65+
return nil, err
66+
}
67+
return &ClickHouseRows{rows: rows}, nil
68+
}
69+
70+
func (p *ClickHouseBackendConnector) Exec(ctx context.Context, query string, args ...interface{}) error {
71+
if len(args) == 0 {
72+
_, err := p.connection.ExecContext(ctx, query)
73+
return err
74+
}
75+
_, err := p.connection.ExecContext(ctx, query, args...)
76+
return err
77+
}
78+
79+
// func initDBConnection(c *config.QuesmaConfiguration, tlsConfig *tls.Config) *sql.DB {
80+
func initDBConnection() (*sql.DB, error) {
81+
options := clickhouse.Options{Addr: []string{"localhost:9000"}}
82+
info := struct {
83+
Name string
84+
Version string
85+
}{
86+
Name: "quesma",
87+
Version: "NEW ODD VERSION", //buildinfo.Version,
88+
}
89+
options.ClientInfo.Products = append(options.ClientInfo.Products, info)
90+
return clickhouse.OpenDB(&options), nil
91+
92+
}
93+
94+
func NewClickHouseBackendConnector(endpoint string) *ClickHouseBackendConnector {
95+
return &ClickHouseBackendConnector{
96+
Endpoint: endpoint,
97+
}
98+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright Quesma, licensed under the Elastic License 2.0.
2+
// SPDX-License-Identifier: Elastic-2.0
3+
4+
package backend_connectors
5+
6+
import (
7+
"bytes"
8+
"context"
9+
"crypto/tls"
10+
"fmt"
11+
"net/http"
12+
"quesma/elasticsearch"
13+
"quesma/quesma/config"
14+
quesma_api "quesma_v2/core"
15+
"time"
16+
)
17+
18+
const esRequestTimeout = 5 * time.Second
19+
20+
type Rows struct {
21+
Hits []map[string]interface{}
22+
}
23+
24+
// ElasticsearchBackendConnector is just a test impl -
25+
// TODO: THIS IS A TRUE QUESTION MARK WHETHER IT IS GOING TO STAY LIKE THIS
26+
type ElasticsearchBackendConnector struct {
27+
client *http.Client
28+
config config.ElasticsearchConfiguration
29+
}
30+
31+
func NewElasticsearchBackendConnector(cfg config.ElasticsearchConfiguration) *ElasticsearchBackendConnector {
32+
conn := &ElasticsearchBackendConnector{
33+
config: cfg,
34+
client: &http.Client{
35+
Transport: &http.Transport{
36+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
37+
},
38+
Timeout: esRequestTimeout,
39+
},
40+
}
41+
return conn
42+
}
43+
44+
func (e *ElasticsearchBackendConnector) RequestWithHeaders(ctx context.Context, method, endpoint string, body []byte, headers http.Header) (*http.Response, error) {
45+
return e.doRequest(ctx, method, endpoint, body, headers)
46+
}
47+
48+
func (e *ElasticsearchBackendConnector) doRequest(ctx context.Context, method, endpoint string, body []byte, headers http.Header) (*http.Response, error) {
49+
req, err := http.NewRequestWithContext(ctx, method, fmt.Sprintf("%s/%s", e.config.Url, endpoint), bytes.NewBuffer(body))
50+
if err != nil {
51+
return nil, err
52+
}
53+
if req.Header.Get("Content-Type") == "" {
54+
req.Header.Set("Content-Type", "application/json")
55+
}
56+
req = elasticsearch.AddBasicAuthIfNeeded(req, e.config.User, e.config.Password)
57+
for key, values := range headers {
58+
for _, value := range values {
59+
req.Header.Set(key, value)
60+
}
61+
}
62+
return e.client.Do(req)
63+
}
64+
65+
// HttpBackendConnector is a base interface for sending http requests, for now
66+
type HttpBackendConnector interface {
67+
Send(r *http.Request) *http.Response
68+
}
69+
70+
func (e *ElasticsearchBackendConnector) Send(r *http.Request) *http.Response {
71+
r.Host = e.config.Url.Host
72+
r.URL.Host = e.config.Url.Host
73+
r.URL.Scheme = e.config.Url.Scheme
74+
r.RequestURI = "" // this is important for the request to be sent correctly to a different host
75+
maybeAuthdReq := elasticsearch.AddBasicAuthIfNeeded(r, e.config.User, e.config.Password)
76+
if resp, err := e.client.Do(maybeAuthdReq); err != nil {
77+
fmt.Printf("Error: %v\n", err)
78+
panic(err)
79+
} else {
80+
return resp
81+
}
82+
}
83+
84+
func (e *ElasticsearchBackendConnector) GetId() quesma_api.BackendConnectorType {
85+
return quesma_api.ElasticsearchBackend
86+
}
87+
88+
func (e *ElasticsearchBackendConnector) Open() error {
89+
return nil
90+
}
91+
92+
func (e *ElasticsearchBackendConnector) Query(ctx context.Context, query string, args ...interface{}) (quesma_api.Rows, error) {
93+
panic("not implemented")
94+
}
95+
96+
func (e *ElasticsearchBackendConnector) Exec(ctx context.Context, query string, args ...interface{}) error {
97+
panic("not implemented")
98+
}
99+
100+
func (e *ElasticsearchBackendConnector) Close() error {
101+
return nil
102+
}

0 commit comments

Comments
 (0)