Skip to content

Commit aada98a

Browse files
committed
use inline static const string
1 parent 24edfd5 commit aada98a

File tree

3 files changed

+112
-91
lines changed

3 files changed

+112
-91
lines changed

src/iceberg/snapshot.cc

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,18 @@
2121

2222
namespace iceberg {
2323

24-
const std::string SnapshotSummaryFields::kOperation = "operation";
25-
const std::string SnapshotSummaryFields::kAddedDataFiles = "added-data-files";
26-
const std::string SnapshotSummaryFields::kDeletedDataFiles = "deleted-data-files";
27-
const std::string SnapshotSummaryFields::kTotalDataFiles = "total-data-files";
28-
const std::string SnapshotSummaryFields::kAddedDeleteFiles = "added-delete-files";
29-
const std::string SnapshotSummaryFields::kAddedEqDeleteFiles =
30-
"added-equality-delete-files";
31-
const std::string SnapshotSummaryFields::kRemovedEqDeleteFiles =
32-
"removed-equality-delete-files";
33-
const std::string SnapshotSummaryFields::kAddedPosDeleteFiles =
34-
"added-position-delete-files";
35-
const std::string SnapshotSummaryFields::kRemovedPosDeleteFiles =
36-
"removed-position-delete-files";
37-
const std::string SnapshotSummaryFields::kAddedDVs = "added-dvs";
38-
const std::string SnapshotSummaryFields::kRemovedDVs = "removed-dvs";
39-
const std::string SnapshotSummaryFields::kRemovedDeleteFiles = "removed-delete-files";
40-
const std::string SnapshotSummaryFields::kTotalDeleteFiles = "total-delete-files";
41-
const std::string SnapshotSummaryFields::kAddedRecords = "added-records";
42-
const std::string SnapshotSummaryFields::kDeletedRecords = "deleted-records";
43-
const std::string SnapshotSummaryFields::kTotalRecords = "total-records";
44-
const std::string SnapshotSummaryFields::kAddedFileSize = "added-files-size";
45-
const std::string SnapshotSummaryFields::kRemovedFileSize = "removed-files-size";
46-
const std::string SnapshotSummaryFields::kTotalFileSize = "total-files-size";
47-
const std::string SnapshotSummaryFields::kAddedPosDeletes = "added-position-deletes";
48-
const std::string SnapshotSummaryFields::kRemovedPosDeletes = "removed-position-deletes";
49-
const std::string SnapshotSummaryFields::kTotalPosDeletes = "total-position-deletes";
50-
const std::string SnapshotSummaryFields::kAddedEqDeletes = "added-equality-deletes";
51-
const std::string SnapshotSummaryFields::kRemovedEqDeletes = "removed-equality-deletes";
52-
const std::string SnapshotSummaryFields::kTotalEqDeletes = "total-equality-deletes";
53-
const std::string SnapshotSummaryFields::kDeletedDuplicatedFiles =
54-
"deleted-duplicate-files";
55-
const std::string SnapshotSummaryFields::kChangedPartitionCountProp =
56-
"changed-partition-count";
57-
58-
const std::string SnapshotSummaryFields::kWAPID = "wap.id";
59-
const std::string SnapshotSummaryFields::kPublishedWAPID = "published-wap-id";
60-
const std::string SnapshotSummaryFields::kSourceSnapshotID = "source-snapshot-id";
61-
const std::string SnapshotSummaryFields::kEngineName = "engine-name";
62-
const std::string SnapshotSummaryFields::kEngineVersion = "engine-version";
24+
SnapshotRefType SnapshotRef::type() const noexcept {
25+
return std::visit(
26+
[&](const auto& retention) -> SnapshotRefType {
27+
using T = std::decay_t<decltype(retention)>;
28+
if constexpr (std::is_same_v<T, Branch>) {
29+
return SnapshotRefType::kBranch;
30+
} else {
31+
return SnapshotRefType::kTag;
32+
}
33+
},
34+
retention);
35+
}
6336

6437
std::optional<std::string_view> Snapshot::operation() const {
6538
auto it = summary.find(SnapshotSummaryFields::kOperation);

src/iceberg/snapshot.h

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <string_view>
2525
#include <unordered_map>
2626
#include <variant>
27-
#include <vector>
2827

2928
#include "iceberg/iceberg_export.h"
3029

@@ -42,100 +41,113 @@ enum class SnapshotRefType {
4241

4342
/// \brief A reference to a snapshot, either a branch or a tag.
4443
struct ICEBERG_EXPORT SnapshotRef {
44+
struct ICEBERG_EXPORT Branch {
45+
/// A positive number for the minimum number of snapshots to keep in a branch while
46+
/// expiring snapshots. Defaults to table property
47+
/// history.expire.min-snapshots-to-keep.
48+
std::optional<int32_t> min_snapshots_to_keep;
49+
/// A positive number for the max age of snapshots to keep when
50+
/// expiring, including the latest snapshot. Defaults to table property
51+
/// history.expire.max-snapshot-age-ms.
52+
std::optional<int64_t> max_snapshot_age_ms;
53+
/// For snapshot references except the main branch, a positive number for the max age
54+
/// of the snapshot reference to keep while expiring snapshots. Defaults to table
55+
/// property history.expire.max-ref-age-ms. The main branch never expires.
56+
std::optional<int64_t> max_ref_age_ms;
57+
};
58+
59+
struct ICEBERG_EXPORT Tag {
60+
/// For snapshot references except the main branch, a positive number for the max age
61+
/// of the snapshot reference to keep while expiring snapshots. Defaults to table
62+
/// property history.expire.max-ref-age-ms. The main branch never expires.
63+
std::optional<int64_t> max_ref_age_ms;
64+
};
65+
4566
/// A reference's snapshot ID. The tagged snapshot or latest snapshot of a branch.
4667
int64_t snapshot_id;
47-
/// Type of the reference, tag or branch
48-
SnapshotRefType type;
49-
/// For branch type only, a positive number for the minimum number of snapshots to keep
50-
/// in a branch while expiring snapshots. Defaults to table property
51-
/// history.expire.min-snapshots-to-keep.
52-
std::optional<int32_t> min_snapshots_to_keep;
53-
/// For branch type only, a positive number for the max age of snapshots to keep when
54-
/// expiring, including the latest snapshot. Defaults to table property
55-
/// history.expire.max-snapshot-age-ms.
56-
std::optional<int64_t> max_snapshot_age_ms;
57-
/// For snapshot references except the main branch, a positive number for the max age of
58-
/// the snapshot reference to keep while expiring snapshots. Defaults to table property
59-
/// history.expire.max-ref-age-ms. The main branch never expires.
60-
std::optional<int64_t> max_ref_age_ms;
68+
/// Snapshot retention policy
69+
std::variant<Branch, Tag> retention;
70+
71+
SnapshotRefType type() const noexcept;
6172
};
6273

6374
/// \brief Optional Snapshot Summary Fields
6475
struct SnapshotSummaryFields {
6576
/// \brief The operation field key
66-
static const std::string kOperation;
77+
inline static const std::string kOperation = "operation";
6778

6879
/// Metrics, see https://iceberg.apache.org/spec/#metrics
6980

7081
/// \brief Number of data files added in the snapshot
71-
static const std::string kAddedDataFiles;
82+
inline static const std::string kAddedDataFiles = "added-data-files";
7283
/// \brief Number of data files deleted in the snapshot
73-
static const std::string kDeletedDataFiles;
84+
inline static const std::string kDeletedDataFiles = "deleted-data-files";
7485
/// \brief Total number of live data files in the snapshot
75-
static const std::string kTotalDataFiles;
86+
inline static const std::string kTotalDataFiles = "total-data-files";
7687
/// \brief Number of positional/equality delete files and deletion vectors added in the
7788
/// snapshot
78-
static const std::string kAddedDeleteFiles;
89+
inline static const std::string kAddedDeleteFiles = "added-delete-files";
7990
/// \brief Number of equality delete files added in the snapshot
80-
static const std::string kAddedEqDeleteFiles;
91+
inline static const std::string kAddedEqDeleteFiles = "added-equality-delete-files";
8192
/// \brief Number of equality delete files removed in the snapshot
82-
static const std::string kRemovedEqDeleteFiles;
93+
inline static const std::string kRemovedEqDeleteFiles = "removed-equality-delete-files";
8394
/// \brief Number of position delete files added in the snapshot
84-
static const std::string kAddedPosDeleteFiles;
95+
inline static const std::string kAddedPosDeleteFiles = "added-position-delete-files";
8596
/// \brief Number of position delete files removed in the snapshot
86-
static const std::string kRemovedPosDeleteFiles;
97+
inline static const std::string kRemovedPosDeleteFiles =
98+
"removed-position-delete-files";
8799
/// \brief Number of deletion vectors added in the snapshot
88-
static const std::string kAddedDVs;
100+
inline static const std::string kAddedDVs = "added-dvs";
89101
/// \brief Number of deletion vectors removed in the snapshot
90-
static const std::string kRemovedDVs;
102+
inline static const std::string kRemovedDVs = "removed-dvs";
91103
/// \brief Number of positional/equality delete files and deletion vectors removed in
92104
/// the snapshot
93-
static const std::string kRemovedDeleteFiles;
105+
inline static const std::string kRemovedDeleteFiles = "removed-delete-files";
94106
/// \brief Total number of live positional/equality delete files and deletion vectors in
95107
/// the snapshot
96-
static const std::string kTotalDeleteFiles;
108+
inline static const std::string kTotalDeleteFiles = "total-delete-files";
97109
/// \brief Number of records added in the snapshot
98-
static const std::string kAddedRecords;
110+
inline static const std::string kAddedRecords = "added-records";
99111
/// \brief Number of records deleted in the snapshot
100-
static const std::string kDeletedRecords;
112+
inline static const std::string kDeletedRecords = "deleted-records";
101113
/// \brief Total number of records in the snapshot
102-
static const std::string kTotalRecords;
114+
inline static const std::string kTotalRecords = "total-records";
103115
/// \brief The size of files added in the snapshot
104-
static const std::string kAddedFileSize;
116+
inline static const std::string kAddedFileSize = "added-files-size";
105117
/// \brief The size of files removed in the snapshot
106-
static const std::string kRemovedFileSize;
118+
inline static const std::string kRemovedFileSize = "removed-files-size";
107119
/// \brief Total size of live files in the snapshot
108-
static const std::string kTotalFileSize;
120+
inline static const std::string kTotalFileSize = "total-files-size";
109121
/// \brief Number of position delete records added in the snapshot
110-
static const std::string kAddedPosDeletes;
122+
inline static const std::string kAddedPosDeletes = "added-position-deletes";
111123
/// \brief Number of position delete records removed in the snapshot
112-
static const std::string kRemovedPosDeletes;
124+
inline static const std::string kRemovedPosDeletes = "removed-position-deletes";
113125
/// \brief Total number of position delete records in the snapshot
114-
static const std::string kTotalPosDeletes;
126+
inline static const std::string kTotalPosDeletes = "total-position-deletes";
115127
/// \brief Number of equality delete records added in the snapshot
116-
static const std::string kAddedEqDeletes;
128+
inline static const std::string kAddedEqDeletes = "added-equality-deletes";
117129
/// \brief Number of equality delete records removed in the snapshot
118-
static const std::string kRemovedEqDeletes;
130+
inline static const std::string kRemovedEqDeletes = "removed-equality-deletes";
119131
/// \brief Total number of equality delete records in the snapshot
120-
static const std::string kTotalEqDeletes;
132+
inline static const std::string kTotalEqDeletes = "total-equality-deletes";
121133
/// \brief Number of duplicate files deleted (duplicates are files recorded more than
122134
/// once in the manifest)
123-
static const std::string kDeletedDuplicatedFiles;
135+
inline static const std::string kDeletedDuplicatedFiles = "deleted-duplicate-files";
124136
/// \brief Number of partitions with files added or removed in the snapshot
125-
static const std::string kChangedPartitionCountProp;
137+
inline static const std::string kChangedPartitionCountProp = "changed-partition-count";
126138

127139
/// Other Fields, see https://iceberg.apache.org/spec/#other-fields
128140

129141
/// \brief The Write-Audit-Publish id of a staged snapshot
130-
static const std::string kWAPID;
142+
inline static const std::string kWAPID = "wap.id";
131143
/// \brief The Write-Audit-Publish id of a snapshot already been published
132-
static const std::string kPublishedWAPID;
144+
inline static const std::string kPublishedWAPID = "published-wap-id";
133145
/// \brief The original id of a cherry-picked snapshot
134-
static const std::string kSourceSnapshotID;
146+
inline static const std::string kSourceSnapshotID = "source-snapshot-id";
135147
/// \brief Name of the engine that created the snapshot
136-
static const std::string kEngineName;
148+
inline static const std::string kEngineName = "engine-name";
137149
/// \brief Version of the engine that created the snapshot
138-
static const std::string kEngineVersion;
150+
inline static const std::string kEngineVersion = "engine-version";
139151
};
140152

141153
/// \brief Data operation that produce snapshots.
@@ -145,16 +157,16 @@ struct SnapshotSummaryFields {
145157
/// does not need to clean up deleted files for appends, which have no deleted files.
146158
struct ICEBERG_EXPORT DataOperation {
147159
/// \brief Only data files were added and no files were removed.
148-
static constexpr std::string kAppend = "append";
160+
inline static const std::string kAppend = "append";
149161
/// \brief Data and delete files were added and removed without changing table data;
150162
/// i.e. compaction, change the data file format, or relocating data files.
151-
static constexpr std::string kReplace = "replace";
163+
inline static const std::string kReplace = "replace";
152164
/// \brief Data and delete files were added and removed in a logical overwrite
153165
/// operation.
154-
static constexpr std::string kOverwrite = "overwrite";
166+
inline static const std::string kOverwrite = "overwrite";
155167
/// \brief Data files were removed and their contents logically deleted and/or delete
156168
/// files were added to delete rows.
157-
static constexpr std::string kDelete = "delete";
169+
inline static const std::string kDelete = "delete";
158170
};
159171

160172
/// \brief A snapshot of the data in a table at a point in time.

test/snapshot_test.cc

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,49 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
2019
#include "iceberg/snapshot.h"
2120

2221
#include <gtest/gtest.h>
2322

2423
namespace iceberg {
2524

25+
TEST(SnapshotRefTest, SnapshotRefBranchInitialization) {
26+
SnapshotRef snapshot_ref;
27+
28+
snapshot_ref.snapshot_id = 12345;
29+
snapshot_ref.retention = SnapshotRef::Branch{
30+
.min_snapshots_to_keep = 10, .max_snapshot_age_ms = 5000, .max_ref_age_ms = 10000};
31+
32+
EXPECT_EQ(snapshot_ref.snapshot_id, 12345);
33+
EXPECT_TRUE(std::holds_alternative<SnapshotRef::Branch>(snapshot_ref.retention));
34+
EXPECT_EQ(snapshot_ref.type(), SnapshotRefType::kBranch);
35+
36+
auto* branch = std::get_if<SnapshotRef::Branch>(&snapshot_ref.retention);
37+
ASSERT_NE(branch, nullptr);
38+
EXPECT_TRUE(branch->min_snapshots_to_keep.has_value());
39+
EXPECT_EQ(branch->min_snapshots_to_keep.value(), 10);
40+
EXPECT_TRUE(branch->max_snapshot_age_ms.has_value());
41+
EXPECT_EQ(branch->max_snapshot_age_ms.value(), 5000);
42+
EXPECT_TRUE(branch->max_ref_age_ms.has_value());
43+
EXPECT_EQ(branch->max_ref_age_ms.value(), 10000);
44+
}
45+
46+
TEST(SnapshotRefTest, SnapshotRefTagInitialization) {
47+
SnapshotRef snapshot_ref;
48+
49+
snapshot_ref.snapshot_id = 67890;
50+
snapshot_ref.retention = SnapshotRef::Tag{.max_ref_age_ms = 20000};
51+
52+
EXPECT_EQ(snapshot_ref.snapshot_id, 67890);
53+
EXPECT_TRUE(std::holds_alternative<SnapshotRef::Tag>(snapshot_ref.retention));
54+
EXPECT_EQ(snapshot_ref.type(), SnapshotRefType::kTag);
55+
56+
auto* tag = std::get_if<SnapshotRef::Tag>(&snapshot_ref.retention);
57+
ASSERT_NE(tag, nullptr);
58+
EXPECT_TRUE(tag->max_ref_age_ms.has_value());
59+
EXPECT_EQ(tag->max_ref_age_ms.value(), 20000);
60+
}
61+
2662
class SnapshotTest : public ::testing::Test {
2763
protected:
2864
void SetUp() override {

0 commit comments

Comments
 (0)