db

package
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2021 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AddedGroupColumnsInfo added in v1.0.3

type AddedGroupColumnsInfo struct {
	GroupName     string   `json:"groupName" binding:"required"`
	ColumnNames   []string `json:"columnNames" binding:"required"`
	DefaultValues []string `json:"defaultValues" binding:"required"`
}

type AddedGroupInfo added in v1.0.3

type AddedGroupInfo struct {
	GroupName   string   `json:"groupName" binding:"required"`
	ColumnNames []string `json:"columnNames" binding:"required"`
}

type AddedGroupInfos added in v1.0.3

type AddedGroupInfos struct {
	GroupInfos []AddedGroupInfo `json:"groupInfos" binding:"required"`
}

type AddedItemsInfo added in v1.0.3

type AddedItemsInfo struct {
	GroupName  string              `json:"groupName" binding:"required"`
	ItemValues []map[string]string `json:"itemValues" binding:"required"`
}

type CheckItemsInfo added in v1.0.3

type CheckItemsInfo struct {
	GroupName string   `json:"groupName"`
	ItemNames []string `json:"itemNames"`
}

type Cols

type Cols struct {
	EffectedCols int `json:"effectedCols"`
}

type Config added in v1.0.3

type Config struct {
	GdbConfigs `json:"gdbConfigs"`
	LogConfigs `json:"logConfigs"`
}

type DeadZone

type DeadZone struct {
	ItemName      string `json:"itemName"`
	DeadZoneCount int    `json:"deadZoneCount"`
}

type DeletedGroupColumnNamesInfo added in v1.0.3

type DeletedGroupColumnNamesInfo struct {
	GroupName   string   `json:"groupName" binding:"required"`
	ColumnNames []string `json:"columnNames" binding:"required"`
}

type DeletedItemsInfo added in v1.0.3

type DeletedItemsInfo struct {
	GroupName string `json:"groupName"`
	Condition string `json:"condition"`
}

type Gdb

type Gdb struct {
	DbPath     string // path of leveldb
	ItemDbPath string // path of item of leveldb
	// contains filtered or unexported fields
}

func NewGdb

func NewGdb(dbPath, itemDbPath string) (*Gdb, error)

func (*Gdb) AddGroupColumns added in v1.0.1

func (gdb *Gdb) AddGroupColumns(info AddedGroupColumnsInfo) (Cols, error)

AddGroupColumns add columns to group, all columns type are text

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if _, err := gdb.AddGroupColumns(AddedGroupColumnsInfo{
	GroupName:     "2DCS",
	ColumnNames:   []string{"Column3", "Column4"},
	DefaultValues: []string{"", ""},
}); err != nil {
	log.Fatal(err)
} else {
	if r, err := gdb.GetGroupProperty("2DCS", "1=1"); err != nil {
		log.Fatal(err)
	} else {
		fmt.Println("add group columns:", r.ItemColumnNames)
	}
}
Output:

func (*Gdb) AddGroups added in v1.0.1

func (gdb *Gdb) AddGroups(groupInfos ...AddedGroupInfo) (Rows, error)

AddGroups add group to gdb

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
// you can not add itemName column
if _, err := gdb.AddGroups(AddedGroupInfo{
	GroupName:   "1DCS",
	ColumnNames: []string{"description"},
}); err != nil {
	log.Fatal(err)
}
Output:

func (*Gdb) AddItems

func (gdb *Gdb) AddItems(itemInfo AddedItemsInfo) (Rows, error)

AddItems to gdb,you can not add existed items

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
// assume group 1DCS has two columns itemName and description
if _, err := gdb.AddItems(AddedItemsInfo{
	GroupName: "1DCS",
	ItemValues: []map[string]string{{"itemName": "x", "description": "x"}, {"itemName": "y", "description": "y"}, {"itemName": "z", "description": "z"},
		{"itemName": "item1", "description": "item1"}, {"itemName": "item2", "description": "item2"}},
}); err != nil {
	log.Fatal(err)
}
Output:

