github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/load_stats.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package interlock
    15  
    16  import (
    17  	"context"
    18  	"encoding/json"
    19  
    20  	"github.com/whtcorpsinc/errors"
    21  	"github.com/whtcorpsinc/milevadb/petri"
    22  	"github.com/whtcorpsinc/milevadb/schemareplicant"
    23  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    24  	"github.com/whtcorpsinc/milevadb/statistics/handle"
    25  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    26  )
    27  
    28  var _ InterlockingDirectorate = &LoadStatsInterDirc{}
    29  
    30  // LoadStatsInterDirc represents a load statistic interlock.
    31  type LoadStatsInterDirc struct {
    32  	baseInterlockingDirectorate
    33  	info *LoadStatsInfo
    34  }
    35  
    36  // LoadStatsInfo saves the information of loading statistic operation.
    37  type LoadStatsInfo struct {
    38  	Path string
    39  	Ctx  stochastikctx.Context
    40  }
    41  
    42  // loadStatsVarKeyType is a dummy type to avoid naming defCauslision in context.
    43  type loadStatsVarKeyType int
    44  
    45  // String defines a Stringer function for debugging and pretty printing.
    46  func (k loadStatsVarKeyType) String() string {
    47  	return "load_stats_var"
    48  }
    49  
    50  // LoadStatsVarKey is a variable key for load statistic.
    51  const LoadStatsVarKey loadStatsVarKeyType = 0
    52  
    53  // Next implements the InterlockingDirectorate Next interface.
    54  func (e *LoadStatsInterDirc) Next(ctx context.Context, req *chunk.Chunk) error {
    55  	req.GrowAndReset(e.maxChunkSize)
    56  	if len(e.info.Path) == 0 {
    57  		return errors.New("Load Stats: file path is empty")
    58  	}
    59  	val := e.ctx.Value(LoadStatsVarKey)
    60  	if val != nil {
    61  		e.ctx.SetValue(LoadStatsVarKey, nil)
    62  		return errors.New("Load Stats: previous load stats option isn't closed normally")
    63  	}
    64  	e.ctx.SetValue(LoadStatsVarKey, e.info)
    65  	return nil
    66  }
    67  
    68  // Close implements the InterlockingDirectorate Close interface.
    69  func (e *LoadStatsInterDirc) Close() error {
    70  	return nil
    71  }
    72  
    73  // Open implements the InterlockingDirectorate Open interface.
    74  func (e *LoadStatsInterDirc) Open(ctx context.Context) error {
    75  	return nil
    76  }
    77  
    78  // UFIDelate uFIDelates the stats of the corresponding causet according to the data.
    79  func (e *LoadStatsInfo) UFIDelate(data []byte) error {
    80  	jsonTbl := &handle.JSONBlock{}
    81  	if err := json.Unmarshal(data, jsonTbl); err != nil {
    82  		return errors.Trace(err)
    83  	}
    84  	do := petri.GetPetri(e.Ctx)
    85  	h := do.StatsHandle()
    86  	if h == nil {
    87  		return errors.New("Load Stats: handle is nil")
    88  	}
    89  	return h.LoadStatsFromJSON(schemareplicant.GetSchemaReplicant(e.Ctx), jsonTbl)
    90  }