Skip to content

Commit

Permalink
refactor: extract newBuffer and innerWrite method (openGemini#32)
Browse files Browse the repository at this point in the history
Signed-off-by: PennyYoon <525296438@qq.com>
Co-authored-by: ZhangJian He <shoothzj@gmail.com>
  • Loading branch information
Chenxulin97 and hezhangjian authored Jan 7, 2024
1 parent ec0d81a commit 2bd110b
Showing 1 changed file with 26 additions and 36 deletions.
62 changes: 26 additions & 36 deletions opengemini/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ import (

func (c *client) WriteBatchPoints(database string, bp *BatchPoints) error {
var buffer bytes.Buffer
var writer io.Writer
writer := c.newBuffer(&buffer)

if c.config.GzipEnabled {
writer = gzip.NewWriter(&buffer)
} else {
writer = &buffer
}
for _, p := range bp.Points {
if p == nil {
continue
Expand All @@ -35,17 +30,7 @@ func (c *client) WriteBatchPoints(database string, bp *BatchPoints) error {
}
}

req := requestDetails{
queryValues: make(url.Values),
body: &buffer,
}
if c.config.GzipEnabled {
req.header = make(http.Header)
req.header.Set("Content-Encoding", "gzip")
req.header.Set("Accept-Encoding", "gzip")
}
req.queryValues.Add("db", database)
resp, err := c.executeHttpPost(UrlWrite, req)
resp, err := c.innerWrite(database, &buffer)
if err != nil {
return err
}
Expand All @@ -63,14 +48,7 @@ func (c *client) WriteBatchPoints(database string, bp *BatchPoints) error {

func (c *client) WritePoint(database string, point *Point, callbackFunc func(error)) error {
var buffer bytes.Buffer

var writer io.Writer

if c.config.GzipEnabled {
writer = gzip.NewWriter(&buffer)
} else {
writer = &buffer
}
writer := c.newBuffer(&buffer)

if _, err := io.WriteString(writer, point.String()); err != nil {
return err
Expand All @@ -82,17 +60,7 @@ func (c *client) WritePoint(database string, point *Point, callbackFunc func(err
}
}

req := requestDetails{
queryValues: make(url.Values),
body: &buffer,
}
req.queryValues.Add("db", database)
resp, err := c.executeHttpPost(UrlWrite, req)
if c.config.GzipEnabled {
req.header = make(http.Header)
req.header.Set("Content-Encoding", "gzip")
req.header.Set("Accept-Encoding", "gzip")
}
resp, err := c.innerWrite(database, &buffer)
if err != nil {
callbackFunc(err)
} else if resp.StatusCode != http.StatusNoContent {
Expand All @@ -108,3 +76,25 @@ func (c *client) WritePoint(database string, point *Point, callbackFunc func(err
}
return nil
}

func (c *client) newBuffer(buffer *bytes.Buffer) io.Writer {
if c.config.GzipEnabled {
return gzip.NewWriter(buffer)
} else {
return buffer
}
}

func (c *client) innerWrite(database string, buffer *bytes.Buffer) (*http.Response, error) {
req := requestDetails{
queryValues: make(url.Values),
body: buffer,
}
if c.config.GzipEnabled {
req.header = make(http.Header)
req.header.Set("Content-Encoding", "gzip")
req.header.Set("Accept-Encoding", "gzip")
}
req.queryValues.Add("db", database)
return c.executeHttpPost(UrlWrite, req)
}

0 comments on commit 2bd110b

Please sign in to comment.