Пример #1
0
func (p *BitcaskStore) Append(key string, value []byte) (bool, os.Error) {
	if p.before > 0 {
		return false, nil
	}

	i := htree.Fnv1a([]byte(key)) % uint32(len(p.locks))
	p.locks[i].Lock()
	defer p.locks[i].Unlock()

	bc := p.getByKey(key)
	r, flag := bc.Get(key)
	if flag != 0 {
		log.Print("append %s, but flag !=0", key)
		return false, os.NewError("bad flag")
	}
	r = append(r, value...)
	bc.Set(key, r, flag, 0) // use timestamp later

	return true, nil
}
Пример #2
0
func (p *BitcaskStore) Incr(key string, value int) (int, os.Error) {
	if p.before > 0 {
		return 0, nil
	}

	i := htree.Fnv1a([]byte(key)) % uint32(len(p.locks))
	p.locks[i].Lock()
	defer p.locks[i].Unlock()

	r, _ := p.Get(key)
	n := 0
	if r != nil {
		var e os.Error
		n, e = strconv.Atoi(string(r.Body))
		if e != nil {
			log.Printf("invalid number %s, %s", key, string(r.Body))
			return 0, os.NewError("invalid number")
		}
	}
	n += value
	p.Set(key, &memcache.Item{Body: []byte(strconv.Itoa(n)), Flag: 0x4}, false) // use timestamp later

	return n, nil
}
Пример #3
0
func (p *BitcaskStore) getByKey(key string) *htree.Bitcask {
	h := htree.Fnv1a([]byte(key))
	i := h >> (uint32(8-p.depth) * 4)
	return p.bc[i]
}