github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/search/init.go (about)

     1  // Copyright 2019 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package search
     6  
     7  import (
     8  	"context"
     9  	"path/filepath"
    10  
    11  	"github.com/keybase/client/go/kbfs/data"
    12  	"github.com/keybase/client/go/kbfs/libcontext"
    13  	"github.com/keybase/client/go/kbfs/libkbfs"
    14  	"github.com/keybase/client/go/logger"
    15  	"github.com/keybase/client/go/protocol/keybase1"
    16  )
    17  
    18  const (
    19  	indexStorageDir    = "kbfs_index"
    20  	bleveIndexDir      = "bleve"
    21  	bserverStorageDir  = "bserver"
    22  	mdserverStorageDir = "mdserver"
    23  
    24  	currentIndexVersion = "v1"
    25  
    26  	indexBlocksInCache = 100
    27  )
    28  
    29  // Params returns a set of default parameters for search-related
    30  // operations.
    31  func Params(kbCtx libkbfs.Context, storageRoot string, uid keybase1.UID) (
    32  	params libkbfs.InitParams, err error) {
    33  	params = libkbfs.DefaultInitParams(kbCtx)
    34  	params.Debug = true
    35  	params.LogToFile = true
    36  
    37  	params.EnableJournal = false
    38  	params.DiskCacheMode = libkbfs.DiskCacheModeOff
    39  
    40  	// Try to balance not using too much memory vs. the time/CPU it
    41  	// takes to keep pulling in index blocks from the disk.
    42  	params.CleanBlockCacheCapacity =
    43  		uint64(data.MaxBlockSizeBytesDefault) * indexBlocksInCache
    44  
    45  	// Make a per-user index for all the TLFs indexed locally by that
    46  	// user.  This means on one hand that the user can get
    47  	// high-quality, ranked search results across all TLFs, all
    48  	// encrypted just for that user; on the other hand, different
    49  	// users with access to the same synced TLFs will have to re-index
    50  	// them.
    51  	params.StorageRoot = filepath.Join(
    52  		storageRoot, indexStorageDir, currentIndexVersion, uid.String())
    53  
    54  	params.BServerAddr = "dir:" + filepath.Join(
    55  		params.StorageRoot, bserverStorageDir)
    56  	params.MDServerAddr = "dir:" + filepath.Join(
    57  		params.StorageRoot, mdserverStorageDir)
    58  	params.Mode = libkbfs.InitSingleOpWithQRString
    59  
    60  	return params, nil
    61  }
    62  
    63  // Init initializes a context and a libkbfs.Config for search
    64  // operations.  The config should be shutdown when it is done being
    65  // used.
    66  func Init(ctx context.Context, kbCtx libkbfs.Context, params libkbfs.InitParams,
    67  	keybaseServiceCn libkbfs.KeybaseServiceCn,
    68  	log logger.Logger, vlogLevel string) (
    69  	context.Context, libkbfs.Config, error) {
    70  	ctx, err := libcontext.NewContextWithCancellationDelayer(
    71  		libcontext.NewContextReplayable(
    72  			ctx, func(ctx context.Context) context.Context {
    73  				return ctx
    74  			}))
    75  	if err != nil {
    76  		return ctx, nil, err
    77  	}
    78  
    79  	config, err := libkbfs.InitWithLogPrefix(
    80  		ctx, kbCtx, params, keybaseServiceCn, nil, log, "search")
    81  	if err != nil {
    82  		return ctx, nil, err
    83  	}
    84  	config.SetVLogLevel(vlogLevel)
    85  
    86  	return ctx, config, nil
    87  }