github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/state/impl_query_processor_state.go (about)

     1  /*
     2   * Copyright (c) 2022-present unTill Pro, Ltd.
     3   */
     4  
     5  package state
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/voedger/voedger/pkg/isecrets"
    11  	"github.com/voedger/voedger/pkg/istructs"
    12  	"github.com/voedger/voedger/pkg/itokens"
    13  	"github.com/voedger/voedger/pkg/utils/federation"
    14  )
    15  
    16  type QPStateOptFunc func(opts *qpStateOpts)
    17  
    18  type qpStateOpts struct {
    19  	customHttpClient         IHttpClient
    20  	federationCommandHandler FederationCommandHandler
    21  }
    22  
    23  func QPWithCustomHttpClient(client IHttpClient) QPStateOptFunc {
    24  	return func(opts *qpStateOpts) {
    25  		opts.customHttpClient = client
    26  	}
    27  }
    28  
    29  func QPWithFedearationCommandHandler(handler FederationCommandHandler) QPStateOptFunc {
    30  	return func(opts *qpStateOpts) {
    31  		opts.federationCommandHandler = handler
    32  	}
    33  }
    34  
    35  func implProvideQueryProcessorState(ctx context.Context, appStructsFunc AppStructsFunc, partitionIDFunc PartitionIDFunc, wsidFunc WSIDFunc,
    36  	secretReader isecrets.ISecretReader, principalsFunc PrincipalsFunc, tokenFunc TokenFunc, itokens itokens.ITokens, argFunc ArgFunc, resultBuilderFunc ObjectBuilderFunc,
    37  	federation federation.IFederation, queryCallbackFunc ExecQueryCallbackFunc, options ...QPStateOptFunc) IHostState {
    38  
    39  	opts := &qpStateOpts{}
    40  	for _, optFunc := range options {
    41  		optFunc(opts)
    42  	}
    43  
    44  	bs := newHostState("QueryProcessor", queryProcessorStateMaxIntents, appStructsFunc)
    45  
    46  	bs.addStorage(View, newViewRecordsStorage(ctx, appStructsFunc, wsidFunc, nil), S_GET|S_GET_BATCH|S_READ)
    47  	bs.addStorage(Record, newRecordsStorage(appStructsFunc, wsidFunc, nil), S_GET|S_GET_BATCH)
    48  
    49  	bs.addStorage(WLog, &wLogStorage{
    50  		ctx:        ctx,
    51  		eventsFunc: func() istructs.IEvents { return appStructsFunc().Events() },
    52  		wsidFunc:   wsidFunc,
    53  	}, S_GET|S_READ)
    54  
    55  	bs.addStorage(Http, &httpStorage{
    56  		customClient: opts.customHttpClient,
    57  	}, S_READ)
    58  
    59  	bs.addStorage(FederationCommand, &federationCommandStorage{
    60  		appStructs: appStructsFunc,
    61  		wsid:       wsidFunc,
    62  		emulation:  opts.federationCommandHandler,
    63  		federation: federation,
    64  		tokens:     itokens,
    65  	}, S_GET)
    66  
    67  	bs.addStorage(AppSecret, &appSecretsStorage{secretReader: secretReader}, S_GET)
    68  
    69  	bs.addStorage(RequestSubject, &subjectStorage{
    70  		principalsFunc: principalsFunc,
    71  		tokenFunc:      tokenFunc,
    72  	}, S_GET)
    73  
    74  	bs.addStorage(QueryContext, &queryContextStorage{
    75  		argFunc:  argFunc,
    76  		wsidFunc: wsidFunc,
    77  	}, S_GET)
    78  
    79  	bs.addStorage(Response, &cmdResponseStorage{}, S_INSERT)
    80  
    81  	bs.addStorage(Result, newQueryResultStorage(appStructsFunc, resultBuilderFunc, queryCallbackFunc), S_INSERT)
    82  
    83  	return bs
    84  }