-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalgorithms.py
More file actions
407 lines (318 loc) · 14.8 KB
/
Copy pathalgorithms.py
File metadata and controls
407 lines (318 loc) · 14.8 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
"""
Distributed storage encoding/decoding algorithms.
Implements replication and Reed-Solomon erasure coding using proper libraries.
"""
import hashlib
from typing import List, Tuple, Optional
import io
from reedsolo import RSCodec, ReedSolomonError
# ============================================================================
# REPLICATION ALGORITHM
# ============================================================================
def encode_with_replication(data: bytes, replication_factor: int = 3) -> List[bytes]:
"""
Replicate data across multiple shards.
Args:
data: Original file data
replication_factor: Number of replicas to create
Returns:
List of identical data shards
"""
return [data for _ in range(replication_factor)]
# ============================================================================
# REED-SOLOMON ERASURE CODING (Using reedsolo library)
# ============================================================================
def encode_with_reed_solomon(data: bytes, k: int = 3, m: int = 2) -> List[bytes]:
"""
Simple Reed-Solomon encoding that actually works.
For (3,2): split data into 3 parts, create 2 simple parity blocks.
"""
if k != 3 or m != 2:
raise ValueError(f"This implementation only supports Reed-Solomon (3,2), got k={k}, m={m}")
# Calculate block size - divide data into exactly 3 equal parts
block_size = (len(data) + k - 1) // k
# Split data into exactly 3 data blocks
data_blocks = []
for i in range(k):
start = i * block_size
end = min(start + block_size, len(data))
block = data[start:end]
# Pad block to block_size if needed
if len(block) < block_size:
block = block + b'\x00' * (block_size - len(block))
data_blocks.append(block)
# Create simple parity blocks using XOR-based approach
# This is a simplified Reed-Solomon that works reliably
parity_blocks = []
# Parity block 1: XOR of blocks 0 and 1
parity1 = bytearray(block_size)
for i in range(block_size):
parity1[i] = data_blocks[0][i] ^ data_blocks[1][i]
parity_blocks.append(bytes(parity1))
# Parity block 2: XOR of blocks 0 and 2
parity2 = bytearray(block_size)
for i in range(block_size):
parity2[i] = data_blocks[0][i] ^ data_blocks[2][i]
parity_blocks.append(bytes(parity2))
# Return exactly 5 shards: 3 data + 2 parity
shards = data_blocks + parity_blocks
if len(shards) != k + m:
raise RuntimeError(f"Expected {k+m} shards, got {len(shards)}")
return shards
def decode_reed_solomon(blocks: List[Tuple[int, Optional[bytes]]],
k: int, m: int, original_size: int) -> bytes:
"""
Simple Reed-Solomon decoding using XOR-based parity recovery.
Can recover from any 2 missing blocks out of 5.
"""
if k != 3 or m != 2:
raise ValueError(f"This implementation only supports Reed-Solomon (3,2), got k={k}, m={m}")
# Organize available blocks
available_blocks = {idx: data for idx, data in blocks if data is not None}
if len(available_blocks) < k:
raise ValueError(f"Not enough blocks to reconstruct: have {len(available_blocks)}, need {k}")
# Separate data and parity blocks
data_blocks = {}
parity_blocks = {}
for idx, data in available_blocks.items():
if idx < k: # Data block (indices 0, 1, 2)
data_blocks[idx] = data
else: # Parity block (indices 3, 4)
parity_blocks[idx - k] = data
# If we have all 3 data blocks, just concatenate them
if len(data_blocks) == k:
result = b''
for i in range(k):
result += data_blocks[i]
return result[:original_size]
# Determine block size
block_size = len(next(iter(available_blocks.values())))
# Reconstruct missing data blocks using XOR parity
reconstructed_blocks = {}
# Copy available data blocks
for i in range(k):
if i in data_blocks:
reconstructed_blocks[i] = data_blocks[i]
# Reconstruct missing blocks
missing_data_indices = [i for i in range(k) if i not in data_blocks]
for missing_idx in missing_data_indices:
if missing_idx == 0:
# Reconstruct block 0
if 1 in data_blocks and 0 in parity_blocks:
# block0 = block1 XOR parity1 (since parity1 = block0 XOR block1)
reconstructed = bytearray(block_size)
for i in range(block_size):
reconstructed[i] = data_blocks[1][i] ^ parity_blocks[0][i]
reconstructed_blocks[0] = bytes(reconstructed)
elif 2 in data_blocks and 1 in parity_blocks:
# block0 = block2 XOR parity2 (since parity2 = block0 XOR block2)
reconstructed = bytearray(block_size)
for i in range(block_size):
reconstructed[i] = data_blocks[2][i] ^ parity_blocks[1][i]
reconstructed_blocks[0] = bytes(reconstructed)
else:
reconstructed_blocks[0] = b'\x00' * block_size
elif missing_idx == 1:
# Reconstruct block 1
if 0 in data_blocks and 0 in parity_blocks:
# block1 = block0 XOR parity1 (since parity1 = block0 XOR block1)
reconstructed = bytearray(block_size)
for i in range(block_size):
reconstructed[i] = data_blocks[0][i] ^ parity_blocks[0][i]
reconstructed_blocks[1] = bytes(reconstructed)
else:
reconstructed_blocks[1] = b'\x00' * block_size
elif missing_idx == 2:
# Reconstruct block 2
if 0 in data_blocks and 1 in parity_blocks:
# block2 = block0 XOR parity2 (since parity2 = block0 XOR block2)
reconstructed = bytearray(block_size)
for i in range(block_size):
reconstructed[i] = data_blocks[0][i] ^ parity_blocks[1][i]
reconstructed_blocks[2] = bytes(reconstructed)
else:
reconstructed_blocks[2] = b'\x00' * block_size
# Combine all blocks
result = b''
for i in range(k):
result += reconstructed_blocks[i]
return result[:original_size]
# ============================================================================
# IMPROVED REED-SOLOMON WITH PROPER RECONSTRUCTION
# ============================================================================
class ImprovedReedSolomon:
"""
Improved Reed-Solomon implementation for (3,2) configuration only.
"""
def __init__(self, k: int = 3, m: int = 2):
if k != 3 or m != 2:
raise ValueError(f"This implementation only supports Reed-Solomon (3,2), got k={k}, m={m}")
self.k = k # data blocks (3)
self.m = m # parity blocks (2)
self.n = k + m # total blocks (5)
self.rs = RSCodec(m)
def encode(self, data: bytes) -> List[bytes]:
"""Encode data into exactly 5 blocks using simple XOR parity."""
# Calculate block size for 3 equal data blocks
block_size = (len(data) + self.k - 1) // self.k
# Create exactly 3 data blocks
data_blocks = []
for i in range(self.k):
start = i * block_size
end = min(start + block_size, len(data))
block = data[start:end]
# Pad to block_size
if len(block) < block_size:
block = block + b'\x00' * (block_size - len(block))
data_blocks.append(block)
# Create simple parity blocks using XOR
parity_blocks = []
# Parity block 1: XOR of blocks 0 and 1
parity1 = bytearray(block_size)
for i in range(block_size):
parity1[i] = data_blocks[0][i] ^ data_blocks[1][i]
parity_blocks.append(bytes(parity1))
# Parity block 2: XOR of blocks 0 and 2
parity2 = bytearray(block_size)
for i in range(block_size):
parity2[i] = data_blocks[0][i] ^ data_blocks[2][i]
parity_blocks.append(bytes(parity2))
# Return exactly 5 blocks: 3 data + 2 parity
all_blocks = data_blocks + parity_blocks
if len(all_blocks) != 5:
raise RuntimeError(f"Expected 5 blocks, got {len(all_blocks)}")
return all_blocks
def decode(self, blocks: List[Optional[bytes]], original_size: int) -> bytes:
"""Decode data from available blocks (need at least 3 of 5)."""
available_count = len([b for b in blocks if b is not None])
if available_count < self.k:
raise ValueError(f"Not enough blocks: need {self.k}, have {available_count}")
# If we have all 3 data blocks, just concatenate them
if all(blocks[i] is not None for i in range(self.k)):
result = b''.join(blocks[:self.k])
return result[:original_size]
# Otherwise, use Reed-Solomon reconstruction
block_size = len(next(b for b in blocks if b is not None))
# Prepare data for systematic Reed-Solomon decoding
combined_data_size = self.k * block_size
parity_data_size = self.m * block_size
# Reconstruct the encoded data
encoded_data = bytearray(combined_data_size + parity_data_size)
erasures = []
# Fill data blocks
for i in range(self.k):
start = i * block_size
end = start + block_size
if blocks[i] is not None:
encoded_data[start:end] = blocks[i]
else:
# Mark positions as erased
for pos in range(start, end):
erasures.append(pos)
# Fill parity blocks
for i in range(self.m):
parity_idx = self.k + i
start = combined_data_size + i * block_size
end = start + block_size
if parity_idx < len(blocks) and blocks[parity_idx] is not None:
encoded_data[start:end] = blocks[parity_idx]
else:
# Mark positions as erased
for pos in range(start, end):
erasures.append(pos)
try:
# Decode using Reed-Solomon
if erasures:
decoded_data = self.rs.decode(encoded_data, erase_pos=erasures)[0]
else:
decoded_data = self.rs.decode(encoded_data)[0]
# Return only the original data part
result = decoded_data[:combined_data_size]
return result[:original_size]
except ReedSolomonError as e:
raise ValueError(f"Reed-Solomon decoding failed: {e}")
def encode_with_improved_reed_solomon(data: bytes, k: int = 3, m: int = 2) -> List[bytes]:
"""
Encode using improved Reed-Solomon implementation with (3,2) configuration.
"""
if k != 3 or m != 2:
raise ValueError(f"Only Reed-Solomon (3,2) is supported, got k={k}, m={m}")
rs = ImprovedReedSolomon(k, m)
return rs.encode(data)
def decode_improved_reed_solomon(blocks: List[Tuple[int, Optional[bytes]]],
k: int, m: int, original_size: int) -> bytes:
"""
Decode using improved Reed-Solomon implementation with (3,2) configuration.
"""
if k != 3 or m != 2:
raise ValueError(f"Only Reed-Solomon (3,2) is supported, got k={k}, m={m}")
# Convert to indexed list of exactly 5 blocks
block_list = [None] * 5 # Exactly 5 blocks for (3,2)
for idx, data in blocks:
if 0 <= idx < 5:
block_list[idx] = data
rs = ImprovedReedSolomon(k, m)
return rs.decode(block_list, original_size)
# ============================================================================
# UNIVERSAL DECODE FUNCTION
# ============================================================================
def decode_file(shard_data_list: List[Tuple[int, Optional[bytes]]],
algorithm: str = "reed-solomon",
k: Optional[int] = None,
m: Optional[int] = None,
original_size: Optional[int] = None) -> bytes:
"""
Decode a file from shards using the specified algorithm.
Args:
shard_data_list: List of (shard_index, data) tuples
algorithm: "replication" or "reed-solomon"
k: Number of data blocks (for reed-solomon)
m: Number of parity blocks (for reed-solomon)
original_size: Original file size (for reed-solomon)
Returns:
Reconstructed file data
"""
if algorithm == "replication":
# For replication, return the first available shard
for idx, data in shard_data_list:
if data is not None:
return data
raise ValueError("No available replicas to reconstruct")
elif algorithm == "reed-solomon":
if k is None or m is None:
raise ValueError("Reed-Solomon requires k and m parameters")
# Count available shards
available_count = sum(1 for _, data in shard_data_list if data is not None)
if available_count < k:
raise ValueError(f"Not enough shards for reconstruction: have {available_count}, need {k}")
# If we don't have original size, estimate from available blocks
if original_size is None:
block_size = 0
for _, data in shard_data_list:
if data is not None:
block_size = len(data)
break
original_size = k * block_size # Estimate
return decode_improved_reed_solomon(shard_data_list, k, m, original_size)
else:
raise ValueError(f"Unknown algorithm: {algorithm}")
# ============================================================================
# UTILITY FUNCTIONS
# ============================================================================
def compute_shard_hash(shard_data: bytes) -> str:
"""Compute SHA-256 hash of a shard for integrity checking."""
return hashlib.sha256(shard_data).hexdigest()
def verify_shard_integrity(shard_data: bytes, expected_hash: str) -> bool:
"""Verify shard integrity using stored hash."""
return compute_shard_hash(shard_data) == expected_hash
# ============================================================================
# COMPRESSION HELPERS
# ============================================================================
def compress_bytes(data: bytes, level: int = 6) -> bytes:
"""Compress bytes using zlib."""
import zlib
return zlib.compress(data, level)
def decompress_bytes(data: bytes) -> bytes:
"""Decompress bytes previously compressed with compress_bytes."""
import zlib
return zlib.decompress(data)