forked from gregorynoma/tsbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creator.go
120 lines (104 loc) · 2.45 KB
/
creator.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
type dbCreator struct {
daemonURL string
}
func (d *dbCreator) Init() {
d.daemonURL = daemonURLs[0] // pick first one since it always exists
}
func (d *dbCreator) DBExists(dbName string) bool {
dbs, err := d.listDatabases()
if err != nil {
log.Fatal(err)
}
for _, db := range dbs {
if db == loader.DatabaseName() {
return true
}
}
return false
}
func (d *dbCreator) listDatabases() ([]string, error) {
u := fmt.Sprintf("%s/query?q=show%%20databases", d.daemonURL)
resp, err := http.Get(u)
if err != nil {
return nil, fmt.Errorf("listDatabases error: %s", err.Error())
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Do ad-hoc parsing to find existing database names:
// {"results":[{"series":[{"name":"databases","columns":["name"],"values":[["_internal"],["benchmark_db"]]}]}]}%
type listingType struct {
Results []struct {
Series []struct {
Values [][]string
}
}
}
var listing listingType
err = json.Unmarshal(body, &listing)
if err != nil {
return nil, err
}
ret := []string{}
for _, nestedName := range listing.Results[0].Series[0].Values {
name := nestedName[0]
// the _internal database is skipped:
if name == "_internal" {
continue
}
ret = append(ret, name)
}
return ret, nil
}
func (d *dbCreator) RemoveOldDB(dbName string) error {
u := fmt.Sprintf("%s/query?q=drop+database+%s", d.daemonURL, dbName)
resp, err := http.Post(u, "text/plain", nil)
if err != nil {
return fmt.Errorf("drop db error: %s", err.Error())
}
if resp.StatusCode != 200 {
return fmt.Errorf("drop db returned non-200 code: %d", resp.StatusCode)
}
time.Sleep(time.Second)
return nil
}
func (d *dbCreator) CreateDB(dbName string) error {
u, err := url.Parse(d.daemonURL)
if err != nil {
return err
}
// serialize params the right way:
u.Path = "query"
v := u.Query()
v.Set("consistency", "all")
v.Set("q", fmt.Sprintf("CREATE DATABASE %s WITH REPLICATION %d", dbName, replicationFactor))
u.RawQuery = v.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// does the body need to be read into the void?
if resp.StatusCode != 200 {
return fmt.Errorf("bad db create")
}
time.Sleep(time.Second)
return nil
}