diff options
| author | Paul Buetow <paul@buetow.org> | 2021-09-12 19:01:34 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2021-10-02 12:26:29 +0300 |
| commit | 842fd5800000bb68d6306a9ecad80a98ed762a2f (patch) | |
| tree | 593e6c606f5eb3f8ef2634ab9ca7c267165ccdc8 | |
| parent | 16dc57e1e1c28e9d762424e596223a980770e059 (diff) | |
limit mapreduce table output to 10 rows by default
| -rw-r--r-- | TODO.md | 1 | ||||
| -rw-r--r-- | internal/clients/maprclient.go | 17 | ||||
| -rw-r--r-- | internal/mapr/globalgroupset.go | 4 | ||||
| -rw-r--r-- | internal/mapr/groupset.go | 8 |
4 files changed, 23 insertions, 7 deletions
@@ -10,6 +10,7 @@ This is a loose list of what to do. Maybe for the next releae or maybe for a lat [x] Paint ^CLIENT messages (e.g. use yellow backgrounds here) [x] Paint ^SERVER messages (e.g. use cyan backgrounds here) [x] Adjust dmap with color schemas +[x] Auto limit stdout map output to 10 results. [ ] Fix JSONSchema for the colors [ ] Implement Benchmark cat-ing a file and compare to prev version. [?] Client 4.x should print a warning when trying to connect to a 3.x server. diff --git a/internal/clients/maprclient.go b/internal/clients/maprclient.go index cab9a6c..2cad15d 100644 --- a/internal/clients/maprclient.go +++ b/internal/clients/maprclient.go @@ -158,12 +158,18 @@ func (c *MaprClient) reportResults() { func (c *MaprClient) printResults() { var result string var err error - var numRows int + var numRows, rowsLimit int + + if c.query.Limit == -1 { + // Limit output to 10 rows when the result is printed to stdout. + // This can be overriden with the limit clause though. + rowsLimit = 10 + } if c.cumulative { - result, numRows, err = c.globalGroup.Result(c.query) + result, numRows, err = c.globalGroup.Result(c.query, rowsLimit) } else { - result, numRows, err = c.globalGroup.SwapOut().Result(c.query) + result, numRows, err = c.globalGroup.SwapOut().Result(c.query, rowsLimit) } if err != nil { @@ -189,6 +195,11 @@ func (c *MaprClient) printResults() { config.Client.TermColors.MaprTable.RawQueryAttr) } logger.Raw(rawQuery) + + if rowsLimit > 0 && numRows > rowsLimit { + logger.Warn(fmt.Sprintf("Got %d results but limited output to %d rows! Use 'limit' clause to override!", + numRows, rowsLimit)) + } logger.Raw(result) } diff --git a/internal/mapr/globalgroupset.go b/internal/mapr/globalgroupset.go index 21bf990..50bac37 100644 --- a/internal/mapr/globalgroupset.go +++ b/internal/mapr/globalgroupset.go @@ -93,9 +93,9 @@ func (g *GlobalGroupSet) WriteResult(query *Query) error { } // Result returns the result of the mapreduce aggregation as a string. -func (g *GlobalGroupSet) Result(query *Query) (string, int, error) { +func (g *GlobalGroupSet) Result(query *Query, rowsLimit int) (string, int, error) { g.semaphore <- struct{}{} defer func() { <-g.semaphore }() - return g.GroupSet.Result(query) + return g.GroupSet.Result(query, rowsLimit) } diff --git a/internal/mapr/groupset.go b/internal/mapr/groupset.go index d29559a..9bff790 100644 --- a/internal/mapr/groupset.go +++ b/internal/mapr/groupset.go @@ -68,12 +68,16 @@ func (g *GroupSet) Serialize(ctx context.Context, ch chan<- string) { } // Result returns a nicely formated result of the query from the group set. -func (g *GroupSet) Result(query *Query) (string, int, error) { +func (g *GroupSet) Result(query *Query, rowsLimit int) (string, int, error) { rows, widths, err := g.result(query, true) if err != nil { return "", 0, err } + if query.Limit != -1 { + rowsLimit = query.Limit + } + sb := pool.BuilderBuffer.Get().(*strings.Builder) defer pool.RecycleBuilderBuffer(sb) @@ -141,7 +145,7 @@ func (g *GroupSet) Result(query *Query) (string, int, error) { // And now write the data for i, r := range rows { - if i == query.Limit { + if i == rowsLimit { break } for j, value := range r.values { |
