func showRes(stub *pub.WebApiStub, ID, resName string) error { var oldver int var err error oldver, err = pub.O2Int(stub.Hget(ID, resName, pub.FD_LastVer)) log.Info("ResName:", resName) if err != nil { return errors.New("failed to get last version of " + resName + "\nerror: " + err.Error()) } log.Info("Last Version:", oldver) oldver, err = pub.O2Int(stub.Hget(ID, resName, pub.FD_Release)) if err != nil { return errors.New("failed to get release version of " + resName + "\nerror: " + err.Error()) } log.Info("Release Version:", oldver) // if log.verbose { // keys, err := stub.Hkeys(ID, resName) // if err != nil { // return errors.New("failed to get info of ", resName, "\nerror: ", err.Error()) // } // for _, key := range keys { // fmt.Println(key) // } // } return nil }
func showRes(stub *pub.WebApiStub, ID, resName string) error { keys, err := stub.Hkeys(ID, resName) if err != nil { return err } for _, key := range keys { fmt.Println(key) } var oldver int oldver, err = pub.O2Int(stub.Hget(ID, resName, pub.FD_LastVer)) if err != nil { oldver = 0 } fmt.Printf("last version is %d\r\n", oldver) oldver, err = pub.O2Int(stub.Hget(ID, resName, pub.FD_Release)) if err != nil { oldver = 0 } fmt.Printf("release version is %d\r\n", oldver) return nil }
func ReleaseRes(stub *pub.WebApiStub, id, resName string, ver int) (err error) { if ver == 0 { ver, err = pub.O2Int(stub.Hget(id, resName, pub.FD_LastVer)) if err != nil { ver = 0 } } if _, err = stub.Hset(id, resName, pub.FD_Release, ver); err != nil { return err } fmt.Printf("set res[%s] version to [%d]\r\n", resName, ver) return nil }
func ReleaseRes(stub *pub.WebApiStub, id, resName string, ver int) error { lastVer, err := pub.O2Int(stub.Hget(id, resName, pub.FD_LastVer)) if err != nil { return errors.New("failed to get version for this resouce \"" + resName + "\"\nerror:" + err.Error()) } if ver == -1 { ver = lastVer } if _, err = stub.Hset(id, resName, pub.FD_Release, ver); err != nil { return err } fmt.Printf("released res \"%s\" version %d \r\n", resName, ver) return nil }
func PushRes(stub *pub.WebApiStub, BID, name, filename string, ver int) (ID string, err error) { log.V(" push", name, filename) if name == "" { return "", errName.Format("is null") } var oldver int var oldId string oldver, err = pub.O2Int(stub.Hget(BID, name, pub.FD_LastVer)) if err != nil { oldver = 0 } log.V("last version:", oldver) if ver > 0 && ver <= oldver { return "", errVer.Format(ver, oldver) } strVer := fmt.Sprint(oldver) if oldId, err = pub.O2Str(stub.Hget(BID, name, strVer)); err != nil { fmt.Fprintln(os.Stderr, err.Error()) oldId = "" } log.V("last id:", oldId) //读取文件 var rd []byte rd, err = ioutil.ReadFile(filename) if err != nil { return } ID, err = stub.Setdata(BID, rd) if err != nil { return } log.V("new id:", ID) if ID == oldId { log.V("resource not changed") return } ver = oldver + 1 strver := fmt.Sprintf("%d", oldver+1) stub.Hset(BID, name, strver, ID) stub.Hset(BID, name, pub.FD_LastVer, ver) return }
// 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 }
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 }