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

Method handleCompact

rest/api.go:180–274  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

178}
179
180func (h *handler) handleCompact() error {
181 action := h.getQuery("action")
182 if action == "" {
183 action = string(db.BackgroundProcessActionStart)
184 }
185
186 if action != string(db.BackgroundProcessActionStart) && action != string(db.BackgroundProcessActionStop) {
187 return base.HTTPErrorf(http.StatusBadRequest, "Unknown parameter for 'action'. Must be start or stop")
188 }
189
190 compactionType := h.getQuery("type")
191 if compactionType == "" {
192 compactionType = compactionTypeTombstone
193 }
194
195 if compactionType != compactionTypeTombstone && compactionType != compactionTypeAttachment {
196 return base.HTTPErrorf(http.StatusBadRequest, "Unknown parameter for 'type'. Must be 'tombstone' or 'attachment'")
197 }
198
199 auditFields := base.AuditFields{base.AuditFieldCompactionType: compactionType}
200 if compactionType == compactionTypeTombstone {
201 if action == string(db.BackgroundProcessActionStart) {
202 if atomic.CompareAndSwapUint32(&h.db.CompactState, db.DBCompactNotRunning, db.DBCompactRunning) {
203 err := h.db.TombstoneCompactionManager.Start(h.ctx(), map[string]interface{}{
204 "database": h.db,
205 })
206 if err != nil {
207 return err
208 }
209
210 status, err := h.db.TombstoneCompactionManager.GetStatus(h.ctx())
211 if err != nil {
212 return err
213 }
214 h.writeRawJSON(status)
215 base.Audit(h.ctx(), base.AuditIDDatabaseCompactStart, auditFields)
216 } else {
217 return base.HTTPErrorf(http.StatusServiceUnavailable, "Database compact already in progress")
218
219 }
220 } else if action == string(db.BackgroundProcessActionStop) {
221 dbState := atomic.LoadUint32(&h.db.CompactState)
222 if dbState != db.DBCompactRunning {
223 return base.HTTPErrorf(http.StatusBadRequest, "Database compact is not running")
224 }
225
226 err := h.db.TombstoneCompactionManager.Stop()
227 if err != nil {
228 return err
229 }
230 status, err := h.db.TombstoneCompactionManager.GetStatus(h.ctx())
231 if err != nil {
232 return err
233 }
234 h.writeRawJSON(status)
235 base.Audit(h.ctx(), base.AuditIDDatabaseCompactStop, auditFields)
236 }
237 }

Callers

nothing calls this directly

Calls 9

getQueryMethod · 0.95
ctxMethod · 0.95
writeRawJSONMethod · 0.95
getBoolQueryMethod · 0.95
HTTPErrorfFunction · 0.92
AuditFunction · 0.92
StartMethod · 0.65
StopMethod · 0.65
GetStatusMethod · 0.45

Tested by

no test coverage detected