// NewFileFragment returns a FileFragment which will read only from startLoc to
// endLoc in a file.
493492
func NewFileFragment(f *os.File, startLoc, endLoc int64) (*FileFragment, error) {
	thisF, err := os.Open(f.Name())
	if err != nil {
		return nil, errors.Wrap(err, "opening file fragment")
	}
	_, err = thisF.Seek(startLoc, io.SeekStart)
	if err != nil {
		return nil, errors.Wrap(err, "seeking to start location in new file handle")
	}
	return &FileFragment{
		file:     thisF,
		startLoc: startLoc,
		endLoc:   endLoc,
	}, nil
}
450175
func Fragment(expr I, values ...interface{}) Condition {
	return Condition{
		Type:  ConditionFragment,
		Left:  NewOperand(expr),
		Right: NewOperand(values...),
	}
}
868052
func NewFilePager(tableName string, fs *FileSource) *FilePager {
	fp := &FilePager{
		fs:      fs,
		table:   tableName,
		exit:    make(chan bool),
		readers: make(chan *FileReader, FileBufferSize),
		partid:  -1,
	}
	return fp
}
55462
func (v *view) fragmentPath(shard uint64) string {
	return filepath.Join(v.path, "fragments", strconv.FormatUint(shard, 10))
}
1194365
func (w *Writer) FlushFragment() error {
	if w.Buffered() == 0 || w.err != nil {
		return w.err
	}

	w.err = w.flushFragment(false)
	w.n = 0
	w.fragmented = true

	return w.err
}
925986
func NewExclusiveLock(path string) (*UpdateableFileLock, error) {
	flock, err := lock.TryExclusiveLock(path, lock.RegFile)
	if err != nil {
		return nil, err
	}
	fi, err := os.Stat(path)
	if err != nil {
		return nil, err
	}
	// Note: if we expected the file to ever be deleted and recreated while an
	// ExLock isn't held, we would have to check the fd is still correct here.
	// However, the file is created once and never deleted outside of the safely
	// handled renames in this package, so it's safe to assume the fd hasn't
	// changed.
	// The rename call in this package is "safe" because it leaves no window
	// where the file at that path does not have an exclusive lock
	return &UpdateableFileLock{
		lock:     flock,
		path:     path,
		isLocked: true,
		perms:    fi.Mode(),
	}, nil
}
451656
func NewFileCommentItem(f *File, c *Comment) Item {
	return Item{Type: TYPE_FILE_COMMENT, File: f, Comment: c}
}
1180318
func NewFile(fd uintptr, name string) *os.File {
	return newfile(fd, name)
}
423277
func secondFragments(second string) (sec, milliSec, microSec, nanoSec, picoSec int) {
	fragments := strings.Split(second, ".")

	if len(fragments) > 0 {
		sec, _ = strconv.Atoi(fragments[0])
	}
	if len(fragments) > 1 {
		milliSec, _ = strconv.Atoi(fragments[1])
	}
	if len(fragments) > 2 {
		microSec, _ = strconv.Atoi(fragments[2])
	}
	if len(fragments) > 3 {
		nanoSec, _ = strconv.Atoi(fragments[3])
	}
	if len(fragments) > 4 {
		picoSec, _ = strconv.Atoi(fragments[4])
	}
	return
}
1125064
func NewFileWriter(logPath string) (io.WriteCloser, error) {
	if err := primeLogFile(logPath); err != nil {
		// This isn't a fatal error so log and continue if priming fails.
		logger.Warningf("Unable to prime %s (proceeding anyway): %v", logPath, err)
	}
	return &lumberjack.Logger{
		Filename:   logPath,
		MaxSize:    300, // MB
		MaxBackups: 2,
		Compress:   true,
	}, nil
}
820947
func SplitFileLines(f *os.File, numParts int64) ([]*FileFragment, error) {
	stats, err := f.Stat()
	if err != nil {
		return nil, err
	}
	splitSize := stats.Size() / numParts

	ret := make([]*FileFragment, 0)
	var startLoc int64
	for {
		endLoc, errSeek := seekAndSearch(f, splitSize, '\n')
		if errSeek != nil && errSeek != io.EOF {
			return nil, errors.Wrap(errSeek, "searching for next split location")
		}
		ff, err := NewFileFragment(f, startLoc, endLoc)
		if err != nil {
			return nil, errors.Wrap(err, "creating new file fragment")
		}
		ret = append(ret, ff)
		if errSeek == io.EOF {
			break
		}
		startLoc = endLoc
	}
	return ret, nil
}
740441
func (c *CommitLog) NewReader(offset int64) (io.Reader, error) {
	c.mu.RLock()
	defer c.mu.RUnlock()
	log.With("num_segments", len(c.segments)).
		With("offset", offset).
		Infoln("searching segments")

	// in the event there has been data committed to the segment but no offset for a path,
	// then we need to create a reader that starts from the very beginning
	if offset < 0 && len(c.segments) > 0 {
		// if err := c.segments[0].Open(); err != nil {
		// 	log.Errorf("unable to open segment, %s", err)
		// }
		return &Reader{commitlog: c, idx: 0, position: 0}, nil
	}

	var idx int
	for i := 0; i < len(c.segments); i++ {
		idx = i
		if (i + 1) != len(c.segments) {
			lowerOffset := c.segments[i].BaseOffset
			upperOffset := c.segments[i+1].BaseOffset
			log.With("lower_offset", lowerOffset).
				With("upper_offset", upperOffset).
				With("offset", offset).
				With("segment", idx).
				Debugln("checking if offset in segment")
			if offset >= lowerOffset && offset < upperOffset {
				break
			}
		}
	}

	log.With("offset", offset).With("segment_index", idx).Debugln("finding offset in segment")
	// if err := c.segments[idx].Open(); err != nil {
	// 	log.Errorf("unable to open segment, %s", err)
	// }
	position, err := c.segments[idx].FindOffsetPosition(uint64(offset))
	if err != nil {
		return nil, err
	}

	return &Reader{
		commitlog: c,
		idx:       idx,
		position:  position,
	}, nil
}
986524
func NewGFileForPath(filename string) *GFile {
	ptrFilename := C.CString(filename)
	defer cfree(ptrFilename)

	return &GFile{C.g_file_new_for_path(ptrFilename)}
}
1278578
func NewFile(lines []*Line) *File {
	// create
	ret := &File{
		lines:    lines,
		sections: make([]*Section, 0),

		// copy manipulation funcs
		SectionManipFunc: SectionManipFunc,
		SectionNameFunc:  SectionNameFunc,
		SectionCompFunc:  nil,
		KeyManipFunc:     KeyManipFunc,
		KeyCompFunc:      KeyCompFunc,
		ValueManipFunc:   ValueManipFunc,
		NameSplitFunc:    NameSplitFunc,
	}

	// create default section
	lastSection := NewSection(position{}, "", "", nil)
	lastSection.file = ret
	ret.sections = append(ret.sections, lastSection)

	// loop over lines and build sections/keys
	for _, l := range lines {
		switch l.item.(type) {
		case *Section:
			// get section
			lastSection, _ = l.item.(*Section)

			// save data
			lastSection.file = ret
			ret.sections = append(ret.sections, lastSection)

		case *KeyValuePair:
			// save kvp
			kvp, _ := l.item.(*KeyValuePair)
			lastSection.keys = append(lastSection.keys, kvp.key)
		}
	}

	return ret
}
832952
func newBlockfileStream(rootDir string, fileNum int, startOffset int64) (*blockfileStream, error) {
	filePath := deriveBlockfilePath(rootDir, fileNum)
	logger.Debugf("newBlockfileStream(): filePath=[%s], startOffset=[%d]", filePath, startOffset)
	var file *os.File
	var err error
	if file, err = os.OpenFile(filePath, os.O_RDONLY, 0600); err != nil {
		return nil, errors.Wrapf(err, "error opening block file %s", filePath)
	}
	var newPosition int64
	if newPosition, err = file.Seek(startOffset, 0); err != nil {
		return nil, errors.Wrapf(err, "error seeking block file [%s] to startOffset [%d]", filePath, startOffset)
	}
	if newPosition != startOffset {
		panic(fmt.Sprintf("Could not seek block file [%s] to startOffset [%d]. New position = [%d]",
			filePath, startOffset, newPosition))
	}
	s := &blockfileStream{fileNum, file, bufio.NewReader(file), startOffset}
	return s, nil
}
878986
func (kcp *KCP) delSegment(seg *segment) {
	if seg.data != nil {
		xmitBuf.Put(seg.data)
		seg.data = nil
	}
}
1115112
func unprotectedWriteToFragment(f *fragment, bm *roaring.Bitmap) (n int64, err error) { // nolint: interfacer

	completeMessage := fmt.Sprintf("fragment: snapshot complete %s/%s/%s/%d", f.index, f.field, f.view, f.shard)
	start := time.Now()
	defer track(start, completeMessage, f.stats, f.Logger)

	// Create a temporary file to snapshot to.
	snapshotPath := f.path + snapshotExt
	file, err := os.Create(snapshotPath)
	if err != nil {
		return n, fmt.Errorf("create snapshot file: %s", err)
	}
	defer file.Close()

	// Write storage to snapshot.
	bw := bufio.NewWriter(file)
	if n, err = bm.WriteTo(bw); err != nil {
		return n, fmt.Errorf("snapshot write to: %s", err)
	}

	if err := bw.Flush(); err != nil {
		return n, fmt.Errorf("flush: %s", err)
	}

	// Close current storage.
	if err := f.closeStorage(); err != nil {
		return n, fmt.Errorf("close storage: %s", err)
	}

	// Move snapshot to data file location.
	if err := os.Rename(snapshotPath, f.path); err != nil {
		return n, fmt.Errorf("rename snapshot: %s", err)
	}

	// Reopen storage.
	if err := f.openStorage(); err != nil {
		return n, fmt.Errorf("open storage: %s", err)
	}

	// Reset operation count.
	f.opN = 0

	return n, nil
}