Skip to content

Commit a09b14c

Browse files
committed
Refactor code structure for improved readability and maintainability
1 parent 2a7db96 commit a09b14c

File tree

7 files changed

+99
-27
lines changed

7 files changed

+99
-27
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.exe
2+
*.rdb

dump.rdb

-191 Bytes
Binary file not shown.

internal/db/db.go

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,26 @@ var DefaultStore = NewStore()
4040
type HandlerFunc func(args []string) (string, error)
4141

4242
var Commands = map[string]HandlerFunc{
43-
"SET": setHandler,
44-
"GET": getHandler,
45-
"DEL": delHandler,
46-
"EXISTS": existsHandler,
47-
"LPUSH": lpushHandler,
48-
"RPUSH": rpushHandler,
49-
"LPOP": lpopHandler,
50-
"RPOP": rpopHandler,
51-
"LRANGE": lrangeHandler,
52-
"SADD": saddHandler,
53-
"SREM": sremHandler,
54-
"SMEMBERS": smembersHandler,
55-
"KEYS": keysHandler,
56-
"FLUSHDB": flushdbHandler,
57-
"INFO": infoHandler,
58-
"EXPIRE": expireHandler,
59-
"TTL": ttlHandler,
60-
"SAVE": snapshotHandler,
43+
"SET": setHandler,
44+
"GET": getHandler,
45+
"DEL": delHandler,
46+
"EXISTS": existsHandler,
47+
"LPUSH": lpushHandler,
48+
"RPUSH": rpushHandler,
49+
"LPOP": lpopHandler,
50+
"RPOP": rpopHandler,
51+
"LRANGE": lrangeHandler,
52+
"LGET": lgetHandler,
53+
"LLEN": llengHandler,
54+
"SADD": saddHandler,
55+
"SREM": sremHandler,
56+
"SGET": sgetHandler,
57+
"KEYS": keysHandler,
58+
"FLUSHDB": flushdbHandler,
59+
"INFO": infoHandler,
60+
"EXPIRE": expireHandler,
61+
"TTL": ttlHandler,
62+
"SAVE": snapshotHandler,
6163
}
6264

