MCPcopy
hub / github.com/rclone/rclone / CleanChunksBySize

Method CleanChunksBySize

backend/cache/storage_persistent.go:544–607  ·  view source on GitHub ↗

CleanChunksBySize will cleanup chunks after the total size passes a certain point

(maxSize int64)

Source from the content-addressed store, hash-verified

542
543// CleanChunksBySize will cleanup chunks after the total size passes a certain point
544func (b *Persistent) CleanChunksBySize(maxSize int64) {
545 b.cleanupMux.Lock()
546 defer b.cleanupMux.Unlock()
547 var cntChunks int
548 var roughlyCleaned fs.SizeSuffix
549
550 err := b.db.Update(func(tx *bolt.Tx) error {
551 dataTsBucket := tx.Bucket([]byte(DataTsBucket))
552 if dataTsBucket == nil {
553 return fmt.Errorf("couldn't open (%v) bucket", DataTsBucket)
554 }
555 // iterate through ts
556 c := dataTsBucket.Cursor()
557 totalSize := int64(0)
558 for k, v := c.First(); k != nil; k, v = c.Next() {
559 var ci chunkInfo
560 err := json.Unmarshal(v, &ci)
561 if err != nil {
562 continue
563 }
564
565 totalSize += ci.Size
566 }
567
568 if totalSize > maxSize {
569 needToClean := totalSize - maxSize
570 roughlyCleaned = fs.SizeSuffix(needToClean)
571 for k, v := c.First(); k != nil; k, v = c.Next() {
572 var ci chunkInfo
573 err := json.Unmarshal(v, &ci)
574 if err != nil {
575 continue
576 }
577 // delete this ts entry
578 err = c.Delete()
579 if err != nil {
580 fs.Errorf(ci.Path, "failed deleting chunk ts during cleanup (%v): %v", ci.Offset, err)
581 continue
582 }
583 err = os.Remove(path.Join(b.dataPath, ci.Path, strconv.FormatInt(ci.Offset, 10)))
584 if err == nil {
585 cntChunks++
586 needToClean -= ci.Size
587 if needToClean <= 0 {
588 break
589 }
590 }
591 }
592 }
593 if cntChunks > 0 {
594 fs.Infof("cache-cleanup", "chunks %v, est. size: %v", cntChunks, roughlyCleaned.String())
595
596 }
597 return nil
598 })
599
600 if err != nil {
601 if err == errors.ErrDatabaseNotOpen {

Callers

nothing calls this directly

Calls 14

StringMethod · 0.95
SizeSuffixTypeAlias · 0.92
ErrorfFunction · 0.92
InfofFunction · 0.92
FirstMethod · 0.80
JoinMethod · 0.80
LockMethod · 0.65
UnlockMethod · 0.65
UpdateMethod · 0.65
CursorMethod · 0.65
NextMethod · 0.65
DeleteMethod · 0.65

Tested by

no test coverage detected