-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag_manager.cpp
More file actions
632 lines (577 loc) · 32.1 KB
/
tag_manager.cpp
File metadata and controls
632 lines (577 loc) · 32.1 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#include "tag_manager.h"
TagManager::TagManager(int M, int N, int slicing_count)
: tag_delete_prob(M + 1, std::vector<double>(slicing_count, 0.0f)),
disk_partition_usage_tagnum(N + 1, std::vector<std::vector<int>>(DISK_PARTITIONS + 1, std::vector<int>(M + 1, 0))), // 支持多个标签共存
disk_partition_usage_tagkind(N + 1, std::vector<std::set<int>>(DISK_PARTITIONS + 1)), // 记录每个硬盘上的区间块已分配的标签种类数
disk_tag_kind(N + 1), // 记录每个硬盘上的标签
disk_tag_partition_num(N + 1), // 记录每个硬盘上的标签及其区间块数量
tag_disk_partition(M + 1) // 记录每个标签分配的所有硬盘id和区间块id
{
// 清空属性
for (auto& inner_vec : disk_partition_usage_tagkind) {
for (auto& s : inner_vec) {
s.clear();
}
}
for (auto& matrix : disk_partition_usage_tagnum) { // 已经初始化完毕,不需要再清空
for (auto& vec : matrix) {
std::fill(vec.begin(), vec.end(), 0);
}
}
for (auto& s : disk_tag_kind) s.clear(); // 清空硬盘上的标签数量
// 下面两个效果相同:对于 int 来说默认就是 0,因此,两种方法在后续访问时都能确保未存在的键对应的值是 0
// for (auto& m : disk_tag_partition_num) m.clear(); // 清空硬盘上的标签及其区间块数量
for (int disk_id = 1; disk_id <= N; disk_id++) {
for (int tag = 1; tag <= M; tag++) {
disk_tag_partition_num[disk_id][tag] = 0;
}
}
// for (auto& n : tag_disk_partition) n.clear(); // 清空标签分配的所有硬盘id和区间块id
for (int tag = 1; tag <= M; ++tag) {
for (auto& pair : tag_disk_partition[tag]) {
pair.second.clear();
}
}
zero_tag_partitions.clear();
one_tag_partitions.clear();
two_tag_partitions.clear();
three_tag_partitions.clear();
more_tag_partitions.clear();
// 因为一开始所有区间块含有的标签数量为0,所以将所有区间块添加到 zero_tag
for (int disk_id = 1; disk_id <= N; ++disk_id) {
for (int block = 1; block <= DISK_PARTITIONS; ++block) {
zero_tag_partitions.insert({disk_id, block});
}
}
}
void TagManager::init(const std::vector<std::vector<int>>& sum, const std::vector<std::vector<int>>& conflict_matrix, std::vector<Disk>& disks) {
// 初始化局部变量
std::vector<int> tag_required_blocks(M + 1, 0); // 每个标签需要的初始划分区间块数量
std::vector<int> disk_remaining_partitions(N + 1, DISK_PARTITIONS); // 每个硬盘剩余区间块
std::vector<bool> disk_used(N + 1, false); // 记录硬盘是否已被占用
std::set<int> selected_disks; // 记录标签已选择的硬盘,最多3个
std::vector<int> disk_first_empty_partition(N + 1, 1); // 记录每个硬盘第一个为空的区间块id
// // 清空属性
// for (auto& inner_vec : disk_partition_usage_tagkind) {
// for (auto& s : inner_vec) {
// s.clear();
// }
// }
// for (auto& matrix : disk_partition_usage_tagnum) { // 已经初始化完毕,不需要再清空
// for (auto& vec : matrix) {
// std::fill(vec.begin(), vec.end(), 0);
// }
// }
// for (auto& s : disk_tag_kind) s.clear(); // 清空硬盘上的标签数量
// // 下面两个效果相同:对于 int 来说默认就是 0,因此,两种方法在后续访问时都能确保未存在的键对应的值是 0
// // for (auto& m : disk_tag_partition_num) m.clear(); // 清空硬盘上的标签及其区间块数量
// for (int disk_id = 1; disk_id <= N; disk_id++) {
// for (int tag = 1; tag <= M; tag++) {
// disk_tag_partition_num[disk_id][tag] = 0;
// }
// }
// // for (auto& n : tag_disk_partition) n.clear(); // 清空标签分配的所有硬盘id和区间块id
// for (int tag = 1; tag <= M; ++tag) {
// for (auto& pair : tag_disk_partition[tag]) {
// pair.second.clear();
// }
// }
// zero_tag_partitions.clear();
// one_tag_partitions.clear();
// two_tag_partitions.clear();
// three_tag_partitions.clear();
// more_tag_partitions.clear();
// // 因为一开始所有区间块含有的标签数量为0,所以将所有区间块添加到 zero_tag
// for (int disk_id = 1; disk_id <= N; ++disk_id) {
// for (int block = 1; block <= DISK_PARTITIONS; ++block) {
// zero_tag_partitions.insert({disk_id, block});
// }
// }
// ========== Lambda:更新变量,维护属性 ==========
// disk_id: 硬盘id,tag: 标签id
auto update_tag_info_after_init = [&](int disk_id, int tag) {
// 维护属性
int allocated = 0;
for (int partition_id = disk_first_empty_partition[disk_id]; partition_id <= DISK_PARTITIONS; partition_id++) {
disk_partition_usage_tagkind[disk_id][partition_id].insert(tag);
// tag_disk_partition[tag][disk_id].push_back(partition_id);
tag_disk_partition[tag][disk_id].insert(partition_id);
zero_tag_partitions.erase({disk_id, partition_id}); // 将该区间块从 zero_tag_partitions 中移除
one_tag_partitions.insert({disk_id, partition_id}); // 将该区间块加入 one_tag_partitions
allocated++;
if (allocated == std::min(tag_required_blocks[tag], disk_remaining_partitions[disk_id])) break; // 分配足够的区间块后退出
}
disk_tag_kind[disk_id].insert(tag);
// disk_tag_partition_num[disk_id][tag] += std::min(tag_required_blocks[tag], disk_remaining_partitions[disk_id]);
// disk_tag_partition_num[disk_id][tag] = std::min(tag_required_blocks[tag], disk_remaining_partitions[disk_id]);
// disk_tag_partition_num[disk_id][tag] = allocated;
disk_tag_partition_num[disk_id][tag] += allocated;
// 更新变量
// disk_first_empty_partition[disk_id] += std::min(tag_required_blocks[tag], disk_remaining_partitions[disk_id]);
disk_first_empty_partition[disk_id] += allocated;
// disk_remaining_partitions[disk_id] = std::max(disk_remaining_partitions[disk_id] - tag_required_blocks[tag], 0);
disk_remaining_partitions[disk_id] = disk_remaining_partitions[disk_id] - allocated;
disk_used[disk_id] = true; // 标记该硬盘已被占用
selected_disks.insert(disk_id); // 记录已选择的硬盘
};
// ========== Lambda:从未选择的硬盘中选择最佳硬盘 + 更新变量,维护属性 ==========
// tag: 标签id,mode:选择剩余区间块是否足够的硬盘(0:足够,1:不足)
auto select_best_disk = [&](int tag, int mode) {
// std::vector<int> disk_conflict(N + 1, std::numeric_limits<int>::max()); // 记录硬盘冲突值
// std::vector<int> disk_used_count(N + 1, std::numeric_limits<int>::max()); // 记录硬盘使用的区间块数量
bool found = false; // 记录是否找到最佳硬盘
int best_disk_id = -1; // 记录最佳硬盘id
int best_conflict_sum = std::numeric_limits<int>::max(); // 记录最佳硬盘冲突值
int best_write_conflict_sum = std::numeric_limits<int>::max(); // 记录最佳硬盘写冲突值
int best_used_count = std::numeric_limits<int>::max(); // 记录最佳硬盘使用的区间块数量
double best_score = std::numeric_limits<double>::lowest(); // 记录最佳硬盘得分
// 计算最大值
double max_used_count;
double max_conflict_sum;
double max_write_conflict_sum;
for (int disk_id = 1; disk_id <= N; disk_id++) {
if (selected_disks.count(disk_id)) continue;
if (disk_tag_partition_num[disk_id][tag] > MAX_PARTITIONS_PER_TAG) continue; // 不允许该标签在该硬盘上的区间块数超出上限
if (mode == 0) {
if (disk_remaining_partitions[disk_id] < tag_required_blocks[tag]) continue; // 剩余区间块不足
}
// 计算硬盘冲突值
int conflict_sum = 0;
for (const auto& exist_tag : disk_tag_kind[disk_id])
conflict_sum += conflict_matrix[tag][exist_tag];
if (conflict_sum>max_conflict_sum) max_conflict_sum = conflict_sum;
// 计算硬盘已使用区间块数量
int used_count = 0;
for (const auto &entry : disk_tag_partition_num[disk_id])
used_count += entry.second;
if (used_count>max_used_count) max_used_count = used_count;
// 计算硬盘写冲突值
int write_conflict_sum = 0;
for (const auto& exist_tag : disk_tag_kind[disk_id])
write_conflict_sum += write_conflict_matrix[tag][exist_tag];
if (write_conflict_sum>max_write_conflict_sum) max_write_conflict_sum = write_conflict_sum;
}
for (int disk_id = 1; disk_id <= N; disk_id++) {
if (selected_disks.count(disk_id)) continue;
if (disk_tag_partition_num[disk_id][tag] > MAX_PARTITIONS_PER_TAG) continue; // 不允许该标签在该硬盘上的区间块数超出上限
if (mode == 0) {
if (disk_remaining_partitions[disk_id] < tag_required_blocks[tag]) continue; // 剩余区间块不足
}
// 计算硬盘冲突值
int conflict_sum = 0;
for (const auto& exist_tag : disk_tag_kind[disk_id])
conflict_sum += conflict_matrix[tag][exist_tag];
// 计算硬盘已使用区间块数量
int used_count = 0;
for (const auto &entry : disk_tag_partition_num[disk_id])
used_count += entry.second;
// 计算硬盘写冲突值
int write_conflict_sum = 0;
for (const auto& exist_tag : disk_tag_kind[disk_id])
write_conflict_sum += write_conflict_matrix[tag][exist_tag];
// // 选择策略1:
// // 1.选择硬盘冲突值最低的硬盘
// // 2.选择硬盘使用的区间块数量最小的硬盘
// if (!found ||
// (conflict_sum < best_conflict_sum) ||
// (conflict_sum == best_conflict_sum && used_count < best_used_count)) {
// best_conflict_sum = conflict_sum;
// best_used_count = used_count;
// best_disk_id = disk_id;
// found = true;
// }
// 选择策略2:
// 1.选择硬盘使用的区间块数量最小的硬盘
// 2.选择硬盘冲突值最低的硬盘
if (!found ||
(used_count < best_used_count) ||
(used_count = best_used_count && conflict_sum < best_conflict_sum)
) {
best_conflict_sum = conflict_sum;
best_used_count = used_count;
best_disk_id = disk_id;
found = true;
}
// // 选择策略3:
// // 1.选择硬盘写冲突值最低的硬盘
// // 2.选择硬盘使用的区间块数量最小的硬盘
// if (!found ||
// (write_conflict_sum < best_write_conflict_sum) ||
// (write_conflict_sum = best_write_conflict_sum && used_count < best_used_count)
// ) {
// best_write_conflict_sum = write_conflict_sum;
// best_used_count = used_count;
// best_disk_id = disk_id;
// found = true;
// }
// // 选择策略4:
// // used_count 惩罚
// // conflict_sum 惩罚
// // socre = w1 * used_count + w2 * conflict_sum
// double w1 = 10.0; // 1 10 都一样的低
// double w2 = 1.0;
// double norm_used_count = used_count / max_used_count;
// double norm_conflict_sum = conflict_sum / max_conflict_sum;
// double score = - w1 * norm_used_count - w2 * norm_conflict_sum;
// if (!found ||
// (score > best_score)
// ) {
// best_score = score;
// best_disk_id = disk_id;
// found = true;
// }
// // 选择策略5:
// // socre = w1 * used_count + w2 * conflict_sum + w3 * write_conflict_sum
// double w1 = 0.5;
// double w2 = 0.1;
// double w3 = 0.1;
// double norm_used_count = used_count / max_used_count;
// double norm_conflict_sum = conflict_sum / max_conflict_sum;
// double norm_write_conflict_sum = write_conflict_sum / max_write_conflict_sum;
// double score = - w1 * norm_used_count - w2 * norm_conflict_sum - w3 * norm_write_conflict_sum;
// if (!found ||
// (score > best_score)
// ) {
// best_score = score;
// best_disk_id = disk_id;
// found = true;
// }
// // 无脑添加第一个硬盘
// if (found) {
// update_tag_info_after_init(best_disk_id, tag);
// if (selected_disks.size() == REP_NUM) return; // 已经选择了3个硬盘,返回
// }
}
// 遍历完成后的最佳硬盘选择
if (found) {
update_tag_info_after_init(best_disk_id, tag);
if (selected_disks.size() == REP_NUM) return; // 已经选择了3个硬盘,返回
}
};
// 计算每个标签需要的最小区间块数
for (int i = 1; i <= M; i++) {
int max_storage = SCALE * sum[i][std::min(PARTITION_ALLOCATION_THRESHOLD, (int)sum[i].size() - 1)];
tag_required_blocks[i] = std::ceil((double)max_storage / disks[1].get_partition_size());
}
// // 打印 tag_required_blocks
// std::cerr << "tag_required_blocks: " << std::endl;
// for (int i = 1; i <= M; i++) {
// std::cerr << "tag" << i << " : "<< tag_required_blocks[i] << std::endl;
// }
// 遍历标签,为每个标签分配三个硬盘
std::vector<int> indices(M);
for (int i = 0; i < M; ++i) {
indices[i] = i + 1;
}
// 根据 tag_conflict_sum 降序排序索引
std::sort(indices.begin(), indices.end(), [&](int a, int b) {
return tag_conflict_sum[a] > tag_conflict_sum[b];
});
// // 打印排序后的indices值
// std::cerr << "indices:";
// for (int index : indices) {
// std::cerr << index << " ";
// }
// std::cerr << std::endl;
// 优先给最冲突的标签分配硬盘,indices[i]为标签索引范围1~M
for (int i = 0; i < M; i++) {
selected_disks.clear(); // 记录该标签已选择的硬盘
// 第一选择该标签没有选过的硬盘,空的硬盘,剩余区间块足够的硬盘
for (int disk_id = 1; disk_id <= N; disk_id++) {
if (selected_disks.count(disk_id)) continue; // 不能重复选择硬盘
if (disk_used[disk_id]) continue;
if (disk_remaining_partitions[disk_id] < tag_required_blocks[indices[i]]) continue; // 剩余区间块不足
update_tag_info_after_init(disk_id, indices[i]);
if (selected_disks.size() == REP_NUM) break; // 已经选择了3个硬盘,退出循环
}
if (selected_disks.size() == REP_NUM) continue; // 已经选择了3个硬盘,退出循环
// 第二选择该标签没有选过的硬盘,剩余区间块足够的硬盘
for (int a = selected_disks.size() + 1; a <= REP_NUM; a++) {
select_best_disk(indices[i], 0);
if (selected_disks.size() == REP_NUM) break; // 已经选择了3个硬盘,退出循环
}
if (selected_disks.size() == REP_NUM) continue; // 已经选择了3个硬盘,退出循环
// 第三选择该标签没有选过的硬盘,剩余区间块不足的硬盘
for (int a = selected_disks.size() + 1; a <= REP_NUM; a++) {
select_best_disk(indices[i], 1);
if (selected_disks.size() == REP_NUM) break; // 已经选择了3个硬盘,退出循环
}
// 如果该标签还没有选择到3个硬盘,则报错
assert(selected_disks.size() == REP_NUM && "Cannot allocate enough disks for this tag.(init)");
}
}
void TagManager::update_tag_info_after_delete(const Object& object) {
int tag = object.get_tag_id();
std::vector<std::pair<int, int>> chosen_partitions = object.get_chosen_partitions();
for (int j = 0; j < REP_NUM; j++) {
int disk_id = chosen_partitions[j].first;
int partition_id = chosen_partitions[j].second;
disk_partition_usage_tagnum[disk_id][partition_id][tag] -= 1; // 硬盘区间块的使用情况
assert(disk_partition_usage_tagnum[disk_id][partition_id][tag] >= 0 && "The partition usage count is negative.");
if (disk_partition_usage_tagnum[disk_id][partition_id][tag] == 0) { // 该硬盘该区间块该标签的对象个数减到0
disk_partition_usage_tagkind[disk_id][partition_id].erase(tag); // 从该区间块的标签种类中移除该标签
disk_tag_partition_num[disk_id][tag] -= 1; // 该硬盘该标签的区间块数量减1
assert(disk_tag_partition_num[disk_id][tag] >= 0 && "The tag partition count is negative.");
if (disk_tag_partition_num[disk_id][tag] == 0) { // 该硬盘该标签的区间块数量减到0
disk_tag_kind[disk_id].erase(tag); // 从该硬盘上移除该标签
}
// tag_disk_partition[tag][disk_id].erase(std::remove(tag_disk_partition[tag][disk_id].begin(), tag_disk_partition[tag][disk_id].end(), partition_id),
// tag_disk_partition[tag][disk_id].end()); // 从该标签分配的硬盘id和区间块id中移除该区间块
tag_disk_partition[tag][disk_id].erase(partition_id);
int count = disk_partition_usage_tagkind[disk_id][partition_id].size(); // 在删除之后,该硬盘该区间块的标签数量
if (count == 0) { // 该区间块减到只有0个标签
one_tag_partitions.erase({disk_id, partition_id}); // 将该区间块从 one_tag_partitions 中移除
zero_tag_partitions.insert({disk_id, partition_id}); // 将该区间块加入 zero_tag_partitions
} else if (count == 1) { // 该区间块减到只有1个标签
two_tag_partitions.erase({disk_id, partition_id}); // 将该区间块从 two_tag_partitions 中移除
one_tag_partitions.insert({disk_id, partition_id}); // 将该区间块加入 one_tag_partitions
} else if (count == 2) { // 该区间块减到只有2个标签
three_tag_partitions.erase({disk_id, partition_id}); // 将该区间块从 three_tag_partitions 中移除
two_tag_partitions.insert({disk_id, partition_id}); // 将该区间块加入 two_tag_partitions
} else if (count == 3) { // 该区间块减到只有3个标签
more_tag_partitions.erase({disk_id, partition_id}); // 将该区间块从 more_tag_partitions 中移除
three_tag_partitions.insert({disk_id, partition_id}); // 将该区间块加入 three_tag_partitions
}
}
}
}
// // 原版本 有问题???
// void TagManager::update_tag_info_after_write(const Object& object) {
// int tag = object.get_tag_id();
// const auto& chosen_partitions = object.get_chosen_partitions();
// for (const auto& [disk_id, part_id] : chosen_partitions) {
// disk_partition_usage_tagnum[disk_id][part_id][tag] += 1;
// if (disk_partition_usage_tagnum[disk_id][part_id][tag] == 1) {
// disk_partition_usage_tagkind[disk_id][part_id].insert(tag);
// }
// disk_tag_kind[disk_id].insert(tag);
// disk_tag_partition_num[disk_id][tag]++;
// tag_disk_partition[tag][disk_id].push_back(part_id);
// }
// }
// 现版本
void TagManager::update_tag_info_after_write(const Object& object) {
int tag = object.get_tag_id();
const auto& chosen_partitions = object.get_chosen_partitions();
for (const auto& [disk_id, part_id] : chosen_partitions) {
disk_partition_usage_tagnum[disk_id][part_id][tag] += 1; // 每次都维护
if (disk_partition_usage_tagkind[disk_id][part_id].count(tag) == 0) { // 第一次使用该区间块
disk_partition_usage_tagkind[disk_id][part_id].insert(tag); // 插入该标签
disk_tag_partition_num[disk_id][tag]++; // 该硬盘该标签的区间块数量加1
// tag_disk_partition[tag][disk_id].push_back(part_id); // 该标签分配的硬盘id和区间块id中插入该区间块
tag_disk_partition[tag][disk_id].insert(part_id);
}
if (disk_tag_kind[disk_id].count(tag) == 0) { // 第一次使用该硬盘
disk_tag_kind[disk_id].insert(tag); // 该硬盘上插入该标签
}
}
}
void TagManager::compute_delete_prob(const std::vector<std::vector<int>>& sum,
const std::vector<std::vector<int>>& fre_del){
for (int i = 1; i <= tag_delete_prob.size()-1; i++)
{
for (int j = 1; j <= tag_delete_prob[0].size()-1; j++)
{
tag_delete_prob[i][j] = sum[i][j] > 0 ? (double)fre_del[i][j] / sum[i][j] : 0;
}
}
}
void TagManager::check_tag_partition_sets() const {
// 检查 zero_tag_partitions:要求每个区间的标签数 == 0
for (const auto &p : zero_tag_partitions) {
int disk_id = p.first;
int part_id = p.second;
int count = disk_partition_usage_tagkind[disk_id][part_id].size();
assert(count == 0 && "Zero tag partition check failed: Expected 0 tags.");
}
// 检查 one_tag_partitions:要求每个区间的标签数 == 1
for (const auto &p : one_tag_partitions) {
int disk_id = p.first;
int part_id = p.second;
int count = disk_partition_usage_tagkind[disk_id][part_id].size();
assert(count == 1 && "One tag partition check failed: Expected 1 tag.");
}
// 检查 two_tag_partitions:要求每个区间的标签数 == 2
for (const auto &p : two_tag_partitions) {
int disk_id = p.first;
int part_id = p.second;
int count = disk_partition_usage_tagkind[disk_id][part_id].size();
assert(count == 2 && "Two tag partition check failed: Expected 2 tags.");
}
// 检查 three_tag_partitions:要求每个区间的标签数 == 3
for (const auto &p : three_tag_partitions) {
int disk_id = p.first;
int part_id = p.second;
int count = disk_partition_usage_tagkind[disk_id][part_id].size();
assert(count == 3 && "Three tag partition check failed: Expected 3 tags.");
}
// 检查 more_tag_partitions:要求每个区间的标签数 >= 4
for (const auto &p : more_tag_partitions) {
int disk_id = p.first;
int part_id = p.second;
int count = disk_partition_usage_tagkind[disk_id][part_id].size();
assert(count >= 4 && "More tag partition check failed: Expected at least 4 tags.");
}
// 检查所有集合的总数必须等于 N * DISK_PARTITIONS
int total = zero_tag_partitions.size() + one_tag_partitions.size() +
two_tag_partitions.size() + three_tag_partitions.size() +
more_tag_partitions.size();
assert(total == (N * DISK_PARTITIONS) && "Total partition sets do not sum up to N * DISK_PARTITIONS.");
// std::cerr << "All tag partition set checks passed." << std::endl;
}
void TagManager::check_consistency() const {
// 检查 1:对每个硬盘、每个分区,对每个标签,验证 disk_partition_usage_tagnum 与 disk_partition_usage_tagkind 是否一致
for (size_t disk_id = 1; disk_id < disk_partition_usage_tagkind.size(); ++disk_id) {
for (int part_id = 1; part_id <= DISK_PARTITIONS; ++part_id) {
for (int tag = 1; tag <= M; ++tag) {
int count = disk_partition_usage_tagnum[disk_id][part_id][tag];
bool inSet = (disk_partition_usage_tagkind[disk_id][part_id].count(tag) > 0);
if (count > 0) {
assert(inSet && "Inconsistency: usage count > 0 but tag not found in tagkind set.");
}
}
}
}
// 检查 2:对每个硬盘,验证 disk_tag_kind、disk_tag_partition_num 和 tag_disk_partition 的记录一致
for (size_t disk_id = 1; disk_id < disk_tag_kind.size(); ++disk_id) {
// 对于出现在 disk_tag_kind[disk_id] 中的每个标签 tag
for (int tag : disk_tag_kind[disk_id]) {
int computedCount = 0;
// 统计该硬盘上有多少分区包含该标签
for (int part_id = 1; part_id <= DISK_PARTITIONS; ++part_id) {
if (disk_partition_usage_tagkind[disk_id][part_id].count(tag) > 0)
computedCount++;
}
// 检查 disk_tag_partition_num
int recordedCount = 0;
auto it = disk_tag_partition_num[disk_id].find(tag);
if (it != disk_tag_partition_num[disk_id].end())
recordedCount = it->second;
assert(computedCount == recordedCount && "Inconsistency: computed partition count does not equal disk_tag_partition_num.");
// 检查 tag_disk_partition
int vectorCount = 0;
auto it2 = tag_disk_partition[tag].find(disk_id);
if (it2 != tag_disk_partition[tag].end())
vectorCount = it2->second.size();
assert(computedCount == vectorCount && "Inconsistency: computed partition count does not equal size of tag_disk_partition.");
}
}
// 检查 3:通过 disk_partition_usage_tagnum 检查其他三个变量的一致性
// disk_tag_kind
// disk_tag_partition_num
// tag_disk_partition
for (size_t disk_id = 1; disk_id < disk_partition_usage_tagnum.size(); ++disk_id) {
std::set<int> computedDiskTagKind; // 从 tagnum 计算出的该硬盘所有标签集合
std::unordered_map<int, int> computedPartitionCount; // 每个标签出现的分区数
std::unordered_map<int, std::set<int>> computedTagDiskPartition; // 每个标签对应的分区集合
// 遍历每个分区和每个标签,重建信息
for (int part_id = 1; part_id <= DISK_PARTITIONS; ++part_id) {
for (int tag = 1; tag <= M; ++tag) {
int count = disk_partition_usage_tagnum[disk_id][part_id][tag];
if (count > 0) {
computedDiskTagKind.insert(tag);
computedPartitionCount[tag]++;
computedTagDiskPartition[tag].insert(part_id);
}
}
}
// 检查 disk_tag_kind 是否与 computedDiskTagKind 一致
assert(computedDiskTagKind == disk_tag_kind[disk_id] &&
"Inconsistency: computed disk tag kind from tagnum does not match disk_tag_kind.");
// 对于每个标签,检查 disk_tag_partition_num 和 tag_disk_partition
for (int tag : computedDiskTagKind) {
// 检查 disk_tag_partition_num:统计出的分区数应与记录相同
int recordedCount = 0;
auto it = disk_tag_partition_num[disk_id].find(tag);
if (it != disk_tag_partition_num[disk_id].end())
recordedCount = it->second;
assert(computedPartitionCount[tag] == recordedCount &&
"Inconsistency: computed partition count from tagnum does not equal disk_tag_partition_num.");
// 检查 tag_disk_partition:记录中该硬盘的分区数量应与统计数一致
int vectorCount = 0;
auto it2 = tag_disk_partition[tag].find(disk_id);
if (it2 != tag_disk_partition[tag].end())
vectorCount = it2->second.size();
assert(computedPartitionCount[tag] == vectorCount &&
"Inconsistency: computed partition count from tagnum does not equal size of tag_disk_partition.");
}
}
// std::cerr << "All consistency checks passed." << std::endl;
}
void TagManager::printDiskPartitionUsageTagkind() const {
std::cerr << "disk_partition_usage_tagkind:" << std::endl;
for (int disk_id = 1; disk_id <= N; ++disk_id) {
std::cerr << "Disk " << disk_id << ":" << std::endl;
for (int partition = 1; partition <= DISK_PARTITIONS; ++partition) {
std::cerr << " Partition " << partition << ": ";
for (const auto &tag : disk_partition_usage_tagkind[disk_id][partition]) {
std::cerr << tag << " ";
}
std::cerr << std::endl;
}
}
}
void TagManager::printDiskPartitionUsageTagnum() const {
std::cerr << "disk_partition_usage_tagnum:" << std::endl;
// 假设外层下标从 1 到 N 有效(即 disk_partition_usage_tagnum[0] 未使用)
for (int disk_id = 1; disk_id <= N; ++disk_id) {
std::cerr << "Disk " << disk_id << ":" << std::endl;
// 同样假设每个磁盘的区间块从 1 到 DISK_PARTITIONS 有效
for (int partition = 1; partition <= DISK_PARTITIONS; ++partition) {
std::cerr << " Partition " << partition << ": ";
// 打印当前区间块中各标签对应的数字
if (disk_partition_usage_tagnum[disk_id].size() > partition &&
disk_partition_usage_tagnum[disk_id][partition].empty() == false) {
for (size_t tag = 1; tag < disk_partition_usage_tagnum[disk_id][partition].size(); ++tag) {
std::cerr << "Tag " << tag << ": "
<< disk_partition_usage_tagnum[disk_id][partition][tag] << " ";
}
} else {
std::cerr << "(empty)";
}
std::cerr << std::endl;
}
}
}
// 打印 disk_tag_kind
void TagManager::printDiskTagKind() const {
std::cerr << "disk_tag_kind:" << std::endl;
// 假设 disk_tag_kind[0] 未使用,磁盘从 1 到 N
for (int disk_id = 1; disk_id <= N; ++disk_id) {
std::cerr << "Disk " << disk_id << ": ";
// 遍历当前磁盘上的所有标签
for (const auto &tag : disk_tag_kind[disk_id]) {
std::cerr << tag << " ";
}
std::cerr << std::endl;
}
}
void TagManager::printDiskTagPartitionNum() const {
std::cerr << "disk_tag_partition_num:" << std::endl;
// 从 1 开始(假设 disk_tag_partition_num[0] 未使用)
for (size_t disk_id = 1; disk_id < disk_tag_partition_num.size(); ++disk_id) {
std::cerr << "Disk " << disk_id << ":" << std::endl;
// 遍历当前硬盘上的每个标签及其对应的区间块数量
for (const auto& entry : disk_tag_partition_num[disk_id]) {
std::cerr << " Tag " << entry.first << ": " << entry.second << std::endl;
}
}
}
// 打印 tag_disk_partition
void TagManager::printTagDiskPartition() const {
std::cerr << "tag_disk_partition:" << std::endl;
// 假设 tag_disk_partition 的下标从 1 到 M 有效(若下标 0 也有数据,可从 0 开始)
for (size_t tag = 1; tag < tag_disk_partition.size(); ++tag) {
std::cerr << "Tag " << tag << ":" << std::endl;
// map 默认按 key(磁盘号)升序排列
const auto& diskMap = tag_disk_partition[tag];
for (const auto& [disk, partitionSet] : diskMap) {
std::cerr << " Disk " << disk << ": ";
// std::set 默认按升序排列区间块号
for (int partition : partitionSet) {
std::cerr << partition << " ";
}
std::cerr << std::endl;
}
}
}