Skip to content

Commit

Permalink
feat: support drop measurement (#157)
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <shoothzj@gmail.com>
  • Loading branch information
ZhangJian He authored Aug 13, 2024
1 parent 64f8635 commit 842826a
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 31 deletions.
41 changes: 41 additions & 0 deletions opengemini/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package opengemini

import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func TestClientShowTagKeys(t *testing.T) {
c := testDefaultClient(t)
databaseName := randomDatabaseName()
err := c.CreateDatabase(databaseName)
require.Nil(t, err)
measurement := randomMeasurement()
cmd := fmt.Sprintf("CREATE MEASUREMENT %s (tag1 TAG,tag2 TAG,tag3 TAG, field1 INT64 FIELD, field2 BOOL, field3 STRING, field4 FLOAT64)", measurement)
_, err = c.Query(Query{Command: cmd, Database: databaseName})
assert.Nil(t, err)
showKeyCmd := fmt.Sprintf("SHOW TAG KEYS FROM %s limit 3 OFFSET 0", measurement)
tagKeyResult, err := c.ShowTagKeys(databaseName, showKeyCmd)
assert.Nil(t, err)
assert.Equal(t, 1, len(tagKeyResult))
err = c.DropDatabase(databaseName)
require.Nil(t, err)
}

func TestClient_ShowFieldKeys(t *testing.T) {
c := testDefaultClient(t)
databaseName := randomDatabaseName()
err := c.CreateDatabase(databaseName)
require.Nil(t, err)
measurement := randomMeasurement()
cmd := fmt.Sprintf("CREATE MEASUREMENT %s (tag1 TAG,tag2 TAG,tag3 TAG, field1 INT64 FIELD, field2 BOOL, field3 STRING, field4 FLOAT64)", measurement)
_, err = c.Query(Query{Command: cmd, Database: databaseName})
assert.Nil(t, err)
tagFieldResult, err := c.ShowFieldKeys(databaseName, fmt.Sprintf("SHOW FIELD KEYS FROM %s", measurement))
assert.Nil(t, err)
assert.Equal(t, 1, len(tagFieldResult))
err = c.DropDatabase(databaseName)
require.Nil(t, err)
}
27 changes: 26 additions & 1 deletion opengemini/measurement.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package opengemini

import (
"errors"
"io"
"net/http"
"net/url"
)

type ValuesResult struct {
Measurement string
Values []interface{}
Expand All @@ -10,5 +17,23 @@ func (c *client) DropMeasurement(database, retentionPolicy, measurement string)
if err != nil {
return err
}
panic("implement me")
req := requestDetails{
queryValues: make(url.Values),
}
req.queryValues.Add("db", database)
req.queryValues.Add("rp", retentionPolicy)
req.queryValues.Add("q", "DROP MEASUREMENT \""+measurement+"\"")
resp, err := c.executeHttpPost("/query", req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return errors.New("read resp failed, error: " + err.Error())
}
if resp.StatusCode != http.StatusOK {
return errors.New("error resp, code: " + resp.Status + "body: " + string(body))
}
return nil
}
90 changes: 72 additions & 18 deletions opengemini/measurement_test.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,95 @@
package opengemini

import (
"fmt"
"github.com/stretchr/testify/assert"
"context"
"github.com/stretchr/testify/require"
"testing"
"time"
)

func TestClientShowTagKeys(t *testing.T) {
func TestClientDropMeasurementExistSpecifyRp(t *testing.T) {
c := testDefaultClient(t)
databaseName := randomDatabaseName()
retentionPolicy := randomRetentionPolicy()
measurement := randomMeasurement()
err := c.CreateDatabase(databaseName)
require.Nil(t, err)
measurement := randomMeasurement()
cmd := fmt.Sprintf("CREATE MEASUREMENT %s (tag1 TAG,tag2 TAG,tag3 TAG, field1 INT64 FIELD, field2 BOOL, field3 STRING, field4 FLOAT64)", measurement)
_, err = c.Query(Query{Command: cmd, Database: databaseName})
assert.Nil(t, err)
showKeyCmd := fmt.Sprintf("SHOW TAG KEYS FROM %s limit 3 OFFSET 0", measurement)
tagKeyResult, err := c.ShowTagKeys(databaseName, showKeyCmd)
assert.Nil(t, err)
assert.Equal(t, 1, len(tagKeyResult))
err = c.CreateRetentionPolicy(databaseName, RpConfig{Name: retentionPolicy, Duration: "3d"}, false)
require.Nil(t, err)
err = c.WriteBatchPointsWithRp(context.Background(), databaseName, retentionPolicy, []*Point{
{
Measurement: measurement,
Precision: 0,
Time: time.Time{},
Tags: nil,
Fields: nil,
},
})
require.Nil(t, err)
err = c.DropMeasurement(databaseName, retentionPolicy, measurement)
require.Nil(t, err)
err = c.DropRetentionPolicy(databaseName, retentionPolicy)
require.Nil(t, err)
}

func TestClientDropMeasurementNonExistent(t *testing.T) {
c := testDefaultClient(t)
databaseName := randomDatabaseName()
retentionPolicy := randomRetentionPolicy()
err := c.CreateDatabase(databaseName)
require.Nil(t, err)
err = c.CreateRetentionPolicy(databaseName, RpConfig{Name: retentionPolicy, Duration: "3d"}, false)
require.Nil(t, err)
err = c.DropMeasurement(databaseName, retentionPolicy, "non_existent_measurement")
require.Nil(t, err)
err = c.DropRetentionPolicy(databaseName, retentionPolicy)
require.Nil(t, err)
err = c.DropDatabase(databaseName)
require.Nil(t, err)
}

func TestClient_ShowFieldKeys(t *testing.T) {
func TestClientDropMeasurementEmptyMeasurementName(t *testing.T) {
c := testDefaultClient(t)
databaseName := randomDatabaseName()
retentionPolicy := randomRetentionPolicy()
err := c.CreateDatabase(databaseName)
require.Nil(t, err)
err = c.CreateRetentionPolicy(databaseName, RpConfig{Name: retentionPolicy, Duration: "3d"}, false)
require.Nil(t, err)
err = c.DropMeasurement(databaseName, retentionPolicy, "")
require.NotNil(t, err)
err = c.DropRetentionPolicy(databaseName, retentionPolicy)
require.Nil(t, err)
err = c.DropDatabase(databaseName)
require.Nil(t, err)
}

func TestClientDropMeasurementEmptyRetentionPolicy(t *testing.T) {
c := testDefaultClient(t)
databaseName := randomDatabaseName()
measurement := randomMeasurement()
cmd := fmt.Sprintf("CREATE MEASUREMENT %s (tag1 TAG,tag2 TAG,tag3 TAG, field1 INT64 FIELD, field2 BOOL, field3 STRING, field4 FLOAT64)", measurement)
_, err = c.Query(Query{Command: cmd, Database: databaseName})
assert.Nil(t, err)
tagFieldResult, err := c.ShowFieldKeys(databaseName, fmt.Sprintf("SHOW FIELD KEYS FROM %s", measurement))
assert.Nil(t, err)
assert.Equal(t, 1, len(tagFieldResult))
err := c.CreateDatabase(databaseName)
require.Nil(t, err)
err = c.WriteBatchPoints(context.Background(), databaseName, []*Point{
{
Measurement: measurement,
Precision: 0,
Time: time.Time{},
Tags: nil,
Fields: nil,
},
})
require.Nil(t, err)
err = c.DropMeasurement(databaseName, "", measurement)
require.NotNil(t, err)
err = c.DropDatabase(databaseName)
require.Nil(t, err)
}

func TestClientDropMeasurementEmptyDatabaseName(t *testing.T) {
c := testDefaultClient(t)
retentionPolicy := randomRetentionPolicy()
measurement := randomMeasurement()
err := c.DropMeasurement("", retentionPolicy, measurement)
require.NotNil(t, err)
}
24 changes: 12 additions & 12 deletions opengemini/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ import (

type WriteCallback func(error)

type sendBatchWithCB struct {
point *Point
callback WriteCallback
}

type dbRp struct {
db string
rp string
}

// CallbackDummy if user don't want to handle WritePoint error, could use this function as empty callback
//
//goland:noinspection GoUnusedExportedFunction
func CallbackDummy(_ error) {
// Do nothing
}

func (c *client) WriteBatchPoints(ctx context.Context, database string, bp []*Point) error {
return c.WriteBatchPointsWithRp(ctx, database, "", bp)
}

func (c *client) WritePoint(database string, point *Point, callback WriteCallback) error {
return c.WritePointWithRp(database, "", point, callback)
}

type sendBatchWithCB struct {
point *Point
callback WriteCallback
}

type dbRp struct {
db string
rp string
func (c *client) WriteBatchPoints(ctx context.Context, database string, bp []*Point) error {
return c.WriteBatchPointsWithRp(ctx, database, "", bp)
}

func (c *client) WritePointWithRp(database string, rp string, point *Point, callback WriteCallback) error {
Expand Down

0 comments on commit 842826a

Please sign in to comment.