-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: api for managing databases and retention policies
Signed-off-by: L-UOjin <[email protected]>
- Loading branch information
Showing
8 changed files
with
485 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package opengemini | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func (c *client) CreateDatabase(database string) error { | ||
if len(database) == 0 { | ||
return errors.New("empty database name") | ||
} | ||
|
||
cmd := fmt.Sprintf("CREATE DATABASE %s", database) | ||
queryResult, err := c.queryPost(Query{Command: cmd}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = queryResult.ResultError() | ||
if err != nil { | ||
return fmt.Errorf("create database err: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) CreateDatabaseWithRp(database string, rpConfig RpConfig) error { | ||
if len(database) == 0 { | ||
return errors.New("empty database name") | ||
} | ||
|
||
var buf strings.Builder | ||
buf.WriteString(fmt.Sprintf("CREATE DATABASE %s WITH DURATION %s REPLICATION 1", database, rpConfig.Duration)) | ||
if len(rpConfig.ShardGroupDuration) > 0 { | ||
buf.WriteString(fmt.Sprintf(" SHARD DURATION %s", rpConfig.ShardGroupDuration)) | ||
} | ||
if len(rpConfig.IndexDuration) > 0 { | ||
buf.WriteString(fmt.Sprintf(" INDEX DURATION %s", rpConfig.IndexDuration)) | ||
} | ||
buf.WriteString(fmt.Sprintf(" NAME %s", rpConfig.Name)) | ||
queryResult, err := c.queryPost(Query{Command: buf.String()}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = queryResult.ResultError() | ||
if err != nil { | ||
return fmt.Errorf("create database err: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) ShowDatabase() ([]string, error) { | ||
var ( | ||
ShowDatabases = "SHOW DATABASES" | ||
dbResult = make([]string, 0) | ||
) | ||
queryResult, err := c.Query(Query{Command: ShowDatabases}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(queryResult.Error) > 0 { | ||
return nil, fmt.Errorf("show datababse err: %s", queryResult.Error) | ||
} | ||
if len(queryResult.Results) == 0 || len(queryResult.Results[0].Series) == 0 { | ||
return dbResult, nil | ||
} | ||
|
||
for _, v := range queryResult.Results[0].Series[0].Values { | ||
if len(v) == 0 { | ||
continue | ||
} | ||
val, ok := v[0].(string) | ||
if !ok { | ||
continue | ||
} | ||
dbResult = append(dbResult, val) | ||
} | ||
return dbResult, nil | ||
} | ||
|
||
// Drop Database | ||
func (c *client) DropDatabase(database string) error { | ||
if len(database) == 0 { | ||
return errors.New("empty database name") | ||
} | ||
cmd := fmt.Sprintf("DROP DATABASE %s", database) | ||
queryResult, err := c.queryPost(Query{Command: cmd}) | ||
if err != nil { | ||
return err | ||
} | ||
err = queryResult.ResultError() | ||
if err != nil { | ||
return fmt.Errorf("drop database err: %s", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package opengemini | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestClientCreateDatabase(t *testing.T) { | ||
c := testNewClient(t, &Config{ | ||
Addresses: []*Address{{ | ||
Host: "localhost", | ||
Port: 8086, | ||
}}, | ||
}) | ||
err := c.CreateDatabase("test4_database") | ||
assert.Nil(t, err) | ||
} | ||
func TestClientCreateDatabaseEmptyDatabase(t *testing.T) { | ||
c := testNewClient(t, &Config{ | ||
Addresses: []*Address{{ | ||
Host: "localhost", | ||
Port: 8086, | ||
}}, | ||
}) | ||
err := c.CreateDatabase("") | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestClientCreateDatabaseWithRp(t *testing.T) { | ||
c := testNewClient(t, &Config{ | ||
Addresses: []*Address{{ | ||
Host: "localhost", | ||
Port: 8086, | ||
}}, | ||
}) | ||
err := c.CreateDatabaseWithRp("test4_database", RpConfig{Name: "test4", Duration: "1d", ShardGroupDuration: "1h", IndexDuration: "7h"}) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestClientCreateDatabaseWithRpInvalid(t *testing.T) { | ||
c := testNewClient(t, &Config{ | ||
Addresses: []*Address{{ | ||
Host: "localhost", | ||
Port: 8086, | ||
}}, | ||
}) | ||
err := c.CreateDatabaseWithRp("test4_database", RpConfig{Name: "test4", Duration: "1", ShardGroupDuration: "1h", IndexDuration: "7h"}) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestClientCreateDatabaseWithRpEmptyDatabase(t *testing.T) { | ||
c := testNewClient(t, &Config{ | ||
Addresses: []*Address{{ | ||
Host: "localhost", | ||
Port: 8086, | ||
}}, | ||
}) | ||
err := c.CreateDatabaseWithRp("", RpConfig{Name: "test4", Duration: "1h", ShardGroupDuration: "1h", IndexDuration: "7h"}) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
|
||
func TestClientShowDatabase(t *testing.T) { | ||
c := testNewClient(t, &Config{ | ||
Addresses: []*Address{{ | ||
Host: "localhost", | ||
Port: 8086, | ||
}}, | ||
}) | ||
_, err := c.ShowDatabase() | ||
assert.Nil(t,err) | ||
} | ||
|
||
func TestClientDropDatabase(t *testing.T) { | ||
c := testNewClient(t, &Config{ | ||
Addresses: []*Address{{ | ||
Host: "localhost", | ||
Port: 8086, | ||
}}, | ||
}) | ||
err := c.DropDatabase("vvv_database") | ||
assert.Nil(t,err) | ||
} | ||
|
||
func TestClientDropDatabaseEmptyDatabase(t *testing.T) { | ||
c := testNewClient(t, &Config{ | ||
Addresses: []*Address{{ | ||
Host: "localhost", | ||
Port: 8086, | ||
}}, | ||
}) | ||
err := c.DropDatabase("") | ||
assert.NotNil(t,err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.