-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
164 lines (155 loc) · 3.64 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
package main
import (
"bufio"
"fmt"
"github.com/peterh/liner"
"minisql/src/API"
"minisql/src/BufferManager"
"minisql/src/CatalogManager"
"minisql/src/Interpreter/parser"
"minisql/src/Interpreter/types"
"minisql/src/RecordManager"
"minisql/src/Utils/Error"
"minisql/src/BackEnd"
"os"
"path/filepath"
"strings"
"time"
)
const historyCommmandFile="~/.minisql_history"
const firstPrompt="minisql>"
const secondPrompt=" ->"
//FlushALl 结束时做的所有工作
func FlushALl() {
BufferManager.BlockFlushAll() //缓存block
RecordManager.FlushFreeList() //free list写回
CatalogManager.FlushDatabaseMeta(CatalogManager.UsingDatabase.DatabaseId) //刷新记录长度和余量
}
func InitDB() error {
err:= CatalogManager.LoadDbMeta()
if err!=nil {
return err
}
BufferManager.InitBuffer()
return nil
}
func expandPath(path string) (string,error) {
if strings.HasPrefix(path, "~/") {
parts := strings.SplitN(path, "/", 2)
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, parts[1]), nil
}
return path, nil
}
func loadHistoryCommand() (*os.File,error) {
var file *os.File
path,err:=expandPath(historyCommmandFile)
if err!=nil{
return nil,err
}
_,err=os.Stat(path)
if os.IsNotExist(err) {
file,err=os.Create(path)
if err!=nil {
return nil,err
}
} else {
file,err=os.OpenFile(path,os.O_RDWR,0666)
if err !=nil {
return nil,err
}
}
return file,err
}
func runShell(r chan<- error) {
ll:=liner.NewLiner()
defer ll.Close()
ll.SetCtrlCAborts(true)
file,err:= loadHistoryCommand()
if err !=nil {
panic(err)
}
defer func() {
_,err:=ll.WriteHistory(file)
if err !=nil{
panic(err)
}
_=file.Close()
}()
s:= bufio.NewScanner(file)
for s.Scan() {
//fmt.Println(s.Text())
ll.AppendHistory(s.Text())
}
InitDB()
StatementChannel:=make(chan types.DStatements,500) //用于传输操作指令通道
FinishChannel:=make(chan Error.Error,500) //用于api执行完成反馈通道
FlushChannel:=make(chan struct{}) //用于每条指令结束后协程flush
go API.HandleOneParse(StatementChannel,FinishChannel) //begin the runtime for exec
go BufferManager.BeginBlockFlush(FlushChannel)
var beginSQLParse=false
var sqlText=make([]byte,0,100)
for { //each sql
LOOP:
beginSQLParse=false
sqlText=sqlText[:0]
var input string
var err error
for { //each line
if beginSQLParse{
input, err = ll.Prompt(secondPrompt)
} else {
input, err = ll.Prompt(firstPrompt)
}
if err !=nil{
if err ==liner.ErrPromptAborted {
goto LOOP
}
}
trimInput:=strings.TrimSpace(input) //get the input without front and backend space
if len(trimInput)!=0 {
ll.AppendHistory(input)
if !beginSQLParse&&(trimInput=="quit"||strings.HasPrefix(trimInput,"quit;")) {
close(StatementChannel)
for _=range FinishChannel {
}
FlushALl();
r<-err
return
}
sqlText=append(sqlText,append([]byte{' '},[]byte(trimInput)[0:]...)...)
if !beginSQLParse {
beginSQLParse=true
}
if strings.Contains(trimInput,";") {
break
}
}
}
beginTime:=time.Now()
err=parser.Parse(strings.NewReader(string(sqlText)),StatementChannel)
//fmt.Println(string(sqlText))
if err != nil {
fmt.Println(err)
continue
}
<-FinishChannel //等待指令执行完成
durationTime:=time.Since(beginTime)
fmt.Println("Finish operation at: ",durationTime)
FlushChannel<- struct{}{} //开始刷新cache
}
}
func main() {
//errChan 用于接收shell返回的err
errChan:=make(chan error)
go runShell(errChan) //开启shell协程
go BackEnd.Regist()
err:=<-errChan
fmt.Println("bye")
if err!=nil {
fmt.Println(err)
}
}