Example #1
0
func hash(v interface{}) int {
	switch v := v.(type) {
	case []interface{}:
		var h int
		for _, e := range v {
			h += hash(e)
		}
		return h
	case map[string]interface{}:
		var h int
		for k, v := range v {
			h += hash(k) + hash(v)
		}
		return h
	case string:
		h := fnv.New64a()
		h.Write([]byte(v))
		return int(h.Sum64())
	case bool:
		if v {
			return 1
		}
		return 0
	case nil:
		return 0
	case json.Number:
		h := fnv.New64a()
		h.Write([]byte(v))
		return int(h.Sum64())
	}
	panic(fmt.Sprintf("illegal argument: %v (%T)", v, v))
}
func putGetParallel(power int, factor float64) {
	var h Hashmap
	h = NewChainedHash(power, fnv.New64a())

	n := uint64(math.Pow(2, float64(power)))

	var wg sync.WaitGroup
	wg.Add(2)
	go func() {

		for i := uint64(0); i < uint64((float64(n) * factor)); i++ {
			h.Put(strconv.FormatUint(i, 10), strconv.FormatUint(i, 10))
		}
		wg.Done()
	}()

	go func() {

		for cycle := 0; cycle < 5; cycle++ {
			for i := uint64(0); i < n; i++ {
				h.Get(strconv.FormatUint(i, 10))
			}
		}
		wg.Done()
	}()

	wg.Wait()

}
func (c *myCipher) hashKey(key string) {
	k := []byte(key)
	hash := fnv.New64a()
	hash.Write(k)
	rand.Seed(int64(hash.Sum64()))
	c.cipherTextIdx = rand.Perm(c.d.col)
}
Example #4
0
func (n *node) getKey() string {
	t := []byte(strconv.FormatInt(time.Now().UnixNano(), 10))
	h := fnv.New64a()
	h.Write(t)
	s := strconv.FormatUint(h.Sum64(), 10)
	return s
}
Example #5
0
func (tab *Table) Lookup(proto protocols.Protocol, hostID string, port uint16) (Rule, bool) {
	var sum = fnv.New64a()
	var buf [2]byte
	sum.Reset()
	buf[0] = byte(proto)
	sum.Write(buf[:1])
	io.WriteString(sum, hostID)
	binary.BigEndian.PutUint16(buf[:], port)
	sum.Write(buf[:])
	id := sum.Sum64()

	nEntries := len(tab.entries)

	idx := sort.Search(nEntries, func(idx int) bool {
		return tab.entries[idx].id >= id
	})

	for _, entry := range tab.entries[idx:] {
		if entry.id != id {
			break
		}

		if entry.Protocol == proto && entry.SrcHostID == hostID && entry.SrcPort == port {
			return entry.Rule, true
		}
	}

	return Rule{}, false
}
Example #6
0
func buildTable(rules []Rule) *Table {
	var entries = make([]tableEntry, len(rules))
	var sum = fnv.New64a()

	for i, rule := range rules {
		var e tableEntry

		var buf [2]byte
		sum.Reset()
		buf[0] = byte(rule.Protocol)
		sum.Write(buf[:1])
		io.WriteString(sum, rule.SrcHostID)
		binary.BigEndian.PutUint16(buf[:], rule.SrcPort)
		sum.Write(buf[:])
		id := sum.Sum64()

		e.id = id
		e.Rule = rule

		entries[i] = e
	}

	sort.Sort(sortedByID(entries))

	return &Table{entries: entries}
}
Example #7
0
// Goroutine for philosopher actions.  An instance is run for each
// philosopher.  Instances run concurrently.
func philosopher(phName string,
	dominantHand, otherHand chan fork, done chan bool) {
	fmt.Println(phName, "seated")
	// each philosopher goroutine has a random number generator,
	// seeded with a hash of the philosopher's name.
	h := fnv.New64a()
	h.Write([]byte(phName))
	rg := rand.New(rand.NewSource(int64(h.Sum64())))
	// utility function to sleep for a randomized nominal time
	rSleep := func(t time.Duration) {
		time.Sleep(t/2 + time.Duration(rg.Int63n(int64(t))))
	}
	for h := hunger; h > 0; h-- {
		fmt.Println(phName, "hungry")
		<-dominantHand // pick up forks
		<-otherHand
		fmt.Println(phName, "eating")
		rSleep(eat)
		dominantHand <- 'f' // put down forks
		otherHand <- 'f'
		fmt.Println(phName, "thinking")
		rSleep(think)
	}
	fmt.Println(phName, "satisfied")
	done <- true
	fmt.Println(phName, "left the table")
}
Example #8
0
func Hash(image image.Image) uint64 {
	hash := fnv.New64a()
	rect := image.Bounds()
	size := (rect.Max.X - rect.Min.X) * (rect.Max.Y - rect.Min.Y) * 16
	data := make([]byte, size)
	pos := 0

	for y := rect.Min.Y; y < rect.Max.Y; y++ {
		for x := rect.Min.X; x < rect.Max.X; x++ {
			color := image.At(x, y)
			r, g, b, a := color.RGBA()

			convertUint32ToBytes(r, data[pos:pos+4])
			pos += 4
			convertUint32ToBytes(g, data[pos:pos+4])
			pos += 4
			convertUint32ToBytes(b, data[pos:pos+4])
			pos += 4
			convertUint32ToBytes(a, data[pos:pos+4])
		}
	}

	hash.Write(data)
	return hash.Sum64()
}
Example #9
0
File: ring.go Project: bpot/libring
func (r ring) partitionForKey(key string) uint {
	hasher := fnv.New64a()
	hasher.Write([]byte(key))
	keyHash := hasher.Sum64()

	return uint(jump.Hash(keyHash, len(r.members)))
}
Example #10
0
func sliceStringID64(sign []string) int64 {
	h := fnv.New64a()
	for _, s := range sign {
		h.Write([]byte(s))
	}
	return int64Bytes(h.Sum(nil))
}
Example #11
0
func (e *Engine) fnv64a() error {
	data, err := computeHash(fnv.New64a(), e.stack.Pop())
	if err == nil {
		e.stack.Push(data)
	}
	return err
}
Example #12
0
func getResources(w http.ResponseWriter, r *http.Request) *appError {
	//omitted code to get top, filter et cetera from the Request

	c := appengine.NewContext(r)
	h := fnv.New64a()
	h.Write([]byte("rap_query" + top + filter + orderby + skip))
	cacheKey := h.Sum64()

	cv, err := memcache.Get(c, fmt.Sprint(cacheKey))
	if err == nil {
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Write(cv.Value)
		return nil
	}

	//omitting code that get resources from DataStore

	//cache this result with it's query as the key
	memcache.Set(c, &memcache.Item{
		Key:   fmt.Sprint(cacheKey),
		Value: result,
	})
	//return the json version
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.Write(result)
	return nil
}
Example #13
0
func (s *Store) TodayUnique(siteId int64, uid int64) (cnt int64, e error) {
	var (
		hl *hyperloglog.HyperLogLogPlus
	)
	hl, e = s.getHyperLoglog(siteId)
	if e != nil {
		return
	}

	h := fnv.New64a()
	b := make([]byte, 4)
	binary.BigEndian.PutUint32(b, uint32(uid))
	io.WriteString(h, hex.EncodeToString(b))
	hl.Add(h)

	// encode and store hyperloglog
	var bts []byte
	bts, e = hl.GobEncode()
	if e != nil {
		return
	}
	e = s.ldb.Put(itob32(siteId), bts, nil)
	if e != nil {
		return
	}

	cnt = int64(hl.Count())

	return
}
Example #14
0
// hash returns an identifying hash for the target.
func (t *Target) hash() uint64 {
	h := fnv.New64a()
	h.Write([]byte(t.labels.Fingerprint().String()))
	h.Write([]byte(t.URL().String()))

	return h.Sum64()
}
Example #15
0
func init() {
	fnv1 := fnv.New64a()
	fnv2 := fnv.New64a()
	h1 = func(b []byte) uint64 {
		fnv1.Reset()
		fnv1.Write([]byte{0xd5, 0x9d, 0x34, 0x24, 0xf4, 0x15, 0xa4, 0xfe})
		fnv1.Write(b)
		return fnv1.Sum64()
	}
	h2 = func(b []byte) uint64 {
		fnv2.Reset()
		fnv2.Write([]byte{0x06, 0x43, 0x0c, 0xd9, 0xde, 0x74, 0xbf, 0xf1})
		fnv2.Write(b)
		return fnv2.Sum64()
	}
}
Example #16
0
func TestDeleteLabelValues(t *testing.T) {
	desc := NewDesc("test", "helpless", []string{"l1", "l2"}, nil)
	vec := MetricVec{
		children: map[uint64]Metric{},
		desc:     desc,
		hash:     fnv.New64a(),
		newMetric: func(lvs ...string) Metric {
			return newValue(desc, UntypedValue, 0, lvs...)
		},
	}

	if got, want := vec.DeleteLabelValues("v1", "v2"), false; got != want {
		t.Errorf("got %v, want %v", got, want)
	}

	vec.With(Labels{"l1": "v1", "l2": "v2"}).(Untyped).Set(42)
	if got, want := vec.DeleteLabelValues("v1", "v2"), true; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	if got, want := vec.DeleteLabelValues("v1", "v2"), false; got != want {
		t.Errorf("got %v, want %v", got, want)
	}

	vec.With(Labels{"l1": "v1", "l2": "v2"}).(Untyped).Set(42)
	if got, want := vec.DeleteLabelValues("v2", "v1"), false; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	if got, want := vec.DeleteLabelValues("v1"), false; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
}
Example #17
0
func (s *Set) Submit(value string) error {
	s.Count++
	h := fnv.New64a()
	h.Write([]byte(value))
	s.HLL.Add(h)
	return nil
}
Example #18
0
// Fingerprint returns a Metric's Fingerprint.
func (m Metric) Fingerprint() Fingerprint {
	labelNames := make([]string, 0, len(m))
	maxLength := 0

	for labelName, labelValue := range m {
		labelNames = append(labelNames, string(labelName))
		if len(labelName) > maxLength {
			maxLength = len(labelName)
		}
		if len(labelValue) > maxLength {
			maxLength = len(labelValue)
		}
	}

	sort.Strings(labelNames)

	summer := fnv.New64a()
	buf := make([]byte, maxLength)

	for _, labelName := range labelNames {
		labelValue := m[LabelName(labelName)]

		copy(buf, labelName)
		summer.Write(buf[:len(labelName)])

		summer.Write(separator)

		copy(buf, labelValue)
		summer.Write(buf[:len(labelValue)])
	}

	return Fingerprint(binary.LittleEndian.Uint64(summer.Sum(nil)))
}
Example #19
0
func getHashAndBuf() *hashAndBuf {
	hb := hashAndBufPool.Get()
	if hb == nil {
		return &hashAndBuf{h: fnv.New64a()}
	}
	return hb.(*hashAndBuf)
}
Example #20
0
func (a asset) init() asset {

	h := fnv.New64a()
	_, _ = io.WriteString(h, a.Content)
	a.etag = `"` + base64.StdEncoding.EncodeToString(h.Sum(nil)) + `"`
	return a
}
Example #21
0
func getHashAndBuf() *hashAndBuf {
	select {
	case hb := <-hashAndBufPool:
		return hb
	default:
		return &hashAndBuf{h: fnv.New64a()}
	}
}
Example #22
0
func hashConfigBid(c *ConfigBid) int64 {
	h := fnv.New64a()
	encoder := gob.NewEncoder(h)
	encoder.Encode(c.Config.Num)
	encoder.Encode(c.Config.Shards)
	encoder.Encode(c.Bid)
	return int64(h.Sum64() >> 1)
}
Example #23
0
// GetStreamID returns the integer representation of a given stream name.
func GetStreamID(stream string) MessageStreamID {
	hash := fnv.New64a()
	hash.Write([]byte(stream))
	streamID := MessageStreamID(hash.Sum64())

	StreamTypes.name[streamID] = stream
	return streamID
}
Example #24
0
// fnv64a
// use FNV64a Hash to set start (l) and value to be added (h)
// returns l,h
func (bf bloom64) fnv64a(b []byte) (l, h uint64) {
	h64 := fnv.New64a()
	h64.Write(b)
	hash64 := h64.Sum64()
	l = hash64 << 32 >> 32
	h = hash64 >> 32
	return l, h
}
Example #25
0
func (node *VectorAggregation) labelsToGroupingKey(labels clientmodel.Metric) uint64 {
	summer := fnv.New64a()
	for _, label := range node.groupBy {
		fmt.Fprint(summer, labels[label])
	}

	return summer.Sum64()
}
Example #26
0
// Fingerprint returns a quasi-unique fingerprint for the NotifyInfo.
func (n *NotifyInfo) Fingerprint() model.Fingerprint {
	h := fnv.New64a()
	h.Write([]byte(n.Receiver))

	fp := model.Fingerprint(h.Sum64())

	return fp ^ n.Alert
}
Example #27
0
// Hash calculates hash values
func (k KFloat64) Hash() uint {
	b := *(*[unsafe.Sizeof(k)]byte)(unsafe.Pointer(&k))

	h := fnv.New64a()
	h.Write(b[:])

	return uint(h.Sum64())
}
Example #28
0
// tagsHash returns a hash of tag key/value pairs.
func (r *Row) tagsHash() uint64 {
	h := fnv.New64a()
	keys := r.tagsKeys()
	for _, k := range keys {
		h.Write([]byte(k))
		h.Write([]byte(r.Tags[k]))
	}
	return h.Sum64()
}
Example #29
0
func (f Filters) fingerprint() uint64 {
	summer := fnv.New64a()

	for i, f := range f {
		fmt.Fprintln(summer, i, f.fingerprint)
	}

	return summer.Sum64()
}
Example #30
0
func computeEtag(values interface{}) (uint64, error) {
	h := fnv.New64a()
	s := fmt.Sprintf("%v", values)
	_, err := h.Write(([]byte)(s))
	if err != nil {
		return 0, err
	}
	return h.Sum64(), nil
}