Skip to content

Commit

Permalink
CBG-3467 add verbose mode for _all_dbs (#6493)
Browse files Browse the repository at this point in the history
  • Loading branch information
torcolvin committed Oct 5, 2023
1 parent 718b476 commit b4ce7dd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions rest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (h *handler) handleRoot() error {
}

func (h *handler) handleAllDbs() error {
if h.getBoolQuery("verbose") {
h.writeJSON(h.server.allDatabaseSummaries())
return nil
}
h.writeJSON(h.server.AllDatabaseNames())
return nil
}
Expand Down Expand Up @@ -369,6 +373,12 @@ type DatabaseRoot struct {
ServerUUID string `json:"server_uuid,omitempty"`
}

type dbSummary struct {
DBName string `json:"db_name"`
Bucket string `json:"bucket"`
State string `json:"state"`
}

func (h *handler) handleGetDB() error {
if h.rq.Method == "HEAD" {
return nil
Expand Down
13 changes: 13 additions & 0 deletions rest/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9566,3 +9566,16 @@ func rawDocWithAttachmentAndSyncMeta() []byte {
"key": "value"
}`)
}

func TestAllDbs(t *testing.T) {
rt := NewRestTester(t, nil)
defer rt.Close()

resp := rt.SendAdminRequest(http.MethodGet, "/_all_dbs", "")
RequireStatus(t, resp, http.StatusOK)
require.Equal(t, fmt.Sprintf(`["%s"]`, rt.GetDatabase().Name), resp.Body.String())

resp = rt.SendAdminRequest(http.MethodGet, "/_all_dbs?verbose=true", "")
RequireStatus(t, resp, http.StatusOK)
require.Equal(t, fmt.Sprintf(`[{"db_name":"%s","bucket":"%s","state":"Online"}]`, rt.GetDatabase().Name, rt.GetDatabase().Bucket.GetName()), resp.Body.String())
}
20 changes: 20 additions & 0 deletions rest/server_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net"
"net/http"
"os"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -256,9 +257,28 @@ func (sc *ServerContext) AllDatabaseNames() []string {
for name := range sc.databases_ {
names = append(names, name)
}
sort.Strings(names)
return names
}

func (sc *ServerContext) allDatabaseSummaries() []dbSummary {
sc.lock.RLock()
defer sc.lock.RUnlock()

dbs := make([]dbSummary, 0, len(sc.databases_))
for name, dbctx := range sc.databases_ {
dbs = append(dbs, dbSummary{
DBName: name,
Bucket: dbctx.Bucket.GetName(),
State: db.RunStateString[atomic.LoadUint32(&dbctx.State)],
})
}
sort.Slice(dbs, func(i, j int) bool {
return dbs[i].DBName < dbs[j].DBName
})
return dbs
}

// AllDatabases returns a copy of the databases_ map.
func (sc *ServerContext) AllDatabases() map[string]*db.DatabaseContext {
sc.lock.RLock()
Expand Down

0 comments on commit b4ce7dd

Please sign in to comment.