49 lines
1.5 KiB
Go
49 lines
1.5 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
// handleSchemaGet returns the per-field schema, folded from counters the write
|
|
// path keeps exact (see schema.Indexer).
|
|
func (s *Server) handleSchemaGet(w http.ResponseWriter, r *http.Request) {
|
|
if s.schemaIndexer == nil {
|
|
http.Error(w, "schema indexer not running", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
snap, err := s.schemaIndexer.Snapshot(r.Context())
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(snap)
|
|
}
|
|
|
|
// handleSchemaRescan rebuilds the schema counters from a full read of the
|
|
// records and returns the result. The counters are exact by construction, so
|
|
// this is a repair tool, not routine: it runs as one write transaction, and
|
|
// every other write waits for it (minutes on a large store).
|
|
func (s *Server) handleSchemaRescan(w http.ResponseWriter, r *http.Request) {
|
|
if s.schemaIndexer == nil {
|
|
http.Error(w, "schema indexer not running", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
if err := s.storage.RebuildCounters(); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
snap, err := s.schemaIndexer.Snapshot(r.Context())
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
snap.Source = "rescan"
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"published_events": 0,
|
|
"schema": snap,
|
|
})
|
|
}
|