Пример #1
0
// 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
0
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
}