• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang clientv3.KV类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/coreos/etcd/clientv3.KV的典型用法代码示例。如果您正苦于以下问题:Golang KV类的具体用法?Golang KV怎么用?Golang KV使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了KV类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: doPutForWatch

func doPutForWatch(ctx context.Context, client v3.KV, requests <-chan v3.Op) {
	for op := range requests {
		_, err := client.Do(ctx, op)
		if err != nil {
			fmt.Fprintf(os.Stderr, "failed to Put for watch benchmark: %v\n", err)
			os.Exit(1)
		}
	}
}
开发者ID:CliffYuan,项目名称:etcd,代码行数:9,代码来源:watch.go


示例2: deleteRevKey

// deleteRevKey deletes a key by revision, returning false if key is missing
func deleteRevKey(kv v3.KV, key string, rev int64) (bool, error) {
	cmp := v3.Compare(v3.ModRevision(key), "=", rev)
	req := v3.OpDelete(key)
	txnresp, err := kv.Txn(context.TODO()).If(cmp).Then(req).Commit()
	if err != nil {
		return false, err
	} else if !txnresp.Succeeded {
		return false, nil
	}
	return true, nil
}
开发者ID:CliffYuan,项目名称:etcd,代码行数:12,代码来源:client.go


示例3: putNewKV

// putNewKV attempts to create the given key, only succeeding if the key did
// not yet exist.
func putNewKV(kv v3.KV, key, val string, leaseID v3.LeaseID) (int64, error) {
	cmp := v3.Compare(v3.Version(key), "=", 0)
	req := v3.OpPut(key, val, v3.WithLease(leaseID))
	txnresp, err := kv.Txn(context.TODO()).If(cmp).Then(req).Commit()
	if err != nil {
		return 0, err
	}
	if !txnresp.Succeeded {
		return 0, ErrKeyExists
	}
	return txnresp.Header.Revision, nil
}
开发者ID:luxas,项目名称:flannel,代码行数:14,代码来源:key.go


示例4: GetRemoteKV

func GetRemoteKV(kv v3.KV, key string) (*RemoteKV, error) {
	resp, err := kv.Get(context.TODO(), key)
	if err != nil {
		return nil, err
	}
	rev := resp.Header.Revision
	val := ""
	if len(resp.Kvs) > 0 {
		rev = resp.Kvs[0].ModRevision
		val = string(resp.Kvs[0].Value)
	}
	return &RemoteKV{kv: kv, key: key, rev: rev, val: val}, nil
}
开发者ID:luxas,项目名称:flannel,代码行数:13,代码来源:key.go


示例5: doPut

func doPut(ctx context.Context, client v3.KV, requests <-chan v3.Op) {
	defer wg.Done()

	for op := range requests {
		st := time.Now()
		_, err := client.Do(ctx, op)

		var errStr string
		if err != nil {
			errStr = err.Error()
		}
		results <- result{errStr: errStr, duration: time.Since(st)}
		bar.Increment()
	}
}
开发者ID:youtube,项目名称:doorman,代码行数:15,代码来源:put.go


示例6: NewUniqueKV

func NewUniqueKV(ctx context.Context, kv v3.KV, pfx, val string, opts ...v3.OpOption) (string, int64, error) {
	for {
		newKey := fmt.Sprintf("%s/%v", pfx, time.Now().UnixNano())
		put := v3.OpPut(newKey, val, opts...)
		cmp := v3.Compare(v3.ModifiedRevision(newKey), "=", 0)
		resp, err := kv.Txn(ctx).If(cmp).Then(put).Commit()
		if err != nil {
			return "", 0, err
		}
		if !resp.Succeeded {
			continue
		}
		return newKey, resp.Header.Revision, nil
	}
}
开发者ID:siddontang,项目名称:etcd,代码行数:15,代码来源:key.go


示例7: doRange

func doRange(client v3.KV, requests <-chan v3.Op) {
	defer wg.Done()

	for op := range requests {
		st := time.Now()
		_, err := client.Do(context.Background(), op)

		var errStr string
		if err != nil {
			errStr = err.Error()
		}
		results <- result{errStr: errStr, duration: time.Since(st), happened: time.Now()}
		bar.Increment()
	}
}
开发者ID:oywc410,项目名称:etcd,代码行数:15,代码来源:range.go


示例8: cas

func cas(key, value, newValue string, kvc clientv3.KV) error {
	ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
	tnxRes, err := kvc.Txn(ctx).
		If(clientv3.Compare(clientv3.Value(key), "=", value)).
		Then(clientv3.OpPut(key, newValue)).
		Else(clientv3.OpGet(key)).
		Commit()
	cancel()

	if err != nil {
		return err
	}

	if tnxRes.Succeeded {
		return nil
	}
	log.Println(string(tnxRes.Responses[0].GetResponseRange().Kvs[0].Value))
	return errors.New("release error")
}
开发者ID:yorkart,项目名称:etcd-demo,代码行数:19,代码来源:key.go


示例9: newSequentialKV

// newSequentialKV allocates a new sequential key <prefix>/nnnnn with a given
// value and lease.  Note: a bookkeeping node __<prefix> is also allocated.
func newSequentialKV(kv v3.KV, prefix, val string, leaseID v3.LeaseID) (*RemoteKV, error) {
	resp, err := kv.Get(context.TODO(), prefix, v3.WithLastKey()...)
	if err != nil {
		return nil, err
	}

	// add 1 to last key, if any
	newSeqNum := 0
	if len(resp.Kvs) != 0 {
		fields := strings.Split(string(resp.Kvs[0].Key), "/")
		_, serr := fmt.Sscanf(fields[len(fields)-1], "%d", &newSeqNum)
		if serr != nil {
			return nil, serr
		}
		newSeqNum++
	}
	newKey := fmt.Sprintf("%s/%016d", prefix, newSeqNum)

	// base prefix key must be current (i.e., <=) with the server update;
	// the base key is important to avoid the following:
	// N1: LastKey() == 1, start txn.
	// N2: New Key 2, New Key 3, Delete Key 2
	// N1: txn succeeds allocating key 2 when it shouldn't
	baseKey := "__" + prefix

	// current revision might contain modification so +1
	cmp := v3.Compare(v3.ModRevision(baseKey), "<", resp.Header.Revision+1)
	reqPrefix := v3.OpPut(baseKey, "", v3.WithLease(leaseID))
	reqNewKey := v3.OpPut(newKey, val, v3.WithLease(leaseID))

	txn := kv.Txn(context.TODO())
	txnresp, err := txn.If(cmp).Then(reqPrefix, reqNewKey).Commit()
	if err != nil {
		return nil, err
	}
	if !txnresp.Succeeded {
		return newSequentialKV(kv, prefix, val, leaseID)
	}
	return &RemoteKV{kv, newKey, txnresp.Header.Revision, val}, nil
}
开发者ID:luxas,项目名称:flannel,代码行数:42,代码来源:key.go


示例10: initKey

func initKey(key string, kvc clientv3.KV) error {
	if _, err := kvc.Put(context.TODO(), key, ""); err != nil {
		return err
	}
	return nil
}
开发者ID:yorkart,项目名称:etcd-demo,代码行数:6,代码来源:key.go



注:本文中的github.com/coreos/etcd/clientv3.KV类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang clientv3.Watcher类代码示例发布时间:2022-05-23
下一篇:
Golang clientv3.Config类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap