-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
executable file
·397 lines (364 loc) · 10.7 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
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
/*
Copyright (C) 2023, 2024 Carl-Philip Hänsch
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
memcp smart clusterable distributed database working best on nvme
https://pkelchte.wordpress.com/2013/12/31/scm-go/
*/
package main
import "os"
import "io"
import "fmt"
import "flag"
import "time"
import "bufio"
import "sync"
import "syscall"
import "runtime"
import "io/ioutil"
import "os/signal"
import "crypto/rand"
import "path/filepath"
import "runtime/pprof"
import "github.com/google/uuid"
import "github.com/fsnotify/fsnotify"
import "github.com/launix-de/memcp/scm"
import "github.com/launix-de/memcp/storage"
var IOEnv scm.Env
func getImport(path string) func (a ...scm.Scmer) scm.Scmer {
return func (a ...scm.Scmer) scm.Scmer {
filename := path + "/" + scm.String(a[0])
// TODO: filepath.Walk for wildcards
wd := filepath.Dir(filename)
otherPath := scm.Env {
scm.Vars {
"__DIR__": path,
"__FILE__": filename,
"import": getImport(wd),
"load": getLoad(wd),
"watch": getWatch(wd),
"serveStatic": scm.HTTPStaticGetter(wd),
},
nil,
&IOEnv,
true,
}
bytes, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
return scm.EvalAll(filename, string(bytes), &otherPath)
}
}
func getLoad(path string) func (a ...scm.Scmer) scm.Scmer {
return func (a ...scm.Scmer) scm.Scmer {
filename := path + "/" + scm.String(a[0])
if len(a) > 2 {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
splitter := bufio.NewReader(file)
delimiter := scm.String(a[2])
if len(delimiter) != 1 {
panic("load delimiter must be 1 byte long")
}
for {
str, err := splitter.ReadString(delimiter[0])
if err == io.EOF {
break // file is finished
}
if err != nil {
panic(err)
}
// go??
scm.Apply(a[1], str);
}
} else {
// read in whole
bytes, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
if len(a) > 1 {
scm.Apply(a[1], string(bytes));
} else {
return string(bytes)
}
}
return true
}
}
func getWatch(path string) func (a ...scm.Scmer) scm.Scmer {
return func (a ...scm.Scmer) scm.Scmer {
filename := path + "/" + scm.String(a[0])
reread := func () {
// read in whole
bytes, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
scm.Apply(a[1], string(bytes))
}
reread() // read once at the beginning in sync
// watch for changes
watcher, err := fsnotify.NewWatcher()
if err != nil {
panic(err)
}
go func() {
for {
select {
case /*event :=*/ <- watcher.Events:
// flush all other events
for {
time.Sleep(10 * time.Millisecond) // delay a bit, so we don't read empty files
select {
case <- watcher.Events:
// ignore
default:
goto to_reread
}
}
to_reread:
// now reread the file
func () {
defer func() {
if err := recover(); err != nil {
// error happens during reload: log to console
fmt.Println(err)
}
}()
reread()
}()
watcher.Add(filename) // text editors rename, so we have to rewatch
}
}
}()
err = watcher.Add(filename)
if err != nil {
panic(err)
}
return true
}
}
// workaround for flags package to allow multiple values
type arrayFlags []string
func (i *arrayFlags) String() string {
return "dummy"
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
func setupIO(wd string) {
// define some IO functions (scm will not provide them since it is sandboxable)
IOEnv = scm.Env {
scm.Vars {},
nil,
&scm.Globalenv,
true, // other defines go into Globalenv
}
scm.DeclareTitle("IO")
scm.Declare(&IOEnv, &scm.Declaration{
"print", "Prints values to stdout (only in IO environment)",
1, 1000,
[]scm.DeclarationParameter{
scm.DeclarationParameter{"value...", "any", "values to print"},
}, "bool",
func (a ...scm.Scmer) scm.Scmer {
for _, s := range a {
fmt.Print(scm.String(s))
}
fmt.Println()
return true
},
})
scm.Declare(&IOEnv, &scm.Declaration{
"help", "Lists all functions or print help for a specific function",
0, 1,
[]scm.DeclarationParameter{
scm.DeclarationParameter{"topic", "string", "function to print help about"},
}, "nil",
func (a ...scm.Scmer) scm.Scmer {
if len(a) == 0 {
scm.Help(nil)
} else {
scm.Help(a[0])
}
return nil
},
})
scm.Declare(&IOEnv, &scm.Declaration{
"import", "Imports a file .scm file into current namespace",
1, 1,
[]scm.DeclarationParameter{
scm.DeclarationParameter{"filename", "string", "filename relative to folder of source file"},
}, "any",
(func(...scm.Scmer) scm.Scmer)(getImport(wd)),
})
scm.Declare(&IOEnv, &scm.Declaration{
"load", "Loads a file and returns the string",
1, 3,
[]scm.DeclarationParameter{
scm.DeclarationParameter{"filename", "string", "filename relative to folder of source file"},
scm.DeclarationParameter{"linehandler", "func", "handler that reads each line"},
scm.DeclarationParameter{"delimiter", "string", "delimiter to extract"},
}, "bool",
(func(...scm.Scmer) scm.Scmer)(getLoad(wd)),
})
scm.Declare(&IOEnv, &scm.Declaration{
"watch", "Loads a file and calls the callback. Whenever the file changes on disk, the file is load again.",
2, 2,
[]scm.DeclarationParameter{
scm.DeclarationParameter{"filename", "string", "filename relative to folder of source file"},
scm.DeclarationParameter{"updatehandler", "func", "handler that receives the file content func(content)"},
}, "bool",
(func(...scm.Scmer) scm.Scmer)(getWatch(wd)),
})
scm.Declare(&IOEnv, &scm.Declaration{
"serve", "Opens a HTTP server at a given port",
2, 2,
[]scm.DeclarationParameter{
scm.DeclarationParameter{"port", "number", "port number for HTTP server"},
scm.DeclarationParameter{"handler", "func", "handler: lambda(req res) that handles the http request (TODO: detailed documentation)"},
}, "bool",
scm.HTTPServe,
})
scm.Declare(&IOEnv, &scm.Declaration{
"serveStatic", "creates a static handler for use as a callback in (serve) - returns a handler lambda(req res)",
1, 1,
[]scm.DeclarationParameter{
scm.DeclarationParameter{"directory", "string", "folder with the files to serve"},
}, "func",
(func(...scm.Scmer) scm.Scmer)(scm.HTTPStaticGetter(wd)),
})
scm.Declare(&IOEnv, &scm.Declaration{
"mysql", "Imports a file .scm file into current namespace",
4, 4,
[]scm.DeclarationParameter{
scm.DeclarationParameter{"port", "number", "port number for MySQL server"},
scm.DeclarationParameter{"getPassword", "func", "lambda(username string) string|nil has to return the password for a user or nil to deny login"},
scm.DeclarationParameter{"schemacallback", "func", "lambda(username schema) bool handler check whether user is allowed to schem (string) - you should check access rights here"},
scm.DeclarationParameter{"handler", "func", "lambda(schema sql resultrow session) handler to process sql query (string) in schema (string). resultrow is a lambda(list)"},
}, "bool",
scm.MySQLServe,
})
scm.Declare(&IOEnv, &scm.Declaration{
"password", "Hashes a password with sha1 (for mysql user authentication)",
1, 1,
[]scm.DeclarationParameter{
scm.DeclarationParameter{"password", "string", "plain text password to hash"},
}, "string",
scm.MySQLPassword,
})
}
func main() {
fmt.Print(`memcp Copyright (C) 2023, 2024 Carl-Philip Hänsch
This program comes with ABSOLUTELY NO WARRANTY;
This is free software, and you are welcome to redistribute it
under certain conditions;
`)
// init random generator for UUIDs
uuid.SetRand(rand.Reader)
// parse command line options
var commands arrayFlags
flag.Var(&commands, "c", "Execute scm command")
basepath := "data"
flag.StringVar(&basepath, "data", "data", "Data folder for persistence")
profile := ""
flag.StringVar(&profile, "profile", "", "Data folder for persistence")
wd, _ := os.Getwd() // libraries are relative to working directory... or change with -wd PATH
flag.StringVar(&wd, "wd", wd, "Working Directory for (import) and (load) (Default: .)")
flag.Parse()
imports := flag.Args()
// storage initialization
setupIO(wd)
storage.Init(scm.Globalenv)
storage.Basepath = basepath
storage.LoadDatabases()
// scripts initialization
if len(imports) == 0 {
// load default script
IOEnv.Vars["import"].(func(...scm.Scmer)scm.Scmer)("lib/main.scm")
} else {
// load scripts from command line
for _, scmfile := range imports {
fmt.Println("Loading " + scmfile + " ...")
IOEnv.Vars["import"].(func(...scm.Scmer)scm.Scmer)(scmfile)
}
}
for _, command := range commands {
fmt.Println("Executing " + command + " ...")
code := scm.Read("command line", command)
scm.Validate(code, "any")
code = scm.Optimize(code, &IOEnv)
scm.Eval(code, &IOEnv)
}
// install exit handler
cancelChan := make(chan os.Signal, 1)
signal.Notify(cancelChan, syscall.SIGTERM, syscall.SIGINT)
go (func () {
<-cancelChan
exitroutine()
os.Exit(1)
})()
fmt.Print(`
Type (help) to show help
`)
// init profiling
if profile != "" {
f, err := os.Create(profile)
if err != nil {
panic(err)
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// start cron
go cronroutine()
// REPL shell
scm.Repl(&IOEnv)
// normal shutdown
exitroutine()
}
var exitsignal chan bool = make(chan bool, 1) // set true to start shutdown routine and wait for all jobs
var exitable sync.WaitGroup
func cronroutine() {
exitable.Add(1)
for {
// wait first
select {
case <- exitsignal:
// memcp is about to exit; confirm the waitgroup and exit
exitable.Done()
return
case <- time.After(time.Minute * 15): // rebuild shards for all 15 minutes
// continue
}
fmt.Println("running 15min cron ...")
fmt.Println("table compression done in ", storage.Rebuild(false, true))
}
}
func exitroutine() {
exitsignal <- true
exitable.Wait()
fmt.Println("Exit procedure...")
fmt.Println("finalizing storage...")
storage.UnloadDatabases()
fmt.Println("finalizing memory...")
runtime.GC() // this will call the finalizers on shards
fmt.Println("Exit procedure finished")
os.Exit(0)
}