-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
214 lines (182 loc) · 4.33 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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
)
type ops interface {
}
type write struct {
data string
}
type deleteCommand struct {
}
type transaction struct {
parent *transaction
transactionLog map[string]ops
}
type root struct {
db map[string]string
transactions *transaction
}
func repl(root *root) bool {
var quit bool
fmt.Println("Please enter a command:")
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
root.parseCommand(input)
return quit
}
func newRoot() root {
return root{transactions: nil, db: make(map[string]string)}
}
func newTransaction(passedtransaction *transaction) transaction {
var t transaction
if passedtransaction != nil {
t = transaction{parent: passedtransaction, transactionLog: make(map[string]ops)}
} else {
t = transaction{parent: nil, transactionLog: make(map[string]ops)}
return t
}
return t
}
func (r *root) write(key string, value string) {
if r.transactions != nil {
r.transactions.transactionLog[key] = write{data: value}
} else {
r.db[key] = value
}
}
func (r *root) deleteFunction(key string) {
if r.transactions != nil {
r.transactions.transactionLog[key] = deleteCommand{}
} else {
delete(r.db, key)
}
}
func (r *root) read(key string) (string, error) {
currentTransaction := r.transactions
for currentTransaction != nil {
if _, ok := currentTransaction.transactionLog[key]; ok {
switch currentTransaction.transactionLog[key].(type) {
default:
return "", errors.New("Do not recognize command, delete and write are recognized transactions")
case deleteCommand:
return "", errors.New("We have deleted that key")
case write:
return currentTransaction.transactionLog[key].(write).data, nil
// fmt.Println(displayValue, " this is the data in the write struct")
}
} else {
currentTransaction = currentTransaction.parent
}
}
if value, ok := r.db[key]; ok {
return value, nil
}
return "", errors.New("That key was not found")
}
func (r *root) start() {
var newestTransaction transaction
if r.transactions != nil {
newestTransaction = newTransaction(r.transactions)
} else {
newestTransaction = newTransaction(nil)
}
r.transactions = &newestTransaction
}
func (r *root) abort() error {
if r.transactions == nil {
return errors.New("You do not have a transaction open at this time")
}
r.transactions = r.transactions.parent
return nil
}
func (r *root) commit() {
if r.transactions == nil {
return
}
if r.transactions.parent == nil {
for key, value := range r.transactions.transactionLog {
switch value.(type) {
case write:
r.db[key] = value.(write).data
case deleteCommand:
delete(r.db, key)
}
// I've already confirmed that there is no open parent transaction so at this point I reset the head to nil
r.transactions = nil
}
} else {
for key, value := range r.transactions.transactionLog {
r.transactions.parent.transactionLog[key] = value
}
// I've already checked that r.transactions.parent exists and now im resetting the head
r.transactions = r.transactions.parent
}
}
func (r *root) parseCommand(input string) {
input = strings.ToLower(input)
command := strings.Fields(input)
switch command[0] {
default:
fmt.Println("That is not a valid command")
case "read":
if len(command) == 2 {
value, err := r.read(command[1])
if err != nil {
fmt.Println(err)
} else {
fmt.Println(value)
}
} else {
fmt.Println("That is not a valid command")
}
case "write":
if len(command) == 3 {
r.write(command[1], command[2])
} else {
err := errors.New("You have not supplied enough arguments for the WRITE command")
fmt.Println(err)
}
case "delete":
if len(command) == 2 {
r.deleteFunction(command[1])
} else {
fmt.Println("That is not a valid command")
}
case "start":
if len(command) != 1 {
fmt.Println("that is not a valid command")
} else {
r.start()
}
case "abort":
if len(command) != 1 {
fmt.Println("that is not a valid command")
} else {
r.abort()
}
case "commit":
if len(command) != 1 {
fmt.Println("that is not a valid command")
} else {
r.commit()
}
case "quit":
if len(command) != 1 {
fmt.Println("that is not a valid command")
} else {
fmt.Println("Exiting")
os.Exit(0)
}
}
}
func main() {
datastore := newRoot()
for {
repl(&datastore)
}
}