eloqstore-sys 1.1.1

Low-level Rust FFI bindings for EloqStore
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
#include "file_gc.h"

#include <jsoncpp/json/json.h>

#include <algorithm>
#include <boost/algorithm/string/predicate.hpp>
#include <filesystem>
#include <iterator>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "async_io_manager.h"
#include "common.h"
#include "eloq_store.h"
#include "error.h"
#include "kv_options.h"
#include "replayer.h"
#include "storage/mem_index_page.h"
#include "storage/object_store.h"
#include "storage/shard.h"
#include "tasks/task.h"
#include "utils.h"
namespace eloqstore
{
void GetRetainedFiles(absl::flat_hash_set<RetainedFileKey> &result,
                      const MappingSnapshot::MappingTbl &tbl,
                      const BranchFileMapping &file_ranges,
                      uint8_t pages_per_file_shift)
{
    const size_t tbl_size = tbl.size();
    for (PageId page_id = 0; page_id < tbl_size; ++page_id)
    {
        uint64_t val = tbl.Get(page_id);
        if (MappingSnapshot::IsFilePageId(val))
        {
            FilePageId fp_id = MappingSnapshot::DecodeId(val);
            FileId file_id = fp_id >> pages_per_file_shift;

            // Look up the branch and term for this file_id
            std::string branch_name;
            uint64_t term = 0;
            if (GetBranchNameAndTerm(file_ranges, file_id, branch_name, term))
            {
                result.emplace(
                    RetainedFileKey{file_id, std::move(branch_name), term});
            }
            else
            {
                // File ID not in any known branch range; skip it
                DLOG(WARNING) << "GetRetainedFiles: file_id " << file_id
                              << " not found in any branch range";
            }
        }
        else if (MappingSnapshot::IsSwizzlingPointer(val))
        {
            MemIndexPage *idx_page = reinterpret_cast<MemIndexPage *>(val);
            FilePageId fp_id = idx_page->GetFilePageId();
            FileId file_id = fp_id >> pages_per_file_shift;

            // Look up the branch and term for this file_id
            std::string branch_name;
            uint64_t term = 0;
            if (GetBranchNameAndTerm(file_ranges, file_id, branch_name, term))
            {
                result.emplace(
                    RetainedFileKey{file_id, std::move(branch_name), term});
            }
            else
            {
                // File ID not in any known branch range; skip it
                DLOG(WARNING)
                    << "GetRetainedFiles: file_id " << file_id
                    << " not found in any branch range (swizzling pointer)";
            }
        }
        if ((page_id & 0xFF) == 0)
        {
            ThdTask()->YieldToLowPQ();
        }
    }
}

namespace FileGarbageCollector
{

namespace
{
bool ParseCloudCleanupFilename(std::string_view filename,
                               FileId &file_id,
                               uint64_t &term)
{
    auto [type, suffix] = ParseFileName(filename);
    if (type == FileNameData)
    {
        std::string_view branch_name;
        return ParseDataFileSuffix(suffix, file_id, branch_name, term);
    }
    if (type == FileNameManifest)
    {
        std::string_view branch_name;
        std::optional<std::string> tag;
        if (!ParseManifestFileSuffix(suffix, branch_name, term, tag) ||
            tag.has_value())
        {
            return false;
        }
        file_id = IouringMgr::LruFD::kManifest;
        return true;
    }
    return false;
}

void CollectLocalCleanupTargets(
    const TableIdent &tbl_id,
    const std::vector<std::string> &deleted_filenames,
    CloudStoreMgr *cloud_mgr,
    std::vector<std::string> &targets)
{
    targets.clear();
    targets.reserve(deleted_filenames.size());
    for (const std::string &filename : deleted_filenames)
    {
        FileId file_id = 0;
        uint64_t term = 0;
        if (!ParseCloudCleanupFilename(filename, file_id, term))
        {
            continue;
        }

        IouringMgr::LruFD::Ref fd_ref = cloud_mgr->GetOpenedFD(tbl_id, file_id);
        if (fd_ref != nullptr && fd_ref.Get()->term_ == term)
        {
            if (fd_ref.Get()->ref_count_ > 1)
            {
                targets.push_back(filename);
                continue;
            }

            KvError close_err = cloud_mgr->CloseFile(std::move(fd_ref));
            if (close_err != KvError::NoError)
            {
                LOG(WARNING)
                    << "Failed to close idle file before local GC "
                    << "cleanup, table=" << tbl_id << " file=" << filename
                    << " err=" << ErrorString(close_err);
                continue;
            }
        }

        targets.push_back(filename);
    }
}
}  // namespace

KvError ExecuteLocalGC(const TableIdent &tbl_id,
                       RetainedFiles &retained_files,
                       IouringMgr *io_mgr)
{
    DLOG(INFO) << "ExecuteLocalGC: starting for table " << tbl_id.tbl_name_
               << ", partition " << tbl_id.partition_id_
               << ", retained_files count=" << retained_files.size();

    // 1. list all files in local directory.
    std::vector<std::string> local_files;
    KvError err = ListLocalFiles(tbl_id, local_files, io_mgr);
    if (err != KvError::NoError)
    {
        LOG(ERROR) << "ExecuteLocalGC: ListLocalFiles failed, error="
                   << static_cast<int>(err);
        return err;
    }

    // 2. classify files.
    std::vector<std::string> archive_files;
    std::vector<std::string> archive_tags;
    std::vector<std::string> archive_branch_names;
    std::vector<std::string> data_files;
    std::vector<uint64_t> manifest_terms;
    std::vector<std::string> manifest_branch_names;
    ClassifyFiles(local_files,
                  archive_files,
                  archive_tags,
                  archive_branch_names,
                  data_files,
                  manifest_terms,
                  manifest_branch_names);

    // No need to check term expired for local mode.

    // 2a. augment retained_files from all branch manifests (regular + archive)
    // on disk; also build max_file_id_per_branch_term map.
    absl::flat_hash_map<std::string, FileId> max_file_id_per_branch_term;
    err = AugmentRetainedFilesFromBranchManifests(
        tbl_id,
        manifest_branch_names,
        manifest_terms,
        archive_files,
        archive_branch_names,
        retained_files,
        max_file_id_per_branch_term,
        io_mgr->options_->pages_per_file_shift,
        io_mgr);
    if (err != KvError::NoError)
    {
        LOG(ERROR) << "ExecuteLocalGC: AugmentRetainedFilesFromBranchManifests "
                      "failed, error="
                   << static_cast<int>(err) << "; aborting GC cycle";
        return err;
    }

    // 3. delete unreferenced data files.
    err = DeleteUnreferencedLocalFiles(tbl_id,
                                       data_files,
                                       retained_files,
                                       max_file_id_per_branch_term,
                                       io_mgr);
    if (err != KvError::NoError)
    {
        LOG(ERROR)
            << "ExecuteLocalGC: DeleteUnreferencedLocalFiles failed, error="
            << static_cast<int>(err);
        return err;
    }

    // If the partition is now empty except for the current manifest, clean the
    // manifest as well so the local directory can be removed without waiting
    // for later root-meta eviction.
    local_files.clear();
    err = ListLocalFiles(tbl_id, local_files, io_mgr);
    if (err != KvError::NoError)
    {
        LOG(ERROR) << "ExecuteLocalGC: post-delete ListLocalFiles failed, "
                   << "error=" << static_cast<int>(err);
        return err;
    }

    archive_files.clear();
    archive_tags.clear();
    archive_branch_names.clear();
    data_files.clear();
    manifest_terms.clear();
    manifest_branch_names.clear();
    ClassifyFiles(local_files,
                  archive_files,
                  archive_tags,
                  archive_branch_names,
                  data_files,
                  manifest_terms,
                  manifest_branch_names);

    if (data_files.empty() && archive_files.empty())
    {
        const StoreMode mode = eloq_store->Mode();
        if (mode == StoreMode::Cloud)
        {
            auto *cloud_mgr = static_cast<CloudStoreMgr *>(io_mgr);
            err = cloud_mgr->CleanupLocalPartitionFiles(tbl_id);
            if (err != KvError::NoError)
            {
                LOG(ERROR)
                    << "ExecuteLocalGC: CleanupLocalPartitionFiles failed, "
                    << "error=" << static_cast<int>(err);
                return err;
            }
        }
        else
        {
            err = io_mgr->CleanManifest(tbl_id);
            if (err != KvError::NoError)
            {
                LOG(ERROR) << "ExecuteLocalGC: CleanManifest failed, error="
                           << static_cast<int>(err);
                return err;
            }

            CHECK(shard != nullptr);
            RootMetaMgr *root_meta_mgr =
                shard->IndexManager()->RootMetaManager();
            auto *entry = root_meta_mgr->Find(tbl_id);
            if (entry != nullptr)
            {
                RootMeta &meta = entry->meta_;
                if (meta.mapper_ != nullptr && meta.manifest_size_ != 0 &&
                    meta.root_id_ == MaxPageId &&
                    meta.ttl_root_id_ == MaxPageId &&
                    meta.mapper_->MappingCount() == 0)
                {
                    meta.manifest_size_ = 0;
                }
            }
        }
    }

    /*
    // 4. delete old archives beyond num_retained_archives per branch.
    // NOTE: this step is intentionally AFTER DeleteUnreferencedLocalFiles so
    // that ALL archives (including those about to be pruned) contribute their
    // file IDs to retained_files first.  Files exclusively referenced by pruned
    // archives become deletable only on the next GC cycle.
    err = DeleteOldArchives(tbl_id,
                            archive_files,
                            archive_tags,
                            archive_branch_names,
                            io_mgr->options_->num_retained_archives,
                            io_mgr);
    if (err != KvError::NoError)
    {
        LOG(ERROR) << "ExecuteLocalGC: DeleteOldArchives failed, error="
                   << static_cast<int>(err);
        return err;
    }
    */

    return KvError::NoError;
}

KvError ListLocalFiles(const TableIdent &tbl_id,
                       std::vector<std::string> &local_files,
                       IouringMgr *io_mgr)
{
    namespace fs = std::filesystem;

    fs::path dir_path = tbl_id.StorePath(io_mgr->options_->store_path,
                                         io_mgr->options_->store_path_lut);

    for (auto &ent : fs::directory_iterator{dir_path})
    {
        const std::string name = ent.path().filename();
        if (boost::algorithm::ends_with(name, TmpSuffix))
        {
            // Skip temporary files.
            continue;
        }
        local_files.emplace_back(name);
    }

    return KvError::NoError;
}

// Helper functions for cloud GC optimization
KvError ListCloudFiles(const TableIdent &tbl_id,
                       std::vector<std::string> &cloud_files,
                       CloudStoreMgr *cloud_mgr)
{
    std::string table_path = tbl_id.ToString();
    KvTask *current_task = ThdTask();

    ObjectStore &object_store = cloud_mgr->GetObjectStore();
    cloud_files.clear();

    std::string continuation_token;
    do
    {
        // List files in batches (S3 ListObjectsV2 caps responses at 1000)
        ObjectStore::ListTask list_task(table_path);
        list_task.SetContinuationToken(continuation_token);
        list_task.SetKvTask(current_task);

        cloud_mgr->AcquireCloudSlot(current_task);
        cloud_mgr->GetObjectStore().SubmitTask(&list_task, shard);
        current_task->WaitIo();

        if (list_task.error_ != KvError::NoError)
        {
            LOG(ERROR) << "Failed to list cloud files for table " << table_path
                       << ", error: " << static_cast<int>(list_task.error_);
            return list_task.error_;
        }

        std::vector<std::string> batch_files;
        std::string next_token;
        if (!object_store.ParseListObjectsResponse(
                list_task.response_data_.view(),
                list_task.json_data_,
                &batch_files,
                nullptr,
                &next_token))
        {
            LOG(ERROR) << "Failed to parse cloud list response";
            return KvError::Corrupted;
        }

        cloud_files.insert(cloud_files.end(),
                           std::make_move_iterator(batch_files.begin()),
                           std::make_move_iterator(batch_files.end()));

        continuation_token = std::move(next_token);
    } while (!continuation_token.empty());

    return KvError::NoError;
}

void ClassifyFiles(const std::vector<std::string> &files,
                   std::vector<std::string> &archive_files,
                   std::vector<std::string> &archive_tags,
                   std::vector<std::string> &archive_branch_names,
                   std::vector<std::string> &data_files,
                   std::vector<uint64_t> &manifest_terms,
                   std::vector<std::string> &manifest_branch_names)
{
    archive_files.clear();
    archive_tags.clear();
    archive_branch_names.clear();
    data_files.clear();
    manifest_terms.clear();
    manifest_branch_names.clear();
    data_files.reserve(files.size());

    for (const std::string &file_name : files)
    {
        // Ignore temporary files.
        if (boost::algorithm::ends_with(file_name, TmpSuffix))
        {
            continue;
        }

        // Cloud list responses already strip the prefix, so file_name contains
        // just the basename we need to classify.
        auto ret = ParseFileName(file_name);

        if (ret.first == FileNameManifest)
        {
            // Only support term-aware archive format:
            // manifest_<term>_<tag> Legacy format manifest_<ts> is no longer
            // supported.
            std::string_view branch_name;
            uint64_t term = 0;
            std::optional<std::string> tag;
            if (!ParseManifestFileSuffix(ret.second, branch_name, term, tag))
            {
                continue;
            }
            // Only recognize term-aware archives (both term and tag must
            // be present).
            if (tag.has_value())
            {
                archive_files.push_back(file_name);
                archive_tags.push_back(std::move(*tag));
                archive_branch_names.emplace_back(branch_name);
            }
            else
            {
                manifest_terms.push_back(term);
                manifest_branch_names.emplace_back(branch_name);
            }
        }
        else if (ret.first == FileNameData)
        {
            // data file: data_<file_id>
            data_files.push_back(file_name);
        }
        // Ignore other types of files.
    }
}

KvError ReadCloudFile(const TableIdent &tbl_id,
                      const std::string &cloud_file,
                      DirectIoBuffer &content,
                      CloudStoreMgr *cloud_mgr)
{
    KvTask *current_task = ThdTask();

    // Download the file from cloud.
    ObjectStore::DownloadTask download_task(&tbl_id, cloud_file);

    // Set KvTask pointer and initialize inflight_io_
    download_task.SetKvTask(current_task);

    cloud_mgr->AcquireCloudSlot(current_task);
    cloud_mgr->GetObjectStore().SubmitTask(&download_task, shard);
    current_task->WaitIo();

    if (download_task.error_ != KvError::NoError)
    {
        LOG(ERROR) << "Failed to download cloud file: " << cloud_file
                   << ", error: " << static_cast<int>(download_task.error_);
        return download_task.error_;
    }
    content = std::move(download_task.response_data_);

    DLOG(INFO) << "Successfully downloaded and read cloud file: " << cloud_file;
    return KvError::NoError;
}

// Helper: process one manifest file (regular or archive) — replay it,
// add all referenced file IDs to retained_files, and update
// max_file_id_per_branch_term from BranchManifestMetadata.file_ranges.
static KvError ProcessOneManifest(
    const std::string &filename,
    uint64_t term,
    DirectIoBuffer &buf,
    absl::flat_hash_set<RetainedFileKey> &retained_files,
    absl::flat_hash_map<std::string, FileId> &max_file_id_per_branch_term,
    uint8_t pages_per_file_shift)
{
    MemStoreMgr::Manifest manifest(buf.view());
    Replayer replayer(Options());
    replayer.branch_metadata_.term = term;

    KvError replay_err = replayer.Replay(&manifest);
    if (replay_err != KvError::NoError)
    {
        LOG(WARNING) << "ProcessOneManifest: failed to replay manifest "
                     << filename << " term " << term
                     << ", error=" << static_cast<int>(replay_err);
        return replay_err;
    }

    GetRetainedFiles(retained_files,
                     replayer.mapping_tbl_,
                     replayer.branch_metadata_.file_ranges,
                     pages_per_file_shift);

    // Update max_file_id_per_branch_term from all file_ranges in this manifest.
    for (const BranchFileRange &range : replayer.branch_metadata_.file_ranges)
    {
        std::string key = range.branch_name + "_" + std::to_string(range.term);
        auto it = max_file_id_per_branch_term.find(key);
        if (it == max_file_id_per_branch_term.end() ||
            range.max_file_id > it->second)
        {
            max_file_id_per_branch_term[key] = range.max_file_id;
        }
    }

    DLOG(INFO) << "ProcessOneManifest: processed " << filename
               << ", retained_files now size=" << retained_files.size();
    return KvError::NoError;
}

KvError AugmentRetainedFilesFromBranchManifests(
    const TableIdent &tbl_id,
    const std::vector<std::string> &manifest_branch_names,
    const std::vector<uint64_t> &manifest_terms,
    const std::vector<std::string> &archive_files,
    const std::vector<std::string> &archive_branch_names,
    absl::flat_hash_set<RetainedFileKey> &retained_files,
    absl::flat_hash_map<std::string, FileId> &max_file_id_per_branch_term,
    uint8_t pages_per_file_shift,
    IouringMgr *io_mgr)
{
    assert(manifest_branch_names.size() == manifest_terms.size());
    assert(archive_files.size() == archive_branch_names.size());

    bool is_cloud = eloq_store->Mode() == StoreMode::Cloud;
    CloudStoreMgr *cloud_mgr =
        is_cloud ? static_cast<CloudStoreMgr *>(io_mgr) : nullptr;

    // --- Process regular manifests ---
    for (size_t i = 0; i < manifest_branch_names.size(); ++i)
    {
        const std::string &branch = manifest_branch_names[i];
        uint64_t term = manifest_terms[i];
        std::string filename = BranchManifestFileName(branch, term);

        DirectIoBuffer buf;
        KvError err = KvError::NoError;

        if (is_cloud)
        {
            err = ReadCloudFile(tbl_id, filename, buf, cloud_mgr);
        }
        else
        {
            err = io_mgr->ReadFile(tbl_id, filename, buf);
        }

        if (err == KvError::NoError)
        {
            err = ProcessOneManifest(filename,
                                     term,
                                     buf,
                                     retained_files,
                                     max_file_id_per_branch_term,
                                     pages_per_file_shift);
            if (err != KvError::NoError)
            {
                return err;
            }
        }
        else if (err != KvError::NotFound)
        {
            LOG(WARNING) << "AugmentRetainedFilesFromBranchManifests: "
                            "failed to read "
                            "manifest "
                         << filename << " for branch " << branch << " term "
                         << term << ", error=" << static_cast<int>(err);
            return err;
        }
        else
        {
            LOG(ERROR) << "AugmentRetainedFilesFromBranchManifests: manifest "
                       << filename << " for branch " << branch << " term "
                       << term
                       << " not found during GC scan, skipping retained-file "
                          "augmentation for this manifest";
        }
    }

    // --- Process archive manifests ---
    for (size_t i = 0; i < archive_files.size(); ++i)
    {
        const std::string &filename = archive_files[i];
        // Extract term from archive filename.
        uint64_t term = ManifestTermFromFilename(filename);

        DirectIoBuffer buf;
        KvError err = KvError::NoError;

        if (is_cloud)
        {
            err = ReadCloudFile(tbl_id, filename, buf, cloud_mgr);
        }
        else
        {
            err = io_mgr->ReadFile(tbl_id, filename, buf);
        }

        if (err != KvError::NoError)
        {
            LOG(WARNING)
                << "AugmentRetainedFilesFromBranchManifests: failed to read "
                   "archive "
                << filename << " for branch " << archive_branch_names[i]
                << " term " << term << ", error=" << static_cast<int>(err);
            return err;
        }

        err = ProcessOneManifest(filename,
                                 term,
                                 buf,
                                 retained_files,
                                 max_file_id_per_branch_term,
                                 pages_per_file_shift);
        if (err != KvError::NoError)
        {
            return err;
        }
    }

    return KvError::NoError;
}

KvError DeleteOldArchives(const TableIdent &tbl_id,
                          const std::vector<std::string> &archive_files,
                          const std::vector<std::string> &archive_tags,
                          const std::vector<std::string> &archive_branch_names,
                          uint32_t num_retained_archives,
                          IouringMgr *io_mgr)
{
    assert(archive_files.size() == archive_tags.size());
    assert(archive_files.size() == archive_branch_names.size());

    if (num_retained_archives == 0 || archive_files.empty())
    {
        return KvError::NoError;
    }

    // Group archive indices by branch name.
    std::unordered_map<std::string, std::vector<size_t>> branch_indices;
    for (size_t i = 0; i < archive_files.size(); ++i)
    {
        branch_indices[archive_branch_names[i]].push_back(i);
    }

    // For each branch, sort by tag descending (lexicographic) and collect
    // excess archives.
    std::vector<std::string> to_delete;
    for (auto &[branch, indices] : branch_indices)
    {
        if (indices.size() <= num_retained_archives)
        {
            continue;
        }
        // Sort descending by tag (newest first, assuming lexicographic order).
        std::sort(indices.begin(),
                  indices.end(),
                  [&](size_t a, size_t b)
                  { return archive_tags[a] > archive_tags[b]; });
        // Keep the first num_retained_archives, delete the rest.
        for (size_t j = num_retained_archives; j < indices.size(); ++j)
        {
            to_delete.push_back(archive_files[indices[j]]);
        }
    }

    if (to_delete.empty())
    {
        return KvError::NoError;
    }

    if (!io_mgr->options_->cloud_store_path.empty())
    {
        // Cloud mode: batch delete via object store.
        CloudStoreMgr *cloud_mgr = static_cast<CloudStoreMgr *>(io_mgr);
        KvTask *current_task = ThdTask();

        std::vector<ObjectStore::DeleteTask> delete_tasks;
        delete_tasks.reserve(to_delete.size());

        for (const std::string &file_name : to_delete)
        {
            std::string remote_path = tbl_id.ToString() + "/" + file_name;
            delete_tasks.emplace_back(remote_path);
            ObjectStore::DeleteTask &task = delete_tasks.back();
            task.SetKvTask(current_task);
            cloud_mgr->AcquireCloudSlot(current_task);
            cloud_mgr->GetObjectStore().SubmitTask(&task, shard);
        }

        current_task->WaitIo();

        for (const auto &task : delete_tasks)
        {
            if (task.error_ != KvError::NoError)
            {
                LOG(ERROR) << "DeleteOldArchives: failed to delete archive "
                           << task.remote_path_ << ": "
                           << ErrorString(task.error_);
                return task.error_;
            }
        }
    }
    else
    {
        // Local mode: delete files from filesystem.
        namespace fs = std::filesystem;
        fs::path dir_path = tbl_id.StorePath(io_mgr->options_->store_path,
                                             io_mgr->options_->store_path_lut);

        std::vector<std::string> full_paths;
        full_paths.reserve(to_delete.size());
        for (const std::string &file_name : to_delete)
        {
            full_paths.push_back((dir_path / file_name).string());
        }

        KvError delete_err = io_mgr->DeleteFiles(full_paths);
        if (delete_err != KvError::NoError)
        {
            LOG(ERROR) << "DeleteOldArchives: failed to delete archive files, "
                          "error: "
                       << static_cast<int>(delete_err);
            return delete_err;
        }
    }

    DLOG(INFO) << "DeleteOldArchives: deleted " << to_delete.size()
               << " old archive(s) for table " << tbl_id;
    return KvError::NoError;
}

KvError DeleteUnreferencedCloudFiles(
    const TableIdent &tbl_id,
    const std::vector<std::string> &data_files,
    const std::vector<uint64_t> &manifest_terms,
    const std::vector<std::string> &manifest_branch_names,
    const absl::flat_hash_set<RetainedFileKey> &retained_files,
    const absl::flat_hash_map<std::string, FileId> &max_file_id_per_branch_term,
    std::vector<std::string> &deleted_filenames,
    CloudStoreMgr *cloud_mgr)
{
    std::vector<std::string> files_to_delete;
    std::vector<std::string> basenames_to_delete;
    auto process_term = cloud_mgr->ProcessTerm();
    const std::string current_manifest =
        BranchManifestFileName(cloud_mgr->GetActiveBranch(), process_term);
    deleted_filenames.clear();

    for (const std::string &file_name : data_files)
    {
        auto ret = ParseFileName(file_name);
        if (ret.first != FileNameData)
        {
            continue;
        }

        FileId file_id = 0;
        std::string_view branch_name;
        uint64_t term = 0;
        if (!ParseDataFileSuffix(ret.second, file_id, branch_name, term))
        {
            LOG(ERROR) << "Failed to parse data file suffix: " << file_name
                       << ", skipping";
            continue;
        }

        if (term > process_term)
        {
            // Skip files with term greater than process term.
            continue;
        }

        if (retained_files.contains(
                RetainedFileKey{file_id, std::string(branch_name), term}))
        {
            DLOG(INFO) << "skip file " << file_name << " (in retained_files)";
            continue;
        }

        // Check max_file_id_per_branch_term to detect in-flight writes.
        std::string key = std::string(branch_name) + "_" + std::to_string(term);
        auto it = max_file_id_per_branch_term.find(key);
        if (it != max_file_id_per_branch_term.end() && file_id > it->second)
        {
            // file_id beyond known max → in-flight write, preserve.
            DLOG(INFO) << "skip file " << file_name << " (file_id=" << file_id
                       << " > max_known=" << it->second << ", in-flight)";
            continue;
        }

        // No map entry → deleted/orphaned branch; or file_id within known
        // range and not retained → safe to delete.
        std::string remote_path = tbl_id.ToString() + "/" + file_name;
        files_to_delete.push_back(remote_path);
        basenames_to_delete.push_back(file_name);
    }

    if (files_to_delete.size() == data_files.size())
    {
        // Every data file for this table is unreferenced and will be deleted.
        // The active branch's manifest is now empty, so delete it too rather
        // than leaving a stale manifest in cloud storage.  We use
        // GetActiveBranch() directly (instead of scanning by term alone)
        // because multiple branches may share the same term value.
        std::string_view active_branch = cloud_mgr->GetActiveBranch();
        bool found_current = false;
        for (size_t i = 0; i < manifest_terms.size(); ++i)
        {
            if (manifest_branch_names[i] == active_branch &&
                manifest_terms[i] == process_term)
            {
                found_current = true;
                break;
            }
        }
        if (!found_current)
        {
            LOG(WARNING)
                << "ExecuteCloudGC: no manifest found for active_branch="
                << active_branch << " process_term=" << process_term
                << " in tbl=" << tbl_id.ToString()
                << "; skipping current-manifest deletion";
        }
        else
        {
            files_to_delete.emplace_back(
                tbl_id.ToString() + "/" +
                BranchManifestFileName(active_branch, process_term));
            basenames_to_delete.emplace_back(
                BranchManifestFileName(active_branch, process_term));
        }
    }

    // Delete superseded manifest files: only manifests belonging to the same
    // branch as the current process_term manifest are version-chained and safe
    // to prune. Manifests for OTHER branches are managed by DeleteBranch and
    // must not be deleted here.
    {
        // Use the known active branch directly rather than scanning by term.
        std::string_view active_branch = cloud_mgr->GetActiveBranch();
        for (size_t i = 0; i < manifest_terms.size(); ++i)
        {
            if (manifest_terms[i] < process_term &&
                manifest_branch_names[i] == active_branch)
            {
                files_to_delete.emplace_back(
                    tbl_id.ToString() + "/" +
                    BranchManifestFileName(manifest_branch_names[i],
                                           manifest_terms[i]));
                basenames_to_delete.emplace_back(BranchManifestFileName(
                    manifest_branch_names[i], manifest_terms[i]));
            }
        }
    }

    if (files_to_delete.empty())
    {
        return KvError::NoError;
    }

    KvTask *current_task = ThdTask();

    // If we're deleting every file in the directory, issue a single purge
    // request for the table path.
    std::vector<ObjectStore::DeleteTask> delete_tasks;
    delete_tasks.reserve(files_to_delete.size());

    for (const std::string &remote_path : files_to_delete)
    {
        delete_tasks.emplace_back(remote_path);
        ObjectStore::DeleteTask &task = delete_tasks.back();
        task.SetKvTask(current_task);
        cloud_mgr->AcquireCloudSlot(current_task);
        cloud_mgr->GetObjectStore().SubmitTask(&task, shard);
    }

    current_task->WaitIo();

    KvError first_error = KvError::NoError;
    deleted_filenames.reserve(basenames_to_delete.size());
    for (size_t i = 0; i < delete_tasks.size(); ++i)
    {
        const auto &task = delete_tasks[i];
        const bool manifest_gone = basenames_to_delete[i] == current_manifest &&
                                   (task.error_ == KvError::NoError ||
                                    task.error_ == KvError::NotFound);
        if (manifest_gone)
        {
            RootMetaMgr *root_meta_mgr =
                shard->IndexManager()->RootMetaManager();
            auto *entry = root_meta_mgr->Find(tbl_id);
            if (entry != nullptr)
            {
                RootMeta &meta = entry->meta_;
                // Cloud GC only deletes the current manifest for an empty
                // partition. Keep the in-memory RootMeta consistent so the
                // next write rebuilds a fresh snapshot instead of appending to
                // a manifest that no longer exists.
                if (meta.mapper_ != nullptr && meta.manifest_size_ != 0 &&
                    meta.root_id_ == MaxPageId &&
                    meta.ttl_root_id_ == MaxPageId &&
                    meta.mapper_->MappingCount() == 0)
                {
                    meta.manifest_size_ = 0;
                }
            }
        }
        if (task.error_ != KvError::NoError && task.error_ != KvError::NotFound)
        {
            LOG(ERROR) << "Failed to delete file " << task.remote_path_ << ": "
                       << ErrorString(task.error_);
            if (first_error == KvError::NoError)
            {
                first_error = task.error_;
            }
            continue;
        }

        deleted_filenames.push_back(basenames_to_delete[i]);
    }

    return first_error;
}

KvError DeleteUnreferencedLocalFiles(
    const TableIdent &tbl_id,
    const std::vector<std::string> &data_files,
    const absl::flat_hash_set<RetainedFileKey> &retained_files,
    const absl::flat_hash_map<std::string, FileId> &max_file_id_per_branch_term,
    IouringMgr *io_mgr)
{
    namespace fs = std::filesystem;
    fs::path dir_path = tbl_id.StorePath(io_mgr->options_->store_path,
                                         io_mgr->options_->store_path_lut);

    std::vector<std::string> files_to_delete;
    std::vector<FileId> file_ids_to_close;
    files_to_delete.reserve(data_files.size());
    file_ids_to_close.reserve(data_files.size());

    for (const std::string &file_name : data_files)
    {
        auto ret = ParseFileName(file_name);
        if (ret.first != FileNameData)
        {
            DLOG(INFO) << "ExecuteLocalGC: skipping non-data file: "
                       << file_name;
            continue;
        }

        FileId file_id = 0;
        std::string_view branch_name;
        uint64_t term = 0;
        if (!ParseDataFileSuffix(ret.second, file_id, branch_name, term))
        {
            continue;
        }

        if (retained_files.contains(
                RetainedFileKey{file_id, std::string(branch_name), term}))
        {
            DLOG(INFO) << "ExecuteLocalGC: keep file " << file_name
                       << " (in retained_files)";
            continue;
        }

        // Check max_file_id_per_branch_term to detect in-flight writes.
        std::string key = std::string(branch_name) + "_" + std::to_string(term);
        auto it = max_file_id_per_branch_term.find(key);
        if (it != max_file_id_per_branch_term.end() && file_id > it->second)
        {
            // file_id beyond known max → in-flight write, preserve.
            DLOG(INFO) << "ExecuteLocalGC: keep file " << file_name
                       << " (file_id=" << file_id
                       << " > max_known=" << it->second << ", in-flight)";
            continue;
        }

        // No map entry → deleted/orphaned branch; or file_id within known
        // range and not retained → safe to delete.
        fs::path file_path = dir_path / file_name;
        files_to_delete.push_back(file_path.string());
        file_ids_to_close.push_back(file_id);
        DLOG(INFO) << "ExecuteLocalGC: marking file for deletion: " << file_name
                   << " (file_id=" << file_id << ")";
    }

    DLOG(INFO) << "ExecuteLocalGC: total files to delete: "
               << files_to_delete.size();
    if (!files_to_delete.empty())
    {
        KvError close_err = io_mgr->CloseFiles(tbl_id, file_ids_to_close);
        if (close_err != KvError::NoError)
        {
            LOG(ERROR) << "ExecuteLocalGC: Failed to close files before "
                          "deletion, error: "
                       << static_cast<int>(close_err);
            return close_err;
        }
        // Delete files using batch operation
        KvError delete_err = io_mgr->DeleteFiles(files_to_delete);
        if (delete_err != KvError::NoError)
        {
            LOG(ERROR) << "ExecuteLocalGC: Failed to delete files, error: "
                       << static_cast<int>(delete_err);
            return delete_err;
        }
        DLOG(INFO) << "ExecuteLocalGC: Successfully deleted "
                   << files_to_delete.size() << " unreferenced files";
    }
    else
    {
        DLOG(INFO) << "ExecuteLocalGC: No files to delete";
    }

    return KvError::NoError;
}

KvError ExecuteCloudGC(const TableIdent &tbl_id,
                       RetainedFiles &retained_files,
                       CloudStoreMgr *cloud_mgr)
{
    // Check term file before proceeding
    uint64_t process_term = cloud_mgr->ProcessTerm();
    auto [term_file_term, etag, err] =
        cloud_mgr->ReadTermFile(cloud_mgr->GetActiveBranch());

    if (err == KvError::NotFound)
    {
        LOG(INFO) << "ExecuteCloudGC: term file not found for partition_group "
                  << cloud_mgr->PartitionGroupId();
        return KvError::NoError;
    }
    else if (err != KvError::NoError)
    {
        LOG(ERROR) << "ExecuteCloudGC: failed to read term file for "
                   << "partition_group " << cloud_mgr->PartitionGroupId()
                   << " : " << ErrorString(err);
        return err;
    }
    else
    {
        if (term_file_term != process_term)
        {
            LOG(WARNING) << "ExecuteCloudGC: term file term " << term_file_term
                         << " != process_term " << process_term << " for table "
                         << tbl_id << ", partition_group "
                         << cloud_mgr->PartitionGroupId() << ", skipping GC";
            return KvError::ExpiredTerm;
        }
    }

    // 1. list all files in cloud.
    std::vector<std::string> cloud_files;
    err = ListCloudFiles(tbl_id, cloud_files, cloud_mgr);
    DLOG(INFO) << "ListCloudFiles got " << cloud_files.size() << " files";
    if (err != KvError::NoError)
    {
        return err;
    }

    // 2. classify files.
    std::vector<std::string> archive_files;
    std::vector<std::string> archive_tags;
    std::vector<std::string> archive_branch_names;
    std::vector<std::string> data_files;
    std::vector<uint64_t> manifest_terms;
    std::vector<std::string> manifest_branch_names;
    ClassifyFiles(cloud_files,
                  archive_files,
                  archive_tags,
                  archive_branch_names,
                  data_files,
                  manifest_terms,
                  manifest_branch_names);

    // 3. check if term expired to avoid deleting invisible files.
    for (auto term : manifest_terms)
    {
        if (term > process_term)
        {
            return KvError::ExpiredTerm;
        }
    }

    // 3a. augment retained_files from all branch manifests (regular + archive)
    // in cloud; also build max_file_id_per_branch_term map.
    absl::flat_hash_map<std::string, FileId> max_file_id_per_branch_term;
    err = AugmentRetainedFilesFromBranchManifests(
        tbl_id,
        manifest_branch_names,
        manifest_terms,
        archive_files,
        archive_branch_names,
        retained_files,
        max_file_id_per_branch_term,
        cloud_mgr->options_->pages_per_file_shift,
        static_cast<IouringMgr *>(cloud_mgr));
    if (err != KvError::NoError)
    {
        LOG(ERROR) << "ExecuteCloudGC: AugmentRetainedFilesFromBranchManifests "
                      "failed, error="
                   << static_cast<int>(err) << "; aborting GC cycle";
        return err;
    }

    // 4. delete unreferenced data files.
    std::vector<std::string> deleted_filenames;
    err = DeleteUnreferencedCloudFiles(tbl_id,
                                       data_files,
                                       manifest_terms,
                                       manifest_branch_names,
                                       retained_files,
                                       max_file_id_per_branch_term,
                                       deleted_filenames,
                                       cloud_mgr);
    if (err != KvError::NoError)
    {
        return err;
    }

    if (!deleted_filenames.empty())
    {
        std::vector<std::string> local_cleanup_targets;
        CollectLocalCleanupTargets(
            tbl_id, deleted_filenames, cloud_mgr, local_cleanup_targets);
        if (!local_cleanup_targets.empty())
        {
            cloud_mgr->ScheduleLocalFileCleanup(tbl_id, local_cleanup_targets);
        }
    }

    /*
    // 5. delete old archives beyond num_retained_archives per branch.
    // NOTE: intentionally AFTER DeleteUnreferencedCloudFiles so all archives
    // contribute their file IDs to retained_files before any are pruned.
    err = DeleteOldArchives(tbl_id,
                            archive_files,
                            archive_tags,
                            archive_branch_names,
                            cloud_mgr->options_->num_retained_archives,
                            static_cast<IouringMgr *>(cloud_mgr));
    if (err != KvError::NoError)
    {
        LOG(ERROR) << "ExecuteCloudGC: DeleteOldArchives failed, error="
                   << static_cast<int>(err);
        return err;
    }
    */

    return KvError::NoError;
}

}  // namespace FileGarbageCollector

}  // namespace eloqstore