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

Golang client.KV类代码示例

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

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



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

示例1: incCmd

// incCmd adds one to the value of c.key in the env and writes
// it to the db. If c.key isn't in the db, writes 1.
func incCmd(c *cmd, db *client.KV, t *testing.T) error {
	r := &proto.IncrementResponse{}
	if err := db.Call(proto.Increment, &proto.IncrementRequest{
		RequestHeader: proto.RequestHeader{Key: c.getKey()},
		Increment:     int64(1),
	}, r); err != nil {
		return err
	}
	c.env[c.key] = r.NewValue
	c.debug = fmt.Sprintf("[%d ts=%d]", r.NewValue, r.Timestamp.Logical)
	return nil
}
开发者ID:josephwinston,项目名称:cockroach,代码行数:14,代码来源:txn_correctness_test.go


示例2: allocateStoreIDs

// allocateStoreIDs increments the store id generator key for the
// specified node to allocate "inc" new, unique store ids. The
// first ID in a contiguous range is returned on success.
func allocateStoreIDs(nodeID proto.NodeID, inc int64, db *client.KV) (proto.StoreID, error) {
	iReply := &proto.IncrementResponse{}
	if err := db.Call(proto.Increment, &proto.IncrementRequest{
		RequestHeader: proto.RequestHeader{
			Key:  engine.MakeKey(engine.KeyStoreIDGeneratorPrefix, []byte(strconv.Itoa(int(nodeID)))),
			User: storage.UserRoot,
		},
		Increment: inc,
	}, iReply); err != nil {
		return 0, util.Errorf("unable to allocate %d store IDs for node %d: %v", inc, nodeID, err)
	}
	return proto.StoreID(iReply.NewValue - inc + 1), nil
}
开发者ID:josephwinston,项目名称:cockroach,代码行数:16,代码来源:node.go


示例3: allocateNodeID

// allocateNodeID increments the node id generator key to allocate
// a new, unique node id.
func allocateNodeID(db *client.KV) (proto.NodeID, error) {
	iReply := &proto.IncrementResponse{}
	if err := db.Call(proto.Increment, &proto.IncrementRequest{
		RequestHeader: proto.RequestHeader{
			Key:  engine.KeyNodeIDGenerator,
			User: storage.UserRoot,
		},
		Increment: 1,
	}, iReply); err != nil {
		return 0, util.Errorf("unable to allocate node ID: %v", err)
	}
	return proto.NodeID(iReply.NewValue), nil
}
开发者ID:josephwinston,项目名称:cockroach,代码行数:15,代码来源:node.go


示例4: sumCmd

// sumCmd sums the values of all keys read during the transaction
// and writes the result to the db.
func sumCmd(c *cmd, db *client.KV, t *testing.T) error {
	sum := int64(0)
	for _, v := range c.env {
		sum += v
	}
	r := &proto.PutResponse{}
	err := db.Call(proto.Put, &proto.PutRequest{
		RequestHeader: proto.RequestHeader{Key: c.getKey()},
		Value:         proto.Value{Integer: gogoproto.Int64(sum)},
	}, r)
	c.debug = fmt.Sprintf("[%d ts=%d]", sum, r.Timestamp.Logical)
	return err
}
开发者ID:josephwinston,项目名称:cockroach,代码行数:15,代码来源:txn_correctness_test.go


示例5: readCmd

// readCmd reads a value from the db and stores it in the env.
func readCmd(c *cmd, db *client.KV, t *testing.T) error {
	r := &proto.GetResponse{}
	if err := db.Call(proto.Get, &proto.GetRequest{
		RequestHeader: proto.RequestHeader{Key: c.getKey()},
	}, r); err != nil {
		return err
	}
	if r.Value != nil {
		c.env[c.key] = r.Value.GetInteger()
		c.debug = fmt.Sprintf("[%d ts=%d]", r.Value.GetInteger(), r.Timestamp.Logical)
	}
	return nil
}
开发者ID:josephwinston,项目名称:cockroach,代码行数:14,代码来源:txn_correctness_test.go


示例6: scanCmd

// scanCmd reads the values from the db from [key, endKey).
func scanCmd(c *cmd, db *client.KV, t *testing.T) error {
	r := &proto.ScanResponse{}
	if err := db.Call(proto.Scan, &proto.ScanRequest{
		RequestHeader: proto.RequestHeader{Key: c.getKey(), EndKey: c.getEndKey()},
	}, r); err != nil {
		return err
	}
	var vals []string
	keyPrefix := []byte(fmt.Sprintf("%d.", c.historyIdx))
	for _, kv := range r.Rows {
		key := bytes.TrimPrefix(kv.Key, keyPrefix)
		c.env[string(key)] = kv.Value.GetInteger()
		vals = append(vals, fmt.Sprintf("%d", kv.Value.GetInteger()))
	}
	c.debug = fmt.Sprintf("[%s ts=%d]", strings.Join(vals, " "), r.Timestamp.Logical)
	return nil
}
开发者ID:josephwinston,项目名称:cockroach,代码行数:18,代码来源:txn_correctness_test.go


示例7: runTxn

