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

Method handleFlush

rest/api.go:276–351  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

274}
275
276func (h *handler) handleFlush() error {
277
278 baseBucket := base.GetBaseBucket(h.db.Bucket)
279
280 // If it can be flushed, then flush it
281 if _, ok := baseBucket.(sgbucket.FlushableStore); ok {
282
283 // If it's not a walrus bucket, don't allow flush unless the unsupported config is set
284 if !h.db.BucketSpec.IsWalrusBucket() {
285 if !h.db.DatabaseContext.AllowFlushNonCouchbaseBuckets() {
286 return errors.New("Flush not allowed on Couchbase buckets by default.")
287 }
288 }
289
290 name := h.db.Name
291 config := h.server.GetDatabaseConfig(name)
292
293 // This needs to first call RemoveDatabase since flushing the bucket under Sync Gateway might cause issues.
294 h.server.RemoveDatabase(h.ctx(), name, fmt.Sprintf("called from %s", h.rq.URL))
295
296 // Create a bucket connection spec from the database config
297 spec, err := GetBucketSpec(h.ctx(), &config.DatabaseConfig, h.server.Config)
298 if err != nil {
299 return err
300 }
301
302 // Manually re-open a temporary bucket connection just for flushing purposes
303 tempBucketForFlush, err := db.ConnectToBucket(h.ctx(), spec, false)
304 if err != nil {
305 return err
306 }
307 defer tempBucketForFlush.Close(h.ctx()) // Close the temporary connection to the bucket that was just for purposes of flushing it
308
309 // Flush the bucket (assuming it conforms to sgbucket.DeleteableStore interface
310 if tempBucketForFlush, ok := tempBucketForFlush.(sgbucket.FlushableStore); ok {
311
312 // Flush
313 err := tempBucketForFlush.Flush()
314 if err != nil {
315 return err
316 }
317
318 }
319
320 // Re-open database and add to Sync Gateway
321 _, err2 := h.server.AddDatabaseFromConfig(h.ctx(), config.DatabaseConfig)
322 if err2 != nil {
323 return err2
324 }
325 base.Audit(h.ctx(), base.AuditIDDatabaseFlush, nil)
326
327 } else if bucket, ok := baseBucket.(sgbucket.DeleteableStore); ok {
328
329 // If it's not flushable, but it's deletable, then delete it
330
331 name := h.db.Name
332 config := h.server.GetDatabaseConfig(name)
333 h.server.RemoveDatabase(h.ctx(), name, fmt.Sprintf("called from %s", h.rq.URL))

Callers

nothing calls this directly

Calls 14

ctxMethod · 0.95
GetBaseBucketFunction · 0.92
ConnectToBucketFunction · 0.92
AuditFunction · 0.92
HTTPErrorfFunction · 0.92
GetBucketSpecFunction · 0.85
IsWalrusBucketMethod · 0.80
GetDatabaseConfigMethod · 0.80
RemoveDatabaseMethod · 0.80
AddDatabaseFromConfigMethod · 0.80
CloseAndDeleteMethod · 0.80

Tested by

no test coverage detected