// 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 { // falloc(FALLOC_FL_KEEP_SIZE) 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, options Options) (i *Indexer, err error) { var stat os.FileInfo i = &Indexer{} i.File = file i.closed = false i.syncOffset = 0 i.Options = options i.ring = NewRing(options.RingBuffer) i.bn = 0 i.buf = make([]byte, options.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 }
// NewIndexer new a indexer for async merge index data to disk. func NewIndexer(file string, sigTime time.Duration, sigCnt, ring, buf int) (i *Indexer, err error) { var stat os.FileInfo i = &Indexer{} i.ring = NewRing(ring) i.sigCnt = sigCnt i.sigTime = sigTime i.bufSize = buf i.File = file i.closed = false if i.f, err = os.OpenFile(file, os.O_RDWR|os.O_CREATE, 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(), 1, 0, fallocSize); err != nil { log.Errorf("index: %s fallocate() error(err)", i.File, err) i.Close() return nil, err } } i.bw = bufio.NewWriterSize(i.f, buf) i.wg.Add(1) i.signal = make(chan int, 1) go i.write() return }