记一次couchdb模糊查询功能

版权声明:学习中。。。 https://blog.csdn.net/fangdengfu123/article/details/83385366

记一次couchdb模糊查询

在使用fabric的过程中,避免不了使用couchdb,记录一次模糊匹配的使用记录,方便下次查找。

在couchdb中存的数据结构:

type Account struct {
	Addr		string 				`json:"addr"`		// 地址
	Balance 	map[string]*big.Int `json:"balance"`	// 余额(各种账本不同的余额)
	Nonce   	int 				`json:"nonce"`		// 交易序号
	IsToken		bool 				`json:"isToken"`	// 其它业务标识
}

需求:获取拥有指定账本的所有账户的地址、余额、以及所占总量的百分比
技术点:匹配map类型的时候,需要使用 字段.key
匹配语法:

{
   "selector": {
      "balance.test7": {
         "$gt": 0
      }
   }
}

顺便记录一个自己手残写的百分比计算的方法:

var zero = new(big.Int).SetInt64(1000000000000000000)
// 保留4位小数,百分比之后变成2位
var percentSuf = new(big.Int).SetInt64(10000)

func toPercent(per string) string {
	len := len(per)
	var s string
	if len == 2 {
		s = "0" + string([]byte(per)[:len-2]) + "." + string([]byte(per)[len-2:]) + "%"
	} else if len == 1 {
		s = "0.0" + string([]byte(per)[len-1:]) + "%"
	} else {
		s = string([]byte(per)[:len-2]) + "." + string([]byte(per)[len-2:]) + "%"
	}
	return s
}

(方法就是在做除法前先将分子扩大,然后将结果缩小至保留4位小数,将结果在添加小数点和百分号)

	// test
	zero := new(big.Int).SetInt64(1000000000000000000)
	a := new(big.Int).SetInt64(10000)
	fmt.Println(a.String())
	b := new(big.Int).Mul(new(big.Int).SetInt64(9999), zero)
	fmt.Println(b.String())
	c := new(big.Int).Div(b, a)
	fmt.Println(c.String())
	res := c.Div(c, zero.Div(zero, new(big.Int).SetInt64(10000)))
	fmt.Println(res.String())
	percent := toPercent(res.String())
	fmt.Println(percent)

猜你喜欢

转载自blog.csdn.net/fangdengfu123/article/details/83385366