|
19 | 19 |
|
20 | 20 | #include "iceberg/snapshot.h" |
21 | 21 |
|
| 22 | +#include "iceberg/file_io.h" |
| 23 | +#include "iceberg/manifest/manifest_list.h" |
| 24 | +#include "iceberg/manifest/manifest_reader.h" |
| 25 | +#include "iceberg/util/macros.h" |
| 26 | + |
22 | 27 | namespace iceberg { |
23 | 28 |
|
24 | 29 | bool SnapshotRef::Branch::Equals(const SnapshotRef::Branch& other) const { |
@@ -80,4 +85,60 @@ bool Snapshot::Equals(const Snapshot& other) const { |
80 | 85 | schema_id == other.schema_id; |
81 | 86 | } |
82 | 87 |
|
| 88 | +Result<CachedSnapshot::ManifestsCache> CachedSnapshot::InitManifestsCache( |
| 89 | + const Snapshot& snapshot, std::shared_ptr<FileIO> file_io) { |
| 90 | + if (file_io == nullptr) { |
| 91 | + return InvalidArgument("Cannot cache manifests: FileIO is null"); |
| 92 | + } |
| 93 | + |
| 94 | + // Read manifest list |
| 95 | + ICEBERG_ASSIGN_OR_RAISE(auto reader, |
| 96 | + ManifestListReader::Make(snapshot.manifest_list, file_io)); |
| 97 | + ICEBERG_ASSIGN_OR_RAISE(auto manifest_files, reader->Files()); |
| 98 | + |
| 99 | + std::vector<ManifestFile> manifests; |
| 100 | + manifests.reserve(manifest_files.size()); |
| 101 | + |
| 102 | + // Partition manifests: data manifests first, then delete manifests |
| 103 | + // First pass: collect data manifests |
| 104 | + for (const auto& manifest_file : manifest_files) { |
| 105 | + if (manifest_file.content == ManifestContent::kData) { |
| 106 | + manifests.push_back(manifest_file); |
| 107 | + } |
| 108 | + } |
| 109 | + size_t data_manifests_count = manifests.size(); |
| 110 | + |
| 111 | + // Second pass: append delete manifests |
| 112 | + for (const auto& manifest_file : manifest_files) { |
| 113 | + if (manifest_file.content == ManifestContent::kDeletes) { |
| 114 | + manifests.push_back(manifest_file); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return std::make_pair(std::move(manifests), data_manifests_count); |
| 119 | +} |
| 120 | + |
| 121 | +Result<std::span<ManifestFile>> CachedSnapshot::Manifests( |
| 122 | + std::shared_ptr<FileIO> file_io) const { |
| 123 | + ICEBERG_ASSIGN_OR_RAISE(auto cache_ref, manifests_cache_.Get(snapshot_, file_io)); |
| 124 | + auto& cache = cache_ref.get(); |
| 125 | + return std::span<ManifestFile>(cache.first.data(), cache.first.size()); |
| 126 | +} |
| 127 | + |
| 128 | +Result<std::span<ManifestFile>> CachedSnapshot::DataManifests( |
| 129 | + std::shared_ptr<FileIO> file_io) const { |
| 130 | + ICEBERG_ASSIGN_OR_RAISE(auto cache_ref, manifests_cache_.Get(snapshot_, file_io)); |
| 131 | + auto& cache = cache_ref.get(); |
| 132 | + return std::span<ManifestFile>(cache.first.data(), cache.second); |
| 133 | +} |
| 134 | + |
| 135 | +Result<std::span<ManifestFile>> CachedSnapshot::DeleteManifests( |
| 136 | + std::shared_ptr<FileIO> file_io) const { |
| 137 | + ICEBERG_ASSIGN_OR_RAISE(auto cache_ref, manifests_cache_.Get(snapshot_, file_io)); |
| 138 | + auto& cache = cache_ref.get(); |
| 139 | + const size_t delete_start = cache.second; |
| 140 | + const size_t delete_count = cache.first.size() - delete_start; |
| 141 | + return std::span<ManifestFile>(cache.first.data() + delete_start, delete_count); |
| 142 | +} |
| 143 | + |
83 | 144 | } // namespace iceberg |
0 commit comments