Documentation ¶
Index ¶
- type AddedGroupColumnsInfo
- type AddedGroupInfo
- type AddedItemsInfo
- type DeadZone
- type DeletedGroupColumnNamesInfo
- type DeletedItemsInfo
- type FastCacheRt
- type Gdb
- func (gdb *Gdb) AddGroupColumns(info AddedGroupColumnsInfo) (TimeCols, error)
- func (gdb *Gdb) AddGroups(groupInfos ...AddedGroupInfo) (TimeRows, error)
- func (gdb *Gdb) AddItems(itemInfo AddedItemsInfo) (TimeRows, error)
- func (gdb *Gdb) BatchWriteBoolData(groupNames []string, itemNames [][]string, itemValues [][]bool) (TimeRows, error)
- func (gdb *Gdb) BatchWriteBoolHistoricalData(groupNames []string, itemNames []string, timeStamps [][]int32, ...) (TimeRows, error)
- func (gdb *Gdb) BatchWriteFloatData(groupNames []string, itemNames [][]string, itemValues [][]float32) (TimeRows, error)
- func (gdb *Gdb) BatchWriteFloatHistoricalData(groupNames []string, itemNames []string, timeStamps [][]int32, ...) (TimeRows, error)
- func (gdb *Gdb) BatchWriteIntData(groupNames []string, itemNames [][]string, itemValues [][]int32) (TimeRows, error)
- func (gdb *Gdb) BatchWriteIntHistoricalData(groupNames []string, itemNames []string, timeStamps [][]int32, ...) (TimeRows, error)
- func (gdb *Gdb) BatchWriteStringData(groupNames []string, itemNames [][]string, itemValues [][]string) (TimeRows, error)
- func (gdb *Gdb) BatchWriteStringHistoricalData(groupNames []string, itemNames []string, timeStamps [][]int32, ...) (TimeRows, error)
- func (gdb *Gdb) CheckItems(groupName string, itemNames ...string) error
- func (gdb *Gdb) CleanGroupItems(groupNames ...string) (TimeRows, error)
- func (gdb *Gdb) CleanItemData(itemInfo DeletedItemsInfo) (TimeRows, error)
- func (gdb *Gdb) CloseGdb() error
- func (gdb *Gdb) DeleteBoolHistoricalData(groupNames, itemNames []string, startTimes, endTimes []int32) (TimeRows, error)
- func (gdb *Gdb) DeleteFloatHistoricalData(groupNames, itemNames []string, startTimes, endTimes []int32) (TimeRows, error)
- func (gdb *Gdb) DeleteGroupColumns(info DeletedGroupColumnNamesInfo) (TimeCols, error)
- func (gdb *Gdb) DeleteGroups(groupInfos GroupNamesInfo) (TimeRows, error)
- func (gdb *Gdb) DeleteIntHistoricalData(groupNames, itemNames []string, startTimes, endTimes []int32) (TimeRows, error)
- func (gdb *Gdb) DeleteItems(itemInfo DeletedItemsInfo) (TimeRows, error)
- func (gdb *Gdb) DeleteStringHistoricalData(groupNames, itemNames []string, startTimes, endTimes []int32) (TimeRows, error)
- func (gdb *Gdb) GetBoolHistoricalData(groupNames, itemNames []string, startTimes, endTimes, intervals []int32) (GdbHistoricalData, error)
- func (gdb *Gdb) GetBoolHistoricalDataWithCondition(groupName string, itemNames []string, startTime, endTime, interval int32, ...) (GdbHistoricalData, error)
- func (gdb *Gdb) GetBoolHistoricalDataWithStamp(groupNames, itemNames []string, timeStamps [][]int32) (GdbHistoricalData, error)
- func (gdb *Gdb) GetBoolRawHistoricalData(groupNames, itemNames []string) (GdbHistoricalData, error)
- func (gdb *Gdb) GetFloatHistoricalData(groupNames, itemNames []string, startTimes, endTimes, intervals []int32) (GdbHistoricalData, error)
- func (gdb *Gdb) GetFloatHistoricalDataWithCondition(groupName string, itemNames []string, startTime, endTime, interval int32, ...) (GdbHistoricalData, error)
- func (gdb *Gdb) GetFloatHistoricalDataWithStamp(groupNames, itemNames []string, timeStamps [][]int32) (GdbHistoricalData, error)
- func (gdb *Gdb) GetFloatRawHistoricalData(groupNames, itemNames []string) (GdbHistoricalData, error)
- func (gdb *Gdb) GetGroupProperty(groupName, condition string) (GroupPropertyInfo, error)
- func (gdb *Gdb) GetGroups() (GroupNamesInfo, error)
- func (gdb *Gdb) GetIntHistoricalData(groupNames, itemNames []string, startTimes, endTimes, intervals []int32) (GdbHistoricalData, error)
- func (gdb *Gdb) GetIntHistoricalDataWithCondition(groupName string, itemNames []string, startTime, endTime, interval int32, ...) (GdbHistoricalData, error)
- func (gdb *Gdb) GetIntHistoricalDataWithStamp(groupNames, itemNames []string, timeStamps [][]int32) (GdbHistoricalData, error)
- func (gdb *Gdb) GetIntRawHistoricalData(groupNames, itemNames []string) (GdbHistoricalData, error)
- func (gdb *Gdb) GetItems(itemInfo ItemsInfo) (GdbItems, error)
- func (gdb *Gdb) GetRealTimeData(groupNames, itemNames []string) (GdbRealTimeData, error)
- func (gdb *Gdb) GetStringHistoricalData(groupNames, itemNames []string, startTimes, endTimes, intervals []int32) (GdbHistoricalData, error)
- func (gdb *Gdb) GetStringHistoricalDataWithCondition(groupName string, itemNames []string, startTime, endTime, interval int32, ...) (GdbHistoricalData, error)
- func (gdb *Gdb) GetStringHistoricalDataWithStamp(groupNames, itemNames []string, timeStamps [][]int32) (GdbHistoricalData, error)
- func (gdb *Gdb) GetStringRawHistoricalData(groupNames, itemNames []string) (GdbHistoricalData, error)
- func (gdb *Gdb) ReLoadDb() (TimeRows, error)
- func (gdb *Gdb) StopService(_ ...interface{}) error
- func (gdb *Gdb) UpdateGroupColumnNames(info UpdatedGroupColumnNamesInfo) (TimeCols, error)
- func (gdb *Gdb) UpdateGroupNames(groupInfos ...UpdatedGroupNameInfo) (TimeRows, error)
- func (gdb *Gdb) UpdateItems(itemInfo UpdatedItemsInfo) (TimeRows, error)
- type GdbHistoricalData
- type GdbItems
- type GdbRealTimeData
- type GroupNamesInfo
- type GroupPropertyInfo
- type ItemsInfo
- type Options
- type RedisRt
- type ResponseData
- type RtGdb
- type StopGdb
- type TimeCols
- type TimeMap
- type TimeRows
- type UpdatedGroupColumnNamesInfo
- type UpdatedGroupNameInfo
- type UpdatedItemsInfo
Examples ¶
- Gdb.AddGroupColumns
- Gdb.AddGroups
- Gdb.AddItems
- Gdb.BatchWriteBoolData
- Gdb.BatchWriteBoolHistoricalData
- Gdb.BatchWriteFloatData
- Gdb.BatchWriteFloatHistoricalData
- Gdb.BatchWriteIntData
- Gdb.BatchWriteIntHistoricalData
- Gdb.BatchWriteStringData
- Gdb.BatchWriteStringHistoricalData
- Gdb.CheckItems
- Gdb.CleanGroupItems
- Gdb.CleanItemData
- Gdb.DeleteFloatHistoricalData
- Gdb.DeleteGroupColumns
- Gdb.DeleteGroups
- Gdb.DeleteItems
- Gdb.GetBoolHistoricalData
- Gdb.GetBoolHistoricalDataWithStamp
- Gdb.GetBoolRawHistoricalData
- Gdb.GetFloatHistoricalData
- Gdb.GetFloatHistoricalDataWithCondition
- Gdb.GetFloatHistoricalDataWithStamp
- Gdb.GetFloatRawHistoricalData
- Gdb.GetGroupProperty
- Gdb.GetGroups
- Gdb.GetIntHistoricalData
- Gdb.GetIntHistoricalDataWithStamp
- Gdb.GetIntRawHistoricalData
- Gdb.GetItems
- Gdb.GetRealTimeData
- Gdb.GetStringHistoricalData
- Gdb.GetStringHistoricalDataWithStamp
- Gdb.ReLoadDb
- Gdb.UpdateGroupColumnNames
- Gdb.UpdateGroupNames
- Gdb.UpdateItems
- NewGdb
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AddedGroupColumnsInfo ¶ added in v1.0.3
type AddedGroupInfo ¶ added in v1.0.3
type AddedItemsInfo ¶ added in v1.0.3
type DeletedGroupColumnNamesInfo ¶ added in v1.0.3
type DeletedItemsInfo ¶ added in v1.0.3
type FastCacheRt ¶ added in v1.0.7
type FastCacheRt struct { RealTimePath string // contains filtered or unexported fields }
FastCacheRt gdb uses fast cache internally to implement realTimeDatabase interface
func (*FastCacheRt) BatchFetch ¶ added in v1.0.7
func (r *FastCacheRt) BatchFetch(keys [][]byte) ([][]byte, error)
func (*FastCacheRt) BatchWrite ¶ added in v1.0.7
func (r *FastCacheRt) BatchWrite(keys, values [][]byte) error
func (*FastCacheRt) Load ¶ added in v1.0.7
func (r *FastCacheRt) Load(_ ...interface{}) error
func (*FastCacheRt) Sync ¶ added in v1.0.7
func (r *FastCacheRt) Sync() error
type Gdb ¶
type Gdb struct {
// contains filtered or unexported fields
}
Gdb define the struct of gdb, you should use NewGdb to get new instance of gdb
func NewGdb ¶
func NewGdb(dbPath string, rtTimeDuration, hisTimeDuration time.Duration, opt *Options) (*Gdb, error)
NewGdb used to create new instance of gdb
dbPath: path of history data, if folder of path not exist, we will create this path
dsn: dsn string to connect itemDb of gdb, at present we support sqlite3 and mysql. for sqlite3, dsn should like "./itemDb/item.db", for mysql, dsn should like "root:admin@123@tcp(192.168.0.166:3306)/itemDb"
Note: for sqlite3, if itemDb given not exist, we will create it, but for mysql, you should create itemDb database in advance.
Example ¶
//initial db with sqlite3 if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { fmt.Println(gdb.hisTimeDuration) } // initial db with mysql if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, &Options{ DriverName: "mysql", Dsn: "root:admin@123@tcp(192.168.0.166:3306)/itemDb", UseInnerStop: true, RtGdb: &FastCacheRt{RealTimePath: "./realTimeDb"}, }); err != nil { fmt.Println(err) } else { fmt.Println(gdb.hisTimeDuration) } // initial db with mysql + redis if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, &Options{ DriverName: "mysql", Dsn: "root:admin@123@tcp(192.168.0.166:3306)/itemDb", UseInnerStop: true, RtGdb: &RedisRt{ Ip: "192.168.0.199", Port: 6379, PassWord: "", DbNum: 0, KeyName: "gdbRealTime", }, }); err != nil { fmt.Println(err) } else { fmt.Println(gdb.hisTimeDuration) }
Output:
func (*Gdb) AddGroupColumns ¶ added in v1.0.1
func (gdb *Gdb) AddGroupColumns(info AddedGroupColumnsInfo) (TimeCols, error)
AddGroupColumns add columns to group, all columns type are text default.And you can't add duplicate columns
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.AddGroupColumns(AddedGroupColumnsInfo{ GroupName: "3DCS", ColumnNames: []string{"unit"}, DefaultValues: []string{"m/s"}, }); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedCols) } }
Output:
func (*Gdb) AddGroups ¶ added in v1.0.1
func (gdb *Gdb) AddGroups(groupInfos ...AddedGroupInfo) (TimeRows, error)
AddGroups add group to gdb, you can't add duplicate group to gdb and the groupName can't be gdbKeyWords
for columns you MUST NOT add column of id, itemName and dataType because these three columns will be automatically added. The operation is atomic
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.AddGroups(AddedGroupInfo{ GroupName: "1DCS", ColumnNames: []string{"unit", "descriptions"}, }, AddedGroupInfo{ GroupName: "2DCS", ColumnNames: nil, }); err != nil { log.Fatal(err) } else { fmt.Println(r.EffectedRows, r.Times) } }
Output:
func (*Gdb) AddItems ¶
func (gdb *Gdb) AddItems(itemInfo AddedItemsInfo) (TimeRows, error)
AddItems to gdb,you can not add duplicate items.you need not add value of id column, and for value of dataType, it can only be int32 or float32 or bool or string.If column has not default value, you MUST specify value
The operation is atomic
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.AddItems(AddedItemsInfo{ GroupName: "3DCS", ItemValues: []map[string]string{{"itemName": "X", "dataType": "float32", "description": "", "unit": ""}, {"itemName": "Y", "dataType": "float32", "description": "", "unit": ""}}, }); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) BatchWriteBoolData ¶ added in v1.0.7
func (gdb *Gdb) BatchWriteBoolData(groupNames []string, itemNames [][]string, itemValues [][]bool) (TimeRows, error)
BatchWriteBoolData write bool data to database, the dataType of written items MUST be bool and all items MUST exist in gdb mapping relationship between parameters are groupName in groupNames corresponds to the itemName([]string) in itemNames at the same time corresponds to itemValue([]bool) in itemValues
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.BatchWriteBoolData([]string{"3DCS", "4DCS"}, [][]string{{"X", "Y", "Z"}, {"X1", "Y1", "Z1"}}, [][]bool{{true, false, true}, {false, true, false}}); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) BatchWriteBoolHistoricalData ¶ added in v1.0.7
func (gdb *Gdb) BatchWriteBoolHistoricalData(groupNames []string, itemNames []string, timeStamps [][]int32, itemValues [][]bool) (TimeRows, error)
BatchWriteBoolHistoricalData write float32 historicalData to database, all items to be written MUST exist in gdb, and the timeStamp MUST be unix timeStamp, data MUST be sorted by timestamp in ascending order mapping relationship between parameters are the itemName in itemNames corresponds to the groupName in groupNames at the same time corresponds to timeStamp([]int32) in timeStamps and itemValue ([]bool) in itemValues
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { //generate history data of month seconds := 24 * 3600 * 30 now := time.Now() groupNames := []string{"3DCS", "4DCS"} itemNames := []string{"X", "X1"} var timeStamp []int32 var xItemValue []bool var x1ItemValue []bool for i := 0; i < seconds; i++ { timeStamp = append(timeStamp, int32(now.Add(time.Duration(i)*time.Second).Unix()+8*3600)) xItemValue = append(xItemValue, i%2 == 0) x1ItemValue = append(x1ItemValue, i%2 != 0) } if r, err := gdb.BatchWriteBoolHistoricalData(groupNames, itemNames, [][]int32{timeStamp, timeStamp}, [][]bool{xItemValue, x1ItemValue}); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) BatchWriteFloatData ¶ added in v1.0.7
func (gdb *Gdb) BatchWriteFloatData(groupNames []string, itemNames [][]string, itemValues [][]float32) (TimeRows, error)
BatchWriteFloatData write float32 data to database, the dataType of written items MUST be float32, and all items MUST exist in gdb mapping relationship between parameters are groupName in groupNames corresponds to the itemName([]string) in itemNames at the same time corresponds to itemValue([]float32) in itemValues
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.BatchWriteFloatData([]string{"3DCS", "4DCS"}, [][]string{{"X", "Y", "Z"}, {"X1", "Y1", "Z1"}}, [][]float32{{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}}); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) BatchWriteFloatHistoricalData ¶ added in v1.0.7
func (gdb *Gdb) BatchWriteFloatHistoricalData(groupNames []string, itemNames []string, timeStamps [][]int32, itemValues [][]float32) (TimeRows, error)
BatchWriteFloatHistoricalData write float32 historicalData to database, all items to be written MUST exist in gdb, and the timeStamp MUST be unix timeStamp, data MUST be sorted by timestamp in ascending order mapping relationship between parameters are the itemName in itemNames corresponds to the groupName in groupNames at the same time corresponds to timeStamp([]int32) in timeStamps and itemValue ([]float32) in itemValues
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { //generate history data of month seconds := 24 * 3600 * 30 now := time.Now() groupNames := []string{"3DCS", "4DCS"} itemNames := []string{"X", "X1"} var timeStamp []int32 var xItemValue []float32 var x1ItemValue []float32 for i := 0; i < seconds; i++ { timeStamp = append(timeStamp, int32(now.Add(time.Duration(i)*time.Second).Unix()+8*3600)) xItemValue = append(xItemValue, rand.Float32()*math.Pi) x1ItemValue = append(x1ItemValue, rand.Float32()*math.E) } if r, err := gdb.BatchWriteFloatHistoricalData(groupNames, itemNames, [][]int32{timeStamp, timeStamp}, [][]float32{xItemValue, x1ItemValue}); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) BatchWriteIntData ¶ added in v1.0.7
func (gdb *Gdb) BatchWriteIntData(groupNames []string, itemNames [][]string, itemValues [][]int32) (TimeRows, error)
BatchWriteIntData write int32 data to database, the dataType of written items MUST be int32 and all items MUST exist in gdb mapping relationship between parameters are groupName in groupNames corresponds to the itemName([]string) in itemNames at the same time corresponds to itemValue([]int32) in itemValues
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.BatchWriteIntData([]string{"3DCS", "4DCS"}, [][]string{{"X", "Y", "Z"}, {"X1", "Y1", "Z1"}}, [][]int32{{1, 2, 3}, {4, 5, 6}}); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) BatchWriteIntHistoricalData ¶ added in v1.0.7
func (gdb *Gdb) BatchWriteIntHistoricalData(groupNames []string, itemNames []string, timeStamps [][]int32, itemValues [][]int32) (TimeRows, error)
BatchWriteIntHistoricalData write float32 historicalData to database, all items to be written MUST exist in gdb, and the timeStamp MUST be unix timeStamp, data MUST be sorted by timestamp in ascending order mapping relationship between parameters are the itemName in itemNames corresponds to the groupName in groupNames at the same time corresponds to timeStamp([]int32) in timeStamps and itemValue ([]int32) in itemValues
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { //generate history data of month seconds := 24 * 3600 * 30 now := time.Now() groupNames := []string{"3DCS", "4DCS"} itemNames := []string{"X", "X1"} var timeStamp []int32 var xItemValue []int32 var x1ItemValue []int32 for i := 0; i < seconds; i++ { timeStamp = append(timeStamp, int32(now.Add(time.Duration(i)*time.Second).Unix()+8*3600)) xItemValue = append(xItemValue, rand.Int31()) x1ItemValue = append(x1ItemValue, rand.Int31()*int32(i)) } if r, err := gdb.BatchWriteIntHistoricalData(groupNames, itemNames, [][]int32{timeStamp, timeStamp}, [][]int32{xItemValue, x1ItemValue}); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) BatchWriteStringData ¶ added in v1.0.7
func (gdb *Gdb) BatchWriteStringData(groupNames []string, itemNames [][]string, itemValues [][]string) (TimeRows, error)
BatchWriteStringData write string data to database, the dataType of written items MUST be string and all items MUST exist in gdb mapping relationship between parameters are groupName in groupNames corresponds to the itemName([]string) in itemNames at the same time corresponds to itemValue([]string) in itemValues
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.BatchWriteStringData([]string{"3DCS", "4DCS"}, [][]string{{"X", "Y", "Z"}, {"X1", "Y1", "Z1"}}, [][]string{{"1.0", "2.0", "3.0"}, {"4.0", "5.0", "6.0"}}); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) BatchWriteStringHistoricalData ¶ added in v1.0.7
func (gdb *Gdb) BatchWriteStringHistoricalData(groupNames []string, itemNames []string, timeStamps [][]int32, itemValues [][]string) (TimeRows, error)
BatchWriteStringHistoricalData write float32 historicalData to database, all items to be written MUST exist in gdb, and the timeStamp MUST be unix timeStamp, data MUST be sorted by timestamp in ascending order mapping relationship between parameters are the itemName in itemNames corresponds to the groupName in groupNames at the same time corresponds to timeStamp([]int32) in timeStamps and itemValue ([]string) in itemValues
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { //generate history data of month seconds := 24 * 3600 * 30 now := time.Now() groupNames := []string{"3DCS", "4DCS"} itemNames := []string{"X", "X1"} var timeStamp []int32 var xItemValue []string var x1ItemValue []string for i := 0; i < seconds; i++ { timeStamp = append(timeStamp, int32(now.Add(time.Duration(i)*time.Second).Unix()+8*3600)) xItemValue = append(xItemValue, now.Add(time.Duration(-i)*time.Second).Format("2006-01-02 15:04:05")) x1ItemValue = append(x1ItemValue, now.Add(time.Duration(i)*time.Second).Format("2006-01-02 15:04:05")) } if r, err := gdb.BatchWriteStringHistoricalData(groupNames, itemNames, [][]int32{timeStamp, timeStamp}, [][]string{xItemValue, x1ItemValue}); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) CheckItems ¶ added in v1.0.3
CheckItems check whether the given items existed in the given group
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if err := gdb.CheckItems("3DCS", "X", "Z"); err != nil { log.Fatal(err) } }
Output:
func (*Gdb) CleanGroupItems ¶ added in v1.0.3
CleanGroupItems will delete all items and corresponding history data in the given groups,so after this operation you can't write or get data from this group any more,you MUST use this method carefully
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.CleanGroupItems("3DCS"); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) CleanItemData ¶ added in v1.0.7
func (gdb *Gdb) CleanItemData(itemInfo DeletedItemsInfo) (TimeRows, error)
CleanItemData delete item and history data of the given item, this may take long time and may block writing operation, you should use it carefully
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.CleanItemData(DeletedItemsInfo{ GroupName: "4DCS", Condition: "1=1", }); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) CloseGdb ¶ added in v1.0.7
CloseGdb will sync realTimeData and history data, then close database
func (*Gdb) DeleteBoolHistoricalData ¶ added in v1.0.7
func (*Gdb) DeleteFloatHistoricalData ¶ added in v1.0.7
func (gdb *Gdb) DeleteFloatHistoricalData(groupNames, itemNames []string, startTimes, endTimes []int32) (TimeRows, error)
DeleteFloatHistoricalData delete history data from database according to the given groupNames, itemNames, startTimes and endTimes if startTime is -1, that is delete history data from nil-endTime, if endTime is -1, that is delete historical data from startTime-nil if both startTime and endTime is -1, that is delete all historical data
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { st, et := int32(1626201902), int32(1626288302) if r, err := gdb.DeleteFloatHistoricalData([]string{"4DCS", "4DCS"}, []string{"xFloat", "yFloat"}, []int32{st, st}, []int32{et, et}); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) DeleteGroupColumns ¶ added in v1.0.1
func (gdb *Gdb) DeleteGroupColumns(info DeletedGroupColumnNamesInfo) (TimeCols, error)
DeleteGroupColumns delete columns from group, you can't delete id, itemName, dataType column
The operation is atomic
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.DeleteGroupColumns(DeletedGroupColumnNamesInfo{ GroupName: "3DCS", ColumnNames: []string{"units"}, }); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedCols) } }
Output:
func (*Gdb) DeleteGroups ¶ added in v1.0.1
func (gdb *Gdb) DeleteGroups(groupInfos GroupNamesInfo) (TimeRows, error)
DeleteGroups delete group from gdb,this operation will delete items in itemDb as well as the historyData of whole group.
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.DeleteGroups(GroupNamesInfo{GroupNames: []string{"2DCS"}}); err != nil { log.Fatal(err) } else { fmt.Println(r.EffectedRows, r.Times) } }
Output:
func (*Gdb) DeleteIntHistoricalData ¶ added in v1.0.7
func (*Gdb) DeleteItems ¶
func (gdb *Gdb) DeleteItems(itemInfo DeletedItemsInfo) (TimeRows, error)
DeleteItems delete items from given group according to condition, condition is where clause in SQL.
NOTE:this operation will not delete history data of item, if you want to delete history data of item, you should use CleanItemData method
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.DeleteItems(DeletedItemsInfo{ GroupName: "3DCS", Condition: "1=1", }); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) DeleteStringHistoricalData ¶ added in v1.0.7
func (*Gdb) GetBoolHistoricalData ¶ added in v1.0.7
func (gdb *Gdb) GetBoolHistoricalData(groupNames, itemNames []string, startTimes, endTimes, intervals []int32) (GdbHistoricalData, error)
GetBoolHistoricalData will get bool history data from database, the startTimes, endTimes should be unix timeStamp and unit of interval is seconds, interval MUST >= 1.If history data of item not exist,the corresponding value will be nil.All items MUST be exist in database.
The returned format of values is {"itemName1": [timeStamp([]int32), values([]bool)]}, if history of item not exist, the timeStamp and values is nil
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { st := int32(time.Now().Add(time.Hour*9*24).Unix() + 8*3600) et := int32(time.Now().Add(time.Hour*10*24).Unix() + 8*3600) if r, err := gdb.GetBoolHistoricalData([]string{"4DCS"}, []string{"xBool"}, []int32{st}, []int32{et}, []int32{6 * 3600}); err != nil { log.Fatal(err) } else { r1, _ := json.Marshal(r.HistoricalData) fmt.Println(string(r1)) } }
Output: {"xBool":[[1626840057,1626861657,1626883257,1626904857],[true,true,true,true]]}
func (*Gdb) GetBoolHistoricalDataWithCondition ¶ added in v1.0.7
func (*Gdb) GetBoolHistoricalDataWithStamp ¶ added in v1.0.7
func (gdb *Gdb) GetBoolHistoricalDataWithStamp(groupNames, itemNames []string, timeStamps [][]int32) (GdbHistoricalData, error)
GetBoolHistoricalDataWithStamp get bool history data according to the given time stamps, if history corresponding not exist we will not returns value
The returned format of values is {"itemName1": [timeStamp([]int32), values([]bool)]}, if all history of ts not exist, the timeStamp and value slice will be nil
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { now := time.Now() ts := []int32{int32(now.Add(time.Hour*24*-30).Unix() + 8*3600)} for i := 0; i < 5; i++ { ts = append(ts, int32(now.Add(time.Hour*24*time.Duration(i)).Unix()+8*3600)) } ts = append(ts, int32(now.Add(time.Hour*24*60).Unix()+8*3600)) // history of ts[0] and ts[len(ts) - 1] not exist, so we will not // return value of this two timeStamp if r, err := gdb.GetBoolHistoricalDataWithStamp([]string{"4DCS"}, []string{"xBool"}, [][]int32{ts}); err != nil { log.Fatal(err) } else { r1, _ := json.Marshal(r.HistoricalData) fmt.Println(string(r1)) } }
Output: {"xBool":[[1626085566,1626171966,1626258366,1626344766,1626431166],[false,false,false,false,false]]}
func (*Gdb) GetBoolRawHistoricalData ¶ added in v1.0.7
func (gdb *Gdb) GetBoolRawHistoricalData(groupNames, itemNames []string) (GdbHistoricalData, error)
GetBoolRawHistoricalData will get all bool history data of the given items.All items MUST be exist in database.
The returned format of values is {"itemName1": [timeStamp([]int32), values([]bool)]}, if history of item not exist, the timeStamp and values is nil
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.GetBoolRawHistoricalData([]string{"4DCS"}, []string{"xBool"}); err != nil { log.Fatal(err) } else { v, _ := r.HistoricalData.Get("xBool") r1, _ := json.Marshal(r) _ = ioutil.WriteFile("./h.txt", r1, 0766) fmt.Println(len(v.([]interface{})[0].([]int32))) } }
Output: 2592000
func (*Gdb) GetFloatHistoricalData ¶ added in v1.0.7
func (gdb *Gdb) GetFloatHistoricalData(groupNames, itemNames []string, startTimes, endTimes, intervals []int32) (GdbHistoricalData, error)
GetFloatHistoricalData will get float32 history data from database, the startTimes, endTimes should be unix timeStamp and unit of interval is seconds, interval MUST >= 1.If history data of item not exist,the corresponding value will be nil.All items MUST be exist in database.
The returned format of values is {"itemName1": [timeStamp([]int32), values([]float32)]}, if history of item not exist, the timeStamp and values is nil
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { st := int32(time.Now().Add(time.Hour*9*24).Unix() + 8*3600) et := int32(time.Now().Add(time.Hour*10*24).Unix() + 8*3600) if r, err := gdb.GetFloatHistoricalData([]string{"3DCS", "4DCS"}, []string{"Y", "X1"}, []int32{st, st}, []int32{et, et}, []int32{6 * 3600, 6 * 3600}); err != nil { log.Fatal(err) } else { r1, _ := json.Marshal(r.HistoricalData) fmt.Println(string(r1)) } }
Output: {"X1":[[1626838972,1626860572,1626882172,1626903772,1626838972,1626860572,1626882172,1626903772],[2.4913614,2.4554338,3.019772,2.8327777,2.4913614,2.4554338,3.019772,2.8327777]],"Y":[null,null]}
func (*Gdb) GetFloatHistoricalDataWithCondition ¶ added in v1.0.7
func (gdb *Gdb) GetFloatHistoricalDataWithCondition(groupName string, itemNames []string, startTime, endTime, interval int32, filterCondition string, zones []DeadZone) (GdbHistoricalData, error)
GetFloatHistoricalDataWithCondition filter condition must be correct js expression,itemName should be startedWith by item. eg: item["itemName1"]>10 && item["itemName2"] > 30, if you don't want to use filterCondition, you should set it "true" 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, itemNames and itemName in filterCondition and zones eg history of item1 is [1,1,1,1], if deadZoneCount of item1 is 2 after filter, the result will be [1,1]
itemNames and itemName in filterCondition and zones MUST be in the same group, itemNames in filterCondition may be different dataType
judgment priority is startTime, endTime, interval > filterCondition > deadZone condition
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { st, et := int32(1626201902), int32(1626288302) // without deadZone condition if r, err := gdb.GetFloatHistoricalDataWithCondition("4DCS", []string{"xFloat", "yFloat"}, st, et, 10, `item["xFloat"]>= 1 && item["yFloat"]<= 4`, nil); err != nil { log.Fatal(err) } else { fmt.Println(r.Times) r1, _ := json.Marshal(r.HistoricalData) _ = ioutil.WriteFile("./hf2.json", r1, 0766) } // with deadZone condition if r, err := gdb.GetFloatHistoricalDataWithCondition("4DCS", []string{"xFloat", "yFloat"}, st, et, 10, `item["xFloat"]>= 1 && item["yFloat"]<= 4`, []DeadZone{{ItemName: "xFloat", DeadZoneCount: 3}}); err != nil { log.Fatal(err) } else { fmt.Println(r.Times) r1, _ := json.Marshal(r.HistoricalData) _ = ioutil.WriteFile("./hf2.json", r1, 0766) } // withOut filterCondition if r, err := gdb.GetFloatHistoricalDataWithCondition("4DCS", []string{"xFloat", "yFloat"}, st, et, 10, `true`, []DeadZone{{ItemName: "xFloat", DeadZoneCount: 3}}); err != nil { log.Fatal(err) } else { fmt.Println(r.Times) r1, _ := json.Marshal(r.HistoricalData) _ = ioutil.WriteFile("./hf2.json", r1, 0766) } // withOut filterCondition and deadZone condition == GetFloatHistoricalData if r, err := gdb.GetFloatHistoricalDataWithCondition("4DCS", []string{"xFloat", "yFloat"}, st, et, 10, `true`, nil); err != nil { log.Fatal(err) } else { fmt.Println(r.Times) r1, _ := json.Marshal(r.HistoricalData) _ = ioutil.WriteFile("./hf2.json", r1, 0766) } }
Output:
func (*Gdb) GetFloatHistoricalDataWithStamp ¶ added in v1.0.7
func (gdb *Gdb) GetFloatHistoricalDataWithStamp(groupNames, itemNames []string, timeStamps [][]int32) (GdbHistoricalData, error)
GetFloatHistoricalDataWithStamp get float32 history data according to the given time stamps, if history corresponding not exist we will not returns value
The returned format of values is {"itemName1": [timeStamp([]int32), values([]float32)]}, if all history of ts not exist, the timeStamp and value slice will be nil
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { now := time.Now() ts := []int32{int32(now.Add(time.Hour*24*-30).Unix() + 8*3600)} for i := 0; i < 5; i++ { ts = append(ts, int32(now.Add(time.Hour*24*time.Duration(i)).Unix()+8*3600)) } ts = append(ts, int32(now.Add(time.Hour*24*60).Unix()+8*3600)) // history of ts[0] and ts[len(ts) - 1] not exist, so we will not // return value of this two timeStamp if r, err := gdb.GetFloatHistoricalDataWithStamp([]string{"4DCS", "3DCS"}, []string{"X1", "Y"}, [][]int32{ts, ts}); err != nil { log.Fatal(err) } else { r1, _ := json.Marshal(r.HistoricalData) fmt.Println(string(r1)) } }
Output: {"X1":[[1626045377,1626131777,1626218177,1626304577,1626390977],[0.26911953,1.5107508,1.7978884,0.84435517,0.05435284]],"Y":[null,null]}
func (*Gdb) GetFloatRawHistoricalData ¶ added in v1.0.7
func (gdb *Gdb) GetFloatRawHistoricalData(groupNames, itemNames []string) (GdbHistoricalData, error)
GetFloatRawHistoricalData will get all float32 history data of the given items.All items MUST be exist in database.
The returned format of values is {"itemName1": [timeStamp([]int32), values([]float32)]}, if history of item not exist, the timeStamp and values is nil
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.GetFloatRawHistoricalData([]string{"4DCS"}, []string{"X1"}); err != nil { log.Fatal(err) } else { v, _ := r.HistoricalData.Get("X1") r1, _ := json.Marshal(r) _ = ioutil.WriteFile("./h.txt", r1, 0766) fmt.Println(len(v.([]interface{})[0].([]int32))) } }
Output: 2592000
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 ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.GetGroupProperty("1DCS", "1=1"); err != nil { log.Fatal(err) } else { fmt.Println(r.ItemCount, r.ItemColumnNames) } }
Output:
func (*Gdb) GetGroups ¶ added in v1.0.1
func (gdb *Gdb) GetGroups() (GroupNamesInfo, error)
GetGroups get group name
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.GetGroups(); err != nil { log.Fatal(err) } else { fmt.Println(r.GroupNames) } }
Output:
func (*Gdb) GetIntHistoricalData ¶ added in v1.0.7
func (gdb *Gdb) GetIntHistoricalData(groupNames, itemNames []string, startTimes, endTimes, intervals []int32) (GdbHistoricalData, error)
GetIntHistoricalData will get int32 history data from database, the startTimes, endTimes should be unix timeStamp and unit of interval is seconds, interval MUST >= 1.If history data of item not exist,the corresponding value will be nil.All items MUST be exist in database.
The returned format of values is {"itemName1": [timeStamp([]int32), values([]int32)]}, if history of item not exist, the timeStamp and values is nil
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { st := int32(time.Now().Add(time.Hour*9*24).Unix() + 8*3600) et := int32(time.Now().Add(time.Hour*10*24).Unix() + 8*3600) if r, err := gdb.GetIntHistoricalData([]string{"4DCS"}, []string{"xInt"}, []int32{st}, []int32{et}, []int32{6 * 3600}); err != nil { log.Fatal(err) } else { r1, _ := json.Marshal(r.HistoricalData) fmt.Println(string(r1)) } }
Output: {"xInt":[[1626840105,1626861705,1626883305,1626904905],[1932697017,132599088,1758527870,1151289996]]}
func (*Gdb) GetIntHistoricalDataWithCondition ¶ added in v1.0.7
func (*Gdb) GetIntHistoricalDataWithStamp ¶ added in v1.0.7
func (gdb *Gdb) GetIntHistoricalDataWithStamp(groupNames, itemNames []string, timeStamps [][]int32) (GdbHistoricalData, error)
GetIntHistoricalDataWithStamp get int32 history data according to the given time stamps, if history corresponding not exist we will not returns value
The returned format of values is {"itemName1": [timeStamp([]int32), values([]int32)]}, if all history of ts not exist, the timeStamp and value slice will be nil
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { now := time.Now() ts := []int32{int32(now.Add(time.Hour*24*-30).Unix() + 8*3600)} for i := 0; i < 5; i++ { ts = append(ts, int32(now.Add(time.Hour*24*time.Duration(i)).Unix()+8*3600)) } ts = append(ts, int32(now.Add(time.Hour*24*60).Unix()+8*3600)) // history of ts[0] and ts[len(ts) - 1] not exist, so we will not // return value of this two timeStamp if r, err := gdb.GetIntHistoricalDataWithStamp([]string{"4DCS"}, []string{"xInt"}, [][]int32{ts}); err != nil { log.Fatal(err) } else { r1, _ := json.Marshal(r.HistoricalData) fmt.Println(string(r1)) } }
Output: {"xInt":[[1626085279,1626171679,1626258079,1626344479,1626430879],[1208980727,2117437444,1398993897,781075096,2106205256]]}
func (*Gdb) GetIntRawHistoricalData ¶ added in v1.0.7
func (gdb *Gdb) GetIntRawHistoricalData(groupNames, itemNames []string) (GdbHistoricalData, error)
GetIntRawHistoricalData will get all int32 history data of the given items.All items MUST be exist in database.
The returned format of values is {"itemName1": [timeStamp([]int32), values([]int32)]}, if history of item not exist, the timeStamp and values is nil
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.GetIntRawHistoricalData([]string{"4DCS"}, []string{"xInt"}); err != nil { log.Fatal(err) } else { v, _ := r.HistoricalData.Get("xInt") r1, _ := json.Marshal(r) _ = ioutil.WriteFile("./h.txt", r1, 0766) fmt.Println(len(v.([]interface{})[0].([]int32))) } }
Output: 2592000
func (*Gdb) GetItems ¶ added in v1.0.1
GetItems get items from gdb according to the given columnName, condition,startRow and rowCount. columnName is the columnName you want to get, if you want to get all columns info, you should use * 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 ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.GetItems(ItemsInfo{ GroupName: "3DCS", Condition: "1=1", ColumnNames: "*", StartRow: -1, RowCount: 0, }); err != nil { log.Fatal(err) } else { fmt.Println(r.ItemValues) } }
Output:
func (*Gdb) GetRealTimeData ¶
func (gdb *Gdb) GetRealTimeData(groupNames, itemNames []string) (GdbRealTimeData, 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.If realTime of item not exist, the corresponding value will be nil
you MUST NOT get the realTime data of the same name item in different groups such as {groupNames: ['3DCS', '5DCS', '5DCS'], itemNames: ['xInt', 'xInt', 'xString']} otherWise the result of xInt will be unreliable
The format of return value is {"itemName1": value1, "itemName2": value2}, values corresponds to the dataType of item
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.GetRealTimeData([]string{"3DCS", "4DCS", "3DCS"}, []string{"X", "X1", "Y"}); err != nil { log.Fatal(err) } else { r1, _ := json.Marshal(r.RealTimeData) fmt.Println(string(r1)) } }
Output: {"X":1,"X1":4,"Y":null}
func (*Gdb) GetStringHistoricalData ¶ added in v1.0.7
func (gdb *Gdb) GetStringHistoricalData(groupNames, itemNames []string, startTimes, endTimes, intervals []int32) (GdbHistoricalData, error)
GetStringHistoricalData will get string history data from database, the startTimes, endTimes should be unix timeStamp and unit of interval is seconds, interval MUST >= 1.If history data of item not exist,the corresponding value will be nil.All items MUST be exist in database.
The returned format of values is {"itemName1": [timeStamp([]int32), values([]string)]}, if history of item not exist, the timeStamp and values is nil
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { st := int32(time.Now().Add(time.Hour*9*24).Unix() + 8*3600) et := int32(time.Now().Add(time.Hour*10*24).Unix() + 8*3600) if r, err := gdb.GetStringHistoricalData([]string{"4DCS"}, []string{"xString"}, []int32{st}, []int32{et}, []int32{6 * 3600}); err != nil { log.Fatal(err) } else { r1, _ := json.Marshal(r.HistoricalData) fmt.Println(string(r1)) } }
Output: {"xString":[[1626839992,1626861592,1626883192,1626904792],["2021-07-02 15:59:52","2021-07-02 09:59:52","2021-07-02 03:59:52","2021-07-01 21:59:52"]]}
func (*Gdb) GetStringHistoricalDataWithCondition ¶ added in v1.0.7
func (*Gdb) GetStringHistoricalDataWithStamp ¶ added in v1.0.7
func (gdb *Gdb) GetStringHistoricalDataWithStamp(groupNames, itemNames []string, timeStamps [][]int32) (GdbHistoricalData, error)
GetStringHistoricalDataWithStamp get string history data according to the given time stamps, if history corresponding not exist we will not returns value
The returned format of values is {"itemName1": [timeStamp([]int32), values([]string)]}, if all history of ts not exist, the timeStamp and value slice will be nil
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { now := time.Now() ts := []int32{int32(now.Add(time.Hour*24*-30).Unix() + 8*3600)} for i := 0; i < 5; i++ { ts = append(ts, int32(now.Add(time.Hour*24*time.Duration(i)).Unix()+8*3600)) } ts = append(ts, int32(now.Add(time.Hour*24*60).Unix()+8*3600)) // history of ts[0] and ts[len(ts) - 1] not exist, so we will not // return value of this two timeStamp if r, err := gdb.GetStringHistoricalDataWithStamp([]string{"4DCS"}, []string{"xString"}, [][]int32{ts}); err != nil { log.Fatal(err) } else { r1, _ := json.Marshal(r.HistoricalData) fmt.Println(string(r1)) } }
Output: {"xString":[[1626085482,1626171882,1626258282,1626344682,1626431082],["2021-07-11 09:35:02","2021-07-10 09:35:02","2021-07-09 09:35:02","2021-07-08 09:35:02","2021-07-07 09:35:02"]]}
func (*Gdb) GetStringRawHistoricalData ¶ added in v1.0.7
func (gdb *Gdb) GetStringRawHistoricalData(groupNames, itemNames []string) (GdbHistoricalData, error)
GetStringRawHistoricalData will get all string history data of the given items.All items MUST be exist in database.
The returned format of values is {"itemName1": [timeStamp([]int32), values([]string)]}, if history of item not exist, the timeStamp and values is nil
func (*Gdb) ReLoadDb ¶ added in v1.0.7
ReLoadDb delete keys in db whose value is nil, then compact db again, during this time, all write operation will failed so you should not write data to db during reload
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.ReLoadDb(); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) StopService ¶ added in v1.0.7
func (*Gdb) UpdateGroupColumnNames ¶ added in v1.0.1
func (gdb *Gdb) UpdateGroupColumnNames(info UpdatedGroupColumnNamesInfo) (TimeCols, error)
UpdateGroupColumnNames update column names of group,you can't update columns of id, itemName, dataType's column name. the updatedColumnName MUST be existed
The operation is atomic
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.UpdateGroupColumnNames(UpdatedGroupColumnNamesInfo{ GroupName: "3DCS", OldColumnNames: []string{"unit", "descriptions"}, NewColumnNames: []string{"units", "description"}, }); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedCols) } }
Output:
func (*Gdb) UpdateGroupNames ¶ added in v1.0.1
func (gdb *Gdb) UpdateGroupNames(groupInfos ...UpdatedGroupNameInfo) (TimeRows, error)
UpdateGroupNames update groupNames,the history data of oldGroup will migrate to new newGroup as well
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.UpdateGroupNames(UpdatedGroupNameInfo{ OldGroupName: "1DCS", NewGroupName: "3DCS", }); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
func (*Gdb) UpdateItems ¶
func (gdb *Gdb) UpdateItems(itemInfo UpdatedItemsInfo) (TimeRows, error)
UpdateItems update items in gdb according to given condition and clause.condition is where clause in SQL and clause is set clause in SQL.You can't update id or itemName or dataType column
Example ¶
if gdb, err := NewGdb("./historyDb", time.Hour, time.Minute*5, DefaultOptions()); err != nil { fmt.Println(err) } else { if r, err := gdb.UpdateItems(UpdatedItemsInfo{ GroupName: "3DCS", Condition: "itemName='X'", Clause: "description='X'", }); err != nil { log.Fatal(err) } else { fmt.Println(r.Times, r.EffectedRows) } }
Output:
type GdbHistoricalData ¶ added in v1.0.7
type GdbHistoricalData struct { HistoricalData cmap.ConcurrentMap `json:"historicalData"` Times int64 `json:"times"` }
type GdbRealTimeData ¶ added in v1.0.7
type GdbRealTimeData struct { RealTimeData cmap.ConcurrentMap `json:"realTimeData"` Times int64 `json:"times"` }
type GroupNamesInfo ¶ added in v1.0.3
type GroupNamesInfo struct {
GroupNames []string `json:"groupNames" binding:"required"`
}
type GroupPropertyInfo ¶ added in v1.0.3
type Options ¶ added in v1.0.7
type Options struct { DriverName string // driverName of itemDb, default is sqlite3 Dsn string // dsn to connect itemDb, default is "file:itemDb.db?_auth_user=seu&_auth_pass=admin@123&_auth_crypt=SHA1@_vacuum=1" UseInnerStop bool // whether user inner stopService function, if true, we will use StopService to stopService, you can also define your own method by implement StopGdb interface,in your own method, you should call gdb.CloseGdb to ensure gdbService close successfully RtGdb // realTime interface, default is FastCacheRt }
func DefaultOptions ¶ added in v1.0.7
func DefaultOptions() *Options
DefaultOptions returns default options of gdb
DriverName: sqlite3
Dsn: file:itemDB.db?_auth_user=seu&_auth_pass=admin@123&_auth_crypt=SHA1@_vacuum=1
UseInnerStop: true
RtGdb: &FastCacheRt{RealTimePath: "./realTimeDb"}
type RedisRt ¶ added in v1.0.7
type RedisRt struct { Ip string Port int PassWord string DbNum int KeyName string // contains filtered or unexported fields }
RedisRt implementation of using redis as a realTimeDatabase of gdb
func (*RedisRt) BatchFetch ¶ added in v1.0.7
func (*RedisRt) BatchWrite ¶ added in v1.0.7
type ResponseData ¶
type ResponseData struct { Code int `json:"code"` Message string `json:"message"` Data interface{} `json:"data"` }
ResponseData common
type RtGdb ¶ added in v1.0.7
type RtGdb interface { BatchWrite(keys, values [][]byte) error // batch write key-values pairs to realTime database BatchFetch(keys [][]byte) ([][]byte, error) // batch get data according to the given keys from realTime database, if given key not exist you should set nil Sync() error // save realTime data to other db or file system Load(params ...interface{}) error // initial database and load realTime data to database from db or other file system }
RtGdb realTime database interface, you can implement this interface to define your own way to store realTimeData
we provide to ways to store realTimeData, that is fastCache and redis
type StopGdb ¶ added in v1.0.7
type StopGdb interface {
StopService(params ...interface{}) error
}
StopGdb defines the way to stop gdb
type TimeMap ¶ added in v1.0.7
type TimeMap struct { Result cmap.ConcurrentMap `json:"result"` Times int64 `json:"times"` }