Skip to content

Commit

Permalink
Added command show limit.
Browse files Browse the repository at this point in the history
closes #6
  • Loading branch information
liderman committed Jan 15, 2022
1 parent 8656897 commit 79d7895
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ Displays all values, the keys of which are in the range between "START" and "LIM
* `LIMIT` - The key or key prefix indicating the end of the range
* `FORMAT` - Data Display Format (Optional)

> show limit `LIMIT` [`FORMAT`]
Displays all content of the database limited by "LIMIT".
* `LIMIT` - Limiting the number of records displayed
* `FORMAT` - Data Display Format (Optional)

#### The list of formats available to display
* `raw` - Raw data without processing (default)
* `bson` - Attempts to convert the data to be displayed from `bson` to `json`
Expand Down
28 changes: 27 additions & 1 deletion commands/showCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func ShowByPrefix(prefix, format string) string {
return showByIterator(
dbh.NewIterator(util.BytesPrefix([]byte(prefix)), nil),
format,
0,
)
}

Expand All @@ -45,13 +46,31 @@ func ShowByRange(start, limit, format string) string {
return showByIterator(
dbh.NewIterator(&util.Range{Start: []byte(start), Limit: []byte(limit)}, nil),
format,
0,
)
}

// ShowLimit It shows all content of the database limited by the limit.
// Use the field `format` for specifying the display format of data.
// The list of possible values of format options: raw (default), geohash, bson, int64, float64
//
// Returns a string containing information about the result of the operation.
func ShowLimit(limit int, format string) string {
if !isConnected {
return AppError(ErrDbDoesNotOpen)
}

return showByIterator(
dbh.NewIterator(nil, nil),
format,
limit,
)
}

// Show by iterator
//
// Returns a string containing information about the result of the operation.
func showByIterator(iter iterator.Iterator, format string) string {
func showByIterator(iter iterator.Iterator, format string, limit int) string {
if iter.Error() != nil {
return "Empty result!"
}
Expand All @@ -62,11 +81,18 @@ func showByIterator(iter iterator.Iterator, format string) string {

w.Init(writer, 0, 8, 0, '\t', 0)
fmt.Fprintln(w, "Key\t| Value")

count := 0
for iter.Next() {
key := iter.Key()
value := iter.Value()

fmt.Fprintf(w, "%s\t| %s\n", string(key), cliutil.ToString(format, value))

count++
if limit != 0 && count >= limit {
break
}
}

w.Flush()
Expand Down
24 changes: 22 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func main() {
// Command: show
case args[0] == "show":
if len(args) == 1 {
fmt.Println("Bad format. Please use 'show prefix|range'")
fmt.Println("Bad format. Please use 'show prefix|range|limit'")
break
}

Expand Down Expand Up @@ -118,8 +118,28 @@ func main() {

fmt.Println(commands.ShowByPrefix(args[2], format))
break
// Sub-command: limit
case "limit":
if len(args) < 3 || len(args) > 4 {
fmt.Println("Bad format. Please use 'show limit LIMIT [FORMAT]'")
break
}

limit, err := strconv.Atoi(args[2])
if err != nil {
fmt.Println("Bad LIMIT value. LIMIT must be a number")
break
}

format := ""
if len(args) == 4 {
format = args[3]
}

fmt.Println(commands.ShowLimit(limit, format))
break
default:
fmt.Println("Bad format. Please use 'show prefix|range'")
fmt.Println("Bad format. Please use 'show prefix|range|limit'")
}

break
Expand Down

0 comments on commit 79d7895

Please sign in to comment.