// cachePut puts the given Block into the cache if it exists, it returns
// the Block that was evicted or b if it was not retained, and whether
// the Block was retained by the cache.
165088
func (bg *Reader) cachePut(b Block) (evicted Block, retained bool) {
	if b == nil || !b.hasData() {
		return b, false
	}
	return bg.cache.Put(b)
}
406107
func (cache *DiskBlockCacheService) GetPrefetchStatus(
	ctx context.Context, arg kbgitkbfs.GetPrefetchStatusArg) (
	prefetchStatus kbgitkbfs.PrefetchStatus, err error) {
	dbc := cache.config.DiskBlockCache()
	if dbc == nil {
		return NoPrefetch.ToProtocol(), DiskBlockCacheError{"Disk cache is nil"}
	}
	tlfID := tlf.ID{}
	err = tlfID.UnmarshalBinary(arg.TlfID)
	if err != nil {
		return NoPrefetch.ToProtocol(), newDiskBlockCacheError(err)
	}
	blockID := kbfsblock.ID{}
	err = blockID.UnmarshalBinary(arg.BlockID)
	if err != nil {
		return NoPrefetch.ToProtocol(), newDiskBlockCacheError(err)
	}

	cacheType := DiskBlockAnyCache
	if cache.config.IsSyncedTlf(tlfID) {
		cacheType = DiskBlockSyncCache
	}

	dbStatus, err := dbc.GetPrefetchStatus(ctx, tlfID, blockID, cacheType)
	if err != nil {
		return NoPrefetch.ToProtocol(), newDiskBlockCacheError(err)
	}
	return dbStatus.ToProtocol(), nil
}
1132569
func NewBlockDataToJSONStdOut(m ...*sync.Mutex) *BlockDataToJSONStdOut {
	if len(m) > 1 {
		panic("Too many inputs.")
	}
	if len(m) > 0 {
		return &BlockDataToJSONStdOut{m[0]}
	}
	return &BlockDataToJSONStdOut{}
}
1310911
func (f *Fetcher) forgetBlock(hash common.Hash) {
	if insert := f.queued[hash]; insert != nil {
		f.queues[insert.origin]--
		if f.queues[insert.origin] == 0 {
			delete(f.queues, insert.origin)
		}
		delete(f.queued, hash)
	}
}
84228
func (mvcc *MVCCLevelDB) RawBatchPut(keys, values [][]byte) {
	mvcc.mu.Lock()
	defer mvcc.mu.Unlock()

	batch := &leveldb.Batch{}
	for i, key := range keys {
		value := values[i]
		if value == nil {
			value = []byte{}
		}
		batch.Put(key, value)
	}
	terror.Log(mvcc.db.Write(batch, nil))
}
1206933
func (r *RenderTemplateResult) renderOutput(wr io.Writer) (err error) {
	defer func() {
		if rerr := recover(); rerr != nil {
			resultsLog.Error("ApplyBytes: panic recovery", "recover-error", rerr)
			err = fmt.Errorf("Template Execution Panic in %s:\n%s", r.Template.Name(), rerr)
		}
	}()
	err = r.Template.Render(wr, r.ViewArgs)
	return
}
767254
func (c *C2) Evicted() []byte {
	if c.totalWritten <= c.blocksize {
		return nil
	}

	bufferToRead := c.a
	if c.b.head < c.a.head {
		bufferToRead = c.b
	}

	bufferStart := bufferToRead.head + c.blocksize
	readLength := c.lastWritten

	// if the buffer wasn't full, we don't read the full length
	if c.totalWritten-c.lastWritten < c.blocksize {
		readLength -= c.lastWritten - c.totalWritten + c.blocksize
	}

	return bufferToRead.buffer[bufferStart-readLength : bufferStart]
}
89557
func (p *pageState) initOutputFormat(isRenderingSite bool, idx int) error {
	if err := p.shiftToOutputFormat(isRenderingSite, idx); err != nil {
		return err
	}

	if !p.renderable {
		if _, err := p.Content(); err != nil {
			return err
		}
	}

	return nil

}
290654
func (c DiskBlockCacheClient) PutBlock(ctx context.Context, __arg PutBlockArg) (err error) {
	err = c.Cli.Call(ctx, "kbgitkbfs.1.DiskBlockCache.PutBlock", []interface{}{__arg}, nil)
	return
}
760305
func (apic *APICache) RemoveCachedBlock(cachedBlock *CachedBlock) {
	apic.mtx.Lock()
	defer apic.mtx.Unlock()
	apic.removeCachedBlock(cachedBlock)
}
436383
func parseDfOutput(r io.Reader) ([]*dfStats, error) {
	getBytes := func(blockStr string) (uint64, error) {
		base := uint64(1024)
		bytes := uint64(0)
		b := uint64(0)
		var err error
		// in df output, kB, MB, etc are powers of 1000, K, M, etc are powers of 1024
		if strings.HasSuffix(blockStr, "B") {
			base = 1000
		}
		blockStr = strings.TrimSuffix(blockStr, "B")
		if strings.HasSuffix(blockStr, "K") {
			b, err = strconv.ParseUint(strings.TrimSuffix(blockStr, "K"), 10, 64)
			bytes = b * base
		} else if strings.HasSuffix(blockStr, "M") {
			b, err = strconv.ParseUint(strings.TrimSuffix(blockStr, "M"), 10, 64)
			bytes = b * base * base
		} else if strings.HasSuffix(blockStr, "G") {
			b, err = strconv.ParseUint(strings.TrimSuffix(blockStr, "G"), 10, 64)
			bytes = b * base * base * base
		} else if strings.HasSuffix(blockStr, "T") {
			b, err = strconv.ParseUint(strings.TrimSuffix(blockStr, "T"), 10, 64)
			bytes = b * base * base * base * base
		} else {
			bytes, err = strconv.ParseUint(blockStr, 10, 64)
		}

		return bytes, err
	}
	scanner := bufio.NewScanner(r)
	statsSlice := make([]*dfStats, 0, 3)
	var err error
	var blockSize uint64
	for scanner.Scan() {
		line := scanner.Text()
		f := strings.Fields(line)
		// Read block size from output header
		if strings.HasPrefix(f[0], "Filesystem") {
			blockSizeStr := strings.Split(f[1], "-")[0]
			blockSize, err = getBytes(blockSizeStr)
			if err != nil {
				return statsSlice, err
			}
		} else {
			filesystemPath := f[0]
			totalBlocks, err := strconv.ParseUint(f[1], 10, 64)
			if err != nil {
				return statsSlice, err
			}
			usedBlocks, err := strconv.ParseUint(f[2], 10, 64)
			if err != nil {
				return statsSlice, err
			}
			availableBlocks, err := strconv.ParseUint(f[3], 10, 64)
			if err != nil {
				return statsSlice, err
			}
			mountPoint := f[5]
			statsSlice = append(statsSlice, &dfStats{
				BlockSize:       blockSize,
				FilesystemPath:  filesystemPath,
				BlocksTotal:     totalBlocks,
				BlocksUsed:      usedBlocks,
				BlocksAvailable: availableBlocks,
				MountPoint:      mountPoint,
			})
		}
	}
	return statsSlice, nil
}
210309
func (b *PCMBuffer) CacheInts() {
	if b == nil || b.DataType == Integer {
		return
	}
	b.Ints = b.AsInts()
}
110345
func (c *ConfigLocal) DiskBlockCache() DiskBlockCache {
	c.lock.RLock()
	defer c.lock.RUnlock()
	return c.diskBlockCache
}
26747
func (b *BlockCacheStandard) Get(ptr BlockPointer) (Block, error) {
	block, _, err := b.GetWithLifetime(ptr)
	return block, err
}
615371
func (cache *Cache) Flush(output Writer, backend Reader) error {
	err := cache.Sync(output)
	if err != nil {
		return err
	}
	cache.Reset(backend)
	return nil
}
1155746
func batchRawPut(value []byte) {
	cli, err := tikv.NewRawKVClient(strings.Split(*pdAddr, ","), config.Security{
		ClusterSSLCA:   *sslCA,
		ClusterSSLCert: *sslCert,
		ClusterSSLKey:  *sslKey,
	})
	if err != nil {
		log.Fatal(err.Error())
	}

	wg := sync.WaitGroup{}
	base := *dataCnt / *workerCnt
	wg.Add(*workerCnt)
	for i := 0; i < *workerCnt; i++ {
		go func(i int) {
			defer wg.Done()

			for j := 0; j < base; j++ {
				k := base*i + j
				key := fmt.Sprintf("key_%d", k)
				err = cli.Put([]byte(key), value)
				if err != nil {
					log.Fatal("put failed", zap.Error(err))
				}
			}
		}(i)
	}
	wg.Wait()
}
862432
func (apic *APICache) GetCachedBlockByHash(hash chainhash.Hash) *CachedBlock {
	// validate the chainhash.Hash
	if _, err := chainhash.NewHashFromStr(hash.String()); err != nil {
		fmt.Printf("that's not a real hash!")
		return nil
	}

	apic.mtx.Lock()
	defer apic.mtx.Unlock()
	return apic.getCachedBlockByHash(hash)
}