當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Batch.Get方法代碼示例

本文整理匯總了Golang中github.com/cockroachdb/cockroach/client.Batch.Get方法的典型用法代碼示例。如果您正苦於以下問題:Golang Batch.Get方法的具體用法?Golang Batch.Get怎麽用?Golang Batch.Get使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/cockroachdb/cockroach/client.Batch的用法示例。


在下文中一共展示了Batch.Get方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: 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.
// The isMultiphase option runs the transaction in multiple phases recreating
// the transaction from the transaction protobuf returned from the server.
func concurrentIncrements(db *client.DB, t *testing.T, isMultiphase bool) {
	// 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(testUser+"/value-%d", (i+1)%2))
			writeKey := []byte(fmt.Sprintf(testUser+"/value-%d", i))
			defer wgEnd.Done()
			wgStart.Done()
			// Wait until the other goroutines are running.
			wgStart.Wait()

			if isMultiphase {
				applyInc := func(txn *client.Txn) (error, proto.Transaction) {
					txn.SetDebugName(fmt.Sprintf("test-%d", i))
					b := client.Batch{}
					// Retrieve the other key.
					b.Get(readKey)
					if err := txn.Run(&b); err != nil {
						return err, txn.GetState()
					}
					otherValue := int64(0)
					gr := b.Results[0].Rows[0]
					if gr.Value != nil {
						otherValue = gr.ValueInt()
					}
					// New txn.
					txn = db.ReconstructTxn(txn.GetState())
					// Write our key.
					b = client.Batch{}
					b.Inc(writeKey, 1+otherValue)
					if err := txn.Run(&b); err != nil {
						return err, txn.GetState()
					}
					// New txn.
					txn = db.ReconstructTxn(txn.GetState())
					err := txn.Commit(&client.Batch{})
					return err, txn.GetState()
				}
				for r := retry.Start(client.DefaultTxnRetryOptions); r.Next(); {
					txn := db.ReconstructTxn(proto.Transaction{})
					if err, txnProto := applyInc(txn); err != nil {
						// New txn.
						txn = db.ReconstructTxn(txnProto)
						if err := txn.Rollback(); err != nil {
							t.Error(err)
						} else {
							// retry
							continue
						}
					}
					// exit retry
					break
				}
			} else {
				if err := db.Txn(func(txn *client.Txn) error {
					txn.SetDebugName(fmt.Sprintf("test-%d", i))

					// Retrieve the other key.
					gr, err := txn.Get(readKey)
					if err != nil {
						return err
					}

					otherValue := int64(0)
					if gr.Value != nil {
						otherValue = gr.ValueInt()
					}

					_, err = txn.Inc(writeKey, 1+otherValue)
					return err
				}); 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(testUser+"/value-%d", i))
//.........這裏部分代碼省略.........
開發者ID:donganwangshi,項目名稱:cockroach,代碼行數:101,代碼來源:client_test.go


注:本文中的github.com/cockroachdb/cockroach/client.Batch.Get方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。