// noteBlobIndexed checks if the recent indexing of br now allows the blobs that
// were depending on br, to be indexed in turn. If yes, they're reindexed
// asynchronously by indexReadyBlobs.
435128
func (ix *Index) noteBlobIndexed(br blob.Ref) {
	for _, needer := range ix.neededBy[br] {
		newNeeds := blobsFilteringOut(ix.needs[needer], br)
		if len(newNeeds) == 0 {
			ix.readyReindex[needer] = true
			delete(ix.needs, needer)
			ix.reindexWg.Add(1)
			go ix.indexReadyBlobs(context.Background())
		} else {
			ix.needs[needer] = newNeeds
		}
	}
	delete(ix.neededBy, br)
}
1023318
func (c *xContext) JSONBlob(b []byte, codes ...int) (err error) {
	c.response.Header().Set(HeaderContentType, MIMEApplicationJSONCharsetUTF8)
	err = c.Blob(b, codes...)
	return
}
863142
func (srs SignerRefSet) blobMatches(br blob.Ref) bool {
	for _, v := range srs {
		if br.EqualString(v) {
			return true
		}
	}
	return false
}
854425
func (zh *zipHandler) blobsFromDir(dirPath string, dirBlob blob.Ref) ([]*blobFile, error) {
	var list []*blobFile
	dr, err := schema.NewDirReader(context.TODO(), zh.fetcher, dirBlob)
	if err != nil {
		return nil, fmt.Errorf("Could not read dir blob %v: %v", dirBlob, err)
	}
	ent, err := dr.Readdir(context.TODO(), -1)
	if err != nil {
		return nil, fmt.Errorf("Could not read dir entries: %v", err)
	}
	for _, v := range ent {
		fullpath := path.Join(dirPath, v.FileName())
		switch v.CamliType() {
		case "file":
			list = append(list, &blobFile{v.BlobRef(), fullpath})
		case "directory":
			children, err := zh.blobsFromDir(fullpath, v.BlobRef())
			if err != nil {
				return nil, fmt.Errorf("Could not get list of blobs from %v: %v", v.BlobRef(), err)
			}
			list = append(list, children...)
		}
	}
	return list, nil
}
1102151
func WriteIndex(w io.Writer, idx *Index, typ string) error {
	idx.sort()
	err := writeIndices(w, idx.Refs, typ)
	if err != nil {
		return err
	}
	if idx.Unmapped != nil {
		err = binary.Write(w, binary.LittleEndian, *idx.Unmapped)
	}
	return err
}
366266
func (buh *blobUploadHandler) PatchBlobData(w http.ResponseWriter, r *http.Request) {
	if buh.Upload == nil {
		buh.Errors = append(buh.Errors, v2.ErrorCodeBlobUploadUnknown)
		return
	}

	ct := r.Header.Get("Content-Type")
	if ct != "" && ct != "application/octet-stream" {
		buh.Errors = append(buh.Errors, errcode.ErrorCodeUnknown.WithDetail(fmt.Errorf("bad Content-Type")))
		// TODO(dmcgowan): encode error
		return
	}

	// TODO(dmcgowan): support Content-Range header to seek and write range

	if err := copyFullPayload(buh, w, r, buh.Upload, -1, "blob PATCH"); err != nil {
		buh.Errors = append(buh.Errors, errcode.ErrorCodeUnknown.WithDetail(err.Error()))
		return
	}

	if err := buh.blobUploadResponse(w, r, false); err != nil {
		buh.Errors = append(buh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
		return
	}

	w.WriteHeader(http.StatusAccepted)
}
589253
func (odr *LesOdr) SetIndexers(chtIndexer, bloomTrieIndexer, bloomIndexer *core.ChainIndexer) {
	odr.chtIndexer = chtIndexer
	odr.bloomTrieIndexer = bloomTrieIndexer
	odr.bloomIndexer = bloomIndexer
}
1128691
func (h *Holder) CreateIndexIfNotExists(name string, opt IndexOptions) (*Index, error) {
	h.mu.Lock()
	defer h.mu.Unlock()

	// Find index in cache first.
	if index := h.indexes[name]; index != nil {
		return index, nil
	}

	return h.createIndex(name, opt)
}
91092
func (i *indexImpl) IndexAdvanced(doc *document.Document) (err error) {
	if doc.ID == "" {
		return ErrorEmptyID
	}

	i.mutex.RLock()
	defer i.mutex.RUnlock()

	if !i.open {
		return ErrorIndexClosed
	}

	err = i.i.Update(doc)
	return
}
1016411
func (ds *Storage) readBlobs(ctx context.Context, opts readBlobRequest) error {
	dirFullPath := filepath.Join(opts.dirRoot, opts.pathInto)
	names, err := ds.fs.ReadDirNames(dirFullPath)
	if err != nil {
		return &enumerateError{"readdirnames of " + dirFullPath, err}
	}
	if len(names) == 0 {
		// remove empty blob dir if we are in a queue but not the queue root itself
		if strings.Contains(dirFullPath, "queue-") &&
			!strings.Contains(filepath.Base(dirFullPath), "queue-") {
			go ds.tryRemoveDir(dirFullPath)
		}
		return nil
	}
	sort.Strings(names)
	stat := make(map[string]*future) // name -> future<os.FileInfo>

	var toStat []func()
	for _, name := range names {
		if skipDir(name) || isShardDir(name) {
			continue
		}
		fullFile := filepath.Join(dirFullPath, name)
		f := newFuture(func() (os.FileInfo, error) {
			fi, err := ds.fs.Stat(fullFile)
			if err != nil {
				return nil, &enumerateError{"stat", err}
			}
			return fi, nil
		})
		stat[name] = f
		toStat = append(toStat, f.ForceLoad)
	}

	// Start pre-statting things.
	go func() {
		for _, f := range toStat {
			ds.statGate.Start()
			f := f
			go func() {
				ds.statGate.Done()
				f()
			}()
		}
	}()

	for _, name := range names {
		if *opts.remain == 0 {
			return nil
		}
		if skipDir(name) {
			continue
		}

		isDir := isShardDir(name)
		if !isDir {
			fi, err := stat[name].Get()
			if err != nil {
				return err
			}
			isDir = fi.IsDir()
		}

		if isDir {
			var newBlobPrefix string
			if opts.blobPrefix == "" {
				newBlobPrefix = name + "-"
			} else {
				newBlobPrefix = opts.blobPrefix + name
			}
			if len(opts.after) > 0 {
				compareLen := len(newBlobPrefix)
				if len(opts.after) < compareLen {
					compareLen = len(opts.after)
				}
				if newBlobPrefix[:compareLen] < opts.after[:compareLen] {
					continue
				}
			}
			ropts := opts
			ropts.blobPrefix = newBlobPrefix
			ropts.pathInto = opts.pathInto + "/" + name
			if err := ds.readBlobs(ctx, ropts); err != nil {
				return err
			}
			continue
		}

		if !strings.HasSuffix(name, ".dat") {
			continue
		}

		fi, err := stat[name].Get()
		if err != nil {
			return err
		}

		if !fi.IsDir() {
			blobName := strings.TrimSuffix(name, ".dat")
			if blobName <= opts.after {
				continue
			}
			if blobRef, ok := blob.Parse(blobName); ok {
				select {
				case opts.ch <- blob.SizedRef{Ref: blobRef, Size: uint32(fi.Size())}:
					(*opts.remain)--
				case <-ctx.Done():
					return ctx.Err()
				}
			}
		}
	}

	return nil
}
685244
func (c *IO) ResolveBlob(objectId string) (string, error) {
	var v IOResolveBlobParams
	v.ObjectId = objectId
	return c.ResolveBlobWithParams(&v)
}
883942
func (db *DB) AddExpectedQueryAtIndex(index int, query string, err error) {
	db.AddExpectedExecuteFetchAtIndex(index, ExpectedExecuteFetch{
		Query:       query,
		QueryResult: &sqltypes.Result{},
		Error:       err,
	})
}
737598
func (c *Storage) TrackIndexedDBForOriginWithParams(v *StorageTrackIndexedDBForOriginParams) (*gcdmessage.ChromeResponse, error) {
	return gcdmessage.SendDefaultRequest(c.target, c.target.GetSendCh(), &gcdmessage.ParamRequest{Id: c.target.GetId(), Method: "Storage.trackIndexedDBForOrigin", Params: v})
}
1220458
func (s *storage) RemoveBlobs(ctx context.Context, blobs []blob.Ref) error {
	batch := s.index.BeginBatch()
	var wg syncutil.Group
	for _, br := range blobs {
		br := br
		removeGate.Start()
		batch.Delete(br.String())
		wg.Go(func() error {
			defer removeGate.Done()
			if err := s.delete(br); err != nil && err != os.ErrNotExist {
				return err
			}
			return nil
		})
	}
	err1 := wg.Err()
	err2 := s.index.CommitBatch(batch)
	if err1 != nil {
		return err1
	}
	return err2
}
1233687
func (s *Storage) BlobContents(br blob.Ref) (contents string, ok bool) {
	s.mu.RLock()
	defer s.mu.RUnlock()
	b, ok := s.m[br]
	if !ok {
		return
	}
	return string(b), true
}
46099
func NewChtIndexer(db ethdb.Database, odr OdrBackend, size, confirms uint64) *core.ChainIndexer {
	trieTable := rawdb.NewTable(db, ChtTablePrefix)
	backend := &ChtIndexerBackend{
		diskdb:      db,
		odr:         odr,
		trieTable:   trieTable,
		triedb:      trie.NewDatabaseWithCache(trieTable, 1), // Use a tiny cache only to keep memory down
		sectionSize: size,
	}
	return core.NewChainIndexer(db, rawdb.NewTable(db, "chtIndexV2-"), backend, size, confirms, time.Millisecond*100, "cht")
}
501852
func (s *SearchSource) IndexBoost(index string, boost float64) *SearchSource {
	s.indexBoosts[index] = boost
	return s
}