forked from couchbaselabs/tuqtng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
87 lines (73 loc) · 2.62 KB
/
server.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
// Copyright (c) 2013 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
package server
import (
"fmt"
"strings"
"time"
"github.com/couchbaselabs/clog"
"github.com/couchbaselabs/tuqtng/catalog"
"github.com/couchbaselabs/tuqtng/catalog/couchbase"
"github.com/couchbaselabs/tuqtng/catalog/file"
"github.com/couchbaselabs/tuqtng/catalog/mock"
"github.com/couchbaselabs/tuqtng/catalog/system"
"github.com/couchbaselabs/tuqtng/compiler"
"github.com/couchbaselabs/tuqtng/executor"
"github.com/couchbaselabs/tuqtng/network"
standardCompiler "github.com/couchbaselabs/tuqtng/compiler/standard"
interpretedExecutor "github.com/couchbaselabs/tuqtng/executor/interpreted"
)
func Site(s string) (catalog.Site, error) {
if strings.HasPrefix(s, ".") || strings.HasPrefix(s, "/") {
return file.NewSite(s)
}
if strings.HasPrefix(s, "dir:") {
return file.NewSite(s[4:])
}
if strings.HasPrefix(s, "mock:") {
return mock.NewSite(s)
}
return couchbase.NewSite(s)
}
func Server(version, siteName, defaultPoolName string,
queryChannel network.QueryChannel, timeout *time.Duration) error {
site, err := Site(siteName)
if err != nil {
return fmt.Errorf("Unable to access site %s, err: %v", siteName, err)
}
systemProxySite, err := system.NewSite(site)
if err != nil {
return fmt.Errorf("Unable to instantiate system catalog: %v", err)
}
// create a StaticQueryPipeline we use to process queries
comp := standardCompiler.NewCompiler(systemProxySite, defaultPoolName)
exec := interpretedExecutor.NewExecutor(systemProxySite, defaultPoolName)
clog.Log("tuqtng started...")
clog.Log("version: %s", version)
clog.Log("site: %s", siteName)
// dispatch each query that comes in
for query := range queryChannel {
go Dispatch(query, comp, exec, timeout)
}
return nil
}
func Dispatch(q network.Query, comp compiler.Compiler, exec executor.Executor,
timeout *time.Duration) {
request := q.Request()
response := q.Response()
switch request := request.(type) {
case network.StringQueryRequest:
plan, err := comp.Compile(request.QueryString)
if err != nil {
response.SendError(err)
return
}
exec.Execute(plan, q, timeout)
}
}