代码拉取完成,页面将自动刷新
package shs
import (
"iter"
"unsafe"
"github.com/cespare/xxhash"
"github.com/cockroachdb/swiss"
)
type Index interface {
Get(key []byte) (uint64, error)
Set(key []byte, value uint64) error
Delete(key []byte) error
Iterator() iter.Seq2[[]byte, uint64]
}
var _ Index = (*DefaultIndex)(nil)
type DefaultIndex struct {
inner *swiss.Map[string, uint64]
}
func NewDefaultIndex() *DefaultIndex {
return &DefaultIndex{
inner: swiss.New(0, swiss.WithHash[string, uint64](func(key *string, seed uintptr) uintptr {
res := xxhash.Sum64String(*key)
return uintptr(res)
})),
}
}
func stringToBytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}
func bytesToString(b []byte) string {
return unsafe.String(&b[0], len(b))
}
func (d *DefaultIndex) Iterator() iter.Seq2[[]byte, uint64] {
return func(yield func([]byte, uint64) bool) {
d.inner.All(func(k string, v uint64) bool {
keyBytes := stringToBytes(k)
return yield(keyBytes, v)
})
}
}
func (d *DefaultIndex) Get(key []byte) (uint64, error) {
u, exists := d.inner.Get(bytesToString(key))
if !exists {
return 0, ErrKeyNotFound
}
return u, nil
}
func (d *DefaultIndex) Set(key []byte, value uint64) error {
d.inner.Put(bytesToString(key), value)
return nil
}
func (d *DefaultIndex) Delete(key []byte) error {
d.inner.Delete(bytesToString(key))
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。