func (*Gdb) BatchWrite

func (gdb *Gdb) BatchWrite(infos ...ItemValue) (Rows, error)

BatchWrite write realTimeData, all items to be written should be existed in gdb. otherWise will fail to writing.

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if _, err := gdb.BatchWrite([]ItemValue{{ItemName: "x", Value: "1"}, {ItemName: "y", Value: "2"}}...); err != nil {
	log.Fatal(err)
}
Output:

func (*Gdb) BatchWriteHistoricalData added in v1.0.3

func (gdb *Gdb) BatchWriteHistoricalData(infos ...HistoricalItemValue) error

BatchWriteHistoricalData write historicalData, all items to be written should be existed in gdb. and the timeStamp should be unix timeStamp

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
var xData, yData, ts []string
now := time.Now()
fmt.Println("now: ", now.Format("2006-01-02 15:04:05"))
r := rand.New(rand.NewSource(99))
for i := 0; i < 3600; i++ {
	x := r.Intn(3600)
	y := 2 * x
	t := now.Add(time.Second*time.Duration(i)).Unix() + 8*3600
	xData = append(xData, fmt.Sprintf("%d", x))
	yData = append(yData, fmt.Sprintf("%d", y))
	ts = append(ts, fmt.Sprintf("%d", t))
}
if err := gdb.BatchWriteHistoricalData([]HistoricalItemValue{{ItemName: "x", Values: xData, TimeStamps: ts}, {ItemName: "y", Values: yData, TimeStamps: ts}}...); err != nil {
	log.Fatal(err)
}
Output:

func (*Gdb) CheckItems added in v1.0.3

func (gdb *Gdb) CheckItems(groupName string, itemNames ...string) error

CheckItems check whether the given items existed in the given group

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if err := gdb.CheckItems("1DCS", "x", "x1"); err != nil {
	fmt.Println(err)
}
Output:

func (*Gdb) CleanGroupItems added in v1.0.3

func (gdb *Gdb) CleanGroupItems(groupNames ...string) (Rows, error)
Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if _, err := gdb.CleanGroupItems("1DCS"); err != nil {
	log.Fatal(err)
}
Output:

func (*Gdb) DeleteGroupColumns added in v1.0.1

func (gdb *Gdb) DeleteGroupColumns(info DeletedGroupColumnNamesInfo) (Cols, error)

DeleteGroupColumns delete columns from group

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if _, err := gdb.DeleteGroupColumns(DeletedGroupColumnNamesInfo{
	GroupName:   "2DCS",
	ColumnNames: []string{"Column3"},
}); err != nil {
	log.Fatal(err)
} else {
	if r, err := gdb.GetGroupProperty("2DCS", "1=1"); err != nil {
		log.Fatal(err)
	} else {
		fmt.Println("delete group columns:", r.ItemColumnNames)
	}
}
Output:

func (*Gdb) DeleteGroups added in v1.0.1

func (gdb *Gdb) DeleteGroups(groupInfos GroupNamesInfo) (Rows, error)

DeleteGroups delete group from gdb

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if _, err := gdb.DeleteGroups(GroupNamesInfo{GroupNames: []string{"1DCS"}}); err != nil {
	log.Fatal(err)
}
Output:

func (*Gdb) DeleteItems

func (gdb *Gdb) DeleteItems(itemInfo DeletedItemsInfo) (Rows, error)

DeleteItems delete items from given group according to condition, condition is where clause in sqlite

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if _, err := gdb.DeleteItems(DeletedItemsInfo{
	GroupName: "1DCS",
	Condition: "itemName like '%item%'",
}); err != nil {
	log.Fatal(err)
}
Output:

func (*Gdb) GetGroupProperty added in v1.0.1

func (gdb *Gdb) GetGroupProperty(groupName, condition string) (GroupPropertyInfo, error)

