本文整理汇总了Golang中lzbgt/go-losapi/pub.WebApiStub.Del方法的典型用法代码示例。如果您正苦于以下问题:Golang WebApiStub.Del方法的具体用法?Golang WebApiStub.Del怎么用?Golang WebApiStub.Del使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lzbgt/go-losapi/pub.WebApiStub
的用法示例。
在下文中一共展示了WebApiStub.Del方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: clearRes
// ver = -1 release
// ver = -2 last
func clearRes(stub *pub.WebApiStub, ID, name string, ver int) (err error) {
//if name == "" {
// if flag.NArg() < 2 {
// return errors.New("Res clear name ver")
// }
// name = flag.Arg(1)
// if *ver == 0 {
// if flag.NArg() < 3 {
// return errors.New("Res clear name ver")
// }
// strVer := flag.Arg(2)
// if strVer == pub.FD_Release {
// *ver, err = pub.O2Int(stub.Hget(ID, name, pub.FD_Release))
// if err != nil {
// *ver = 0
// }
// } else {
// if _, err = fmt.Sscan(strVer, ver); err != nil {
// return err
// }
// }
// }
//}
var c int64
getVer := func(strType string, def int) (ver int) {
ver, err = pub.O2Int(stub.Hget(ID, name, strType))
if err != nil {
ver = def
}
return
}
if ver == -1 {
ver = getVer(pub.FD_Release, 0)
} else if ver == -2 {
ver = getVer(pub.FD_LastVer, 0)
}
rVer := getVer(pub.FD_Release, -1)
lVer := getVer(pub.FD_LastVer, -1)
//rVer, err := pub.O2Int(stub.Hget(ID, name, pub.FD_Release))
//if err != nil {
// //fmt.Println(err.Error())
// rVer = -1
//}
//lVer, err := pub.O2Int(stub.Hget(ID, name, pub.FD_LastVer))
//if err != nil {
// //fmt.Println(err.Error())
// lVer = -1
//}
fmt.Printf("clear name=%s ver =%d\r\n", name, ver)
//versions, err := stub.Hkeys(ID, name)
//if err != nil {
// return err
//}
fvs, err := stub.Hgetall(ID, name)
if err != nil {
return err
}
var mapRef = make(map[string]int, 10)
for _, fv := range fvs {
id, _ := fv.Value.(string) //resid
mapRef[id] = mapRef[id] + 1
}
fmt.Printf("last version:%d, release version:%d\r\n", lVer, rVer)
for _, fv := range fvs {
v, err := pub.O2Int(fv.Field, nil) //版本v
if err != nil {
continue
}
id, _ := fv.Value.(string) //resid
fmt.Println("cur version:", v, " get id :", fv.Value, "refs=", mapRef[id])
if v >= ver || v == lVer || v == rVer {
//要确保release和debug能正常使用
continue
}
//根据版本取资源的id
if mapRef[id] <= 1 {
fmt.Printf("ref[%d] <= 1, no use, to del res\r\n", mapRef[id])
if c, err = stub.Del(ID, id); err != nil {
return err
}
fmt.Println("del res ok. c=", c, "id:", fv.Value)
}
if c, err = stub.Hdel(ID, name, fv.Field); err != nil {
return err
}
fmt.Println("c=", c, "version:", fv.Field, " deleted")
}
return nil
}
示例2: clearRes
func clearRes(stub *pub.WebApiStub, ID, name string, ver int) (err error) {
var c int64
getVer := func(strType string, def int) (ver int) {
ver, err = pub.O2Int(stub.Hget(ID, name, strType))
if err != nil {
ver = def
}
return
}
if ver == -1 {
ver = getVer(pub.FD_Release, -1)
if ver == -1 {
log.Info("failed to get release version for res", name)
return nil
}
} else if ver == -2 {
if ver == -1 {
log.Info("failed to get last version for res", name)
return nil
}
}
if (ver - 4) < 0 {
log.Info("no need to clear")
return
}
rVer := getVer(pub.FD_Release, -1)
lVer := getVer(pub.FD_LastVer, -1)
log.Info("clear resouce ", name)
log.V("range: version 0 ~", ver-4, "except v", rVer, "(release) and", lVer, "(last)")
fvs, err := stub.Hgetall(ID, name)
if err != nil {
return err
}
var mapRef = make(map[string]int, 10)
for _, fv := range fvs {
id, _ := fv.Value.(string) //resid
mapRef[id] = mapRef[id] + 1
}
fmt.Printf("last version:%d, release version:%d\r\n", lVer, rVer)
for _, fv := range fvs {
v, err := pub.O2Int(fv.Field, nil) //版本v
if err != nil {
continue
}
id, _ := fv.Value.(string) //resid
fmt.Println("cur version:", v, " get id :", fv.Value, "refs=", mapRef[id])
// for safety, preserve at least 4 versions
if v >= (ver-4) || v == lVer || v == rVer {
//要确保release和debug能正常使用
continue
}
//根据版本取资源的id
if mapRef[id] <= 1 {
fmt.Printf("ref[%d] <= 1, no use, to del res\r\n", mapRef[id])
if c, err = stub.Del(ID, id); err != nil {
return err
}
fmt.Println("del res ok. c=", c, "id:", fv.Value)
}
if c, err = stub.Hdel(ID, name, fv.Field); err != nil {
return err
}
fmt.Println("c=", c, "version:", fv.Field, " deleted")
}
return nil
}