6365
func (s *Store) ttlCleaner() {
@@ -224,6 +226,34 @@ func lrangeHandler(args []string) (string, error) {
224226
return strings.Join(lst[start:end+1], ","), nil
225227
}
226228

229+
func lgetHandler(args []string) (string, error) {
230+
if len(args) < 1 {
231+
return "", fmt.Errorf("missing argument for LGET")
232+
}
233+
key := args[0]
234+
DefaultStore.mu.RLock()
235+
defer DefaultStore.mu.RUnlock()
236+
if DefaultStore.types[key] != ListType {
237+
return "", nil
238+
}
239+
lst := DefaultStore.data[key].([]string)
240+
return strings.Join(lst, ","), nil
241+
}
242+
243+
func llengHandler(args []string) (string, error) {
244+
if len(args) < 1 {
245+
return "", fmt.Errorf("missing argument for LLEN")
246+
}
247+
key := args[0]
248+
DefaultStore.mu.RLock()
249+
defer DefaultStore.mu.RUnlock()
250+
if DefaultStore.types[key] != ListType {
251+
return "0", nil
252+
}
253+
lst := DefaultStore.data[key].([]string)
254+
return fmt.Sprintf("%d", len(lst)), nil
255+
}
256+
227257
// Set commands
228258
func saddHandler(args []string) (string, error) {
229259
if len(args) < 2 {
@@ -270,9 +300,9 @@ func sremHandler(args []string) (string, error) {
270300
return fmt.Sprintf("%d", removed), nil
271301
}
272302

273-
func smembersHandler(args []string) (string, error) {
303+
func sgetHandler(args []string) (string, error) {
274304
if len(args) < 1 {
275-
return "", fmt.Errorf("missing argument for SMEMBERS")
305+
return "", fmt.Errorf("missing argument for SGET")
276306
}
277307
key := args[0]
278308
DefaultStore.mu.RLock()

internal/db/db_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,48 @@ func TestListCommands(t *testing.T) {
7171
}
7272
}
7373

74+
func TestNewListCommands(t *testing.T) {
75+
DefaultStore = NewStore()
76+
_, _ = lpushHandler([]string{"testlist", "a", "b", "c"}) // c, b, a
77+
78+
// Test LGET - get all elements
79+
out, _ := lgetHandler([]string{"testlist"})
80+
if out != "c,b,a" {
81+
t.Errorf("expected c,b,a, got %s", out)
82+
}
83+
84+
// Test LLEN - get length
85+
length, _ := llengHandler([]string{"testlist"})
86+
if length != "3" {
87+
t.Errorf("expected 3, got %s", length)
88+
}
89+
90+
// Test with non-existent list
91+
out, _ = lgetHandler([]string{"nonexistent"})
92+
if out != "" {
93+
t.Errorf("expected empty string for non-existent list, got %s", out)
94+
}
95+
96+
length, _ = llengHandler([]string{"nonexistent"})
97+
if length != "0" {
98+
t.Errorf("expected 0 for non-existent list, got %s", length)
99+
}
100+
}
101+
74102
func TestSetCommands(t *testing.T) {
75103
DefaultStore = NewStore()
76104
_, _ = saddHandler([]string{"myset", "a", "b", "c"})
77105
_, _ = sremHandler([]string{"myset", "b"})
78-
out, _ := smembersHandler([]string{"myset"})
106+
out, _ := sgetHandler([]string{"myset"})
79107
if !strings.Contains(out, "a") || !strings.Contains(out, "c") || strings.Contains(out, "b") {
80108
t.Errorf("expected a and c, not b; got %s", out)
81109
}
110+
111+
// Test SGET with non-existent set
112+
out2, _ := sgetHandler([]string{"nonexistent"})
113+
if out2 != "" {
114+
t.Errorf("expected empty string for non-existent set, got %s", out2)
115+
}
82116
}
83117

84118
func TestMetaCommands(t *testing.T) {
@@ -151,7 +185,7 @@ func TestSnapshotSaveLoad(t *testing.T) {
151185
if val != "snapval" {
152186
t.Errorf("expected snapval after load, got %s", val)
153187
}
154-
members, _ := smembersHandler([]string{"snapset"})
188+
members, _ := sgetHandler([]string{"snapset"})
155189
if !(strings.Contains(members, "a") && strings.Contains(members, "b")) {
156190
t.Errorf("expected set members a and b, got %s", members)
157191
}

internal/repl/repl.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ func printHelp() {
6868
LPOP k - Pop value from head of list
6969
RPOP k - Pop value from tail of list
7070
LRANGE k s e - Get list elements from s to e
71+
LGET k - Get all list elements
72+
LLEN k - Get list length
7173
SADD k v [v..] - Add value(s) to set
7274
SREM k v [v..] - Remove value(s) from set
73-
SMEMBERS k - List all set members
75+
SGET k - Get all set members
7476
KEYS - List all keys
7577
FLUSHDB - Clear the database
7678
INFO - Show server info

internal/script/script.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func EvalScript(script string) (string, error) {
3737
func getWhitelist() map[string]bool {
3838
return map[string]bool{
3939
"SET": true, "GET": true, "DEL": true, "EXISTS": true,
40-
"LPUSH": true, "RPUSH": true, "LPOP": true, "RPOP": true, "LRANGE": true,
41-
"SADD": true, "SREM": true, "SMEMBERS": true,
40+
"LPUSH": true, "RPUSH": true, "LPOP": true, "RPOP": true, "LRANGE": true, "LGET": true, "LLEN": true,
41+
"SADD": true, "SREM": true, "SGET": true,
4242
}
4343
}
4444

readme.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@
8383
| `LPOP k` | Pop value from head of list |
8484
| `RPOP k` | Pop value from tail of list |
8585
| `LRANGE k s e` | Get list elements from s to e |
86+
| `LGET k` | Get all list elements |
87+
| `LLEN k` | Get list length |
8688
| `SADD k v [v..]`| Add value(s) to set |
8789
| `SREM k v [v..]`| Remove value(s) from set |
88-
| `SMEMBERS k` | List all set members |
90+
| `SGET k` | Get all set members |
8991
| `KEYS` | List all keys |
9092
| `FLUSHDB` | Clear the database |
9193
| `INFO` | Show server info/stats |
@@ -114,12 +116,14 @@ t# mylist is now [b, a, c]
114116
LPOP mylist # returns b
115117
RPOP mylist # returns c
116118
LRANGE mylist 0 1 # returns a
119+
LGET mylist # returns all elements: a
120+
LLEN mylist # returns length: 1
117121
```
118122

119123
#### Set
120124
```
121125
SADD myset x y z
122-
SMEMBERS myset # returns x,y,z
126+
SGET myset # returns x,y,z
123127
SREM myset y
124128
SMEMBERS myset # returns x,z
125129
```

0 commit comments

Comments
 (0)