-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
856 lines (814 loc) · 21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
package main
import (
"bufio"
"encoding/gob"
"fmt"
"net"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/dhravya/radish/redisproto"
)
type DataType int
const (
StringType DataType = iota
ListType
HashType
SetType
SortedSetType
)
type sortedSetMember struct {
Member string
score float64
}
type KeyValueStore struct {
Strings map[string]string
Lists map[string][]string
Hashes map[string]map[string]string
Sets map[string]map[string]struct{}
SortedSets map[string][]sortedSetMember
Expirations map[string]time.Time
mu sync.RWMutex
CurrentTx *Transaction
totalCommandsProcessed int
connectedClients map[string]net.Conn
}
var pubsub = NewPubSub()
var persistence *Persistence
var serverStartTime = time.Now()
func init() {
gob.Register(map[string]string{})
gob.Register(map[string][]string{})
gob.Register(map[string]map[string]string{})
gob.Register(map[string]map[string]struct{}{})
gob.Register(map[string][]sortedSetMember{})
gob.Register(map[string]time.Time{})
gob.Register(sortedSetMember{})
}
func NewKeyValueStore() *KeyValueStore {
return &KeyValueStore{
Strings: make(map[string]string),
Lists: make(map[string][]string),
Hashes: make(map[string]map[string]string),
Sets: make(map[string]map[string]struct{}),
SortedSets: make(map[string][]sortedSetMember),
Expirations: make(map[string]time.Time),
totalCommandsProcessed: 0,
connectedClients: make(map[string]net.Conn),
}
}
func (kv *KeyValueStore) CommandHandler(command *redisproto.Command) string {
parts := make([]string, command.ArgCount())
for i := 0; i < command.ArgCount(); i++ {
parts[i] = string(command.Get(i))
}
parts[0] = strings.ToUpper(parts[0])
if parts[0] == "EXEC" {
return kv.CurrentTx.ExecCommand()
} else if parts[0] == "DISCARD" {
return kv.CurrentTx.DiscardCommand()
}
// Otherwise, add the command to the transaction queue
if kv.CurrentTx != nil {
kv.CurrentTx.Commands = append(kv.CurrentTx.Commands, command)
return "QUEUED"
} else {
return kv.executeCommand(parts)
}
}
func (kv *KeyValueStore) executeCommand(parts []string) string {
kv.totalCommandsProcessed++
fmt.Println("Command:", parts[0])
switch parts[0] {
case "INFO":
kv.mu.Lock()
defer kv.mu.Unlock()
// Calculate server uptime
uptimeSeconds := int(time.Since(serverStartTime).Seconds())
// Assuming you have variables tracking these metrics
totalCommandsProcessed := kv.totalCommandsProcessed
memoryUsage := runtime.MemStats{}
runtime.ReadMemStats(&memoryUsage)
connectedClients := len(kv.connectedClients) // Example of how you might track connected clients
// Building the INFO response
var infoBuilder strings.Builder
infoBuilder.WriteString("# Server\r\n")
infoBuilder.WriteString(fmt.Sprintf("uptime_in_seconds:%d\r\n", uptimeSeconds))
infoBuilder.WriteString(fmt.Sprintf("total_commands_processed:%d\r\n", totalCommandsProcessed))
infoBuilder.WriteString(fmt.Sprintf("used_memory:%d\r\n", memoryUsage.Alloc)) // Using Alloc as an example of memory usage
infoBuilder.WriteString(fmt.Sprintf("connected_clients:%d\r\n", connectedClients))
return infoBuilder.String()
case "LPUSH":
if len(parts) < 3 {
return "ERROR: LPUSH requires at least 2 arguments"
}
kv.mu.Lock()
defer kv.mu.Unlock()
key := parts[1]
values := parts[2:]
if _, exists := kv.Lists[key]; !exists {
kv.Lists[key] = make([]string, 0)
}
for i := len(values) - 1; i >= 0; i-- {
kv.Lists[key] = append([]string{values[i]}, kv.Lists[key]...)
}
return fmt.Sprintf("(integer) %d", len(kv.Lists[key]))
case "LPOP":
if len(parts) != 2 {
return "ERROR: LPOP requires 1 argument"
}
kv.mu.Lock()
defer kv.mu.Unlock()
key := parts[1]
if list, exists := kv.Lists[key]; exists && len(list) > 0 {
// Pop the first element
value := list[0]
kv.Lists[key] = list[1:]
return value
}
return "(nil)"
case "RPUSH":
if len(parts) < 3 {
return "ERR RPUSH requires at least 2 arguments"
}
kv.mu.Lock()
defer kv.mu.Unlock()
key := parts[1]
values := parts[2:]
if _, exists := kv.Lists[key]; !exists {
kv.Lists[key] = make([]string, 0)
}
kv.Lists[key] = append(kv.Lists[key], values...)
return fmt.Sprintf("(integer) %d", len(kv.Lists[key]))
case "RPOP":
if len(parts) != 2 {
return "ERR RPOP requires 1 argument"
}
kv.mu.Lock()
defer kv.mu.Unlock()
key := parts[1]
if list, exists := kv.Lists[key]; exists && len(list) > 0 {
value := list[len(list)-1]
kv.Lists[key] = list[:len(list)-1]
return value
}
return "(nil)"
case "LRANGE":
if len(parts) < 4 {
return "ERROR: LRANGE requires at least 3 arguments"
}
kv.mu.RLock()
defer kv.mu.RUnlock()
key := parts[1]
start, err := strconv.Atoi(parts[2])
if err != nil {
return "ERROR: LRANGE start index must be an integer"
}
end, err := strconv.Atoi(parts[3])
if err != nil {
return "ERROR: LRANGE end index must be an integer"
}
if _, exists := kv.Lists[key]; !exists {
return "ERROR: no such key"
}
if start < 0 {
start = len(kv.Lists[key]) + start
}
if end < 0 {
end = len(kv.Lists[key]) + end
}
if start > end || start >= len(kv.Lists[key]) {
return ""
}
if end >= len(kv.Lists[key]) {
end = len(kv.Lists[key]) - 1
}
return fmt.Sprintf("%v", strings.Join(kv.Lists[key][start : end+1], " "))
case "LLEN":
if len(parts) != 2 {
return "ERR LLEN requires 1 argument"
}
kv.mu.RLock()
defer kv.mu.RUnlock()
key := parts[1]
if list, exists := kv.Lists[key]; exists {
return fmt.Sprintf("(integer) %d", len(list))
}
return "(integer) 0"
case "HSET":
if len(parts) != 4 {
return "ERROR: HSET requires 3 arguments"
}
kv.mu.Lock()
defer kv.mu.Unlock()
if _, exists := kv.Hashes[parts[1]]; !exists {
kv.Hashes[parts[1]] = make(map[string]string)
}
kv.Hashes[parts[1]][parts[2]] = parts[3]
return "OK"
case "HGET":
if len(parts) != 3 {
return "ERROR: HGET requires 2 arguments"
}
kv.mu.RLock()
defer kv.mu.RUnlock()
if hashSet, exists := kv.Hashes[parts[1]]; exists {
if value, exists := hashSet[parts[2]]; exists {
return value
}
return "(nil)"
}
return "(nil)"
case "HMSET":
if len(parts) < 4 || len(parts)%2 != 0 {
return "ERR HMSET requires an even number of arguments >= 4"
}
kv.mu.Lock()
defer kv.mu.Unlock()
key := parts[1]
if _, exists := kv.Hashes[key]; !exists {
kv.Hashes[key] = make(map[string]string)
}
for i := 2; i < len(parts); i += 2 {
kv.Hashes[key][parts[i]] = parts[i+1]
}
return "OK"
case "HMGET":
if len(parts) < 3 {
return "ERR HMGET requires at least 2 arguments"
}
kv.mu.RLock()
defer kv.mu.RUnlock()
key := parts[1]
if hash, exists := kv.Hashes[key]; exists {
result := make([]string, 0)
for i := 2; i < len(parts); i++ {
if value, ok := hash[parts[i]]; ok {
result = append(result, value)
} else {
result = append(result, "(nil)")
}
}
return strings.Join(result, " ")
}
return "ERR no such key"
case "HGETALL":
if len(parts) != 2 {
return "ERR HGETALL requires 1 argument"
}
kv.mu.Lock()
defer kv.mu.Unlock()
key := parts[1]
if hash, exists := kv.Hashes[key]; exists {
result := make([]string, 0, len(hash)*2)
for field, value := range hash {
result = append(result, field, value)
}
return strings.Join(result, " ")
}
return "(empty hash)"
case "HDEL":
if len(parts) < 2 {
return "ERR HDEL requires at least 1 argument"
}
kv.mu.Lock()
defer kv.mu.Unlock()
key := parts[1]
if hash, exists := kv.Hashes[key]; exists {
count := 0
for i := 2; i < len(parts); i++ {
if _, ok := hash[parts[i]]; ok {
delete(hash, parts[i])
count++
}
}
return fmt.Sprintf("(integer) %d", count)
}
return "(integer) 0"
case "SET":
kv.mu.Lock()
defer kv.mu.Unlock()
if len(parts) != 3 {
return "ERROR: SET requires 2 arguments"
}
key, value := parts[1], parts[2]
kv.Strings[key] = value
return "OK"
case "GET":
if len(parts) != 2 {
return "ERROR: GET requires 1 argument"
}
kv.mu.RLock()
defer kv.mu.RUnlock()
key := parts[1]
if value, exists := kv.Strings[key]; exists {
return value
}
return "(nil)"
case "APPEND":
if len(parts) != 3 {
return "ERROR: APPEND requires 2 arguments"
}
kv.mu.Lock()
defer kv.mu.Unlock()
key, valueToAppend := parts[1], parts[2]
if value, exists := kv.Strings[key]; exists {
kv.Strings[key] = value + valueToAppend
} else {
kv.Strings[key] = valueToAppend
}
return "OK"
case "DEL":
if len(parts) < 2 {
return "ERROR: DEL requires at least 1 argument"
}
kv.mu.Lock()
defer kv.mu.Unlock()
count := 0
for _, key := range parts[1:] {
if _, exists := kv.Strings[key]; exists {
delete(kv.Strings, key)
count++
}
// Also, attempt to delete from other data structures
if _, exists := kv.Lists[key]; exists {
delete(kv.Lists, key)
count++
}
if _, exists := kv.Hashes[key]; exists {
delete(kv.Hashes, key)
count++
}
}
return fmt.Sprintf("(integer) %d", count)
case "EXISTS":
if len(parts) != 2 {
return "ERROR: EXISTS requires 1 argument"
}
kv.mu.RLock()
defer kv.mu.RUnlock()
key := parts[1]
_, existsInStrings := kv.Strings[key]
_, existsInLists := kv.Lists[key]
_, existsInHashes := kv.Hashes[key]
exists := existsInStrings || existsInLists || existsInHashes
if exists {
return "(integer) 1"
}
return "(integer) 0"
case "KEYS":
if len(parts) != 2 {
return "ERROR: KEYS requires 1 argument"
}
kv.mu.RLock()
defer kv.mu.RUnlock()
pattern := parts[1]
matchedKeys := ""
for key := range kv.Strings {
if strings.Contains(key, pattern) {
matchedKeys += key + " "
}
}
// Optionally, search in other data structures
return strings.TrimSpace(matchedKeys)
case "EXPIRE":
if len(parts) != 3 {
return "ERROR: EXPIRE requires 2 arguments"
}
kv.mu.RLock()
defer kv.mu.RUnlock()
key := parts[1]
seconds, err := strconv.Atoi(parts[2])
if err != nil {
return "ERROR: Invalid TTL value"
}
expirationTime := time.Now().Add(time.Duration(seconds) * time.Second)
kv.Expirations[key] = expirationTime
return "OK"
case "TTL":
if len(parts) != 2 {
return "ERROR: TTL requires 1 argument"
}
kv.mu.RLock()
defer kv.mu.RUnlock()
key := parts[1]
if expiration, exists := kv.Expirations[key]; exists {
if time.Now().Before(expiration) {
ttl := time.Until(expiration).Seconds()
return fmt.Sprintf("(integer) %d", int(ttl))
}
// Key expired, clean up
delete(kv.Expirations, key)
delete(kv.Strings, key) // Also consider cleaning up from other data structures
return "(integer) -2" // Indicate the key does not exist (expired)
}
return "(integer) -1"
case "SADD":
if len(parts) < 3 {
return "ERR SADD requires at least 2 arguments"
}
kv.mu.Lock()
defer kv.mu.Unlock()
key := parts[1]
if _, exists := kv.Sets[key]; !exists {
kv.Sets[key] = make(map[string]struct{})
}
count := 0
for i := 2; i < len(parts); i++ {
if _, ok := kv.Sets[key][parts[i]]; !ok {
kv.Sets[key][parts[i]] = struct{}{}
count++
}
}
return fmt.Sprintf("(integer) %d", count)
case "SMEMBERS":
if len(parts) != 2 {
return "ERR SMEMBERS requires 1 argument"
}
kv.mu.Lock()
defer kv.mu.Unlock()
key := parts[1]
if set, exists := kv.Sets[key]; exists {
members := make([]string, 0, len(set))
for member := range set {
members = append(members, member)
}
sort.Strings(members) // Sort the slice
return strings.Join(members, " ")
}
return "(empty set)"
case "SISMEMBER":
if len(parts) != 3 {
return "ERR SISMEMBER requires 2 arguments"
}
kv.mu.RLock()
defer kv.mu.RUnlock()
key := parts[1]
member := parts[2]
if set, exists := kv.Sets[key]; exists {
if _, ok := set[member]; ok {
return "(integer) 1"
}
}
return "(integer) 0"
case "SREM":
if len(parts) < 3 {
return "ERR SREM requires at least 2 arguments"
}
kv.mu.Lock()
defer kv.mu.Unlock()
key := parts[1]
if set, exists := kv.Sets[key]; exists {
count := 0
for i := 2; i < len(parts); i++ {
if _, ok := set[parts[i]]; ok {
delete(set, parts[i])
count++
}
}
return fmt.Sprintf("(integer) %d", count)
}
return "(integer) 0"
case "ZADD":
kv.mu.Lock()
defer kv.mu.Unlock()
key := parts[1]
if len(parts[2:])%2 != 0 {
return "ERR ZADD requires an even number of arguments"
}
newElements := 0
for i := 2; i < len(parts); i += 2 {
score, err := strconv.Atoi(parts[i])
if err != nil {
return "ERR ZADD invalid score"
}
member := parts[i+1]
exists := false
for _, existingMember := range kv.SortedSets[key] {
if existingMember.Member == member {
existingMember.score = float64(score)
exists = true
break
}
}
if !exists {
kv.SortedSets[key] = append(kv.SortedSets[key], sortedSetMember{member, float64(score)}) // Convert score to float64
newElements++
}
}
return fmt.Sprintf("(integer) %d", newElements)
case "ZRANGE":
if len(parts) != 4 {
return "ERR ZRANGE requires 3 arguments"
}
kv.mu.Lock()
defer kv.mu.Unlock()
key := parts[1]
start, err := strconv.Atoi(parts[2])
if err != nil {
return "ERR ZRANGE invalid start index"
}
stop, err := strconv.Atoi(parts[3])
if err != nil {
return "ERR ZRANGE invalid stop index"
}
if sortedSet, exists := kv.SortedSets[key]; exists {
// Adjusting start and stop for negative values
if start < 0 {
start = len(sortedSet) + start
}
if stop < 0 {
stop = len(sortedSet) + stop
}
// Ensuring start and stop are within bounds
if start < 0 {
start = 0
}
if stop >= len(sortedSet) {
stop = len(sortedSet) - 1
}
result := make([]string, 0)
for i := start; i <= stop && i < len(sortedSet); i++ {
result = append(result, sortedSet[i].Member)
}
return strings.Join(result, " ")
}
return "(empty sorted set)"
case "ZREM":
if len(parts) < 3 {
return "ERR ZREM requires at least 2 arguments"
}
kv.mu.Lock()
defer kv.mu.Unlock()
key := parts[1]
removed := 0
if sortedSet, exists := kv.SortedSets[key]; exists {
for i := 2; i < len(parts); i++ {
member := parts[i]
for j := 0; j < len(sortedSet); {
if sortedSet[j].Member == member {
// Remove by appending slices before and after the current index
sortedSet = append(sortedSet[:j], sortedSet[j+1:]...)
removed++
continue // Skip the increment step to stay at the same index
}
j++
}
}
kv.SortedSets[key] = sortedSet // Important to assign the modified slice back
return fmt.Sprintf("(integer) %d", removed)
}
return "(integer) 0"
case "MULTI":
tx, err := kv.MultiCommand()
if err != nil {
return err.Error()
}
kv.CurrentTx = tx
return "OK"
case "EXEC":
kv.mu.Lock()
defer kv.mu.Unlock()
if kv.CurrentTx == nil {
return "ERR EXEC without MULTI"
}
return kv.CurrentTx.ExecCommand()
case "DISCARD":
kv.mu.Lock()
defer kv.mu.Unlock()
if kv.CurrentTx == nil {
return "ERR DISCARD without MULTI"
}
return kv.CurrentTx.DiscardCommand()
case "SUBSCRIBE":
if len(parts) != 2 {
return "ERR SUBSCRIBE requires 1 argument"
}
kv.mu.RLock()
defer kv.mu.RUnlock()
channel := parts[1]
ch := pubsub.Subscribe(channel)
go func() {
for message := range ch {
// Handle received message
fmt.Printf("Received message on channel %s: %s\n", channel, message)
}
}()
return "OK"
case "PUBLISH":
if len(parts) != 3 {
return "ERR PUBLISH requires 2 arguments"
}
kv.mu.Lock()
defer kv.mu.Unlock()
channel, message := parts[1], parts[2]
count := pubsub.Publish(channel, message)
return fmt.Sprintf("(integer) %d", count)
case "UNSUBSCRIBE":
// Just close the channel to unsubscribe]
// pubsub.UnsubscribeAll()
// TODO: FIX THIS, just returning OK for now
return "OK"
case "PING":
return "PONG"
case "SHUTDOWN":
kv.mu.Lock()
defer kv.mu.Unlock()
return "OK"
case "SAVE":
kv.mu.Lock()
defer kv.mu.Unlock()
err := persistence.saveData()
if err != nil {
return "ERR " + err.Error()
}
return "OK"
case "BGSAVE":
kv.mu.Lock()
defer kv.mu.Unlock()
persistence.shouldSave = true
return "Background saving started"
case "INCR":
if len(parts) != 2 {
return "ERR INCR requires 1 argument"
}
key := parts[1]
kv.mu.Lock()
defer kv.mu.Unlock()
if value, exists := kv.Strings[key]; exists {
intValue, err := strconv.Atoi(value)
if err != nil {
return "ERR value is not an integer"
}
intValue++
kv.Strings[key] = strconv.Itoa(intValue)
return fmt.Sprintf("(integer) %d", intValue)
} else {
kv.Strings[key] = "1"
return "(integer) 1"
}
case "INCRBY":
// INCRBY key increment (increment is optional. Key is required, but does not need to exist)
if len(parts) < 3 {
return "ERR INCRBY requires at least 2 arguments"
}
key := parts[1]
increment, err := strconv.Atoi(parts[2])
if err != nil {
return "ERR value is not an integer"
}
kv.mu.Lock()
defer kv.mu.Unlock()
if value, exists := kv.Strings[key]; exists {
intValue, err := strconv.Atoi(value)
if err != nil {
return "ERR value is not an integer"
}
intValue += increment
kv.Strings[key] = strconv.Itoa(intValue)
return fmt.Sprintf("(integer) %d", intValue)
} else {
kv.Strings[key] = strconv.Itoa(increment)
return fmt.Sprintf("(integer) %d", increment)
}
case "DECRBY":
// DECRBY key decrement (decrement is optional. Key is required, but does not need to exist)
if len(parts) < 3 {
return "ERR DECRBY requires at least 2 arguments"
}
key := parts[1]
decrement, err := strconv.Atoi(parts[2])
if err != nil {
return "ERR value is not an integer"
}
kv.mu.Lock()
defer kv.mu.Unlock()
if value, exists := kv.Strings[key]; exists {
intValue, err := strconv.Atoi(value)
if err != nil {
return "ERR value is not an integer"
}
intValue -= decrement
kv.Strings[key] = strconv.Itoa(intValue)
return fmt.Sprintf("(integer) %d", intValue)
} else {
kv.Strings[key] = strconv.Itoa(-decrement)
return fmt.Sprintf("(integer) %d", -decrement)
}
case "DECR":
if len(parts) != 2 {
return "ERR DECR requires 1 argument"
}
key := parts[1]
kv.mu.Lock()
defer kv.mu.Unlock()
if value, exists := kv.Strings[key]; exists {
intValue, err := strconv.Atoi(value)
if err != nil {
return "ERR value is not an integer"
}
intValue--
kv.Strings[key] = strconv.Itoa(intValue)
return fmt.Sprintf("(integer) %d", intValue)
} else {
kv.Strings[key] = "-1"
return "(integer) -1"
}
case "MSET":
if len(parts) < 3 || len(parts)%2 != 1 {
return "ERR MSET requires an even number of arguments >= 3"
}
kv.mu.Lock()
defer kv.mu.Unlock()
for i := 1; i < len(parts); i += 2 {
kv.Strings[parts[i]] = parts[i+1]
}
return "OK"
case "MGET":
if len(parts) < 2 {
return "ERR MGET requires at least 1 argument"
}
kv.mu.Lock()
defer kv.mu.Unlock()
result := make([]string, 0)
for _, key := range parts[1:] {
if value, exists := kv.Strings[key]; exists {
result = append(result, value)
} else {
result = append(result, "(nil)")
}
}
return strings.Join(result, " ")
case "FLUSHALL":
kv.mu.Lock()
defer kv.mu.Unlock()
kv.Strings = make(map[string]string)
kv.Lists = make(map[string][]string)
kv.Hashes = make(map[string]map[string]string)
kv.Sets = make(map[string]map[string]struct{})
kv.SortedSets = make(map[string][]sortedSetMember)
kv.Expirations = make(map[string]time.Time)
kv.CurrentTx = nil
return "OK"
default:
return "ERR unknown command"
}
}
func handleConnection(conn net.Conn, kv *KeyValueStore) {
defer conn.Close()
kv.connectedClients[conn.RemoteAddr().String()] = conn
parser := redisproto.NewParser(conn)
writer := redisproto.NewWriter(bufio.NewWriter(conn))
for {
command, err := parser.ReadCommand()
if err != nil {
_, ok := err.(*redisproto.ProtocolError)
if ok {
ew := writer.WriteError(err.Error())
if ew != nil {
fmt.Println("Error writing response:", ew)
break
}
} else {
fmt.Println(err, "closed connection to", conn.RemoteAddr())
break
}
} else {
response := kv.CommandHandler(command)
if response != "" {
ew := writer.WriteBulkString(response)
if ew != nil {
fmt.Println("Error writing response:", ew)
break
}
}
}
if command.IsLast() {
writer.Flush()
}
}
}
func main() {
listener, err := net.Listen("tcp", ":6379")
kv := NewKeyValueStore()
if err != nil {
fmt.Println("Error listening:", err.Error())
return
}
defer listener.Close()
fmt.Println("Listening on :6379")
persistence = NewPersistence(kv, "data.gob")
kv = persistence.kv
go persistence.backgroundSave()
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
return
}
go handleConnection(conn, kv)
}
}