2021-07-29 10:14:06 +08:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-08-03 06:06:26 +08:00
|
|
|
"strings"
|
2021-07-29 10:14:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func SetPeers(newPeers map[string]string, networkName string) bool {
|
|
|
|
areEqual := PeersAreEqual(newPeers, networkName)
|
|
|
|
if !areEqual {
|
|
|
|
jsonData, err := json.Marshal(newPeers)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
InsertPeer(networkName, string(jsonData))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return !areEqual
|
|
|
|
}
|
2021-08-03 00:14:35 +08:00
|
|
|
func GetPeers(networkName string) (map[string]string, error) {
|
2021-07-29 10:14:06 +08:00
|
|
|
record, err := FetchRecord(PEERS_TABLE_NAME, networkName)
|
|
|
|
if err != nil {
|
2021-08-03 00:14:35 +08:00
|
|
|
return nil, err
|
2021-07-29 10:14:06 +08:00
|
|
|
}
|
|
|
|
currentDataMap := make(map[string]string)
|
|
|
|
err = json.Unmarshal([]byte(record), ¤tDataMap)
|
2021-08-03 00:14:35 +08:00
|
|
|
return currentDataMap, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func PeersAreEqual(toCompare map[string]string, networkName string) bool {
|
|
|
|
currentDataMap, err := GetPeers(networkName)
|
2021-07-29 10:14:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(currentDataMap) != len(toCompare) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for k := range currentDataMap {
|
2021-08-03 06:06:26 +08:00
|
|
|
if toCompare[k] != currentDataMap[k] {
|
2021-07-29 10:14:06 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
|
|
|
|
func IsEmptyRecord(err error) bool {
|
2021-08-06 01:05:30 +08:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
return strings.Contains(err.Error(), NO_RECORD) || strings.Contains(err.Error(), NO_RECORDS)
|
|
|
|
}
|