-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
275 lines (219 loc) · 6.52 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
package main
import (
"fmt"
"os"
"slices"
"strings"
"github.com/tidwall/btree"
)
// Scaffolding
func assert(b bool, msg string) {
if !b {
panic(msg)
}
}
func assertEq[C comparable](a C, b C, prefix string) {
if a != b {
panic(fmt.Sprintf("%s '%v' != '%v'", prefix, a, b))
}
}
var DEBUG = slices.Contains(os.Args, "--debug")
func debug(a ...any) {
if !DEBUG {
return
}
args := append([]any{"[DEBUG] "}, a...)
fmt.Println(args...)
}
// A DB value will have a start and end TX ID (TODO: Needs clarification on start and end).
type Value struct {
txStartID uint64
txEndID uint64
value string
}
type TransactionState uint8
// A transaction can be in one of the 3 following states.
const (
InProgressTransaction TransactionState = iota
AbortedTransaction
CommittedTransaction
)
type IsolationLevel uint8
// We want to support the following major isolation levels, ordered in increasing strictness.
const (
ReadUncommittedIsolation IsolationLevel = iota
ReadCommittedIsolation
RepeatableReadIsolation
SnapshotIsolation
SerializableIsolation
)
// A Transaction will have an ID (monoticially increasing), a known isolation level and a transaction state.
type Transaction struct {
id uint64
isolation IsolationLevel
state TransactionState
// A list of inProgress transactions.
//
// In my understanding so far, this will provide this transaction with a "view" into the
// currently running transactions and possibly help decide if it wants to wait for another
// transaction or not before reading or writing data.
// Used only by Repeatable Read and stricter isolation levels.
inProgress btree.Set[uint64]
// Used only by Snapshot Isolation and stricter isolation levels.
// TODO: Understand what this is used for.
writeset btree.Set[string]
readset btree.Set[string]
}
type Database struct {
// By default, this is the isolation level that will be applied on all transsactions.
defaultIsolation IsolationLevel
// The store is a key-value store and stores newer values in an append-only (TODO: verify this) slice.
store map[string][]Value
// An ordered list of transactions IDs
transactions btree.Map[uint64, Transaction]
// The next available transaction ID.
nextTransactionID uint64
}
func newDatabase() Database {
return Database{
defaultIsolation: ReadCommittedIsolation,
store: map[string][]Value{},
// Transaction ID '0' will imply that the ID was not set.
// '1' is the first valid Tx ID.
//
// See: assertValidTransaction.
nextTransactionID: 1,
}
}
// We want to iterate through all transactions and return a list of transactions in progress.
func (d *Database) inProgress() btree.Set[uint64] {
var ids btree.Set[uint64]
iter := d.transactions.Iter()
for ok := iter.First(); ok; ok = iter.Next() {
if iter.Value().state == InProgressTransaction {
ids.Insert(iter.Key())
}
}
return ids
}
func (d *Database) newTransaction() *Transaction {
t := Transaction{
isolation: d.defaultIsolation,
state: InProgressTransaction,
id: d.nextTransactionID,
}
t.inProgress = d.inProgress()
d.nextTransactionID += 1
d.transactions.Set(t.id, t)
debug("starting transaction", t.id)
return &t
}
func (d *Database) completeTransaction(t *Transaction, state TransactionState) error {
debug("completing transactoin", t.id)
// Either the transaction is to be marked as aborted or completed at this stage.
t.state = state
d.transactions.Set(t.id, *t)
return nil
}
func (d *Database) transactionState(txID uint64) Transaction {
t, ok := d.transactions.Get(txID)
assert(ok, "valid transacation")
return t
}
func (d *Database) assertValidTransaction(t *Transaction) {
assert(t.id > 0, "valid id")
assert(d.transactionState(t.id).state == InProgressTransaction, "in progress transaction")
}
// A connection is a reference to a database and may or may not have an associated transaction with it.
type Connection struct {
tx *Transaction
db *Database
}
func (c *Connection) execCommand(command string, args []string) (string, error) {
command = strings.ToLower(command)
debug(command, args)
// A user can start a transaction by using the command: BEGIN
if command == "begin" {
assertEq(c.tx, nil, "no running transactions")
c.tx = c.db.newTransaction()
c.db.assertValidTransaction(c.tx)
return fmt.Sprintf("%d", c.tx.id), nil
}
if command == "abort" {
c.db.assertValidTransaction(c.tx)
err := c.db.completeTransaction(c.tx, AbortedTransaction)
c.tx = nil
return "", err
}
if command == "commit" {
c.db.assertValidTransaction(c.tx)
err := c.db.completeTransaction(c.tx, CommittedTransaction)
c.tx = nil
return "", err
}
if command == "get" {
c.db.assertValidTransaction(c.tx)
key := args[0]
// TODO: What's going on here? Snapshot isolation?
//
// We track the key being read. TODO: Why?
c.tx.readset.Insert(key)
for i := len(c.db.store[key]) - 1; i > 0; i-- {
value := c.db.store[key][i]
// TODO: isvisibile needs to be implemented.
debug(value, c.tx, c.db.isVisible(c.tx, value))
if c.db.isVisible(c.tx, value) {
return value.value, nil
}
}
}
if command == "set" || command == "delete" {
c.db.assertValidTransaction(c.tx)
key := args[0]
found := false
// Any existing versions should be marked as invalid, because this latest set / delete
// operation supersedes all older values.
for i := len(c.db.store[key]) - 1; i >= 0; i-- {
value := &c.db.store[key][i]
debug(value, c.tx, c.db.isVisible(c.tx, *value))
if c.db.isVisible(c.tx, *value) {
value.txEndID = c.tx.id
found = true
}
}
if command == "delete" && !found {
return "", fmt.Errorf("delete failed: key %q not found")
}
// We track the key being written.
c.tx.writeset.Insert(key)
if command == "set" {
value := args[1]
c.db.store[key] = append(c.db.store[key], Value{
txStartID: c.tx.id,
// end ID '0' implies no valid transaction has overwritten this value yet.
txEndID: 0,
value: value,
})
return value, nil
}
// DELETE operation successful, as we have already marked all existing values as invalid (by
// setting a Tx ID to `txEndID`).
return "", nil
}
return "", fmt.Errorf("command unimplemented")
}
// TODO: Why do we need mustExecCommand and execCommand. Why not just execCommand?
func (c *Connection) mustExecCommand(cmd string, args []string) string {
res, err := c.execCommand(cmd, args)
assertEq(err, nil, "unexpected error")
return res
}
func (d *Database) newConnection() *Connection {
return &Connection{
db: d,
tx: nil,
}
}
func main() {
panic("unimplemented")
}