-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
234 lines (212 loc) · 4.54 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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
)
type DB struct {
data map[string]int
count map[int]int
deletedKey map[string]bool
deletedValue map[int]int
root bool // Just for debugging. Not used.
level int // Just for debugging. Not used.
parent *DB
}
func newDB() *DB {
return &DB{
data: make(map[string]int),
count: make(map[int]int),
deletedKey: make(map[string]bool),
deletedValue: make(map[int]int),
root: true,
level: 0,
parent: nil,
}
}
func (db *DB) Get(name string) (int, error) {
// recursively lookup upto the base node
for db != nil {
if db.deletedKey[name] {
return 0, errors.New("KEY NOT FOUND-NULL")
} else if _, exists := db.data[name]; exists {
return db.data[name], nil
}
db = db.parent
}
return 0, errors.New("KEY NOT FOUND-NULL")
}
func (db *DB) Delete(name string) {
valueFromBase, err := db.Get(name)
if err != nil {
fmt.Println(err)
return
}
db.deletedKey[name] = true
db.deletedValue[valueFromBase]++
}
func (db *DB) Set(name string, value int) {
// 1.check if it was deleted in current transaction
// 2. Check if exists in ANY transaction, if yes:
// decrement old value, increment new value, and update the value
if db.deletedKey[name] {
delete(db.deletedKey, name)
}
oldVal, err := db.Get(name)
if err == nil { // there is an old value
db.deletedValue[oldVal]++
}
db.count[value]++
db.data[name] = value
}
func (db *DB) Count(c int) int {
// recursively lookup upto the base node and keep adding
// while adding, make sure to subtract # of times the value was delete if db.delete() was called
totalCount := 0
for db != nil {
fmt.Println(totalCount, db.count[c], db.deletedValue[c])
totalCount = totalCount + db.count[c] - db.deletedValue[c]
db = db.parent
}
if totalCount < 0 {
return 0
}
return totalCount
}
func main() {
fmt.Println("In-memory DB")
db := newDB()
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("Enter command: ")
if !scanner.Scan() {
break
}
input := scanner.Text()
parts := strings.Fields(input)
if len(parts) == 0 {
continue
}
command := parts[0]
switch command {
case "SET":
if len(parts) != 3 {
fmt.Println("Usage: SET name value")
continue
}
name := parts[1]
var value int
fmt.Sscanf(parts[2], "%d", &value)
db.Set(name, value)
db.Log()
fmt.Println("OK")
case "GET":
if len(parts) != 2 {
fmt.Println("Usage: GET name")
continue
}
name := parts[1]
value, err := db.Get(name)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(value)
}
db.Log()
case "DELETE":
if len(parts) != 2 {
fmt.Println("Usage: DELETE name")
continue
}
name := parts[1]
db.Delete(name)
db.Log()
fmt.Println("OK")
case "COUNT":
if len(parts) != 2 {
fmt.Println("Usage: COUNT value")
continue
}
var value int
fmt.Sscanf(parts[1], "%d", &value)
count := db.Count(value)
fmt.Println(count)
case "BEGIN":
db = db.Begin()
db.Log()
fmt.Println("OK")
case "ROLLBACK":
var err error
db, err = db.Rollback()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("OK")
}
case "COMMIT":
db = db.Commit()
db.Log()
fmt.Println("OK")
case "END":
return
default:
fmt.Println("Unknown command")
}
}
}
func (db *DB) Log() {
for db != nil {
fmt.Println(db)
db = db.parent
}
}
func (db *DB) Begin() *DB {
return &DB{
data: make(map[string]int),
count: make(map[int]int),
deletedKey: make(map[string]bool),
deletedValue: make(map[int]int),
root: false,
level: db.level + 1,
parent: db,
}
}
func (db *DB) Rollback() (*DB, error) {
if db.parent != nil {
return db.parent, nil
}
return nil, errors.New("NO TRANSACTION")
}
func (db *DB) Commit() *DB {
if db.parent == nil {
return db
}
curr := db
for curr.parent != nil {
// 1. Deep merge all data to parent node
for k, v := range curr.data {
curr.parent.data[k] = v
}
// 2. Increment count of parent
for k, v := range db.count {
curr.parent.count[k] += v
}
// 3. Delete deleted keys. Also deep merge current deleted map
for k, v := range curr.deletedKey {
curr.parent.deletedKey[k] = v
delete(curr.parent.data, k)
}
// 4. Decrement count of parent
for k, v := range curr.deletedValue {
curr.parent.count[k] -= v
if curr.parent.count[k] <= 0 {
delete(curr.parent.count, k)
}
}
// fmt.Println(curr.parent)
curr = curr.parent
}
return curr
}