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


Golang Event.Unmarshal方法代碼示例

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


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

示例1: RangeEvents

// RangeEvents gets the events from key to end in [startRev, endRev).
// If `end` is nil, the request only observes the events on key.
// If `end` is not nil, it observes the events on key range [key, range_end).
// Limit limits the number of events returned.
// If startRev <=0, rangeEvents returns events from the beginning of uncompacted history.
// If endRev <=0, it indicates there is no end revision.
//
// If the required start rev is compacted, ErrCompacted will be returned.
// If the required start rev has not happened, ErrFutureRev will be returned.
//
// RangeEvents returns events that satisfy the requirement (0 <= n <= limit).
// If events in the revision range have not all happened, it returns immeidately
// what is available.
// It also returns nextRev which indicates the start revision used for the following
// RangeEvents call. The nextRev could be smaller than the given endRev if the store
// has not progressed so far or it hits the event limit.
//
// TODO: return byte slices instead of events to avoid meaningless encode and decode.
func (s *store) RangeEvents(key, end []byte, limit, startRev, endRev int64) (evs []storagepb.Event, nextRev int64, err error) {
	s.mu.Lock()
	defer s.mu.Unlock()

	if startRev > 0 && startRev <= s.compactMainRev {
		return nil, 0, ErrCompacted
	}
	if startRev > s.currentRev.main {
		return nil, 0, ErrFutureRev
	}

	revs := s.kvindex.RangeEvents(key, end, startRev)
	if len(revs) == 0 {
		return nil, s.currentRev.main + 1, nil
	}

	tx := s.b.BatchTx()
	tx.Lock()
	defer tx.Unlock()
	// fetch events from the backend using revisions
	for _, rev := range revs {
		if endRev > 0 && rev.main >= endRev {
			return evs, rev.main, nil
		}
		revbytes := newRevBytes()
		revToBytes(rev, revbytes)

		_, vs := tx.UnsafeRange(keyBucketName, revbytes, nil, 0)
		if len(vs) != 1 {
			log.Fatalf("storage: range cannot find rev (%d,%d)", rev.main, rev.sub)
		}

		e := storagepb.Event{}
		if err := e.Unmarshal(vs[0]); err != nil {
			log.Fatalf("storage: cannot unmarshal event: %v", err)
		}
		evs = append(evs, e)
		if limit > 0 && len(evs) >= int(limit) {
			return evs, rev.main + 1, nil
		}
	}
	return evs, s.currentRev.main + 1, nil
}
開發者ID:hroyrh,項目名稱:etcd,代碼行數:61,代碼來源:kvstore.go


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