-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
one_shot.go
46 lines (35 loc) · 1.11 KB
/
one_shot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package colexec
import (
"context"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
)
// oneShotOp is an operator that does an arbitrary operation on the first batch
// that it gets, then deletes itself from the operator tree. This is useful for
// first-Next initialization that has to happen in an operator.
type oneShotOp struct {
OneInputNode
outputSourceRef *Operator
fn func(batch coldata.Batch)
}
var _ Operator = &oneShotOp{}
func (o *oneShotOp) Init() {
o.input.Init()
}
func (o *oneShotOp) Next(ctx context.Context) coldata.Batch {
batch := o.input.Next(ctx)
// Do our one-time work.
o.fn(batch)
// Swap out our output's input with our input, so we don't have to get called
// anymore.
*o.outputSourceRef = o.input
return batch
}