本文整理汇总了Golang中github.com/Terry-Mao/bfs/store/needle.BlockOffset函数的典型用法代码示例。如果您正苦于以下问题:Golang BlockOffset函数的具体用法?Golang BlockOffset怎么用?Golang BlockOffset使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了BlockOffset函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: flush
// flush flush writer buffer.
func (b *SuperBlock) flush(force bool) (err error) {
var (
fd uintptr
offset int64
size int64
)
if b.write++; !force && b.write < b.Options.SyncAtWrite {
return
}
b.write = 0
offset = needle.BlockOffset(b.syncOffset)
size = needle.BlockOffset(b.Offset - b.syncOffset)
fd = b.w.Fd()
if b.Options.Syncfilerange {
if err = myos.Syncfilerange(fd, offset, size, myos.SYNC_FILE_RANGE_WRITE); err != nil {
log.Errorf("block: %s Syncfilerange() error(%v)", b.File, err)
b.LastErr = err
return
}
} else {
if err = myos.Fdatasync(fd); err != nil {
log.Errorf("block: %s Fdatasync() error(%v)", b.File, err)
b.LastErr = err
return
}
}
if err = myos.Fadvise(fd, offset, size, myos.POSIX_FADV_DONTNEED); err == nil {
b.syncOffset = b.Offset
} else {
log.Errorf("block: %s Fadvise() error(%v)", b.File, err)
b.LastErr = err
}
return
}
开发者ID:1d7500,项目名称:bfs,代码行数:35,代码来源:supper_block.go
示例2: init
// init recovery super block from index or super block.
func (v *Volume) init() (err error) {
var (
size int64
lastOffset, offset uint32
)
// recovery from index
if err = v.Indexer.Recovery(func(ix *index.Index) error {
if ix.Size > int32(v.conf.NeedleMaxSize) || ix.Size < 0 {
log.Error("recovery index: %s error(%v)", ix, errors.ErrIndexSize)
return errors.ErrIndexSize
}
// must no less than last offset
if ix.Offset < lastOffset {
log.Error("recovery index: %s lastoffset: %d error(%v)", ix, lastOffset, errors.ErrIndexOffset)
return errors.ErrIndexOffset
}
// WARN if index's offset more than the block, discard it.
if size = int64(ix.Size) + needle.BlockOffset(ix.Offset); size > v.Block.Size {
log.Error("recovery index: %s EOF", ix)
return errors.ErrIndexEOF
}
v.needles[ix.Key] = needle.NewCache(ix.Offset, ix.Size)
offset = ix.Offset + needle.NeedleOffset(int64(ix.Size))
lastOffset = ix.Offset
return nil
}); err != nil && err != errors.ErrIndexEOF {
return
}
// recovery from super block
if err = v.Block.Recovery(offset, func(n *needle.Needle, so, eo uint32) (err1 error) {
if n.TotalSize > int32(v.conf.NeedleMaxSize) || n.TotalSize < 0 {
log.Error("recovery needle: %s error(%v)", n, errors.ErrNeedleSize)
return errors.ErrNeedleSize
}
if n.Flag == needle.FlagOK {
if err1 = v.Indexer.Write(n.Key, so, n.TotalSize); err1 != nil {
return
}
} else {
so = needle.CacheDelOffset
}
v.needles[n.Key] = needle.NewCache(so, n.TotalSize)
return
}); err != nil {
return
}
// recheck offset, keep size and offset consistency
if v.Block.Size != needle.BlockOffset(v.Block.Offset) {
log.Error("block: %s size: %d, offset: %d (%d size) not consistency", v.Block.File, v.Block.Size, v.Block.Offset, needle.BlockOffset(v.Block.Offset))
return errors.ErrSuperBlockOffset
}
// flush index
err = v.Indexer.Flush()
return
}
开发者ID:1d7500,项目名称:bfs,代码行数:56,代码来源:volume.go
示例3: Recovery
// Recovery recovery needles map from super block.
func (b *SuperBlock) Recovery(offset uint32, fn func(*needle.Needle, uint32, uint32) error) (err error) {
// WARN block may be no left data, must update block offset first
if offset == 0 {
offset = needle.NeedleOffset(_headerOffset)
}
b.Offset = offset
if err = b.Scan(b.r, offset, func(n *needle.Needle, so, eo uint32) (err1 error) {
if err1 = fn(n, so, eo); err1 == nil {
b.Offset = eo
}
return
}); err != nil {
return
}
// advise random read
// POSIX_FADV_RANDOM disables file readahead entirely.
// These changes affect the entire file, not just the specified region
// (but other open file handles to the same file are unaffected).
if err = myos.Fadvise(b.r.Fd(), 0, 0, myos.POSIX_FADV_RANDOM); err != nil {
log.Errorf("block: %s Fadvise() error(%v)", b.File)
return
}
// reset b.w offset, discard left space which can't parse to a needle
if _, err = b.w.Seek(needle.BlockOffset(b.Offset), os.SEEK_SET); err != nil {
log.Errorf("block: %s Seek() error(%v)", b.File, err)
}
return
}
开发者ID:1d7500,项目名称:bfs,代码行数:29,代码来源:supper_block.go
示例4: Get
// Get get a needle from super block.
func (b *SuperBlock) Get(offset uint32, buf []byte) (err error) {
if b.LastErr != nil {
return b.LastErr
}
_, err = b.r.ReadAt(buf, needle.BlockOffset(offset))
b.LastErr = err
return
}
开发者ID:1d7500,项目名称:bfs,代码行数:9,代码来源:supper_block.go
示例5: Repair
// Repair repair the specified offset needle without update current offset.
func (b *SuperBlock) Repair(offset uint32, buf []byte) (err error) {
if b.LastErr != nil {
return b.LastErr
}
_, err = b.w.WriteAt(buf, needle.BlockOffset(offset))
b.LastErr = err
return
}
开发者ID:1d7500,项目名称:bfs,代码行数:9,代码来源:supper_block.go
示例6: Del
// Del logical del a needls, only update the flag to it.
func (b *SuperBlock) Del(offset uint32) (err error) {
if b.LastErr != nil {
return b.LastErr
}
// WriteAt won't update the file offset.
_, err = b.w.WriteAt(needle.FlagDelBytes, needle.BlockOffset(offset)+needle.FlagOffset)
b.LastErr = err
return
}
开发者ID:1d7500,项目名称:bfs,代码行数:10,代码来源:supper_block.go
示例7: Scan
// Scan scan a block file.
func (b *SuperBlock) Scan(r *os.File, offset uint32, fn func(*needle.Needle, uint32, uint32) error) (err error) {
var (
data []byte
so, eo uint32
n = &needle.Needle{}
rd = bufio.NewReaderSize(r, b.bufSize)
)
if offset == 0 {
offset = needle.NeedleOffset(headerOffset)
}
so, eo = offset, offset
log.Infof("scan block: %s from offset: %d", b.File, offset)
if _, err = r.Seek(needle.BlockOffset(offset), os.SEEK_SET); err != nil {
log.Errorf("block: %s Seek() error(%v)", b.File)
return
}
for {
if data, err = rd.Peek(needle.HeaderSize); err != nil {
break
}
if err = n.ParseHeader(data); err != nil {
break
}
if _, err = rd.Discard(needle.HeaderSize); err != nil {
break
}
if data, err = rd.Peek(n.DataSize); err != nil {
break
}
if err = n.ParseData(data); err != nil {
break
}
if _, err = rd.Discard(n.DataSize); err != nil {
break
}
if log.V(1) {
log.Info(n.String())
}
eo += needle.NeedleOffset(int64(n.TotalSize))
if err = fn(n, so, eo); err != nil {
break
}
so = eo
}
if err == io.EOF {
log.Infof("scan block: %s to offset: %d [ok]", b.File, eo)
err = nil
} else {
log.Infof("scan block: %s to offset: %d error(%v) [failed]", b.File, eo, err)
}
return
}
开发者ID:ForkProject,项目名称:bfs,代码行数:53,代码来源:supper_block.go
示例8: sync
// sync sync the in-memory data flush to disk.
func (b *SuperBlock) sync() (err error) {
var (
fd uintptr
offset int64
size int64
)
// append N times call flush then clean the os page cache
// page cache no used here...
// after upload a photo, we cache in our own cache server.
offset = needle.BlockOffset(b.syncOffset)
size = needle.BlockOffset(b.Offset - b.syncOffset)
if b.write < b.Options.SyncAtWrite {
return
}
b.write = 0
fd = b.w.Fd()
if b.Options.Syncfilerange {
if err = myos.Syncfilerange(fd, offset, size, myos.SYNC_FILE_RANGE_WRITE); err != nil {
b.LastErr = err
log.Errorf("block: %s Syncfilerange() error(%v)", b.File, err)
return
}
} else {
if err = myos.Fdatasync(fd); err != nil {
b.LastErr = err
log.Errorf("block: %s Fdatasync() error(%v)", b.File, err)
return
}
}
if err = myos.Fadvise(fd, offset, size, myos.POSIX_FADV_DONTNEED); err == nil {
b.syncOffset = b.Offset
} else {
log.Errorf("block: %s Fadvise() error(%v)", b.File, err)
b.LastErr = err
}
return
}
开发者ID:wtmmac,项目名称:bfs,代码行数:38,代码来源:supper_block.go
示例9: Recovery
// Recovery recovery needles map from super block.
func (b *SuperBlock) Recovery(offset uint32, fn func(*needle.Needle, uint32, uint32) error) (err error) {
if err = b.Scan(b.r, offset, func(n *needle.Needle, so, eo uint32) (err1 error) {
if err1 = fn(n, so, eo); err1 == nil {
b.Offset = eo
}
return
}); err != nil {
return
}
// reset b.w offset, discard left space which can't parse to a needle
if _, err = b.w.Seek(needle.BlockOffset(b.Offset), os.SEEK_SET); err != nil {
log.Errorf("block: %s Seek() error(%v)", b.File, err)
}
return
}
开发者ID:ForkProject,项目名称:bfs,代码行数:16,代码来源:supper_block.go
示例10: compareTestOffset
func compareTestOffset(b *SuperBlock, n *needle.Needle, offset uint32) (err error) {
var v int64
if b.Offset != offset+needle.NeedleOffset(int64(n.TotalSize)) {
err = fmt.Errorf("b.Offset: %d not match", b.Offset)
return
}
if v, err = b.w.Seek(0, os.SEEK_CUR); err != nil {
err = fmt.Errorf("b.Seek() error(%v)", err)
return
} else {
if v != needle.BlockOffset(b.Offset) {
err = fmt.Errorf("offset: %d not match", v)
return
}
}
return
}
开发者ID:jameswei,项目名称:bfs,代码行数:17,代码来源:supper_block_test.go
示例11: Scan
// Scan scan a block file.
func (b *SuperBlock) Scan(r *os.File, offset uint32, fn func(*needle.Needle, uint32, uint32) error) (err error) {
var (
data []byte
so, eo uint32
bso int64
fi os.FileInfo
fd = r.Fd()
n = &needle.Needle{}
rd = bufio.NewReaderSize(r, b.Options.BufferSize)
)
if offset == 0 {
offset = needle.NeedleOffset(_headerOffset)
}
so, eo = offset, offset
bso = needle.BlockOffset(so)
// advise sequential read
if fi, err = r.Stat(); err != nil {
log.Errorf("block: %s Stat() error(%v)", b.File)
return
}
if err = myos.Fadvise(fd, bso, fi.Size(), myos.POSIX_FADV_SEQUENTIAL); err != nil {
log.Errorf("block: %s Fadvise() error(%v)", b.File)
return
}
log.Infof("scan block: %s from offset: %d", b.File, offset)
if _, err = r.Seek(bso, os.SEEK_SET); err != nil {
log.Errorf("block: %s Seek() error(%v)", b.File)
return
}
for {
// header
if data, err = rd.Peek(needle.HeaderSize); err != nil {
break
}
if err = n.ParseHeader(data); err != nil {
break
}
if _, err = rd.Discard(needle.HeaderSize); err != nil {
break
}
// data
if data, err = rd.Peek(int(n.Size)); err != nil {
break
}
if err = n.ParseData(data); err != nil {
break
}
if _, err = rd.Discard(int(n.Size)); err != nil {
break
}
// footer
if data, err = rd.Peek(int(n.FooterSize)); err != nil {
break
}
if err = n.ParseFooter(data); err != nil {
break
}
if _, err = rd.Discard(int(n.FooterSize)); err != nil {
break
}
if log.V(1) {
log.Info(n.String())
}
eo += needle.NeedleOffset(int64(n.TotalSize))
if err = fn(n, so, eo); err != nil {
break
}
so = eo
}
if err == io.EOF {
// advise no need page cache
if err = myos.Fadvise(fd, bso, needle.BlockOffset(eo-so), myos.POSIX_FADV_DONTNEED); err != nil {
log.Errorf("block: %s Fadvise() error(%v)", b.File)
return
}
log.Infof("scan block: %s to offset: %d [ok]", b.File, eo)
err = nil
} else {
log.Infof("scan block: %s to offset: %d error(%v) [failed]", b.File, eo, err)
}
return
}
开发者ID:1d7500,项目名称:bfs,代码行数:83,代码来源:supper_block.go
示例12: Scan
// Scan scan a block file.
func (b *SuperBlock) Scan(r *os.File, offset uint32, fn func(*needle.Needle, uint32, uint32) error) (err error) {
var (
so, eo uint32
bso int64
fi os.FileInfo
fd = r.Fd()
n = needle.NewNeedle(b.Options.NeedleMaxSize)
rd = bufio.NewReaderSize(r, b.Options.BufferSize)
)
if offset == 0 {
offset = needle.NeedleOffset(_headerOffset)
}
so, eo = offset, offset
bso = needle.BlockOffset(so)
// advise sequential read
if fi, err = r.Stat(); err != nil {
log.Errorf("block: %s Stat() error(%v)", b.File)
return
}
if err = myos.Fadvise(fd, bso, fi.Size(), myos.POSIX_FADV_SEQUENTIAL); err != nil {
log.Errorf("block: %s Fadvise() error(%v)", b.File)
return
}
log.Infof("scan block: %s from offset: %d", b.File, offset)
if _, err = r.Seek(bso, os.SEEK_SET); err != nil {
log.Errorf("block: %s Seek() error(%v)", b.File)
return
}
for {
if err = n.ParseFrom(rd); err != nil {
if err != io.EOF {
log.Errorf("block: parse needle from offset: %d:%d error(%v)", so, eo, err)
}
break
}
if n.Size > int32(b.Options.NeedleMaxSize) {
log.Error("scan block: %s error(%v)", n, errors.ErrNeedleSize)
err = errors.ErrNeedleSize
break
}
if log.V(1) {
log.Info(n.String())
}
eo += needle.NeedleOffset(int64(n.TotalSize))
if err = fn(n, so, eo); err != nil {
log.Errorf("block: callback from offset: %d:%d error(%v)", so, eo, err)
break
}
so = eo
}
if err == io.EOF {
// advise no need page cache
if err = myos.Fadvise(fd, bso, needle.BlockOffset(eo-so), myos.POSIX_FADV_DONTNEED); err != nil {
log.Errorf("block: %s Fadvise() error(%v)", b.File)
return
}
log.Infof("scan block: %s to offset: %d [ok]", b.File, eo)
err = nil
} else {
log.Infof("scan block: %s to offset: %d error(%v) [failed]", b.File, eo, err)
}
return
}
开发者ID:wareseeker,项目名称:bfs,代码行数:64,代码来源:supper_block.go
注:本文中的github.com/Terry-Mao/bfs/store/needle.BlockOffset函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论