GetGroupProperty get the column name and item count of the given groupName and condition condition is the where clause of sqlite

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if r, err := gdb.GetGroupProperty("1DCS", "1=1"); err != nil {
	log.Fatal(err)
} else {
	fmt.Println("get group property: ", r.ItemColumnNames, r.ItemCount)
}
Output:

func (*Gdb) GetGroups added in v1.0.1

func (gdb *Gdb) GetGroups() (GroupNamesInfo, error)

GetGroups get group name

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if r, err := gdb.GetGroups(); err != nil {
	log.Fatal(err)
} else {
	fmt.Println("update group name: ", r.GroupNames)
}
Output:

func (*Gdb) GetHistoricalData

func (gdb *Gdb) GetHistoricalData(itemNames []string, startTimeStamps []int, endTimeStamps []int, intervals []int) (cmap.ConcurrentMap, error)

GetHistoricalData get historical data, timeStamp should be unix timeStamp, and units of interval is seconds.All items should be existed in gdb, otherWise will fail to getting data

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
now := time.Now()
stX := int(now.Add(time.Minute*5).Unix() + 8*3600)
etX := int(now.Add(time.Minute*25).Unix() + 8*3600)
stY := int(now.Add(time.Minute*35).Unix() + 8*3600)
etY := int(now.Add(time.Minute*55).Unix() + 8*3600)
// Obtain the historical data of x and y in startTime stX, endTime etX, with an interval of 2s,
// and startTime as stY, endTime as etY, and an interval of 10s.
if r, err := gdb.GetHistoricalData([]string{"x", "y"}, []int{stX, stY}, []int{etX, etY}, []int{2, 10}); err != nil {
	log.Fatal(err)
} else {
	d, _ := json.Marshal(r)
	_ = ioutil.WriteFile("./hX.txt", d, 0644)
}
Output:

func (*Gdb) GetHistoricalDataWithCondition

func (gdb *Gdb) GetHistoricalDataWithCondition(itemNames []string, startTime []int, endTime []int, intervals []int, filterCondition string, zones ...DeadZone) (cmap.ConcurrentMap, error)

GetHistoricalDataWithCondition filter condition must be correct js expression,itemName should be startedWith by item. eg: item["itemName1"]>10 && item["itemName2"] > 30 .... It should be noted that the entire judgment is based on the itemName with less historical value in the condition. If the longest itemName is used as the benchmark, we cannot make an accurate judgment on the AND logic in it. Just imagine the history of Item1 It is [3,4,5], and item2 is [10,11]. If item1 is used as the benchmark, we cannot determine how much other elements of item2 should be expanded, because the condition may have complicated logic about item1 and item2 And or logic, no matter what the number is expanded, there may be a judgment error. DeadZone is used to define the maximum number of continuous data allowed by itemName.eg,the deadZoneCount of item x is 2, that is all data in x whose number of continuous > 2 will be filtered

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
now := time.Now()
stX := int(now.Add(time.Minute*5).Unix() + 8*3600)
etX := int(now.Add(time.Minute*25).Unix() + 8*3600)
stY := int(now.Add(time.Minute*35).Unix() + 8*3600)
etY := int(now.Add(time.Minute*55).Unix() + 8*3600)
// Obtain the historical data of x and y in startTime stX, endTime etX, with an interval of 2s,
// and startTime as stY, endTime as etY, and an interval of 10s under the given condition and deadZone
if r, err := gdb.GetHistoricalDataWithCondition([]string{"x", "y"}, []int{stX, stY}, []int{etX, etY}, []int{2, 10}, `item["x"] > 0 && item["y"] > 1000`, []DeadZone{}...); err != nil {
	log.Fatal(err)
} else {
	d, _ := json.Marshal(r)
	_ = ioutil.WriteFile("./f.txt", d, 0644)
}
Output:

func (*Gdb) GetHistoricalDataWithStamp

func (gdb *Gdb) GetHistoricalDataWithStamp(itemNames []string, timeStamps ...[]int) (cmap.ConcurrentMap, error)

