-
-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
154ba08
commit a98f5b2
Showing
4 changed files
with
88 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package commitlog | ||
|
||
type Cleaner interface { | ||
Clean([]*Segment) ([]*Segment, error) | ||
} | ||
|
||
type DeleteCleaner struct { | ||
Retention struct { | ||
Bytes int64 | ||
} | ||
} | ||
|
||
func NewDeleteCleaner(bytes int64) *DeleteCleaner { | ||
c := &DeleteCleaner{} | ||
c.Retention.Bytes = bytes | ||
return c | ||
} | ||
|
||
func (c *DeleteCleaner) Clean(segments []*Segment) ([]*Segment, error) { | ||
if len(segments) == 0 || c.Retention.Bytes == -1 { | ||
return segments, nil | ||
} | ||
cleanedSegments := []*Segment{segments[len(segments)-1]} | ||
totalBytes := cleanedSegments[0].Position | ||
if len(segments) > 1 { | ||
var i int | ||
for i = len(segments) - 2; i > -1; i-- { | ||
s := segments[i] | ||
totalBytes += s.Position | ||
if totalBytes > c.Retention.Bytes { | ||
break | ||
} | ||
cleanedSegments = append([]*Segment{s}, cleanedSegments...) | ||
} | ||
if i > -1 { | ||
for ; i != 0; i-- { | ||
s := segments[i] | ||
if err := s.Delete(); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
} | ||
return cleanedSegments, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters