-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
151 lines (133 loc) · 3.89 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"os"
"github.com/yowcow/goromdb/handler"
"github.com/yowcow/goromdb/handler/simplehandler"
"github.com/yowcow/goromdb/loader"
"github.com/yowcow/goromdb/protocol"
"github.com/yowcow/goromdb/protocol/memcachedprotocol"
"github.com/yowcow/goromdb/server"
"github.com/yowcow/goromdb/storage"
"github.com/yowcow/goromdb/storage/bdbstorage"
"github.com/yowcow/goromdb/storage/boltstorage"
"github.com/yowcow/goromdb/storage/jsonstorage"
"github.com/yowcow/goromdb/storage/memcdstorage"
"github.com/yowcow/goromdb/watcher"
)
// Version to be embedded
var Version string
func main() {
var addr string
var protoBackend string
var handlerBackend string
var storageBackend string
var file string
var gzipped bool
var bucket string
var basedir string
var help bool
var version bool
flag.StringVar(&addr, "addr", ":11211", "address to bind to")
flag.StringVar(&protoBackend, "proto", "memcached", "protocol: memcached")
flag.StringVar(&handlerBackend, "handler", "simple", "handler: simple")
flag.StringVar(&storageBackend, "storage", "json", "storage: json, bdb, boltdb, memcachedb-bdb")
flag.StringVar(&file, "file", "/tmp/goromdb", "data file to be loaded into store")
flag.BoolVar(&gzipped, "gzipped", false, "whether or not loading file is gzipped")
flag.StringVar(&bucket, "bucket", "default", "bucket name (for boltdb)")
flag.StringVar(&basedir, "basedir", "", "base directory to store loaded data file")
flag.BoolVar(&help, "help", false, "print help")
flag.BoolVar(&version, "version", false, "print version")
flag.Parse()
if help {
flag.Usage()
os.Exit(0)
}
if version {
fmt.Println("goromdb", Version)
os.Exit(0)
}
logger := log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile)
proto, err := createProtocol(protoBackend)
if err != nil {
panic(err)
}
ctx, cancel := context.WithCancel(context.Background())
wcr := watcher.NewSimpleWatcher(file, 5000, logger)
filein := wcr.Start(ctx)
stg, err := createStorage(storageBackend, gzipped, bucket)
if err != nil {
panic(err)
}
l, err := loader.New(basedir, "data.db")
if err != nil {
panic(err)
}
h, err := createHandler(handlerBackend, stg, logger)
if err != nil {
panic(err)
}
done := h.Start(filein, l)
logger.Printf(
"booting goromdb (PID: %d, address: %s, protocol: %s, handler: %s, storage: %s, file: %s)",
os.Getpid(), addr, protoBackend, handlerBackend, storageBackend, file,
)
svr := server.New("tcp", addr, logger)
err = svr.Start(server.OnReadCallbackFunc(func(conn net.Conn, line []byte, logger *log.Logger) {
if keys, err := proto.Parse(line); err != nil {
logger.Printf("server failed parsing a line: %s", err)
} else {
for _, k := range keys {
if v, _ := h.Get(k); v != nil {
proto.Reply(conn, k, v)
}
}
}
proto.Finish(conn)
}))
if err != nil {
logger.Printf("failed booting goromdb: %s", err.Error())
os.Exit(1)
}
cancel()
<-done
}
func createHandler(
handlerBackend string,
stg storage.Storage,
logger *log.Logger,
) (handler.Handler, error) {
switch handlerBackend {
case "simple":
return simplehandler.New(stg, logger), nil
default:
return nil, fmt.Errorf("don't know how to handle handler '%s'", handlerBackend)
}
}
func createStorage(storageBackend string, gzipped bool, bucket string) (storage.Storage, error) {
switch storageBackend {
case "json":
return jsonstorage.New(gzipped), nil
case "bdb":
return bdbstorage.New(), nil
case "boltdb":
return boltstorage.New(bucket), nil
case "memcachedb-bdb":
p := bdbstorage.New()
return memcdstorage.New(p), nil
default:
return nil, fmt.Errorf("don't know how to handle storage '%s'", storageBackend)
}
}
func createProtocol(protoBackend string) (protocol.Protocol, error) {
switch protoBackend {
case "memcached":
return memcachedprotocol.New(), nil
default:
return nil, fmt.Errorf("don't know how to handle protocol '%s'", protoBackend)
}
}