/
db_index_info.go
86 lines (75 loc) · 2.18 KB
/
db_index_info.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
package handler
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/goydb/goydb/internal/adapter/storage"
"github.com/goydb/goydb/pkg/model"
)
type DBIndexInfo struct {
Base
}
func (s *DBIndexInfo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
db := Database{Base: s.Base}.Do(w, r)
if db == nil {
return
}
if _, ok := (Authenticator{Base: s.Base}.DB(w, r, db)); !ok {
return
}
docID := string(model.DesignDocPrefix) + mux.Vars(r)["docid"]
doc, err := db.GetDocument(r.Context(), docID)
if err != nil {
WriteError(w, http.StatusNotFound, err.Error())
return
}
response := &ViewInfoResponse{}
response.ViewIndex.Language = doc.Language()
err = db.Transaction(r.Context(), func(tx *storage.Transaction) error {
for _, fn := range doc.Functions() {
idx, ok := db.Indices()[fn.DesignDocFn().String()]
if ok {
stat, err := idx.Stats(r.Context(), tx)
if err != nil {
return err
}
response.ViewIndex.Sizes.Active += stat.Allocated
response.ViewIndex.Sizes.External += stat.Used
}
}
return nil
})
if err != nil {
WriteError(w, http.StatusInternalServerError, err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response) // nolint: errcheck
}
type ViewInfoResponse struct {
Name string `json:"name"`
ViewIndex ViewIndex `json:"view_index"`
}
type UpdatesPending struct {
Minimum uint64 `json:"minimum"`
Preferred uint64 `json:"preferred"`
Total uint64 `json:"total"`
}
type ViewSizes struct {
File uint64 `json:"file"`
External uint64 `json:"external"`
Active uint64 `json:"active"`
}
type ViewIndex struct {
UpdatesPending UpdatesPending `json:"updates_pending"`
WaitingCommit bool `json:"waiting_commit"`
WaitingClients int `json:"waiting_clients"`
UpdaterRunning bool `json:"updater_running"`
UpdateSeq int `json:"update_seq"`
Sizes ViewSizes `json:"sizes"`
Signature string `json:"signature"`
PurgeSeq int `json:"purge_seq"`
Language string `json:"language"`
CompactRunning bool `json:"compact_running"`
}