func (hv *historyVerifier) runTxn(txnIdx int, priority int32,
	isolation proto.IsolationType, cmds []*cmd, db *client.KV, t *testing.T) error {
	var retry int
	txnName := fmt.Sprintf("txn%d", txnIdx)
	txnOpts := &client.TransactionOptions{
		Name:      txnName,
		Isolation: isolation,
	}
	err := db.RunTransaction(txnOpts, func(txn *client.KV) error {
		txn.UserPriority = -priority

		env := map[string]int64{}
		// TODO(spencer): restarts must create additional histories. They
		// look like: given the current partial history and a restart on
		// txn txnIdx, re-enumerate a set of all histories containing the
		// remaining commands from extant txns and all commands from this
		// restarted txn.

		// If this is attempt > 1, reset cmds so no waits.
		if retry++; retry == 2 {
			for _, c := range cmds {
				c.done()
			}
		}
		log.V(1).Infof("%s, retry=%d", txnName, retry)
		for i := range cmds {
			cmds[i].env = env
			if err := hv.runCmd(txn, txnIdx, retry, i, cmds, t); err != nil {
				return err
			}
		}
		return nil
	})
	hv.wg.Done()
	return err
}
开发者ID:josephwinston,项目名称:cockroach,代码行数:36,代码来源:txn_correctness_test.go


示例8: concurrentIncrements

// concurrentIncrements starts two Goroutines in parallel, both of which
// read the integers stored at the other's key and add it onto their own.
// It is checked that the outcome is serializable, i.e. exactly one of the
// two Goroutines (the later write) sees the previous write by the other.
func concurrentIncrements(kvClient *client.KV, t *testing.T) {
	// wgStart waits for all transactions to line up, wgEnd has the main
	// function wait for them to finish.
	var wgStart, wgEnd sync.WaitGroup
	wgStart.Add(2 + 1)
	wgEnd.Add(2)

	for i := 0; i < 2; i++ {
		go func(i int) {
			// Read the other key, write key i.
			readKey := []byte(fmt.Sprintf("value-%d", (i+1)%2))
			writeKey := []byte(fmt.Sprintf("value-%d", i))
			defer wgEnd.Done()
			wgStart.Done()
			// Wait until the other goroutines are running.
			wgStart.Wait()

			txnOpts := &client.TransactionOptions{
				Name: fmt.Sprintf("test-%d", i),
			}
			if err := kvClient.RunTransaction(txnOpts, func(txn *client.KV) error {
				// Retrieve the other key.
				gr := &proto.GetResponse{}
				if err := txn.Call(proto.Get, proto.GetArgs(readKey), gr); err != nil {
					return err
				}

				otherValue := int64(0)
				if gr.Value != nil && gr.Value.Integer != nil {
					otherValue = *gr.Value.Integer
				}

				pr := &proto.IncrementResponse{}
				pa := proto.IncrementArgs(writeKey, 1+otherValue)
				if err := txn.Call(proto.Increment, pa, pr); err != nil {
					return err
				}

				return nil
			}); err != nil {
				t.Error(err)
			}
		}(i)
	}

	// Kick the goroutines loose.
	wgStart.Done()
	// Wait for the goroutines to finish.
	wgEnd.Wait()
	// Verify that both keys contain something and, more importantly, that
	// one key actually contains the value of the first writer and not only
	// its own.
	total := int64(0)
	results := []int64(nil)
	for i := 0; i < 2; i++ {
		readKey := []byte(fmt.Sprintf("value-%d", i))
		gr := &proto.GetResponse{}
		if err := kvClient.Call(proto.Get, proto.GetArgs(readKey), gr); err != nil {
			log.Fatal(err)
		}
		if gr.Value == nil || gr.Value.Integer == nil {
			t.Fatalf("unexpected empty key: %v=%v", readKey, gr.Value)
		}
		total += *gr.Value.Integer
		results = append(results, *gr.Value.Integer)
	}

	// First writer should have 1, second one 2
	if total != 3 {
		t.Fatalf("got unserializable values %v", results)
	}

}
开发者ID:josephwinston,项目名称:cockroach,代码行数:77,代码来源:client_test.go


示例9: commitCmd

// commitCmd commits the transaction.
func commitCmd(c *cmd, db *client.KV, t *testing.T) error {
	r := &proto.EndTransactionResponse{}
	err := db.Call(proto.EndTransaction, &proto.EndTransactionRequest{Commit: true}, r)
	c.debug = fmt.Sprintf("[ts=%d]", r.Timestamp.Logical)
	return err
}
开发者ID:josephwinston,项目名称:cockroach,代码行数:7,代码来源:txn_correctness_test.go


示例10: deleteRngCmd

// deleteRngCmd deletes the range of values from the db from [key, endKey).
func deleteRngCmd(c *cmd, db *client.KV, t *testing.T) error {
	return db.Call(proto.DeleteRange, &proto.DeleteRangeRequest{
		RequestHeader: proto.RequestHeader{Key: c.getKey(), EndKey: c.getEndKey()},
	}, &proto.DeleteRangeResponse{})
}
开发者ID:josephwinston,项目名称:cockroach,代码行数:6,代码来源:txn_correctness_test.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang client.KeyValue类代码示例发布时间:2022-05-23
下一篇:
Golang client.DB类代码示例发布时间: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