MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / handlePostIndexInit

Method handlePostIndexInit

rest/admin_api.go:297–376  ·  view source on GitHub ↗

handleIndexInit allows async index initialization to be run or managed

()

Source from the content-addressed store, hash-verified

295
296// handleIndexInit allows async index initialization to be run or managed
297func (h *handler) handlePostIndexInit() error {
298 action := cmp.Or(h.getQuery("action"), "start")
299
300 if action == "stop" {
301 h.server.DatabaseInitManager.Cancel(h.db.Name, fmt.Sprintf("Initialization stopped by %s", h.rq.URL))
302 if err := h.db.AsyncIndexInitManager.Stop(); err != nil {
303 return err
304 }
305 b, err := h.db.AsyncIndexInitManager.GetStatus(h.ctx())
306 if err != nil {
307 return err
308 }
309 h.writeRawJSON(b)
310 return nil
311 }
312
313 if action != "start" {
314 return base.HTTPErrorf(http.StatusBadRequest, "action %q not supported... must be either 'start' or 'stop'", action)
315 }
316
317 var req PostIndexInitRequest
318 if err := h.readJSONInto(&req); err != nil {
319 return err
320 }
321 if err := req.Validate(); err != nil {
322 return err
323 }
324
325 currentDbConfig := h.server.GetDatabaseConfig(h.db.Name)
326
327 if h.db.UseViews() {
328 return base.HTTPErrorf(http.StatusBadRequest, "_index_init is a GSI-only feature and is not supported when using views")
329 }
330
331 var newDbConfig DatabaseConfig
332 if err := base.DeepCopyInefficient(&newDbConfig, currentDbConfig); err != nil {
333 return err
334 }
335 newDbConfig.Index.NumPartitions = req.NumPartitions
336
337 var statusMap = make(db.IndexStatusByCollection, len(newDbConfig.Scopes))
338 for scope := range newDbConfig.Scopes {
339 statusMap[scope] = make(map[string]db.CollectionIndexStatus)
340 }
341 // init _default scope because it's still possible that a named scope can still initialize metadata indexes in _default._default
342 if _, ok := statusMap[base.DefaultScope]; !ok {
343 statusMap[base.DefaultScope] = make(map[string]db.CollectionIndexStatus, 1)
344 }
345 var statusCallback CollectionCallbackFunc = func(dbName string, scName base.ScopeAndCollectionName, status db.CollectionIndexStatus) {
346 statusMap[scName.ScopeName()][scName.CollectionName()] = status
347 if err := h.db.AsyncIndexInitManager.UpdateStatusClusterAware(h.ctx()); err != nil {
348 base.WarnfCtx(h.ctx(), "Unable to update async index job status on cluster : %v", err)
349 }
350 }
351
352 useLegacySyncDocsIndex := h.db.UseLegacySyncDocsIndex()
353 if req.SeparatePrincipalIndexes != nil {
354 useLegacySyncDocsIndex = !(*req.SeparatePrincipalIndexes)

Callers

nothing calls this directly

Calls 15

getQueryMethod · 0.95
ctxMethod · 0.95
writeRawJSONMethod · 0.95
readJSONIntoMethod · 0.95
ValidateMethod · 0.95
HTTPErrorfFunction · 0.92
DeepCopyInefficientFunction · 0.92
WarnfCtxFunction · 0.92
CancelMethod · 0.80
GetDatabaseConfigMethod · 0.80
UseViewsMethod · 0.80

Tested by

no test coverage detected