Skip to content

Commit

Permalink
feat: refactor measurement manage api
Browse files Browse the repository at this point in the history
Signed-off-by: Young Xu <[email protected]>
  • Loading branch information
xuthus5 committed Aug 18, 2024
1 parent 2a8df7f commit 6a61161
Show file tree
Hide file tree
Showing 10 changed files with 521 additions and 48 deletions.
28 changes: 25 additions & 3 deletions opengemini/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Client interface {
Query(query Query) (*QueryResult, error)

// WritePoint write single point to assigned database. If you don't want to implement callbackFunc to receive error
// in writing, you cloud use opengemini.CallbackDummy.
// in writing, you could use opengemini.CallbackDummy.
WritePoint(database string, point *Point, callbackFunc WriteCallback) error
// WritePointWithRp write single point with retention policy. If you don't want to implement callbackFunc to
// receive error in writing, you cloud use opengemini.CallbackDummy.
Expand All @@ -46,8 +46,30 @@ type Client interface {
ShowRetentionPolicies(database string) ([]RetentionPolicy, error)
DropRetentionPolicy(database, retentionPolicy string) error

ShowMeasurements(database, retentionPolicy string) ([]string, error)
DropMeasurement(database, retentionPolicy, measurement string) error
// ShowMeasurements use command `SHOW MEASUREMENT` to view the measurements created in the database, calling
// NewMeasurementBuilder.Database("db0").RetentionPolicy("rp0").Show() is the best way to set up
// the builder, don't forget to set the database otherwise it will return an error
ShowMeasurements(builder ShowMeasurementBuilder) ([]string, error)
// DropMeasurement use command `DROP MEASUREMENT` to delete measurement, deleting a measurement
// will delete all indexes, series and data. if retentionPolicy is empty, use default retention policy, calling
// NewMeasurementBuilder.Database("db0").RetentionPolicy("rp0").Drop() is the best way to set up the builder, don't
// forget to set the database otherwise it will return an error
DropMeasurement(builder DropMeasurementBuilder) error
// CreateMeasurement use command `CREATE MEASUREMENT` to create measurement, openGemini supports
// automatic table creation when writing data, but in the following three situations, tables need
// to be created in advance.
// - specify a tag as partition key
// - text search
// - high series cardinality storage engine(HSCE)
// calling NewMeasurementBuilder().Database(databaseName).Measurement(measurement).
// Create().TagList([]string{"tag1", "tag2"}).FieldMap(map[string]fieldType{
// "f_int64": FieldTypeInt64,
// "f_float": FieldTypeFloat64,
// "f_bool": FieldTypeBool,
// "f_string": FieldTypeString,
// }).ShardKeys([]string{"tag1"}) is the best way to set up the
// builder, don't forget to set the database otherwise it will return an error
CreateMeasurement(builder CreateMeasurementBuilder) error

ShowTagKeys(database, command string) ([]ValuesResult, error)
ShowTagValues(database, command string) ([]ValuesResult, error)
Expand Down
9 changes: 9 additions & 0 deletions opengemini/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var (
ErrRetentionPolicy = errors.New("empty retention policy")
ErrEmptyMeasurement = errors.New("empty measurement")
ErrEmptyCommand = errors.New("empty command")
ErrEmptyTagOrField = errors.New("empty tag or field")
)

// checkDatabaseName checks if the database name is empty and returns an error if it is.
Expand All @@ -17,6 +18,14 @@ func checkDatabaseName(database string) error {
return nil
}

// checkMeasurementName checks if the measurement name is empty and returns an error if it is.
func checkMeasurementName(mst string) error {
if len(mst) == 0 {
return ErrEmptyMeasurement
}
return nil
}

func checkDatabaseAndPolicy(database, retentionPolicy string) error {
if len(database) == 0 {
return ErrEmptyDatabaseName
Expand Down
64 changes: 56 additions & 8 deletions opengemini/measurement.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@ type ValuesResult struct {
Values []interface{}
}

func (c *client) ShowMeasurements(database, retentionPolicy string) ([]string, error) {
err := checkDatabaseName(database)
func (c *client) ShowMeasurements(builder ShowMeasurementBuilder) ([]string, error) {
base := builder.getMeasurementBase()
err := checkDatabaseName(base.database)
if err != nil {
return nil, err
}

queryResult, err := c.queryPost(Query{Database: database, RetentionPolicy: retentionPolicy, Command: "SHOW MEASUREMENTS"})
command, err := builder.build()
if err != nil {
return nil, err
}

queryResult, err := c.queryPost(Query{
Database: base.database,
RetentionPolicy: base.retentionPolicy,
Command: command,
})

if err != nil {
return nil, err
Expand All @@ -33,17 +43,55 @@ func (c *client) ShowMeasurements(database, retentionPolicy string) ([]string, e
return queryResult.convertMeasurements(), nil
}

func (c *client) DropMeasurement(database, retentionPolicy, measurement string) error {
err := checkDatabaseAndMeasurement(database, measurement)
func (c *client) DropMeasurement(builder DropMeasurementBuilder) error {
base := builder.getMeasurementBase()
err := checkDatabaseName(base.database)
if err != nil {
return err
}

command, err := builder.build()
if err != nil {
return err
}
req := requestDetails{
queryValues: make(url.Values),
}
req.queryValues.Add("db", base.database)
req.queryValues.Add("rp", base.retentionPolicy)
req.queryValues.Add("q", command)
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
}

func (c *client) CreateMeasurement(builder CreateMeasurementBuilder) error {
base := builder.getMeasurementBase()
err := checkDatabaseName(base.database)
if err != nil {
return err
}

command, err := builder.build()
if err != nil {
return err
}
req := requestDetails{
queryValues: make(url.Values),
}
req.queryValues.Add("db", database)
req.queryValues.Add("rp", retentionPolicy)
req.queryValues.Add("q", "DROP MEASUREMENT \""+measurement+"\"")
req.queryValues.Add("db", base.database)
req.queryValues.Add("rp", base.retentionPolicy)
req.queryValues.Add("q", command)
resp, err := c.executeHttpPost("/query", req)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 6a61161

Please sign in to comment.