github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/processors/query/operator-counter-impl.go (about)

     1  /*
     2   * Copyright (c) 2021-present unTill Pro, Ltd.
     3   */
     4  
     5  package queryprocessor
     6  
     7  import (
     8  	"context"
     9  	"math"
    10  	"time"
    11  
    12  	"github.com/voedger/voedger/pkg/pipeline"
    13  )
    14  
    15  type CounterOperator struct {
    16  	pipeline.AsyncNOOP
    17  	startFrom int64
    18  	count     int64
    19  	counter   int64
    20  	limiter   int64
    21  	metrics   IMetrics
    22  }
    23  
    24  func newCounterOperator(startFrom, count int64, metrics IMetrics) pipeline.IAsyncOperator {
    25  	if count == 0 {
    26  		count = math.MaxInt
    27  	}
    28  	return &CounterOperator{
    29  		startFrom: startFrom,
    30  		count:     count,
    31  		metrics:   metrics,
    32  	}
    33  }
    34  
    35  func (o *CounterOperator) DoAsync(_ context.Context, work pipeline.IWorkpiece) (outWork pipeline.IWorkpiece, err error) {
    36  	begin := time.Now()
    37  	defer func() {
    38  		o.metrics.Increase(execCountSeconds, time.Since(begin).Seconds())
    39  	}()
    40  	if o.counter >= o.startFrom && o.limiter < o.count {
    41  		outWork = work
    42  		o.limiter += 1
    43  	}
    44  	o.counter += 1
    45  	if outWork == nil {
    46  		work.Release()
    47  	}
    48  	return
    49  }