This repository has been archived by the owner on Feb 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.go
147 lines (118 loc) · 3.82 KB
/
database.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
package couchdb_admin
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/cabify/couchdb-admin/httpUtils"
"github.com/cabify/couchdb-admin/sliceUtils"
)
type Database struct {
name string
config Config
}
type Config struct {
Id string `json:"_id"`
Rev string `json:"_rev"`
Shards []int `json:"shard_suffix"`
Changelog [][]string `json:"changelog"`
ByNode map[string][]string `json:"by_node"`
ByRange map[string][]string `json:"by_range"`
}
func LoadDB(name string, ahr *httpUtils.AuthenticatedHttpRequester) (*Database, error) {
db := &Database{
name: name,
}
if err := db.refreshDbConfig(ahr); err != nil {
return nil, err
} else {
return db, nil
}
}
func CreateDatabase(name string, replicas, shards int, ahr *httpUtils.AuthenticatedHttpRequester) (*Database, error) {
req, err := http.NewRequest("PUT", fmt.Sprintf("http://%s:5984/%s?n=%d&q=%d", ahr.Server(), name, replicas, shards), nil)
if err != nil {
return nil, err
}
if err = ahr.RunRequest(req, nil); err != nil {
return nil, err
}
return LoadDB(name, ahr)
}
func (db *Database) refreshDbConfig(ahr *httpUtils.AuthenticatedHttpRequester) error {
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s:5986/_dbs/%s", ahr.Server(), db.name), nil)
if err != nil {
return err
}
if err = ahr.RunRequest(req, &db.config); err != nil {
return err
}
if db.config.Id == "" {
return fmt.Errorf("Could not retrieve config for db: %s", db.name)
}
return nil
}
func (db *Database) Replicate(shard, replica string, ahr *httpUtils.AuthenticatedHttpRequester) error {
replicaNode, err := NodeAt(replica)
if err != nil {
return err
}
if sliceUtils.Contains(db.config.ByNode[replicaNode.Addr()], shard) {
return fmt.Errorf("%s is already replicating %s", replicaNode.Addr(), shard)
}
if _, exists := db.config.ByRange[shard]; !exists {
return fmt.Errorf("%s is not a %s's shard!", shard, db.name)
}
cluster, err := LoadCluster(ahr)
if err != nil {
return err
}
if !cluster.IsNodeUpAndJoined(replicaNode.Addr()) {
return fmt.Errorf("%s is not part of the cluster!", replicaNode.Addr())
}
replicaNode.IntoMaintenance(ahr)
db.config.ByNode[replicaNode.Addr()] = append(db.config.ByNode[replicaNode.Addr()], shard)
db.config.ByRange[shard] = append(db.config.ByRange[shard], replicaNode.Addr())
// TODO add an entry to the changes section.
b, err := json.Marshal(db.config)
if err != nil {
return err
}
req, err := http.NewRequest("PUT", fmt.Sprintf("http://%s:5986/_dbs/%s", ahr.Server(), db.name), bytes.NewBuffer(b))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
return ahr.RunRequest(req, nil)
}
func (db *Database) RemoveReplica(shard, from string, ahr *httpUtils.AuthenticatedHttpRequester) error {
replica := fmt.Sprintf("couchdb@%s", from)
if _, exists := db.config.ByNode[replica]; !exists {
return fmt.Errorf("%s does not have any replicas!", replica)
}
if !sliceUtils.Contains(db.config.ByNode[replica], shard) {
return fmt.Errorf("Shard %s is not at %s", shard, replica)
}
newRange := sliceUtils.RemoveItem(db.config.ByRange[shard], replica)
if len(newRange) == 0 {
return fmt.Errorf("Aborting. Shard %s will be lost if deleted!!", shard)
}
db.config.ByRange[shard] = newRange
newNode := sliceUtils.RemoveItem(db.config.ByNode[replica], shard)
if len(newNode) > 0 {
db.config.ByNode[replica] = newNode
} else {
delete(db.config.ByNode, replica)
}
// TODO add an entry to the changes section.
b, err := json.Marshal(db.config)
if err != nil {
return err
}
req, err := http.NewRequest("PUT", fmt.Sprintf("http://%s:5986/_dbs/%s", ahr.Server(), db.name), bytes.NewBuffer(b))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
return ahr.RunRequest(req, nil)
}