forked from proullon/ramsql
/
engine.go
199 lines (159 loc) · 3.85 KB
/
engine.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
package engine
import (
"errors"
"fmt"
"io"
"sync"
"github.com/IllidanTwister/ramsql/engine/log"
"github.com/IllidanTwister/ramsql/engine/parser"
"github.com/IllidanTwister/ramsql/engine/protocol"
)
type executor func(*Engine, *parser.Decl, protocol.EngineConn) error
// Engine is the root struct of RamSQL server
type Engine struct {
endpoint protocol.EngineEndpoint
relations map[string]*Relation
opsExecutors map[int]executor
// Any value send to this channel (through Engine.stop)
// Will stop the listening loop
stop chan bool
sync.Mutex
}
// New initialize a new RamSQL server
func New(endpoint protocol.EngineEndpoint) (e *Engine, err error) {
e = &Engine{
endpoint: endpoint,
}
e.stop = make(chan bool)
e.opsExecutors = map[int]executor{
parser.CreateToken: createExecutor,
parser.TableToken: createTableExecutor,
parser.SelectToken: selectExecutor,
parser.InsertToken: insertIntoTableExecutor,
parser.DeleteToken: deleteExecutor,
parser.UpdateToken: updateExecutor,
parser.IfToken: ifExecutor,
parser.NotToken: notExecutor,
parser.ExistsToken: existsExecutor,
parser.TruncateToken: truncateExecutor,
parser.DropToken: dropExecutor,
parser.GrantToken: grantExecutor,
}
e.relations = make(map[string]*Relation)
err = e.start()
if err != nil {
return nil, err
}
return
}
func (e *Engine) start() (err error) {
go e.listen()
return nil
}
// Stop shutdown the RamSQL server
func (e *Engine) Stop() {
if e.stop == nil {
// already stopped
return
}
go func() {
e.stop <- true
close(e.stop)
e.stop = nil
}()
}
func (e *Engine) relation(name string) *Relation {
// Lock ?
r := e.relations[name]
// Unlock ?
return r
}
func (e *Engine) drop(name string) {
e.Lock()
delete(e.relations, name)
e.Unlock()
}
func (e *Engine) dropAll() {
e.Lock()
e.relations = make(map[string]*Relation)
e.Unlock()
}
func (e *Engine) listen() {
newConnectionChannel := make(chan protocol.EngineConn)
go func() {
for {
conn, err := e.endpoint.Accept()
if err != nil {
e.Stop()
return
}
newConnectionChannel <- conn
}
}()
for {
select {
case conn := <-newConnectionChannel:
go e.handleConnection(conn)
break
case <-e.stop:
e.endpoint.Close()
return
}
}
}
func (e *Engine) handleConnection(conn protocol.EngineConn) {
for {
stmt, err := conn.ReadStatement()
if err == io.EOF {
// Todo: close engine if there is no conn left
return
}
if err != nil {
log.Warning("Enginge.handleConnection: cannot read : %s", err)
return
}
instructions, err := parser.ParseInstruction(stmt)
if err != nil {
conn.WriteError(err)
continue
}
err = e.executeQueries(instructions, conn)
if err != nil {
conn.WriteError(err)
continue
}
}
}
func (e *Engine) executeQueries(instructions []parser.Instruction, conn protocol.EngineConn) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("fatal error: %s", r)
return
}
}()
for _, i := range instructions {
err = e.executeQuery(i, conn)
if err != nil {
return err
}
}
return nil
}
func (e *Engine) executeQuery(i parser.Instruction, conn protocol.EngineConn) error {
if e.opsExecutors[i.Decls[0].Token] != nil {
return e.opsExecutors[i.Decls[0].Token](e, i.Decls[0], conn)
}
return errors.New("Not Implemented")
}
func createExecutor(e *Engine, createDecl *parser.Decl, conn protocol.EngineConn) error {
if len(createDecl.Decl) == 0 {
return errors.New("Parsing failed, no declaration after CREATE")
}
if e.opsExecutors[createDecl.Decl[0].Token] != nil {
return e.opsExecutors[createDecl.Decl[0].Token](e, createDecl.Decl[0], conn)
}
return errors.New("Parsing failed, unkown token " + createDecl.Decl[0].Lexeme)
}
func grantExecutor(e *Engine, decl *parser.Decl, conn protocol.EngineConn) error {
return conn.WriteResult(0, 0)
}