/
gocache.go
155 lines (139 loc) · 2.78 KB
/
gocache.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Store map[string]string
type Count map[string]int
type Block struct {
store Store
count Count
}
// Store the current values
var store = Store{}
// Store counts for fast NUMEQUALTO
var count = Count{}
// Store previous values/counts for each block, so they can be rolled back
var blocks = []*Block{}
/*
Utility
*/
func cache(key string, oldVal string, newVal string) {
if len(blocks) > 0 {
currBlock := *blocks[len(blocks)-1]
// Make sure not to overwrite the stored value
if _, found := currBlock.store[key]; !found {
currBlock.store[key] = oldVal
}
// Empty string values are always found, so will never be cached
if _, found := currBlock.count[oldVal]; !found {
currBlock.count[oldVal] = count[oldVal]
}
if _, found := currBlock.count[newVal]; !found {
currBlock.count[newVal] = count[newVal]
}
}
}
/*
Operations
*/
func set(key string, val string) {
if key != "" && val != "" {
oldVal := store[key]
cache(key, oldVal, val)
store[key] = val
if count[oldVal] > 0 {
count[oldVal] = count[oldVal] - 1
}
count[val] = count[val] + 1
}
}
func unset(key string) {
if key != "" {
oldVal := store[key]
cache(key, oldVal, "")
delete(store, key)
if count[oldVal] > 0 {
count[oldVal] = count[oldVal] - 1
}
}
}
func get(key string) {
if key != "" {
if val, ok := store[key]; ok && val != "" {
fmt.Println(val)
} else {
fmt.Println("NULL")
}
}
}
func begin() {
blocks = append(blocks, &Block{Store{}, Count{}})
}
func rollback() {
if len(blocks) > 0 {
currBlock := *blocks[len(blocks)-1]
for sk, sv := range currBlock.store {
store[sk] = sv
}
for ck, cv := range currBlock.count {
count[ck] = cv
}
blocks = blocks[:len(blocks)-1]
} else {
fmt.Println("NO TRANSACTION")
}
}
func commit() {
if len(blocks) > 0 {
blocks = []*Block{}
} else {
fmt.Println("NO TRANSACTION")
}
}
/*
Command line
*/
func parseArgs(args []string) (cmd string, key string, val string) {
pos := [2]string{}
copy(pos[:], args[1:])
cmd = strings.ToUpper(args[0])
key, val = strings.TrimSpace(pos[0]), strings.TrimSpace(pos[1])
return cmd, key, val
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
Loop:
for scanner.Scan() {
args := strings.SplitN(scanner.Text(), " ", 3)
cmd, key, val := parseArgs(args)
switch cmd {
case "SET":
set(key, val)
case "UNSET":
unset(key)
case "GET":
get(key)
case "NUMEQUALTO", "NEQ":
fmt.Println(count[key])
case "BEGIN":
begin()
case "ROLLBACK":
rollback()
case "COMMIT":
commit()
case "END":
break Loop
case "":
continue Loop
default:
fmt.Println("INVALID COMMAND")
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "I/0 ERROR:", err)
}
fmt.Println("HAVE A NICE DAY")
}