// NewSegmenter returns a new Segmenter to read from r.
// Defaults to segment using SegmentWords
518626
func NewSegmenter(r io.Reader) *Segmenter {
	return &Segmenter{
		r:            r,
		segment:      SegmentWords,
		maxTokenSize: MaxScanTokenSize,
		buf:          make([]byte, 4096), // Plausible starting size; needn't be large.
	}
}
1206591
func NewSegment(path, format string, baseOffset int64, maxBytes int64) (*Segment, error) {
	logPath := filepath.Join(path, fmt.Sprintf(format, baseOffset))
	log, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		return nil, err
	}

	s := &Segment{
		log:        log,
		path:       logPath,
		writer:     log,
		reader:     log,
		maxBytes:   maxBytes,
		BaseOffset: baseOffset,
		NextOffset: baseOffset,
	}

	err = s.init()
	if err == io.EOF {
		return s, nil
	}

	return s, err
}
216492
func (l *WAL) newSegmentFile() error {
	l.currentSegmentID++
	if l.currentSegmentWriter != nil {
		l.sync()

		if err := l.currentSegmentWriter.close(); err != nil {
			return err
		}
		l.tracker.SetOldSegmentSize(uint64(l.currentSegmentWriter.size))
	}

	fileName := filepath.Join(l.path, fmt.Sprintf("%s%05d.%s", WALFilePrefix, l.currentSegmentID, WALFileExtension))
	fd, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)
	if err != nil {
		return err
	}
	l.currentSegmentWriter = NewWALSegmentWriter(fd)
	l.tracker.IncSegments()

	// Reset the current segment size stat
	l.tracker.SetCurrentSegmentSize(0)
	return nil
}
812137
func NewScanner(name string, d EncodingHint) (UTFScanCloser, error) {
	f, err := OpenFile(name, d)
	if err != nil {
		return nil, err
	}

	return scanCloser{
		scanner: bufio.NewScanner(f),
		file:    f,
	}, nil
}
1045193
func NewSegment(name, traceID, spanID string, conn net.Conn) *Segment {
	return &Segment{
		Mutex:      &sync.Mutex{},
		Name:       name,
		TraceID:    traceID,
		ID:         spanID,
		StartTime:  now(),
		InProgress: true,
		conn:       conn,
	}
}
578102
func (s *Segment) NewSubsegment(name string) *Segment {
	s.Lock()
	defer s.Unlock()

	sub := &Segment{
		Mutex:      &sync.Mutex{},
		ID:         NewID(),
		TraceID:    s.TraceID,
		ParentID:   s.ID,
		Type:       "subsegment",
		Name:       name,
		StartTime:  now(),
		InProgress: true,
		Parent:     s,
		conn:       s.conn,
	}
	s.Subsegments = append(s.Subsegments, sub)
	s.counter++
	return sub
}
1090657
func (r *Reader) Segment() int {
	if b, ok := r.rdr.(*segmentBufReader); ok {
		return b.segs[b.cur].Index()
	}
	return -1
}
829440
func writeSegment(w io.Writer, msg segment) (err error) {
	if !msg.typ.isValid() {
		return fmt.Errorf("[writeSegment] msg.typ %d not defined", msg.typ)
	}
	buf := &bytes.Buffer{}
	buf.WriteByte(byte(msg.typ))
	//name
	if msg.typ.hasName() {
		err = writeString1(buf, msg.name)
		if err != nil {
			fmt.Printf("error returned from writeString1i \n")
			return
		}
	}

	if msg.typ.hasMsg() {
		err = writeMap(buf, msg.msg)
		if err != nil {
			fmt.Printf("error retruned from writeMap \n")
			return
		}
	}

	//写长度
	err = binary.Write(w, binary.BigEndian, uint32(buf.Len()))
	if err != nil {
		fmt.Printf("[writeSegment] error writing to binary \n")
		return
	}

	_, err = buf.WriteTo(w)
	if err != nil {
		fmt.Printf("[writeSegment] error writing to buffer \n")
		return
	}

	return nil
}
400831
func (nr *NewRelic) Segment(name string) Segment {
	return NewRelicSegment{
		newrelic.StartSegment(nr.txn, name),
	}
}
526996
func parseSegment(parent *Node, segment string) *Node {
	if node := parent.getChild(segment); node != nil {
		return node
	}
	node := &Node{
		segment:  segment,
		parent:   parent,
		children: make(map[string]*Node),
		handlers: make(map[string]interface{}),
	}
	// route "/a/" match the last segment empty
	if segment == "" {
		parent.children[segment] = node
		// segment contain any :: will clean up to static segment
		// "::name" convert to ":name"
		// "cms::name::hello" convert to "cms:name:hello"
	} else if strings.Contains(segment, "::") {
		parent.children[strings.Replace(segment, "::", ":", -1)] = node
	} else if segment == "*" {
		node.wildcard = true
		node.regex = wildRegexp
		node.name = []string{":splat"}
		parent.varyChildren = append(parent.varyChildren, node)
	} else if segment == "*.*" {
		node.wildcard = true
		node.regex = extWildRegexp
		node.name = []string{":path", ":ext"}
		parent.varyChildren = append(parent.varyChildren, node)
	} else if optionalParamRegexp.MatchString(segment) {
		node.optional = true
		node.name = []string{segment[1:]}
		parent.optionChildren = append(parent.optionChildren, node)
		parent.segChildren = append(parent.segChildren, node)
	} else if paramRegexp.MatchString(segment) {
		node.name = []string{segment}
		parent.segChildren = append(parent.segChildren, node)
	} else if strings.ContainsAny(segment, ":") {
		node.name, node.regex, node.optional = regexpSegment(segment)
		if node.optional {
			parent.optionChildren = append(parent.optionChildren, node)
		}
		parent.varyChildren = append(parent.varyChildren, node)
	} else {
		parent.children[segment] = node
	}
	return node
}
659504
func (t *walTracker) SetSegments(sz uint64) {
	labels := t.labels
	t.metrics.Segments.With(labels).Set(float64(sz))
}
440507
func NewServiceRenderer(model *surface.Model) (renderer *Renderer, err error) {
	renderer = &Renderer{}
	renderer.Model = model
	return renderer, nil
}
1154452
func NewReader(ra ReaderAt) io.Reader {
	rd := io.NewSectionReader(ra, 0, ra.Size())
	return rd
}
1007701
func (res *Resource) NewSlice() interface{} {
	if res.Value == nil {
		return nil
	}
	sliceType := reflect.SliceOf(reflect.TypeOf(res.Value))
	slice := reflect.MakeSlice(sliceType, 0, 0)
	slicePtr := reflect.New(sliceType)
	slicePtr.Elem().Set(slice)
	return slicePtr.Interface()
}
1230473
func NewScannerReader(src io.Reader) *scanner.Scanner {
	var s scanner.Scanner
	s.Init(src)
	s.Error = func(_ *scanner.Scanner, _ string) {}
	s.Whitespace = 0
	s.Mode = s.Mode ^ scanner.SkipComments
	return &s
}
862445
func NewScanner(r io.Reader) *Scanner {
	sc := Scanner{in: bufio.NewReader(r)}
	return &sc
}
21996
func (pool *ReaderPool) segmentsToString(infos []*SegmentCommitInfo) string {
	// TODO synchronized
	var parts []string
	for _, info := range infos {
		parts = append(parts, pool.segmentToString(info))
	}
	return strings.Join(parts, " ")
}