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


Golang mvcc.KV類代碼示例

本文整理匯總了Golang中github.com/coreos/etcd/mvcc.KV的典型用法代碼示例。如果您正苦於以下問題:Golang KV類的具體用法?Golang KV怎麽用?Golang KV使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


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

示例1: applyDeleteRange

func applyDeleteRange(txnID int64, kv mvcc.KV, req *etcdserverpb.DeleteRangeRequest) (*etcdserverpb.DeleteRangeResponse, error) {
	resp := &etcdserverpb.DeleteRangeResponse{}
	resp.Header = &etcdserverpb.ResponseHeader{}

	var (
		n   int64
		rev int64
		err error
	)

	if isGteRange(req.RangeEnd) {
		req.RangeEnd = []byte{}
	}

	if txnID != noTxn {
		n, rev, err = kv.TxnDeleteRange(txnID, req.Key, req.RangeEnd)
		if err != nil {
			return nil, err
		}
	} else {
		n, rev = kv.DeleteRange(req.Key, req.RangeEnd)
	}

	resp.Deleted = n
	resp.Header.Revision = rev
	return resp, nil
}
開發者ID:weaveworks,項目名稱:mesh,代碼行數:27,代碼來源:etcd_store.go

示例2: applyCompaction

func applyCompaction(kv mvcc.KV, req *etcdserverpb.CompactionRequest) (*etcdserverpb.CompactionResponse, error) {
	resp := &etcdserverpb.CompactionResponse{}
	resp.Header = &etcdserverpb.ResponseHeader{}
	_, err := kv.Compact(req.Revision)
	if err != nil {
		return nil, err
	}
	// get the current revision. which key to get is not important.
	_, resp.Header.Revision, _ = kv.Range([]byte("compaction"), nil, 1, 0)
	return resp, err
}
開發者ID:weaveworks,項目名稱:mesh,代碼行數:11,代碼來源:etcd_store.go

示例3: applyTransaction

func applyTransaction(kv mvcc.KV, lessor lease.Lessor, req *etcdserverpb.TxnRequest) (*etcdserverpb.TxnResponse, error) {
	var revision int64

	ok := true
	for _, c := range req.Compare {
		if revision, ok = applyCompare(kv, c); !ok {
			break
		}
	}

	var reqs []*etcdserverpb.RequestUnion
	if ok {
		reqs = req.Success
	} else {
		reqs = req.Failure
	}

	if err := checkRequestLeases(lessor, reqs); err != nil {
		return nil, err
	}
	if err := checkRequestRange(kv, reqs); err != nil {
		return nil, err
	}

	// When executing the operations of txn, we need to hold the txn lock.
	// So the reader will not see any intermediate results.
	txnID := kv.TxnBegin()
	defer func() {
		err := kv.TxnEnd(txnID)
		if err != nil {
			panic(fmt.Sprint("unexpected error when closing txn", txnID))
		}
	}()

	resps := make([]*etcdserverpb.ResponseUnion, len(reqs))
	for i := range reqs {
		resps[i] = applyUnion(txnID, kv, reqs[i])
	}

	if len(resps) != 0 {
		revision++
	}

	txnResp := &etcdserverpb.TxnResponse{}
	txnResp.Header = &etcdserverpb.ResponseHeader{}
	txnResp.Header.Revision = revision
	txnResp.Responses = resps
	txnResp.Succeeded = ok
	return txnResp, nil
}
開發者ID:weaveworks,項目名稱:mesh,代碼行數:50,代碼來源:etcd_store.go

示例4: checkRequestRange

func checkRequestRange(kv mvcc.KV, reqs []*etcdserverpb.RequestUnion) error {
	for _, requ := range reqs {
		tv, ok := requ.Request.(*etcdserverpb.RequestUnion_RequestRange)
		if !ok {
			continue
		}
		greq := tv.RequestRange
		if greq == nil || greq.Revision == 0 {
			continue
		}

		if greq.Revision > kv.Rev() {
			return mvcc.ErrFutureRev
		}
		if greq.Revision < kv.FirstRev() {
			return mvcc.ErrCompacted
		}
	}
	return nil
}
開發者ID:weaveworks,項目名稱:mesh,代碼行數:20,代碼來源:etcd_store.go

示例5: applyPut

func applyPut(txnID int64, kv mvcc.KV, lessor lease.Lessor, req *etcdserverpb.PutRequest) (*etcdserverpb.PutResponse, error) {
	resp := &etcdserverpb.PutResponse{}
	resp.Header = &etcdserverpb.ResponseHeader{}
	var (
		rev int64
		err error
	)
	if txnID != noTxn {
		rev, err = kv.TxnPut(txnID, req.Key, req.Value, lease.LeaseID(req.Lease))
		if err != nil {
			return nil, err
		}
	} else {
		leaseID := lease.LeaseID(req.Lease)
		if leaseID != lease.NoLease {
			if l := lessor.Lookup(leaseID); l == nil {
				return nil, lease.ErrLeaseNotFound
			}
		}
		rev = kv.Put(req.Key, req.Value, leaseID)
	}
	resp.Header.Revision = rev
	return resp, nil
}
開發者ID:weaveworks,項目名稱:mesh,代碼行數:24,代碼來源:etcd_store.go

