-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
451 lines (395 loc) · 12.1 KB
/
main.go
File metadata and controls
451 lines (395 loc) · 12.1 KB
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
package main
import (
"bufio"
"bytes"
"encoding/csv"
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"text/tabwriter"
"time"
"mit.edu/dsg/godb/catalog"
"mit.edu/dsg/godb/common"
"mit.edu/dsg/godb/execution"
"mit.edu/dsg/godb/indexing"
"mit.edu/dsg/godb/planner"
"mit.edu/dsg/godb/recovery"
"mit.edu/dsg/godb/storage"
"mit.edu/dsg/godb/transaction"
)
// GoDB is the top-level container for the database system.
type GoDB struct {
Catalog *catalog.Catalog
BufferPool *storage.BufferPool
TableManager *execution.TableManager
LogManager storage.LogManager
TransactionManager *transaction.TransactionManager
LockManager *transaction.LockManager
IndexManager *indexing.IndexManager
Planner *planner.SQLPlanner
}
func NewGoDB(catalog *catalog.Catalog, storageDir, logDir string, bufferPoolSize int, truncate bool) (*GoDB, error) {
if truncate {
_ = os.RemoveAll(storageDir)
_ = os.RemoveAll(logDir)
}
if err := os.MkdirAll(storageDir, 0755); err != nil {
return nil, err
}
if err := os.MkdirAll(logDir, 0755); err != nil {
return nil, err
}
// TODO: Replace with your actual log manager implementation from lab 4
logManager := &storage.NoopLogManager{}
bufferPool := storage.NewBufferPool(bufferPoolSize, storage.NewDiskStorageManager(storageDir), logManager)
lockManager := transaction.NewLockManager()
txnManager := transaction.NewTransactionManager(logManager, bufferPool, lockManager)
tableManager, err := execution.NewTableManager(catalog, bufferPool, logManager, lockManager)
if err != nil {
return nil, err
}
indexManager, err := indexing.NewIndexManager(catalog)
if err != nil {
return nil, err
}
// TODO: Use an actual recovery manager once recovery is implemented in lab 4
recoveryManager := recovery.NewNoLogRecoveryManager(bufferPool, txnManager, catalog, tableManager, indexManager)
if err := recoveryManager.Recover(); err != nil {
return nil, err
}
logicalRules := []planner.LogicalRule{
&planner.PredicatePushDownRule{},
//&planner.ProjectionPushDownRule{},
}
// TODO: Activate rules as you implement the relevant executors in Lab 2
physicalRules := []planner.PhysicalConversionRule{
&planner.SeqScanRule{},
// &planner.IndexScanRule{},
// &planner.IndexLookupRule{},
// &planner.IndexNestedLoopJoinRule{},
// &planner.SortMergeJoinRule{},
// &planner.HashJoinRule{},
&planner.BlockNestedLoopJoinRule{},
&planner.LimitRule{},
// You can activate this rule once you implement Materialize in lab2
// &planner.Subquery{},
&planner.AggregationRule{},
&planner.ProjectionRule{},
&planner.FilterRule{},
&planner.SortRule{},
&planner.InsertRule{},
&planner.DeleteRule{},
&planner.UpdateRule{},
&planner.ValuesRule{},
}
return &GoDB{
Catalog: catalog,
BufferPool: bufferPool,
TableManager: tableManager,
LogManager: logManager,
TransactionManager: txnManager,
LockManager: lockManager,
IndexManager: indexManager,
Planner: planner.NewSQLPlanner(catalog, logicalRules, physicalRules),
}, nil
}
// Global config flags
var (
catalogPath string
dbDir string
logDir string
bufferSize int
)
func main() {
if len(os.Args) < 2 {
printUsage()
os.Exit(1)
}
cmd := os.Args[1]
switch cmd {
case "load":
runLoad(os.Args[2:])
case "shell":
runShell(os.Args[2:])
default:
fmt.Printf("Unknown command: %s\n", cmd)
printUsage()
os.Exit(1)
}
}
func printUsage() {
fmt.Println("GoDB - A Go-based Database System")
fmt.Println("\nUsage:")
fmt.Println(" godb <command> [flags] [arguments]")
fmt.Println("\nCommands:")
fmt.Println(" load Batch load data from CSV files.")
fmt.Println(" Usage: godb load [flags] <file1.csv> <file2.csv> ...")
fmt.Println(" shell Start the interactive SQL terminal.")
fmt.Println(" Usage: godb shell [flags]")
fmt.Println("\nCommon Flags:")
fmt.Println(" -catalog <path> Path to the catalog file (default: catalog.json)")
fmt.Println(" -db <dir> Directory for database heap files (default: godb_data)")
fmt.Println(" -log <dir> Directory for write-ahead logs (default: godb_log)")
fmt.Println(" -buffer <int> Buffer pool size in pages (default: 1000)")
fmt.Println("\nShell Commands:")
fmt.Println(" help Show this usage information")
fmt.Println(" exit, \\q Exit the shell")
fmt.Println("\nNotes:")
fmt.Println(" - The CSV loader is a basic implementation. It does not escape characters.")
fmt.Println(" Strings containing quotes (e.g. O'Reilly) or ; may cause SQL syntax errors. You need to escape them.")
fmt.Println(" - Transactions are not supported in the shell (shell is inherently single-threaded).")
fmt.Println(" - In Go, flags must strictly come before arguments.")
}
func setupCommonFlags(fs *flag.FlagSet) {
fs.StringVar(&catalogPath, "catalog", "catalog.json", "Path to the catalog file")
fs.StringVar(&dbDir, "db", "godb_data", "Directory for database heap files")
fs.StringVar(&logDir, "log", "godb_log", "Directory for write-ahead logs")
fs.IntVar(&bufferSize, "buffer", 1000, "Buffer pool size in pages")
}
// ----------------------------------------------------------------------------
// Command: Load
// ----------------------------------------------------------------------------
func runLoad(args []string) {
fs := flag.NewFlagSet("load", flag.ExitOnError)
setupCommonFlags(fs)
truncate := fs.Bool("truncate", false, "Truncate heap files and logs before loading")
fs.Parse(args)
csvFiles := fs.Args()
if len(csvFiles) == 0 {
fmt.Println("Error: No CSV files provided.")
fmt.Println("Usage: godb load [flags] <file1.csv> <file2.csv> ...")
os.Exit(1)
}
// 1. Load Catalog
catProvider := catalog.NewDiskCatalogManager(catalogPath)
cat, err := catalog.NewCatalog(catProvider)
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal: Could not load catalog from '%s'.\nError: %v\n", catalogPath, err)
os.Exit(1)
}
// 2. Initialize Engine
fmt.Println("Initializing Engine...")
db, err := NewGoDB(cat, dbDir, logDir, bufferSize, *truncate)
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal: Failed to initialize database.\nError: %v\n", err)
os.Exit(1)
}
// 3. Load CSVs provided in arguments
fmt.Println("Loading Data...")
start := time.Now()
totalRows := 0
for _, csvPath := range csvFiles {
baseName := filepath.Base(csvPath)
ext := filepath.Ext(baseName)
tableName := strings.TrimSuffix(baseName, ext)
// Verify table exists in catalog
if _, err := cat.GetTableMetadata(tableName); err != nil {
fmt.Printf(" [SKIP] '%s' (Table '%s' not found in catalog)\n", csvPath, tableName)
continue
}
fmt.Printf(" [LOAD] Loading '%s' into table '%s'...\n", csvPath, tableName)
rows, err := loadTableFromCSV(db, tableName, csvPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal: Failed to load table %s: %v\n", tableName, err)
os.Exit(1)
}
fmt.Printf(" -> %d rows inserted.\n", rows)
totalRows += rows
}
// 4. Flush Buffer Pool to ensure durability of loaded data
fmt.Println("Flushing pages to disk...")
if err := db.BufferPool.FlushAllPages(); err != nil {
fmt.Fprintf(os.Stderr, "Fatal: Failed to flush buffer pool: %v\n", err)
os.Exit(1)
}
fmt.Printf("Success. Loaded %d rows in %v.\n", totalRows, time.Since(start))
}
func loadTableFromCSV(db *GoDB, tableName string, fileName string) (int, error) {
table, err := db.Catalog.GetTableMetadata(tableName)
if err != nil {
return 0, err
}
f, err := os.Open(fileName)
if err != nil {
return 0, err
}
defer f.Close()
r := csv.NewReader(f)
records, err := r.ReadAll()
if err != nil {
return 0, err
}
const batchSize = 500
rowCount := 0
for i := 0; i < len(records); i += batchSize {
end := i + batchSize
if end > len(records) {
end = len(records)
}
var sb strings.Builder
sb.WriteString(fmt.Sprintf("INSERT INTO %s VALUES ", tableName))
for j := i; j < end; j++ {
if j > i {
sb.WriteString(", ")
}
sb.WriteString("(")
for k, val := range records[j] {
colType := table.Columns[k].Type
if k > 0 {
sb.WriteString(", ")
}
switch colType {
case common.IntType:
// Validate it is a number, but write exactly what is in CSV if possible,
// or parse/clean it.
if _, err := strconv.Atoi(val); err != nil {
return 0, fmt.Errorf("column %s expects int, got '%s'", table.Columns[i].Name, val)
}
sb.WriteString(val)
case common.StringType:
sb.WriteString(fmt.Sprintf("'%s'", val))
}
}
sb.WriteString(")")
}
// pass false for explain (loading doesn't need plan printing)
if err := executeStatement(db, sb.String(), true, false); err != nil {
return rowCount, fmt.Errorf("batch insert error at row %d: %w", i, err)
}
rowCount += (end - i)
}
return rowCount, nil
}
// ----------------------------------------------------------------------------
// Command: Shell
// ----------------------------------------------------------------------------
func runShell(args []string) {
fs := flag.NewFlagSet("shell", flag.ExitOnError)
setupCommonFlags(fs)
// Add explain flag
explain := fs.Bool("explain", false, "Print the physical execution plan before executing")
fs.Parse(args)
fmt.Println("Initializing Engine...")
catProvider := catalog.NewDiskCatalogManager(catalogPath)
cat, err := catalog.NewCatalog(catProvider)
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal: Could not load catalog from '%s'.\nError: %v\n", catalogPath, err)
os.Exit(1)
}
db, err := NewGoDB(cat, dbDir, logDir, bufferSize, false)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("GoDB SQL Shell")
fmt.Println("Type 'help' for usage, '\\q' to exit.")
fmt.Println("------------------------------------------------")
scanner := bufio.NewScanner(os.Stdin)
var queryBuffer bytes.Buffer
for {
if queryBuffer.Len() == 0 {
fmt.Print("GoDB> ")
} else {
fmt.Print(" -> ")
}
if !scanner.Scan() {
break
}
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 {
continue
}
if queryBuffer.Len() == 0 {
if line == "exit" || line == "\\q" || line == "quit" {
fmt.Println("Bye!")
break
}
if line == "help" {
printUsage()
continue
}
}
queryBuffer.WriteString(line)
queryBuffer.WriteString(" ")
if strings.HasSuffix(line, ";") {
fullQuery := queryBuffer.String()
err := executeStatement(db, fullQuery, false, *explain)
if err != nil {
fmt.Printf("Error: %v\n", err)
}
queryBuffer.Reset()
}
}
}
func executeStatement(db *GoDB, sql string, silent bool, explain bool) error {
plan, err := db.Planner.Plan(sql, (!explain) || silent)
if err != nil {
return err
}
if explain {
fmt.Println("Physical Plan:")
fmt.Println(planner.PrettyPrint(plan))
fmt.Println("")
}
executor, err := execution.BuildExecutorTree(plan, db.Catalog, db.TableManager, db.IndexManager)
if err != nil {
return err
}
err = executor.Init(execution.NewExecutorContext(nil))
if err != nil {
return err
}
defer executor.Close()
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
count := 0
start := time.Now()
if !silent {
if proj, ok := plan.(*planner.ProjectionNode); ok {
headers := projectionHeaders(proj)
if len(headers) > 0 {
fmt.Fprintln(w, strings.Join(headers, "\t"))
}
}
}
for executor.Next() {
tuple := executor.Current()
count++
if !silent {
var fields []string
for i := 0; i < tuple.NumColumns(); i++ {
val := tuple.GetValue(i)
fields = append(fields, val.String())
}
fmt.Fprintln(w, strings.Join(fields, "\t"))
}
}
_ = w.Flush()
if err := executor.Error(); err != nil {
return err
}
if !silent {
duration := time.Since(start)
if count > 0 || duration > time.Millisecond*10 {
fmt.Printf("(%d rows) [%v]\n", count, duration)
}
}
return nil
}
func projectionHeaders(proj *planner.ProjectionNode) []string {
headers := make([]string, len(proj.Expressions))
for i, expr := range proj.Expressions {
switch v := expr.(type) {
case interface{ Name() string }:
name := v.Name()
if name != "" {
headers[i] = name
continue
}
}
headers[i] = expr.String()
}
return headers
}