Skip to content

Commit d988b93

Browse files
committed
added info command
1 parent fce837d commit d988b93

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ set name sojeb
3434
- HDEL
3535
- HLEN
3636
- FLUSHALL
37-
- DEL
37+
- DEL
38+
- INFO

handler.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"sync"
56
)
67

@@ -15,6 +16,7 @@ var Handlers = map[string]func([]Value) Value{
1516
"HLEN": hlen,
1617
"FLUSHALL": flushall,
1718
"DEL": del,
19+
"INFO": info,
1820
}
1921

2022
func ping(args []Value) Value {
@@ -194,3 +196,29 @@ func del(args []Value) Value {
194196

195197
return Value{typ: "string", str: "OK"}
196198
}
199+
200+
// info command which shows stats about the server
201+
func info(args []Value) Value {
202+
if len(args) != 0 {
203+
return Value{typ: "error", str: "ERR wrong number of arguments for 'info' command"}
204+
}
205+
206+
SETsMu.RLock()
207+
totalKeys := len(SETs)
208+
SETsMu.RUnlock()
209+
210+
HSETsMu.RLock()
211+
totalHashes := len(HSETs)
212+
totalFields := 0
213+
for _, m := range HSETs {
214+
totalFields += len(m)
215+
}
216+
HSETsMu.RUnlock()
217+
218+
infoStr := fmt.Sprintf(
219+
"# Server Info\nkeys:%d\nhashes:%d\nfields:%d\n",
220+
totalKeys, totalHashes, totalFields,
221+
)
222+
223+
return Value{typ: "bulk", bulk: infoStr}
224+
}

0 commit comments

Comments
 (0)