-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement vector head operator (#5227)
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package op | ||
|
||
import ( | ||
"github.com/brimdata/zed/vector" | ||
) | ||
|
||
type Head struct { | ||
parent vector.Puller | ||
limit, count int | ||
} | ||
|
||
func NewHead(parent vector.Puller, limit int) *Head { | ||
return &Head{ | ||
parent: parent, | ||
limit: limit, | ||
} | ||
} | ||
|
||
func (h *Head) Pull(done bool) (vector.Any, error) { | ||
if h.count >= h.limit { | ||
// If we are at limit we already sent a done upstream, | ||
// so for either sense of the done flag, we return EOS | ||
// and reset our state. | ||
h.count = 0 | ||
return nil, nil | ||
} | ||
if done { | ||
if _, err := h.parent.Pull(true); err != nil { | ||
return nil, err | ||
} | ||
h.count = 0 | ||
return nil, nil | ||
} | ||
again: | ||
vec, err := h.parent.Pull(false) | ||
if vec == nil || err != nil { | ||
h.count = 0 | ||
return nil, err | ||
} | ||
remaining := h.limit - h.count | ||
if remaining <= 0 { | ||
goto again | ||
} | ||
if n := int(vec.Len()); n < remaining { | ||
// This vector has fewer than the needed records. | ||
// Send them all downstream and update the count. | ||
h.count += n | ||
return vec, nil | ||
} | ||
// This vector has more than the needed records. | ||
// Signal to the parent that we are done. | ||
if _, err := h.parent.Pull(true); err != nil { | ||
return nil, err | ||
} | ||
h.count = h.limit | ||
index := make([]uint32, remaining) | ||
for k := range index { | ||
index[k] = uint32(k) | ||
} | ||
return vector.NewView(index, vec), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
script: | | ||
zq -o t.vng -f vng - | ||
zed dev vector query -z "head" t.vng | ||
echo // | ||
zed dev vector query -z "head 2" t.vng | ||
inputs: | ||
- name: stdin | ||
data: | | ||
1 | ||
2 | ||
3 | ||
outputs: | ||
- name: stdout | ||
data: | | ||
1 | ||
// | ||
1 | ||
2 |