forked from S4tvara/Sietch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.go
More file actions
191 lines (161 loc) · 5.47 KB
/
manager.go
File metadata and controls
191 lines (161 loc) · 5.47 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package deduplication
import (
"fmt"
"github.com/substantialcattle5/sietch/internal/config"
"github.com/substantialcattle5/sietch/internal/fs"
"github.com/substantialcattle5/sietch/util"
)
// Manager handles deduplication operations for a vault
type Manager struct {
vaultRoot string
config config.DeduplicationConfig
index *DeduplicationIndex
progressMgr ProgressManager
}
// ProgressManager is an interface for progress reporting
type ProgressManager interface {
PrintVerbose(format string, args ...interface{})
}
// NewManager creates a new deduplication manager
func NewManager(vaultRoot string, dedupConfig config.DeduplicationConfig) (*Manager, error) {
index, err := NewDeduplicationIndex(vaultRoot)
if err != nil {
return nil, fmt.Errorf("failed to create deduplication index: %w", err)
}
return &Manager{
vaultRoot: vaultRoot,
config: dedupConfig,
index: index,
progressMgr: nil, // Will be set later if needed
}, nil
}
// SetProgressManager sets the progress manager for verbose output
func (m *Manager) SetProgressManager(pm ProgressManager) {
m.progressMgr = pm
}
// ProcessChunk processes a chunk for deduplication
// Returns: (chunkRef, deduplicated, error)
func (m *Manager) ProcessChunk(chunkRef config.ChunkRef, chunkData []byte, storageHash string) (config.ChunkRef, bool, error) {
if !m.config.Enabled {
// Deduplication disabled, store chunk normally
if err := m.storeChunk(storageHash, chunkData); err != nil {
return chunkRef, false, err
}
return chunkRef, false, nil
}
// Check if we should deduplicate this chunk based on size constraints
if !m.shouldDeduplicateChunk(chunkRef.Size) {
// Store chunk normally without deduplication
if err := m.storeChunk(storageHash, chunkData); err != nil {
return chunkRef, false, err
}
return chunkRef, false, nil
}
// Check if chunk already exists in index
entry, deduplicated := m.index.AddChunk(chunkRef, storageHash)
if deduplicated {
// Chunk already exists, no need to store it again
chunkRef.Deduplicated = true
if m.progressMgr != nil {
m.progressMgr.PrintVerbose(" └─ Deduplicated chunk %s (ref count: %d)\n",
chunkRef.Hash[:12], entry.RefCount)
}
} else {
// New chunk, store it
if err := m.storeChunk(storageHash, chunkData); err != nil {
// Remove from index if storage failed
if m.index.RemoveChunk(chunkRef.Hash) != nil {
fmt.Printf("Warning: failed to remove chunk %s from index: %v\n", chunkRef.Hash, err)
}
return chunkRef, false, err
}
chunkRef.Deduplicated = false
}
return chunkRef, deduplicated, nil
}
// shouldDeduplicateChunk checks if a chunk should be deduplicated based on configuration
func (m *Manager) shouldDeduplicateChunk(chunkSize int64) bool {
if !m.config.Enabled {
return false
}
minSize, err := util.ParseChunkSize(m.config.MinChunkSize)
if err != nil {
minSize = 1024 // Default to 1KB
}
maxSize, err := util.ParseChunkSize(m.config.MaxChunkSize)
if err != nil {
maxSize = 64 * 1024 * 1024 // Default to 64MB
}
return chunkSize >= minSize && chunkSize <= maxSize
}
// storeChunk stores a chunk to the filesystem
func (m *Manager) storeChunk(storageHash string, chunkData []byte) error {
return fs.StoreChunk(m.vaultRoot, storageHash, chunkData)
}
// GetStats returns deduplication statistics
func (m *Manager) GetStats() DeduplicationStats {
return m.index.GetStats()
}
// GarbageCollect removes unreferenced chunks
func (m *Manager) GarbageCollect() (int, error) {
return m.index.GarbageCollect()
}
// Save saves the deduplication index
func (m *Manager) Save() error {
return m.index.Save()
}
// RemoveFileChunks removes all chunks associated with a file
func (m *Manager) RemoveFileChunks(chunks []config.ChunkRef) error {
for _, chunk := range chunks {
if err := m.index.RemoveChunk(chunk.Hash); err != nil {
return fmt.Errorf("failed to remove chunk %s: %w", chunk.Hash, err)
}
}
return nil
}
// OptimizeStorage performs optimization operations
func (m *Manager) OptimizeStorage() (*OptimizationResult, error) {
stats := m.GetStats()
// Perform garbage collection
removedChunks, err := m.GarbageCollect()
if err != nil {
return nil, fmt.Errorf("garbage collection failed: %w", err)
}
// Save index after optimization
if err := m.Save(); err != nil {
return nil, fmt.Errorf("failed to save index after optimization: %w", err)
}
return &OptimizationResult{
RemovedChunks: removedChunks,
TotalChunks: stats.TotalChunks,
SavedSpace: stats.SavedSpace,
UnreferencedChunks: stats.UnreferencedChunks,
}, nil
}
// OptimizationResult contains the results of storage optimization
type OptimizationResult struct {
RemovedChunks int `json:"removed_chunks"`
TotalChunks int `json:"total_chunks"`
SavedSpace int64 `json:"saved_space"`
UnreferencedChunks int `json:"unreferenced_chunks"`
}
// ChunkExists checks if a chunk exists (for compatibility with existing code)
func (m *Manager) ChunkExists(hash string) bool {
if !m.config.Enabled {
return fs.ChunkExists(m.vaultRoot, hash)
}
return m.index.HasChunk(hash)
}
// GetChunk retrieves a chunk (for compatibility with existing code)
func (m *Manager) GetChunk(hash string) ([]byte, error) {
if !m.config.Enabled {
return fs.GetChunk(m.vaultRoot, hash)
}
// Get chunk metadata from index
entry, exists := m.index.GetChunk(hash)
if !exists {
return nil, fmt.Errorf("chunk not found: %s", hash)
}
// Retrieve chunk using storage hash
return fs.GetChunk(m.vaultRoot, entry.StorageHash)
}