Exemplo n.º 1
0
func NewTopKCounter(capacity int64) *TopKCounter {
	return &TopKCounter{
		capacity:   capacity,
		counterMap: make(map[string]*list.Element),
		bucketList: list.New(),
	}
}
Exemplo n.º 2
0
func NewTopKCounterBytes(buf []byte) (c *TopKCounter, err error) {
	defer func() {
		if r := recover(); r != nil {
			switch x := r.(type) {
			case string:
				err = errors.New(x)
			case error:
				err = x
			default:
				err = errors.New("Deserialization failed.")
			}
			c = nil
		}
	}()
	capacity := int64(binary.LittleEndian.Uint64(buf))
	bucketListLen := int(binary.LittleEndian.Uint64(buf[8:]))
	p := 16
	counterMap := make(map[string]*list.Element)
	bucketList := list.New()
	for i := 0; i < bucketListLen; i++ {
		count := int64(binary.LittleEndian.Uint64(buf[p:]))
		buck := newBucket(count)
		listLen := int(binary.LittleEndian.Uint64(buf[p+8:]))
		p += 16
		buckElem := bucketList.PushBack(buck)
		counterList := list.New()
		buck.counterList = counterList
		for j := 0; j < listLen; j++ {
			ctr := &counter{}
			ctr.count = int64(binary.LittleEndian.Uint64(buf[p:]))
			ctr.error = int64(binary.LittleEndian.Uint64(buf[p+8:]))
			itemLen := int(binary.LittleEndian.Uint64(buf[p+16:]))
			p += 24
			ctr.item = string(buf[p : p+itemLen])
			p += itemLen
			ctr.bucketNode = buckElem
			ctrElem := counterList.PushBack(ctr)
			counterMap[ctr.item] = ctrElem
		}
	}

	return &TopKCounter{
		capacity:   capacity,
		counterMap: counterMap,
		bucketList: bucketList,
	}, nil
}
Exemplo n.º 3
0
func newBucket(count int64) *bucket {
	return &bucket{
		counterList: list.New(),
		count:       count,
	}
}