示例6: applyCompare

// applyCompare applies the compare request.
// It returns the revision at which the comparison happens. If the comparison
// succeeds, the it returns true. Otherwise it returns false.
func applyCompare(kv mvcc.KV, c *etcdserverpb.Compare) (int64, bool) {
	ckvs, rev, err := kv.Range(c.Key, nil, 1, 0)
	if err != nil {
		if err == mvcc.ErrTxnIDMismatch {
			panic("unexpected txn ID mismatch error")
		}
		return rev, false
	}
	var ckv mvccpb.KeyValue
	if len(ckvs) != 0 {
		ckv = ckvs[0]
	} else {
		// Use the zero value of ckv normally. However...
		if c.Target == etcdserverpb.Compare_VALUE {
			// Always fail if we're comparing a value on a key that doesn't exist.
			// We can treat non-existence as the empty set explicitly, such that
			// even a key with a value of length 0 bytes is still a real key
			// that was written that way
			return rev, false
		}
	}

	// -1 is less, 0 is equal, 1 is greater
	var result int
	switch c.Target {
	case etcdserverpb.Compare_VALUE:
		tv, _ := c.TargetUnion.(*etcdserverpb.Compare_Value)
		if tv != nil {
			result = bytes.Compare(ckv.Value, tv.Value)
		}
	case etcdserverpb.Compare_CREATE:
		tv, _ := c.TargetUnion.(*etcdserverpb.Compare_CreateRevision)
		if tv != nil {
			result = compareInt64(ckv.CreateRevision, tv.CreateRevision)
		}

	case etcdserverpb.Compare_MOD:
		tv, _ := c.TargetUnion.(*etcdserverpb.Compare_ModRevision)
		if tv != nil {
			result = compareInt64(ckv.ModRevision, tv.ModRevision)
		}
	case etcdserverpb.Compare_VERSION:
		tv, _ := c.TargetUnion.(*etcdserverpb.Compare_Version)
		if tv != nil {
			result = compareInt64(ckv.Version, tv.Version)
		}
	}

	switch c.Result {
	case etcdserverpb.Compare_EQUAL:
		if result != 0 {
			return rev, false
		}
	case etcdserverpb.Compare_GREATER:
		if result != 1 {
			return rev, false
		}
	case etcdserverpb.Compare_LESS:
		if result != -1 {
			return rev, false
		}
	}
	return rev, true
}
開發者ID:weaveworks,項目名稱:mesh,代碼行數:67,代碼來源:etcd_store.go

示例7: applyRange

func applyRange(txnID int64, kv mvcc.KV, r *etcdserverpb.RangeRequest) (*etcdserverpb.RangeResponse, error) {
	resp := &etcdserverpb.RangeResponse{}
	resp.Header = &etcdserverpb.ResponseHeader{}

	var (
		kvs []mvccpb.KeyValue
		rev int64
		err error
	)

	if isGteRange(r.RangeEnd) {
		r.RangeEnd = []byte{}
	}

	limit := r.Limit
	if r.SortOrder != etcdserverpb.RangeRequest_NONE {
		// fetch everything; sort and truncate afterwards
		limit = 0
	}
	if limit > 0 {
		// fetch one extra for 'more' flag
		limit = limit + 1
	}

	if txnID != noTxn {
		kvs, rev, err = kv.TxnRange(txnID, r.Key, r.RangeEnd, limit, r.Revision)
		if err != nil {
			return nil, err
		}
	} else {
		kvs, rev, err = kv.Range(r.Key, r.RangeEnd, limit, r.Revision)
		if err != nil {
			return nil, err
		}
	}

	if r.SortOrder != etcdserverpb.RangeRequest_NONE {
		var sorter sort.Interface
		switch {
		case r.SortTarget == etcdserverpb.RangeRequest_KEY:
			sorter = &kvSortByKey{&kvSort{kvs}}
		case r.SortTarget == etcdserverpb.RangeRequest_VERSION:
			sorter = &kvSortByVersion{&kvSort{kvs}}
		case r.SortTarget == etcdserverpb.RangeRequest_CREATE:
			sorter = &kvSortByCreate{&kvSort{kvs}}
		case r.SortTarget == etcdserverpb.RangeRequest_MOD:
			sorter = &kvSortByMod{&kvSort{kvs}}
		case r.SortTarget == etcdserverpb.RangeRequest_VALUE:
			sorter = &kvSortByValue{&kvSort{kvs}}
		}
		switch {
		case r.SortOrder == etcdserverpb.RangeRequest_ASCEND:
			sort.Sort(sorter)
		case r.SortOrder == etcdserverpb.RangeRequest_DESCEND:
			sort.Sort(sort.Reverse(sorter))
		}
	}

	if r.Limit > 0 && len(kvs) > int(r.Limit) {
		kvs = kvs[:r.Limit]
		resp.More = true
	}

	resp.Header.Revision = rev
	for i := range kvs {
		resp.Kvs = append(resp.Kvs, &kvs[i])
	}
	return resp, nil
}
開發者ID:weaveworks,項目名稱:mesh,代碼行數:69,代碼來源:etcd_store.go


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