// Returns the state of pieces of the torrent. They are grouped into runs of
// same state. The sum of the state run lengths is the number of pieces
// in the torrent.
167916
func (t *Torrent) PieceStateRuns() []PieceStateRun {
	t.cl.rLock()
	defer t.cl.rUnlock()
	return t.pieceStateRuns()
}
656626
func (c *Client) GetAllocState(allocID string) (*arstate.State, error) {
	ar, err := c.getAllocRunner(allocID)
	if err != nil {
		return nil, err
	}

	return ar.AllocState(), nil
}
1369230
func (api *DiskAPI) State(diskID int64) (bool, error) {
	disk, err := api.Read(diskID)
	if err != nil {
		return false, err
	}
	return disk.IsAvailable(), nil
}
252470
func (c *ClusterManager) GetGossipState() *cluster.ClusterState {
	gossipStoreKey := types.StoreKey(heartbeatKey + c.config.ClusterId)
	nodeValue := c.gossip.GetStoreKeyValue(gossipStoreKey)
	nodes := make([]types.NodeValue, len(nodeValue), len(nodeValue))
	i := 0
	for _, value := range nodeValue {
		nodes[i] = value
		i++
	}

	return &cluster.ClusterState{NodeStatus: nodes}
}
906745
func (p *ProtobufCarrier) State() (traceID types.TraceID, spanID uint64, parentSpanID *uint64, sampled bool, flags flag.Flags) {
	traceID.Low = p.TraceId
	traceID.High = p.TraceIdHigh
	spanID = p.SpanId
	sampled = p.Sampled
	flags = flag.Flags(p.Flags)
	if flags&flag.IsRoot == 0 {
		parentSpanID = &p.ParentSpanId
	}
	return traceID, spanID, parentSpanID, sampled, flags
}
91861
func GetState() (HealthState, time.Time, time.Time) {
	mutex.RLock()
	defer mutex.RUnlock()

	hs := make(HealthState)
	for key := range healthState {
		hs[key] = healthState[key]
	}

	return hs, healthLastChecked, healthLastSuccess
}
900712
func (cl *Client) Torrent(ih metainfo.Hash) (t *Torrent, ok bool) {
	cl.lock()
	defer cl.unlock()
	t, ok = cl.torrents[ih]
	return
}
19394
func (t *Torrent) Seeding() bool {
	t.cl.lock()
	defer t.cl.unlock()
	return t.seeding()
}
770359
func (t *Torrent) allStats(f func(*ConnStats)) {
	f(&t.stats)
	f(&t.cl.stats)
}
665829
func (t *Torrent) UnmarshalJSON(data []byte) (err error) {
	// Shadow real type for regular unmarshalling
	type RawTorrent Torrent
	tmp := &struct {
		ActivityDate   *int64  `json:"activityDate"`
		AddedDate      *int64  `json:"addedDate"`
		DateCreated    *int64  `json:"dateCreated"`
		DoneDate       *int64  `json:"doneDate"`
		PieceSize      *int64  `json:"pieceSize"`
		SecondsSeeding *int64  `json:"secondsSeeding"`
		SizeWhenDone   *int64  `json:"sizeWhenDone"`
		StartDate      *int64  `json:"startDate"`
		TotalSize      *int64  `json:"totalSize"`
		Wanted         []int64 `json:"wanted"` // boolean in number form
		*RawTorrent
	}{
		RawTorrent: (*RawTorrent)(t),
	}
	// Unmarshall (with timestamps as number)
	if err = json.Unmarshal(data, &tmp); err != nil {
		return
	}
	// Create the real time & duration from timsteamps and seconds
	if tmp.ActivityDate != nil {
		ad := time.Unix(*tmp.ActivityDate, 0)
		t.ActivityDate = &ad
	}
	if tmp.AddedDate != nil {
		ad := time.Unix(*tmp.AddedDate, 0)
		t.AddedDate = &ad
	}
	if tmp.DateCreated != nil {
		dc := time.Unix(*tmp.DateCreated, 0)
		t.DateCreated = &dc
	}
	if tmp.DoneDate != nil {
		dd := time.Unix(*tmp.DoneDate, 0)
		t.DoneDate = &dd
	}
	if tmp.PieceSize != nil {
		ps := cunits.ImportInByte(float64(*tmp.PieceSize))
		t.PieceSize = &ps
	}
	if tmp.SecondsSeeding != nil {
		dur := time.Duration(*tmp.SecondsSeeding) * time.Second
		t.SecondsSeeding = &dur
	}
	if tmp.SizeWhenDone != nil {
		swd := cunits.ImportInByte(float64(*tmp.SizeWhenDone))
		t.SizeWhenDone = &swd
	}
	if tmp.StartDate != nil {
		st := time.Unix(*tmp.StartDate, 0)
		t.StartDate = &st
	}
	if tmp.TotalSize != nil {
		ts := cunits.ImportInByte(float64(*tmp.TotalSize))
		t.TotalSize = &ts
	}
	// Boolean slice in decimal form
	if tmp.Wanted != nil {
		t.Wanted = make([]bool, len(tmp.Wanted))
		for index, value := range tmp.Wanted {
			if value == 1 {
				t.Wanted[index] = true
			} else if value != 0 {
				return fmt.Errorf("Can't convert Wanted index %d value '%d' as boolean", index, value)
			}
		}
	}
	return
}
998265
func (c *Client) GetStateFromCluster() (*State, error) {
	resp, err := c.GetHTTPResponseFromCluster(c.GetURLForStateFile)
	return c.parseStateResponse(resp, err)
}
188559
func (d *Downloader) syncState(root common.Hash) *stateSync {
	s := newStateSync(d, root)
	select {
	case d.stateSyncStart <- s:
	case <-d.quitCh:
		s.err = errCancelStateFetch
		close(s.done)
	}
	return s
}
810482
func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
	r request, ok bool) {
	if offset < 0 || offset >= torrentLength {
		return
	}
	r.Index = pp.Integer(offset / pieceSize)
	r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
	r.Length = pp.Integer(chunkSize)
	pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
	if r.Length > pieceLeft {
		r.Length = pieceLeft
	}
	torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
	if int64(r.Length) > torrentLeft {
		r.Length = pp.Integer(torrentLeft)
	}
	ok = true
	return
}
1184286
func (r *reader) piecesUncached() (ret pieceRange) {
	ra := r.readahead
	if ra < 1 {
		// Needs to be at least 1, because [x, x) means we don't want
		// anything.
		ra = 1
	}
	if ra > r.length-r.pos {
		ra = r.length - r.pos
	}
	ret.begin, ret.end = r.t.byteRegionPieces(r.torrentOffset(r.pos), ra)
	return
}
1147768
func pieceStateRunStatusChars(psr PieceStateRun) (ret string) {
	ret = fmt.Sprintf("%d", psr.Length)
	ret += func() string {
		switch psr.Priority {
		case PiecePriorityNext:
			return "N"
		case PiecePriorityNormal:
			return "."
		case PiecePriorityReadahead:
			return "R"
		case PiecePriorityNow:
			return "!"
		case PiecePriorityHigh:
			return "H"
		default:
			return ""
		}
	}()
	if psr.Checking {
		ret += "H"
	}
	if psr.Partial {
		ret += "P"
	}
	if psr.Complete {
		ret += "C"
	}
	if !psr.Ok {
		ret += "?"
	}
	return
}
881487
func (cs *ConsensusState) GetState() sm.State {
	cs.mtx.RLock()
	defer cs.mtx.RUnlock()
	return cs.state.Copy()
}
1108426
func (r *ring) getPartition(key []byte) *partition {
	return r.partitions[int(xxhash.Sum64(key)%partitions)]
}