1
0
mirror of https://gitee.com/willfree/mlsr.git synced 2026-06-15 18:44:53 +08:00

解决LsaContainer测试错误的问题(输出不规范导致go没有识别);将FaceUri某函数由小写改为大写

This commit is contained in:
free will
2022-05-26 20:36:47 +08:00
parent 3e69ec2dea
commit 1efd9c63aa
2 changed files with 203 additions and 88 deletions
+64 -42
View File
@@ -14,6 +14,7 @@ package buntdb
import (
"errors"
"github.com/tidwall/buntdb"
common2 "minlib/common"
"minlib/component"
"mlsr/lsa"
"strconv"
@@ -48,17 +49,24 @@ type LsaContainer_BuntDB struct {
// @return error 如果成功初始化,则返回nil
//
func (bdb *LsaContainer_BuntDB) Init() error {
// 打开db
// 1. 打开db
db, err := buntdb.Open(":memory:")
if err != nil {
common2.LogError("buntdb open error")
return err
}
bdb.db = db
// 建立基于LsaType的索引,便于根据type进行数据库快速检索
common2.LogInfo("buntdb open success")
// 2. 建立基于LsaType的索引,便于根据type进行数据库快速检索
err = db.CreateIndex("LsaType", "*", buntdb.IndexJSON("LsaTypeInt"))
if err != nil {
common2.LogError("buntdb create index error")
return err
}
common2.LogInfo("buntdb create index success")
// 3. 赋值
bdb.db = db
return nil
}
@@ -109,13 +117,13 @@ func getKeyFromNameAndType(routerIdentifier *component.Identifier,
// @return lsa.ILsa
// @return error
//
func newLsa(lsaType lsa.LsaType) (lsa.ILsa,error) {
func newLsa(lsaType lsa.LsaType) (lsa.ILsa, error) {
if lsaType == lsa.LsaADJACENCYType {
return new(lsa.AdjLsa),nil
}else if lsaType == lsa.LsaNAMEType{
return new(lsa.NameLsa),nil
}else {
return nil,errors.New("invalid lsa type")
return new(lsa.AdjLsa), nil
} else if lsaType == lsa.LsaNAMEType {
return new(lsa.NameLsa), nil
} else {
return nil, errors.New("invalid lsa type")
}
}
@@ -127,8 +135,9 @@ func newLsa(lsaType lsa.LsaType) (lsa.ILsa,error) {
//
func (bdb *LsaContainer_BuntDB) Emplace(lsa lsa.ILsa) error {
// 将lsa转为json字符串
jsonBytes,err := lsa.GetJsonString()
if err!= nil{
jsonBytes, err := lsa.GetJsonString()
if err != nil {
common2.LogError("buntdb emplace get json error, because of ", err.Error())
return err
}
@@ -137,14 +146,15 @@ func (bdb *LsaContainer_BuntDB) Emplace(lsa lsa.ILsa) error {
// 将转为的json存入键值对数据库
err = bdb.db.Update(func(tx *buntdb.Tx) error {
_, _, err := tx.Set(key, string(jsonBytes), nil)
if err != nil {
return err
_, _, err2 := tx.Set(key, string(jsonBytes), nil)
if err2 != nil {
common2.LogError("buntdb emplace set value error, because of ", err2.Error())
return err2
}
return nil
})
if err!= nil{
if err != nil {
return err
}
@@ -159,7 +169,11 @@ func (bdb *LsaContainer_BuntDB) Emplace(lsa lsa.ILsa) error {
//
func (bdb *LsaContainer_BuntDB) EraseLsa(lsa lsa.ILsa) error {
key := getKeyFromLsa(lsa)
return bdb.EraseLsaByKey(key)
err := bdb.EraseLsaByKey(key)
if err != nil {
common2.LogError("buntdb EraseLsa error, because of ", err.Error())
}
return err
}
//
@@ -172,8 +186,9 @@ func (bdb *LsaContainer_BuntDB) EraseLsa(lsa lsa.ILsa) error {
func (bdb *LsaContainer_BuntDB) EraseLsaByKey(key string) error {
errRes := bdb.db.Update(func(tx *buntdb.Tx) error {
_, err := tx.Delete(key)
//fmt.Println("删除的返回值是啥: ",ss) // 返回值是删除的value
// 返回值是删除的value
if err != nil {
common2.LogError("buntdb EraseLsaByKey delete error, because of ", err.Error())
return err
}
@@ -193,31 +208,34 @@ func (bdb *LsaContainer_BuntDB) EraseLsaByKey(key string) error {
// @return error
//
func (bdb *LsaContainer_BuntDB) GetLSAByNameAndType(routerIdentifier *component.Identifier,
lsaType lsa.LsaType) (lsa.ILsa,error) {
glsa,err := newLsa(lsaType) // 要查找的lsa
if err!= nil{
lsaType lsa.LsaType) (lsa.ILsa, error) {
glsa, err := newLsa(lsaType) // 要查找的lsa
if err != nil {
common2.LogError("buntdb GetLSAByNameAndType new lsa error, because of ", err.Error())
return nil, err
}
errRes := bdb.db.View(func(tx *buntdb.Tx) error {
// 获取key
key := getKeyFromNameAndType(routerIdentifier,lsaType)
key := getKeyFromNameAndType(routerIdentifier, lsaType)
// 获取value
value, err := tx.Get(key)
if err != nil {
return err
value, err2 := tx.Get(key)
if err2 != nil {
common2.LogError("buntdb GetLSAByNameAndType get key error, because of ", err2.Error())
return err2
}
// 将value转为lsa
err = glsa.LoadFromJsonString([]byte(value))
if err != nil {
return err
err2 = glsa.LoadFromJsonString([]byte(value))
if err2 != nil {
common2.LogError("buntdb GetLSAByNameAndType load from json error, because of ", err2.Error())
return err2
}
return nil
})
return glsa,errRes
return glsa, errRes
}
//
@@ -228,33 +246,36 @@ func (bdb *LsaContainer_BuntDB) GetLSAByNameAndType(routerIdentifier *component.
// @return []lsa.ILsa
// @return error
//
func (bdb *LsaContainer_BuntDB) GetLSAsByType(lsaType lsa.LsaType) ([]lsa.ILsa,error) {
func (bdb *LsaContainer_BuntDB) GetLSAsByType(lsaType lsa.LsaType) ([]lsa.ILsa, error) {
var lsas []lsa.ILsa
equalString := `{"LsaTypeInt":`+ strconv.Itoa(int(lsaType)) +`}` // 查找的类型
equalString := `{"LsaTypeInt":` + strconv.Itoa(int(lsaType)) + `}` // 查找的类型
errRes := bdb.db.View(func(tx *buntdb.Tx) error {
// 查找类型是lsaType的所有Lsa
err := tx.AscendEqual("LsaType", equalString, func(key, value string) bool {
// 构建一个Lsa
cacheLsa,err1 := newLsa(lsaType)
if err1!= nil{
cacheLsa, err1 := newLsa(lsaType)
if err1 != nil {
common2.LogError("buntdb GetLSAsByType new lsa error, because of ", err1.Error())
return false
}
// 将查找到的lsajson放入该lsa
err1 = cacheLsa.LoadFromJsonString([]byte(value))
if err1!= nil{
if err1 != nil {
common2.LogError("buntdb GetLSAsByType load from json error, because of ", err1.Error())
return false
}
// 将该lsa放入数组
lsas = append(lsas,cacheLsa)
lsas = append(lsas, cacheLsa)
return true
})
if err!=nil {
if err != nil {
common2.LogError("buntdb GetLSAsByType AscendEqual error, because of ", err.Error())
return err
}
return nil
})
return lsas,errRes
return lsas, errRes
}
//
@@ -265,21 +286,22 @@ func (bdb *LsaContainer_BuntDB) GetLSAsByType(lsaType lsa.LsaType) ([]lsa.ILsa,e
// @return []string
// @return error
//
func (bdb *LsaContainer_BuntDB) GetLSAKeysByType(lsaType lsa.LsaType) ([]string,error) {
func (bdb *LsaContainer_BuntDB) GetLSAKeysByType(lsaType lsa.LsaType) ([]string, error) {
var keys []string
equalString := `{"LsaTypeInt":`+ strconv.Itoa(int(lsaType)) +`}` // 查找的类型
equalString := `{"LsaTypeInt":` + strconv.Itoa(int(lsaType)) + `}` // 查找的类型
errRes := bdb.db.View(func(tx *buntdb.Tx) error {
// 查找类型是lsaType的所有Lsa
err := tx.AscendEqual("LsaType", equalString, func(key, value string) bool {
// 将该lsa放入数组
keys = append(keys,key)
keys = append(keys, key)
return true
})
if err!=nil {
if err != nil {
common2.LogError("buntdb GetLSAKeysByType AscendEqual error, because of ", err.Error())
return err
}
return nil
})
return keys,errRes
}
return keys, errRes
}
@@ -9,89 +9,182 @@
package buntdb
import (
"fmt"
"log"
common2 "minlib/common"
"minlib/component"
"mlsr/lsa"
"testing"
)
//
// TestLsaContainer_BuntDB_Emplace
// @Description:
// TestLsaContainer_BuntDB_Init
// @Description: 测试buntdb初始化
// @param t
//
func TestLsaContainer_BuntDB_Emplace_Get_Erase(t *testing.T) {
func TestLsaContainer_BuntDB_Init(t *testing.T) {
// 构造LsaContainer
lsaContainer := new(LsaContainer_BuntDB)
err := lsaContainer.Init()
if err != nil {
common2.LogInfo("lsaContainer创建失败:", err.Error())
}
}
//
// TestLsaContainer_BuntDB_Emplace
// @Description: 测试插入
// @param t
//
func TestLsaContainer_BuntDB_Emplace(t *testing.T) {
// 构造NameLsa
base := new(lsa.NameLsa)
base.SetLsaExpirationTime(2000)
base.SetLsaSequenceNumber(1234)
base.LsaOriginRouterIdentifier,_ = component.CreateIdentifierByString("/min/pku")
ident,_ := component.CreateIdentifierByString("/pku/routerfucker")
base.Insert(ident,"/bind")
base.Insert(ident,"/wechat")
ident,_ = component.CreateIdentifierByString("/pku/router")
base.Insert(ident,"/qq")
fmt.Println("ToString: "+base.ToString())
base.LsaOriginRouterIdentifier, _ = component.CreateIdentifierByString("/min/pku")
ident, _ := component.CreateIdentifierByString("/pku/routerfucker")
base.Insert(ident, "/bind")
base.Insert(ident, "/wechat")
ident, _ = component.CreateIdentifierByString("/pku/router")
base.Insert(ident, "/qq")
common2.LogInfo("ToString: " + base.ToString())
// 构造LsaContainer
lsaContainer := new(LsaContainer_BuntDB)
err := lsaContainer.Init()
if err!=nil{
log.Fatal("lsaContainer创建失败:",err.Error())
if err != nil {
common2.LogInfo("lsaContainer创建失败:", err.Error())
}
// 将NameLsa插入lsaContainer
err = lsaContainer.Emplace(base)
if err!=nil{
log.Fatal("lsaContainer存入lsa失败:",err.Error())
if err != nil {
common2.LogInfo("lsaContainer存入名称lsa失败:", err.Error())
}
fmt.Println("lsaContainer存入lsa成功")
common2.LogInfo("lsaContainer存入名称lsa成功")
}
//
// TestLsaContainer_BuntDB_Emplace_Get_Erase_Adj_Lsa
// @Description: 测试对邻接LSA进行增删改查
// @param t
//
func TestLsaContainer_BuntDB_Emplace_Get_Erase_Adj_Lsa(t *testing.T) {
// 构造AdjLsa
base := new(lsa.AdjLsa)
base.SetLsaExpirationTime(2000)
base.SetLsaSequenceNumber(1234)
base.LsaOriginRouterIdentifier, _ = component.CreateIdentifierByString("/min/pku")
// 构造邻接信息部分
// 先构造一个info
linkCost := lsa.AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := lsa.AdjLsaLogicFaceUri{}
faceUri.SetLogicFaceUri("uri")
faceId := lsa.AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden, _ := component.CreateIdentifierByString("/pku/router1")
adjInfo := lsa.NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1)
base.Insert(adjInfo)
// 再加第二个info
iden2, _ := component.CreateIdentifierByString("/pku/router2")
adjInfo2 := lsa.NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden2, 111, 0)
base.Insert(adjInfo2)
// 构造LsaContainer
lsaContainer := new(LsaContainer_BuntDB)
err := lsaContainer.Init()
if err != nil {
common2.LogInfo("lsaContainer创建失败:", err.Error())
}
// 将AdjLsa插入lsaContainer
err = lsaContainer.Emplace(base)
if err != nil {
common2.LogInfo("lsaContainer存入邻接lsa失败:", err.Error())
}
common2.LogInfo("lsaContainer存入邻接lsa成功")
}
//
// TestLsaContainer_BuntDB_Emplace
// @Description: 测试对名称LSA进行增删改查
// @param t
//
func TestLsaContainer_BuntDB_Emplace_Get_Erase_Name_Lsa(t *testing.T) {
// 构造NameLsa
base := new(lsa.NameLsa)
base.SetLsaExpirationTime(2000)
base.SetLsaSequenceNumber(1234)
base.LsaOriginRouterIdentifier, _ = component.CreateIdentifierByString("/min/pku")
ident, _ := component.CreateIdentifierByString("/pku/routerfucker")
base.Insert(ident, "/bind")
base.Insert(ident, "/wechat")
ident, _ = component.CreateIdentifierByString("/pku/router")
base.Insert(ident, "/qq")
common2.LogInfo("ToString: " + base.ToString())
// 构造LsaContainer
lsaContainer := new(LsaContainer_BuntDB)
err := lsaContainer.Init()
if err != nil {
common2.LogInfo("lsaContainer创建失败:", err.Error())
}
// 将NameLsa插入lsaContainer
err = lsaContainer.Emplace(base)
if err != nil {
common2.LogInfo("lsaContainer存入名称lsa失败:", err.Error())
}
common2.LogInfo("lsaContainer存入名称lsa成功")
common2.LogInfo("fuck")
//for true {
// time.Sleep(20000)
//}
// 获取lsa
origin,_ := component.CreateIdentifierByString("/min/pku")
lsa,err := lsaContainer.GetLSAByNameAndType(origin,2) // 2表示namelsa
if err!=nil{
log.Fatal("lsaContainer取出lsa失败:",err.Error())
origin, _ := component.CreateIdentifierByString("/min/pku")
lsa, err := lsaContainer.GetLSAByNameAndType(origin, 2) // 2表示namelsa
if err != nil {
common2.LogInfo("lsaContainer取出名称lsa失败:", err.Error())
}
ss,_ := lsa.GetJsonString()
fmt.Println("lsa取出来:",string(ss))
ss, _ := lsa.GetJsonString()
common2.LogInfo("lsa取出来:", string(ss))
// 再放入一个namelsa
base.LsaOriginRouterIdentifier,_ = component.CreateIdentifierByString("/min/pku111")
base.LsaOriginRouterIdentifier, _ = component.CreateIdentifierByString("/min/pku111")
err = lsaContainer.Emplace(base)
if err!=nil{
log.Fatal("lsaContainer存入lsa失败:",err.Error())
if err != nil {
common2.LogInfo("lsaContainer存入名称lsa失败:", err.Error())
}
fmt.Println("lsaContainer存入lsa成功")
common2.LogInfo("lsaContainer存入名称lsa成功")
// 取出所有namelsa
lsas,_ := lsaContainer.GetLSAsByType(2)
fmt.Print("取出所有namelsa:")
for i:=0;i<len(lsas);i++ {
fmt.Println(lsas[i].ToString())
lsas, _ := lsaContainer.GetLSAsByType(2)
common2.LogInfo("取出所有namelsa:")
for i := 0; i < len(lsas); i++ {
common2.LogInfo(lsas[i].ToString())
}
fmt.Println("")
common2.LogInfo("")
lsas,_ = lsaContainer.GetLSAsByType(1)
fmt.Print("取出所有adjlsa:")
for i:=0;i<len(lsas);i++ {
fmt.Println(lsas[i].ToString())
lsas, _ = lsaContainer.GetLSAsByType(1)
common2.LogInfo("取出所有adjlsa:")
for i := 0; i < len(lsas); i++ {
common2.LogInfo(lsas[i].ToString())
}
fmt.Println("")
common2.LogInfo("")
// 取出所有Namelsa的key
keys,_ := lsaContainer.GetLSAKeysByType(2)
fmt.Print("取出所有namelsa的key:",keys)
fmt.Println("")
keys, _ := lsaContainer.GetLSAKeysByType(2)
common2.LogInfo("取出所有namelsa的key:", keys)
common2.LogInfo("")
// 删除base2
_ = lsaContainer.EraseLsa(base)
lsas,_ = lsaContainer.GetLSAsByType(2)
fmt.Print("删除一个后,取出所有namelsa:")
for i:=0;i<len(lsas);i++ {
fmt.Println(lsas[i].ToString())
lsas, _ = lsaContainer.GetLSAsByType(2)
common2.LogInfo("删除一个后,取出所有namelsa:")
for i := 0; i < len(lsas); i++ {
common2.LogInfo(lsas[i].ToString())
}
fmt.Println("")
common2.LogInfo("")
}