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
"""Compaction policies — when to compact the oplog.
Silk provides the compaction primitive (`store.compact()`). This module
provides policies that decide WHEN to compact. Two built-in policies
ship with Silk. Custom policies implement the `CompactionPolicy` protocol.
Usage:
from silk.compaction import IntervalPolicy, ThresholdPolicy
# Compact every hour
policy = IntervalPolicy(seconds=3600)
policy.check(store) # call periodically
# Compact when oplog exceeds 1000 entries
policy = ThresholdPolicy(max_entries=1000)
policy.check(store) # call after writes
# Custom policy
class MyPolicy:
def should_compact(self, store) -> bool:
return store.len() > 5000
def check(self, store):
if self.should_compact(store):
store.compact()
Safety note: compact() should only be called when all peers have synced
to the current state. The built-in policies don't know about peers —
the application is responsible for safety in multi-peer deployments.
For single-instance stores (no sync), compaction is always safe.
"""
"""Extension point for custom compaction policies.
Implement should_compact() and check(). Or just check() if you
want full control over the compaction decision + execution.
"""
"""Return True if the store should be compacted now."""
...
"""Check the policy and compact if needed.
Returns the checkpoint hash if compaction happened, None otherwise.
"""
...
"""Compact at most once every N seconds.
Call `check(store)` periodically (e.g., after each sync round,
or on a timer). Compaction only happens if enough time has passed
since the last compaction.
Args:
seconds: Minimum interval between compactions.
"""
=
: = 0.0
return >=
=
=
return
return None
"""Compact when the oplog exceeds a threshold.
Call `check(store)` after writes or batches. Compaction happens
when the oplog entry count exceeds the threshold.
Args:
max_entries: Compact when store.len() exceeds this.
"""
=
return >
return
return None