GetHistoricalDataWithStamp get history data according to the given time stamps

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
now := time.Now()
stX := int(now.Add(time.Minute*5).Unix() + 8*3600)
etX := int(now.Add(time.Minute*25).Unix() + 8*3600)
stY := int(now.Add(time.Minute*35).Unix() + 8*3600)
etY := int(now.Add(time.Minute*55).Unix() + 8*3600)
// Obtain the historical data of x with timeStamp stX, etX, y with timeStamp stY, etY
if r, err := gdb.GetHistoricalDataWithStamp([]string{"x", "y"}, [][]int{{stX, etX}, {stY, etY}}...); err != nil {
	log.Fatal(err)
} else {
	d, _ := json.Marshal(r)
	fmt.Println(string(d))
}
Output:

func (*Gdb) GetItems added in v1.0.1

func (gdb *Gdb) GetItems(itemInfo ItemsInfo) (GdbItems, error)

GetItems get items from gdb according to the given columnName, condition,startRow and rowCount. columnName is the columnName you want to get, and condition is where clause, startRow and rowCount is used to page query, if startRow is -1, that is get all items without pagination

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if r, err := gdb.GetItems(ItemsInfo{
	GroupName:   "1DCS",
	Condition:   "1=1",
	ColumnNames: "*",
	StartRow:    -1,
	RowCount:    0,
}); err != nil {
	log.Fatal(err)
} else {
	fmt.Println(r.ItemValues)
}
Output:

func (*Gdb) GetRawHistoricalData added in v1.0.2

func (gdb *Gdb) GetRawHistoricalData(itemNames ...string) (cmap.ConcurrentMap, error)

