-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcnxinfo.go
106 lines (93 loc) · 3.12 KB
/
cnxinfo.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
// Copyright 2019 dfuse Platform 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 basebigt
import (
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
type DSN struct {
Project string
Instance string
TablePrefix string
CreateTables bool
MaxBlocksBeforeFlush uint64
MaxDurationBeforeFlush time.Duration
}
func ParseDSN(dsn string) (*DSN, error) {
// bigtable://project.instance/tblPrefix?createTables=true
u, err := url.Parse(dsn)
if err != nil {
return nil, err
}
hostParts := strings.Split(u.Host, ".")
if len(hostParts) != 2 {
return nil, fmt.Errorf("dsn: invalid, ensure host component looks like 'project.instance'")
}
maxSecondsBeforeFlush := uint64(10)
if qMaxSeconds := u.Query().Get("max-seconds-before-flush"); qMaxSeconds != "" {
ms, err := strconv.ParseUint(qMaxSeconds, 10, 32)
if err != nil {
return nil, fmt.Errorf("dsn: invalid parameter for max-blocks-before-flush, %s", err)
}
maxSecondsBeforeFlush = ms
}
maxBlocksBeforeFlush := uint64(10)
if qMaxBlocks := u.Query().Get("max-blocks-before-flush"); qMaxBlocks != "" {
mb, err := strconv.ParseUint(qMaxBlocks, 10, 32)
if err != nil {
return nil, fmt.Errorf("dsn: invalid parameter for max-blocks-before-flush, %s", err)
}
maxBlocksBeforeFlush = mb
}
create := u.Query().Get("createTables")
switch create {
case "":
create = "false"
case "true", "false":
default:
return nil, fmt.Errorf("dsn: invalid parameter for createTables, use true or false")
}
path := strings.Trim(u.Path, "/")
if len(strings.Split(path, "/")) > 1 {
return nil, fmt.Errorf("dsn: path component invalid, should only have tablePrefix in there")
}
if path == "" {
return nil, fmt.Errorf("dsn: invalid tablePrefix (in path segment), cannot be empty")
}
return &DSN{
Project: hostParts[0],
Instance: hostParts[1],
TablePrefix: path,
CreateTables: create == "true",
MaxBlocksBeforeFlush: maxBlocksBeforeFlush,
MaxDurationBeforeFlush: time.Duration(maxSecondsBeforeFlush) * time.Second,
}, nil
}
// DEPRECATED, use DSN instead
type ConnectionInfo struct {
Project string
Instance string
TablePrefix string
}
// Deprecated: Use DSN instead. Explode connection string `connection` into its three parts.
func NewConnectionInfo(connection string) (info *ConnectionInfo, err error) {
parts := strings.Split(connection, ":")
if len(parts) != 3 {
return nil, fmt.Errorf("database connection info should be <project>:<instance>:<prefix>")
}
return &ConnectionInfo{Project: parts[0], Instance: parts[1], TablePrefix: parts[2]}, nil
}