Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
litestream/compaction_level.go at main · benbjohnson/litestream · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
benbjohnson
litestream
Repository navigation
Code
Issues
90
(90)
Pull requests
56
(56)
Actions
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
litestream
/
compaction_level.go
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
113 lines (96 loc) · 3.46 KB
main
Breadcrumbs
litestream
/
compaction_level.go
Copy path
Top
File metadata and controls
Code
Blame
113 lines (96 loc) · 3.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package
litestream
import
(
"fmt"
"time"
)
// SnapshotLevel represents the level which full snapshots are held.
const
SnapshotLevel
=
9
// DefaultCompactionLevels provides the canonical default compaction configuration.
// Level 0 is raw LTX files, higher levels compact at increasing intervals.
// These values are also used by cmd/litestream DefaultConfig().
var
DefaultCompactionLevels
=
CompactionLevels
{
{
Level
:
0
,
Interval
:
0
},
{
Level
:
1
,
Interval
:
30
*
time
.
Second
},
{
Level
:
2
,
Interval
:
5
*
time
.
Minute
},
{
Level
:
3
,
Interval
:
time
.
Hour
},
}
// CompactionLevel represents a single part of a multi-level compaction.
// Each level merges LTX files from the previous level into larger time granularities.
type
CompactionLevel
struct
{
// The numeric level. Must match the index in the list of levels.
Level
int
// The frequency that the level is compacted from the previous level.
Interval
time.
Duration
}
// PrevCompactionAt returns the time when the last compaction occurred.
// Returns the current time if it is exactly a multiple of the level interval.
func
(
lvl
*
CompactionLevel
)
PrevCompactionAt
(
now
time.
Time
) time.
Time
{
return
now
.
Truncate
(
lvl
.
Interval
).
UTC
()
}
// NextCompactionAt returns the time until the next compaction occurs.
// Returns the current time if it is exactly a multiple of the level interval.
func
(
lvl
*
CompactionLevel
)
NextCompactionAt
(
now
time.
Time
) time.
Time
{
return
lvl
.
PrevCompactionAt
(
now
).
Add
(
lvl
.
Interval
)
}
// CompactionLevels represents a sorted slice of non-snapshot compaction levels.
type
CompactionLevels
[]
*
CompactionLevel
// Level returns the compaction level at the given index.
// Returns an error if the index is a snapshot level or is out of bounds.
func
(
a
CompactionLevels
)
Level
(
level
int
) (
*
CompactionLevel
,
error
) {
if
level
==
SnapshotLevel
{
return
nil
,
fmt
.
Errorf
(
"invalid argument, snapshot level"
)
}
if
level
<
0
||
level
>
a
.
MaxLevel
() {
return
nil
,
fmt
.
Errorf
(
"level out of bounds: %d"
,
level
)
}
return
a
[
level
],
nil
}
// MaxLevel return the highest non-snapshot compaction level.
func
(
a
CompactionLevels
)
MaxLevel
()
int
{
return
len
(
a
)
-
1
}
// Validate returns an error if the levels are invalid.
func
(
a
CompactionLevels
)
Validate
()
error
{
if
len
(
a
)
==
0
{
return
fmt
.
Errorf
(
"at least one compaction level is required"
)
}
for
i
,
lvl
:=
range
a
{
if
i
!=
lvl
.
Level
{
return
fmt
.
Errorf
(
"compaction level number out of order: %d, expected %d"
,
lvl
.
Level
,
i
)
}
else
if
lvl
.
Level
>
SnapshotLevel
-
1
{
return
fmt
.
Errorf
(
"compaction level cannot exceed %d"
,
SnapshotLevel
-
1
)
}
if
lvl
.
Level
==
0
&&
lvl
.
Interval
!=
0
{
return
fmt
.
Errorf
(
"cannot set interval on compaction level zero"
)
}
if
lvl
.
Level
!=
0
&&
lvl
.
Interval
<=
0
{
return
fmt
.
Errorf
(
"interval required for level %d"
,
lvl
.
Level
)
}
}
return
nil
}
// IsValidLevel returns true if level is a valid compaction level number.
func
(
a
CompactionLevels
)
IsValidLevel
(
level
int
)
bool
{
if
level
==
SnapshotLevel
{
return
true
}
return
level
>=
0
&&
level
<
len
(
a
)
}
// PrevLevel returns the previous compaction level.
// Returns -1 if there is no previous level.
func
(
a
CompactionLevels
)
PrevLevel
(
level
int
)
int
{
if
level
==
SnapshotLevel
{
return
a
.
MaxLevel
()
}
return
level
-
1
}
// NextLevel returns the next compaction level.
// Returns -1 if there is no next level.
func
(
a
CompactionLevels
)
NextLevel
(
level
int
)
int
{
if
level
==
SnapshotLevel
{
return
-
1
}
else
if
level
==
a
.
MaxLevel
() {
return
SnapshotLevel
}
return
level
+
1
}
You can’t perform that action at this time.