// init init block file, add/parse meta info. func (b *SuperBlock) init() (err error) { var stat os.FileInfo if stat, err = b.r.Stat(); err != nil { log.Errorf("block: %s Stat() error(%v)", b.File, err) return } if b.Size = stat.Size(); b.Size == 0 { if err = myos.Fallocate(b.w.Fd(), myos.FALLOC_FL_KEEP_SIZE, 0, _maxSize); err != nil { log.Errorf("block: %s Fallocate() error(%s)", b.File, err) return } if err = b.writeMeta(); err != nil { log.Errorf("block: %s writeMeta() error(%v)", b.File, err) return } b.Size = _headerSize } else { if err = b.parseMeta(); err != nil { log.Errorf("block: %s parseMeta() error(%v)", b.File, err) return } if _, err = b.w.Seek(_headerOffset, os.SEEK_SET); err != nil { log.Errorf("block: %s Seek() error(%v)", b.File, err) return } } b.Offset = needle.NeedleOffset(_headerOffset) return }
// NewIndexer new a indexer for async merge index data to disk. func NewIndexer(file string, conf *conf.Config) (i *Indexer, err error) { var stat os.FileInfo i = &Indexer{} i.File = file i.closed = false i.syncOffset = 0 i.conf = conf // must align size i.ring = NewRing(conf.Index.RingBuffer) i.bn = 0 i.buf = make([]byte, conf.Index.BufferSize) if i.f, err = os.OpenFile(file, os.O_RDWR|os.O_CREATE|myos.O_NOATIME, 0664); err != nil { log.Errorf("os.OpenFile(\"%s\") error(%v)", file, err) return nil, err } if stat, err = i.f.Stat(); err != nil { log.Errorf("index: %s Stat() error(%v)", i.File, err) return nil, err } if stat.Size() == 0 { if err = myos.Fallocate(i.f.Fd(), myos.FALLOC_FL_KEEP_SIZE, 0, _fallocSize); err != nil { log.Errorf("index: %s fallocate() error(err)", i.File, err) i.Close() return nil, err } } i.wg.Add(1) i.signal = make(chan int, 1) go i.merge() return }