// TotalPlainSizeEstimate returns an estimate of the plaintext size of
// this directory block.
7032
func (db *DirBlock) TotalPlainSizeEstimate(
	plainSize int, bsplit BlockSplitter) int {
	if !db.IsIndirect() || len(db.IPtrs) == 0 {
		return plainSize
	}

	// If the top block is indirect, it's too costly to estimate the
	// sizes by checking the plain sizes of all the leafs.  Instead
	// use the following imperfect heuristics:
	//
	// * If there are N child pointers, and the first one is a direct
	//   pointer, assume N-1 of them are full.
	//
	// * If there are N child pointers, and the first one is an
	//   indirect pointer, just give up and max out at the maximum
	//   number of indirect pointers in a block, assuming that at
	//   least one indirect block is full of pointers when there are
	//   at least 2 indirect levels in the tree.
	//
	// This isn't great since it overestimates in many cases
	// (especially when removing entries), and can vastly unerestimate
	// if there are more than 2 levels of indirection.  But it seems
	// unlikely that directory byte size matters for anything in real
	// life.  Famous last words, of course...
	if db.IPtrs[0].DirectType == DirectBlock {
		return MaxBlockSizeBytesDefault * (len(db.IPtrs) - 1)
	}
	return MaxBlockSizeBytesDefault * bsplit.MaxPtrsPerBlock()
}
692469
func (r *RepositoryContent) GetSize() int {
	if r == nil || r.Size == nil {
		return 0
	}
	return *r.Size
}
901012
func (d *namespacedResourcesDeleter) estimateGracefulTermination(gvr schema.GroupVersionResource, ns string, namespaceDeletedAt metav1.Time) (int64, error) {
	groupResource := gvr.GroupResource()
	klog.V(5).Infof("namespace controller - estimateGracefulTermination - group %s, resource: %s", groupResource.Group, groupResource.Resource)
	estimate := int64(0)
	var err error
	switch groupResource {
	case schema.GroupResource{Group: "", Resource: "pods"}:
		estimate, err = d.estimateGracefulTerminationForPods(ns)
	}
	if err != nil {
		return estimate, err
	}
	// determine if the estimate is greater than the deletion timestamp
	duration := time.Since(namespaceDeletedAt.Time)
	allowedEstimate := time.Duration(estimate) * time.Second
	if duration >= allowedEstimate {
		estimate = int64(0)
	}
	return estimate, nil
}
782971
func (loc *Location) Filesize() int64 {
	f, _ := os.Open(loc.Path())
	defer f.Close()

	fi, err := f.Stat()
	if err != nil {
		return 0
	}

	return fi.Size()
}
53285
func (b *BlockServerMemory) GetEncodedSize(
	ctx context.Context, tlfID tlf.ID, id kbfsblock.ID,
	context kbfsblock.Context) (
	size uint32, status keybase1.BlockStatus, err error) {
	if err := checkContext(ctx); err != nil {
		return 0, 0, err
	}

	defer func() {
		err = translateToBlockServerError(err)
	}()
	b.log.CDebugf(ctx,
		"BlockServerMemory.GetEncodedSize id=%s tlfID=%s context=%s",
		id, tlfID, context)
	b.lock.RLock()
	defer b.lock.RUnlock()

	if b.m == nil {
		return 0, 0, errBlockServerMemoryShutdown
	}

	entry, ok := b.m[id]
	if !ok {
		return 0, 0, kbfsblock.ServerErrorBlockNonExistent{
			Msg: fmt.Sprintf("Block ID %s does not exist.", id)}
	}

	if entry.tlfID != tlfID {
		return 0, 0, fmt.Errorf("TLF ID mismatch: expected %s, got %s",
			entry.tlfID, tlfID)
	}

	exists, refStatus, err := entry.refs.checkExists(context)
	if err != nil {
		return 0, 0, err
	}
	if !exists {
		return 0, 0, blockNonExistentError{id}
	}

	return uint32(len(entry.blockData)), refStatus.toBlockStatus(), nil
}
261358
func (r *RepositoryLicense) GetSize() int {
	if r == nil || r.Size == nil {
		return 0
	}
	return *r.Size
}
1066805
func checkCopyPartRangeWithSize(rs *HTTPRangeSpec, resourceSize int64) (err error) {
	if rs == nil {
		return nil
	}
	if rs.IsSuffixLength || rs.Start >= resourceSize || rs.End >= resourceSize {
		return errInvalidRangeSource
	}
	return nil
}
1339159
func (c *cubicSender) BandwidthEstimate() Bandwidth {
	srtt := c.rttStats.SmoothedRTT()
	if srtt == 0 {
		// If we haven't measured an rtt, the bandwidth estimate is unknown.
		return 0
	}
	return BandwidthFromDelta(c.GetCongestionWindow(), srtt)
}
626348
func (s *ItemCollectionMetrics) SetSizeEstimateRangeGB(v []*float64) *ItemCollectionMetrics {
	s.SizeEstimateRangeGB = v
	return s
}
912175
func (db *DirBlock) ClearIndirectPtrSize(i int) {
	if !db.IsInd {
		panic("ClearIndirectPtrSize called on a direct directory block")
	}
	db.IPtrs[i].EncodedSize = 0
}
1240613
func (c *Conn) wndSize() uint32 {
	if len(c.readBuf)+c.inboundWnd > readBufferLen {
		return 0
	}
	return uint32(readBufferLen - len(c.readBuf) - c.inboundWnd)
}
74712
func (c *CacheableBlock) Size() (uint64, error) {
	return uint64(c.Block.MsgBlock().SerializeSize()), nil
}
261336
func calculateEstimateNDV(h *topNHelper, rowCount uint64) (ndv uint64, scaleRatio uint64) {
	sampleSize, sampleNDV, onlyOnceItems := h.sampleSize, uint64(len(h.sorted)), h.onlyOnceItems
	scaleRatio = rowCount / sampleSize

	if onlyOnceItems == sampleSize {
		// Assume this is a unique column, so do not scale up the count of elements
		return rowCount, 1
	} else if onlyOnceItems == 0 {
		// Assume data only consists of sampled data
		// Nothing to do, no change with scale ratio
		return sampleNDV, scaleRatio
	}
	// Charikar, Moses, et al. "Towards estimation error guarantees for distinct values."
	// Proceedings of the nineteenth ACM SIGMOD-SIGACT-SIGART symposium on Principles of database systems. ACM, 2000.
	// This is GEE in that paper.
	// estimateNDV = sqrt(N/n) f_1 + sum_2..inf f_i
	// f_i = number of elements occurred i times in sample

	f1 := float64(onlyOnceItems)
	n := float64(sampleSize)
	N := float64(rowCount)
	d := float64(sampleNDV)

	ndv = uint64(math.Sqrt(N/n)*f1 + d - f1 + 0.5)

	if ndv < sampleNDV {
		ndv = sampleNDV
	}
	if ndv > rowCount {
		ndv = rowCount
	}
	return ndv, scaleRatio
}
177145
func SampleSize(s int) TracerOption {
	if s <= 0 {
		panic("sample size must be greater than 0")
	}
	return func(o *tracerOptions) *tracerOptions {
		o.sampleSize = s
		return o
	}
}
986266
func getDivisibleSize(totalSizes []uint64) (result uint64) {
	gcd := func(x, y uint64) uint64 {
		for y != 0 {
			x, y = y, x%y
		}
		return x
	}
	result = totalSizes[0]
	for i := 1; i < len(totalSizes); i++ {
		result = gcd(result, totalSizes[i])
	}
	return result
}
1113433
func DirSize(dirPath string) (size uint64, numFiles int, err error) {
	err = filepath.Walk(dirPath, func(_ string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if !info.IsDir() {
			size += uint64(info.Size())
			numFiles++
		}
		return nil
	})
	return size, numFiles, err
}
868949
func (cb *CommonBlock) GetEncodedSize() uint32 {
	cb.cacheMtx.RLock()
	defer cb.cacheMtx.RUnlock()
	return cb.cachedEncodedSize
}