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 Sep 1, 2024
1 parent 7eaf6cd commit ca3af32
Show file tree
Hide file tree
Showing 5 changed files with 479 additions and 22 deletions.
23 changes: 22 additions & 1 deletion opengemini/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,28 @@ type Client interface {
ShowRetentionPolicies(database string) ([]RetentionPolicy, error)
DropRetentionPolicy(database, retentionPolicy string) error

ShowMeasurements(database, retentionPolicy string) ([]string, 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().Tags([]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
// 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, don't
// forget to set the database and measurement otherwise it will return an error
DropMeasurement(database, retentionPolicy, measurement string) error

ShowTagKeys(database, command string) ([]ValuesResult, error)
Expand Down
18 changes: 8 additions & 10 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,23 +18,20 @@ func checkDatabaseName(database string) error {
return nil
}

func checkDatabaseAndPolicy(database, retentionPolicy string) error {
if len(database) == 0 {
return ErrEmptyDatabaseName
}
if len(retentionPolicy) == 0 {
return ErrRetentionPolicy
// 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
}

// checkDatabaseAndMeasurement checks if the database name, retention policy, or measurement is empty and returns the appropriate error.
func checkDatabaseAndMeasurement(database, measurement string) error {
func checkDatabaseAndPolicy(database, retentionPolicy string) error {
if len(database) == 0 {
return ErrEmptyDatabaseName
}
if len(measurement) == 0 {
return ErrEmptyMeasurement
if len(retentionPolicy) == 0 {
return ErrRetentionPolicy
}
return nil
}
Expand Down
56 changes: 51 additions & 5 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
}

command, err := builder.build()
if err != nil {
return nil, err
}

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

if err != nil {
return nil, err
Expand All @@ -34,16 +44,52 @@ func (c *client) ShowMeasurements(database, retentionPolicy string) ([]string, e
}

func (c *client) DropMeasurement(database, retentionPolicy, measurement string) error {
err := checkDatabaseAndMeasurement(database, measurement)
err := checkDatabaseName(database)
if err != nil {
return err
}
if err = checkMeasurementName(measurement); 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("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
}

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", 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 ca3af32

Please sign in to comment.