本文整理汇总了Golang中github.com/Terry-Mao/bfs/store/needle.Needle.ParseHeader方法的典型用法代码示例。如果您正苦于以下问题:Golang Needle.ParseHeader方法的具体用法?Golang Needle.ParseHeader怎么用?Golang Needle.ParseHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/Terry-Mao/bfs/store/needle.Needle
的用法示例。
在下文中一共展示了Needle.ParseHeader方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: compareTestNeedle
func compareTestNeedle(t *testing.T, key int64, cookie int32, flag byte, n *needle.Needle, data, buf []byte) (err error) {
if err = n.ParseHeader(buf[:needle.HeaderSize]); err != nil {
t.Errorf("ParseNeedleHeader() error(%v)", err)
return
}
if err = n.ParseData(buf[needle.HeaderSize:]); err != nil {
err = fmt.Errorf("ParseNeedleData() error(%v)", err)
t.Error(err)
return
}
if !bytes.Equal(n.Data, data) {
err = fmt.Errorf("data: %s not match", n.Data)
t.Error(err)
return
}
if n.Cookie != cookie {
err = fmt.Errorf("cookie: %d not match", n.Cookie)
t.Error(err)
return
}
if n.Key != key {
err = fmt.Errorf("key: %d not match", n.Key)
t.Error(err)
return
}
if n.Flag != flag {
err = fmt.Errorf("flag: %d not match", n.Flag)
t.Error(err)
return
}
if n.Size != int32(len(data)) {
err = fmt.Errorf("size: %d not match", n.Size)
t.Error(err)
return
}
return
}
示例2: Get
// Get get a needle by key and cookie.
func (v *Volume) Get(key int64, cookie int32, buf []byte, n *needle.Needle) (err error) {
var (
now = time.Now().UnixNano()
ok bool
nc int64
size int32
offset uint32
)
// WARN pread syscall is atomic, so use rlock
v.lock.RLock()
if nc, ok = v.needles[key]; ok {
offset, size = needle.Cache(nc)
if offset != needle.CacheDelOffset {
err = v.Block.Get(offset, buf[:size])
} else {
err = errors.ErrNeedleDeleted
}
} else {
err = errors.ErrNeedleNotExist
}
v.lock.RUnlock()
if err != nil {
return
}
if log.V(1) {
log.Infof("get needle key: %d, cookie: %d, offset: %d, size: %d", key, cookie, offset, size)
}
if err = n.ParseHeader(buf[:needle.HeaderSize]); err != nil {
return
}
if n.TotalSize != size {
err = errors.ErrNeedleSize
return
}
if err = n.ParseData(buf[needle.HeaderSize:size]); err != nil {
return
}
if log.V(1) {
log.Infof("%v\n", n)
}
if n.Key != key {
err = errors.ErrNeedleKey
return
}
if n.Cookie != cookie {
err = errors.ErrNeedleCookie
return
}
// needles map may be out-dated, recheck
if n.Flag == needle.FlagDel {
v.lock.Lock()
v.needles[key] = needle.NewCache(needle.CacheDelOffset, size)
v.lock.Unlock()
err = errors.ErrNeedleDeleted
} else {
atomic.AddUint64(&v.Stats.TotalGetProcessed, 1)
atomic.AddUint64(&v.Stats.TotalReadBytes, uint64(size))
atomic.AddUint64(&v.Stats.TotalGetDelay, uint64(time.Now().UnixNano()-now))
}
return
}