GetRawHistoricalData get raw(that is all historical data, it should only be used for debugging. All items should be existed in gdb, otherWise will fail to getting data

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if r, err := gdb.GetRawHistoricalData("x"); err != nil {
	log.Fatal(err)
} else {
	d, _ := json.Marshal(r)
	_ = ioutil.WriteFile("./rawX.txt", d, 0644)
}
Output:

func (*Gdb) GetRealTimeData

func (gdb *Gdb) GetRealTimeData(itemNames ...string) (cmap.ConcurrentMap, error)

GetRealTimeData get realTime data,that is the latest updated value of item.All items should be existed in gdb, otherWise will fail to getting data

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if r, err := gdb.GetRealTimeData("x", "y"); err != nil {
	log.Fatal(err)
} else {
	d, _ := json.Marshal(r)
	fmt.Println(string(d))
}
Output:

func (*Gdb) UpdateGroupColumnNames added in v1.0.1

func (gdb *Gdb) UpdateGroupColumnNames(info UpdatedGroupColumnNamesInfo) (Cols, error)

UpdateGroupColumnNames update column names of group

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if _, err := gdb.UpdateGroupColumnNames(UpdatedGroupColumnNamesInfo{
	GroupName:      "2DCS",
	OldColumnNames: []string{"Column1", "Column2"},
	NewColumnNames: []string{"Column11", "Column22"},
}); err != nil {
	log.Fatal(err)
} else {
	if r, err := gdb.GetGroupProperty("2DCS", "1=1"); err != nil {
		log.Fatal(err)
	} else {
		fmt.Println("update group column name:", r.ItemColumnNames)
	}
}
Output:

func (*Gdb) UpdateGroupNames added in v1.0.1

func (gdb *Gdb) UpdateGroupNames(groupInfos ...UpdatedGroupNameInfo) (Rows, error)

UpdateGroupNames update groupNames

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if _, err := gdb.UpdateGroupNames(UpdatedGroupNameInfo{
	OldGroupName: "1DCS",
	NewGroupName: "2DCS",
}); err != nil {
	log.Fatal(err)
} else {
	if r, err := gdb.GetGroups(); err != nil {
		log.Fatal(err)
	} else {
		fmt.Println("update group name: ", r.GroupNames)
	}
}
Output:

func (*Gdb) UpdateItems

func (gdb *Gdb) UpdateItems(itemInfo UpdatedItemsInfo) (Rows, error)

UpdateItems update items in gdb according to given condition and clause.condition is where clause in sqlite and clause is set clause in sqlite

Example
gdb, _ := NewGdb("./leveldb", "./itemDb")
if _, err := gdb.UpdateItems(UpdatedItemsInfo{
	GroupName: "1DCS",
	Clause:    "description='x1'",
	Condition: "itemName='x'",
}); err != nil {
	log.Fatal(err)
}
Output:

type GdbConfigs added in v1.0.3

type GdbConfigs struct {
	Port            int64  `json:"port"`
	DbPath          string `json:"dbPath"`
	ItemDbPath      string `json:"itemDbPath"`
	IP              string `json:"ip"`
	ApplicationName string `json:"applicationName"`
	Authorization   bool   `json:"authorization"`
	Mode            string `json:"mode"`
	HttpsConfigs    `json:"httpsConfigs"`
}

type GdbItems added in v1.0.3

type GdbItems struct {
	ItemValues []map[string]string `json:"itemValues"`
}

type GroupNamesInfo added in v1.0.3

type GroupNamesInfo struct {
	GroupNames []string `json:"groupNames"`
}

type GroupPropertyInfo added in v1.0.3

type GroupPropertyInfo struct {
	ItemCount       string   `json:"itemCount"`
	ItemColumnNames []string `json:"itemColumnNames"`
}

type HistoricalItemValue added in v1.0.3

type HistoricalItemValue struct {
	ItemName   string   `json:"itemName" binding:"required"`
	Values     []string `json:"values" binding:"required"`
	TimeStamps []string `json:"timeStamps" binding:"required"`
}

type HttpsConfigs added in v1.0.3

type HttpsConfigs struct {
	Ca                    bool   `json:"ca"`
	SelfSignedCa          bool   `json:"selfSignedCa"`
	CaCertificateName     string `json:"caCertificateName"`
	ServerCertificateName string `json:"serverCertificateName"`
	ServerKeyName         string `json:"serverKeyName"`
}

type ItemValue added in v1.0.2

type ItemValue struct {
	ItemName string `json:"itemName" binding:"required"`
	Value    string `json:"value" binding:"required"`
}

type ItemsInfo added in v1.0.3

type ItemsInfo struct {
	GroupName   string `json:"groupName" binding:"required"`
	Condition   string `json:"condition" binding:"required"`
	ColumnNames string `json:"columnNames" binding:"required"`
	StartRow    int    `json:"startRow"`
	RowCount    int    `json:"rowCount"`
}

type LogConfigs added in v1.0.3

type LogConfigs struct {
	LogWriting  bool   `json:"logWriting"`
	Level       string `json:"level"`
	ExpiredTime int    `json:"expiredTime"`
}

type ResponseData

type ResponseData struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

ResponseData common

type Rows

type Rows struct {
	EffectedRows int `json:"effectedRows"`
}

type UpdatedGroupColumnNamesInfo added in v1.0.3

type UpdatedGroupColumnNamesInfo struct {
	GroupName      string   `json:"groupName"`
	OldColumnNames []string `json:"oldColumnNames"`
	NewColumnNames []string `json:"newColumnNames"`
}

type UpdatedGroupNameInfo added in v1.0.3

type UpdatedGroupNameInfo struct {
	OldGroupName string `json:"oldGroupName"`
	NewGroupName string `json:"newGroupName"`
}

type UpdatedGroupNamesInfo added in v1.0.3

type UpdatedGroupNamesInfo struct {
	Infos []UpdatedGroupNameInfo `json:"infos"`
}

type UpdatedItemsInfo added in v1.0.3

type UpdatedItemsInfo struct {
	GroupName string `json:"groupName" binding:"required"`
	Condition string `json:"condition" binding:"required"`
	Clause    string `json:"clause" binding:"required"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL