// Misses safely returns the number of prefetch misses.
162009
func (p *BlockPrefetchClient) Misses() uint64 {
	p.Lock()
	defer p.Unlock()
	return p.misses
}
967517
func (brq *blockRetrievalQueue) checkCaches(ctx context.Context,
	kmd libkey.KeyMetadata, ptr data.BlockPointer, block data.Block,
	action BlockRequestAction) (PrefetchStatus, error) {
	dbc := brq.config.DiskBlockCache()
	preferredCacheType := action.CacheType()

	cachedBlock, err := brq.config.BlockCache().Get(ptr)
	if err == nil {
		if dbc == nil {
			block.Set(cachedBlock)
			return brq.getPrefetchStatus(ptr.ID)
		}

		prefetchStatus, err := dbc.GetPrefetchStatus(
			ctx, kmd.TlfID(), ptr.ID, preferredCacheType)
		if err == nil {
			block.Set(cachedBlock)
			return prefetchStatus, nil
		}
		// If the prefetch status wasn't in the preferred cache, do a
		// full `Get()` below in an attempt to move the full block
		// into the preferred cache.
	} else if dbc == nil || action.DelayCacheCheck() {
		return NoPrefetch, err
	}

	blockBuf, serverHalf, prefetchStatus, err := dbc.Get(
		ctx, kmd.TlfID(), ptr.ID, preferredCacheType)
	if err != nil {
		return NoPrefetch, err
	}
	if len(blockBuf) == 0 {
		return NoPrefetch, data.NoSuchBlockError{ID: ptr.ID}
	}

	// Assemble the block from the encrypted block buffer.
	err = brq.config.blockGetter().assembleBlock(ctx, kmd, ptr, block, blockBuf,
		serverHalf)
	if err == nil {
		// Cache the block in memory.  TODO: plumb through whether
		// journaling is enabled for this TLF, to set the right cache
		// behavior.
		brq.config.BlockCache().Put(
			ptr, kmd.TlfID(), block, data.TransientEntry, data.DoCacheHash)
	}
	return prefetchStatus, err
}
1144104
func (d *DefaultMetricCollector) Errors() *rolling.Number {
	d.mutex.RLock()
	defer d.mutex.RUnlock()
	return d.errors
}
384696
func (m *MockQuery) Prefetch(arg0 float64) mongo.Query {
	ret := m.ctrl.Call(m, "Prefetch", arg0)
	ret0, _ := ret[0].(mongo.Query)
	return ret0
}
970713
func (tx *Tx) closePrepared() {
	tx.stmts.Lock()
	for _, stmt := range tx.stmts.v {
		stmt.Close()
	}
	tx.stmts.Unlock()
}
1372285
func (u Util) PerformFetch(f FetchOp) error {
	path := f.Node.Path

	if err := MkdirForFile(path); err != nil {
		return err
	}

	// Create a temporary file in the same directory to ensure it's on the same filesystem
	tmp, err := ioutil.TempFile(filepath.Dir(path), "tmp")
	if err != nil {
		return err
	}
	defer tmp.Close()

	// ioutil.TempFile defaults to 0600
	if err := tmp.Chmod(DefaultFilePermissions); err != nil {
		return err
	}

	// sometimes the following line will fail (the file might be renamed),
	// but that's ok (we wanted to keep the file in that case).
	defer os.Remove(tmp.Name())

	err = u.Fetcher.Fetch(f.Url, tmp, f.FetchOptions)
	if err != nil {
		u.Crit("Error fetching file %q: %v", path, err)
		return err
	}

	if f.Append {
		// Make sure that we're appending to a file
		finfo, err := os.Lstat(path)
		switch {
		case os.IsNotExist(err):
			// No problem, we'll create it.
			break
		case err != nil:
			return err
		default:
			if !finfo.Mode().IsRegular() {
				return fmt.Errorf("can only append to files: %q", path)
			}
		}

		// Open with the default permissions, we'll chown/chmod it later
		targetFile, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, DefaultFilePermissions)
		if err != nil {
			return err
		}
		defer targetFile.Close()

		if _, err = tmp.Seek(0, os.SEEK_SET); err != nil {
			return err
		}
		if _, err = io.Copy(targetFile, tmp); err != nil {
			return err
		}
	} else {
		if err = os.Rename(tmp.Name(), path); err != nil {
			return err
		}
	}

	return nil
}
938390
func ShortAttempt() RetryStrategy {
	return RetryStrategy{
		Count:          10,
		Break:          50 * time.Millisecond,
		BreakIncrement: 0,
		Timeout:        5 * time.Second,
	}
}
287603
func (z *bytesDecReader) skip(accept *bitset256) (token byte) {
	i := z.c
	// if i == len(z.b) {
	// 	goto END
	// 	// panic(io.EOF)
	// }

	// Replace loop with goto construct, so that this can be inlined
	// for i := z.c; i < blen; i++ {
	// 	if !accept.isset(z.b[i]) {
	// 		token = z.b[i]
	// 		i++
	// 		z.a -= (i - z.c)
	// 		z.c = i
	// 		return
	// 	}
	// }

	// i := z.c
LOOP:
	if i < uint(len(z.b)) {
		token = z.b[i]
		i++
		if accept.isset(token) {
			goto LOOP
		}
		// z.a -= (i - z.c)
		z.c = i
		return
	}
	// END:
	panic(io.EOF)
	// // z.a = 0
	// z.c = blen
	// return
}
234487
func (c *ConfigLocal) PrefetchStatus(ctx context.Context, tlfID tlf.ID,
	ptr data.BlockPointer) PrefetchStatus {
	dbc := c.DiskBlockCache()
	if dbc == nil {
		// We must be in testing mode, so check the block retrieval queue.
		bops, ok := c.BlockOps().(*BlockOpsStandard)
		if !ok {
			return NoPrefetch
		}
		status, err := bops.queue.getPrefetchStatus(ptr.ID)
		if err != nil {
			c.MakeLogger("").CDebugf(ctx,
				"Error getting prefetch status: %+v", err)
			return NoPrefetch
		}
		return status
	}

	prefetchStatus, err := dbc.GetPrefetchStatus(
		ctx, tlfID, ptr.ID, DiskBlockAnyCache)
	if err != nil {
		return NoPrefetch
	}
	return prefetchStatus
}
77098
func (s *Session) SetPrefetch(p float64) {
	s.mu.Lock()
	s.prefetch = p
	s.mu.Unlock()
}
348013
func (l *loop) Error() (status int, err error) {
	l.mux.Lock()
	defer l.mux.Unlock()
	status = l.status
	err = l.err
	return
}
407953
func multiErrorFromErrors(ch <-chan error) error {
	if ch == nil {
		return nil
	}
	ret := errors.MultiError(nil)
	for e := range ch {
		if e == nil {
			continue
		}
		ret = append(ret, e)
	}
	if len(ret) == 0 {
		return nil
	}
	return ret
}
827512
func (cache *diskBlockCacheWrapped) GetPrefetchStatus(
	ctx context.Context, tlfID tlf.ID, blockID kbfsblock.ID,
	cacheType DiskBlockCacheType) (prefetchStatus PrefetchStatus, err error) {
	cache.mtx.RLock()
	defer cache.mtx.RUnlock()

	// Try the sync cache first unless working set cache is required.
	if cacheType != DiskBlockWorkingSetCache {
		md, err := cache.syncCache.GetMetadata(ctx, blockID)
		switch errors.Cause(err) {
		case nil:
			return md.PrefetchStatus(), nil
		case ldberrors.ErrNotFound:
			if cacheType == DiskBlockSyncCache {
				// Try moving the block and getting it again.
				moved := cache.moveBetweenCachesLocked(
					ctx, tlfID, blockID, cacheType)
				if moved {
					md, err := cache.syncCache.GetMetadata(ctx, blockID)
					if err != nil {
						return NoPrefetch, err
					}
					return md.PrefetchStatus(), nil
				}
				return NoPrefetch, err
			}
			// Otherwise try the working set cache below.
		default:
			return NoPrefetch, err
		}
	}

	md, err := cache.workingSetCache.GetMetadata(ctx, blockID)
	if err != nil {
		return NoPrefetch, err
	}
	return md.PrefetchStatus(), nil
}
777262
func (s *Slot) finishFetch(completed bool, result interface{}, exp time.Time) {
	s.lock.Lock()
	defer s.lock.Unlock()
	s.fetching = false
	if completed {
		s.current = result
		s.exp = exp
	}
}
564598
func IsAdbServerErrorMatching(err error, predicate func(string) bool) bool {
	if err, ok := err.(*errors.Err); ok && err.Code == errors.AdbError {
		return predicate(err.Details.(ErrorResponseDetails).ServerMsg)
	}
	return false
}
847023
func (dbcm DiskBlockCacheMetadata) PrefetchStatus() PrefetchStatus {
	if dbcm.FinishedPrefetch {
		return FinishedPrefetch
	} else if dbcm.TriggeredPrefetch {
		return TriggeredPrefetch
	}
	return NoPrefetch
}
1265827
func scanForErrors(buf []byte) [][]byte {
	var res [][]byte
	for len(buf) > 0 {
		if buf[0] != '-' {
			return nil
		}
		buf = buf[1:]
		for i := 0; i < len(buf); i++ {
			if buf[i] == '\n' && i > 0 && buf[i-1] == '\r' {
				res = append(res, buf[:i-1])
				buf = buf[i+1:]
				break
			}
		}
	}
	return res
}