Skip to content

Commit 7c325fa

Browse files
authored
refactor(manifest): remove MakeVxWriter functions in favor of MakeWriter (#551)
Remove MakeVxWriter functions in favor of MakeWriter, and apply int8_t to all format version parameters for consistency.
1 parent 02f0335 commit 7c325fa

12 files changed

+145
-325
lines changed

src/iceberg/json_serde.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <nlohmann/json.hpp>
2828

29+
#include "iceberg/constants.h"
2930
#include "iceberg/json_serde_internal.h"
3031
#include "iceberg/name_mapping.h"
3132
#include "iceberg/partition_field.h"

src/iceberg/manifest/manifest_writer.cc

Lines changed: 59 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "iceberg/partition_summary_internal.h"
3131
#include "iceberg/result.h"
3232
#include "iceberg/schema.h"
33-
#include "iceberg/snapshot.h"
3433
#include "iceberg/table_metadata.h"
3534
#include "iceberg/util/macros.h"
3635

@@ -272,87 +271,41 @@ Result<std::unique_ptr<Writer>> OpenFileWriter(
272271
return writer;
273272
}
274273

275-
Result<std::unique_ptr<ManifestWriter>> ManifestWriter::MakeV1Writer(
276-
std::optional<int64_t> snapshot_id, std::string_view manifest_location,
277-
std::shared_ptr<FileIO> file_io, std::shared_ptr<PartitionSpec> partition_spec,
278-
std::shared_ptr<Schema> current_schema) {
279-
if (manifest_location.empty()) {
280-
return InvalidArgument("Manifest location cannot be empty");
281-
}
282-
if (!file_io) {
283-
return InvalidArgument("FileIO cannot be null");
284-
}
285-
if (!partition_spec) {
286-
return InvalidArgument("PartitionSpec cannot be null");
287-
}
288-
if (!current_schema) {
289-
return InvalidArgument("Current schema cannot be null");
290-
}
274+
Result<std::unique_ptr<ManifestWriter>> ManifestWriter::MakeWriter(
275+
int8_t format_version, std::optional<int64_t> snapshot_id,
276+
std::string_view manifest_location, std::shared_ptr<FileIO> file_io,
277+
std::shared_ptr<PartitionSpec> partition_spec, std::shared_ptr<Schema> current_schema,
278+
ManifestContent content, std::optional<int64_t> first_row_id) {
279+
ICEBERG_PRECHECK(!manifest_location.empty(), "Manifest location cannot be empty");
280+
ICEBERG_PRECHECK(file_io, "FileIO cannot be null");
281+
ICEBERG_PRECHECK(partition_spec, "PartitionSpec cannot be null");
282+
ICEBERG_PRECHECK(current_schema, "Current schema cannot be null");
291283

292-
auto adapter = std::make_unique<ManifestEntryAdapterV1>(
293-
snapshot_id, std::move(partition_spec), std::move(current_schema));
294-
ICEBERG_RETURN_UNEXPECTED(adapter->Init());
295-
ICEBERG_RETURN_UNEXPECTED(adapter->StartAppending());
284+
std::unique_ptr<ManifestEntryAdapter> adapter;
285+
std::optional<int64_t> writer_first_row_id = std::nullopt;
296286

297-
auto schema = adapter->schema();
298-
ICEBERG_ASSIGN_OR_RAISE(
299-
auto writer,
300-
OpenFileWriter(manifest_location, std::move(schema), std::move(file_io),
301-
adapter->metadata(), "manifest_entry"));
302-
return std::unique_ptr<ManifestWriter>(new ManifestWriter(
303-
std::move(writer), std::move(adapter), manifest_location, std::nullopt));
304-
}
305-
306-
Result<std::unique_ptr<ManifestWriter>> ManifestWriter::MakeV2Writer(
307-
std::optional<int64_t> snapshot_id, std::string_view manifest_location,
308-
std::shared_ptr<FileIO> file_io, std::shared_ptr<PartitionSpec> partition_spec,
309-
std::shared_ptr<Schema> current_schema, ManifestContent content) {
310-
if (manifest_location.empty()) {
311-
return InvalidArgument("Manifest location cannot be empty");
312-
}
313-
if (!file_io) {
314-
return InvalidArgument("FileIO cannot be null");
315-
}
316-
if (!partition_spec) {
317-
return InvalidArgument("PartitionSpec cannot be null");
318-
}
319-
if (!current_schema) {
320-
return InvalidArgument("Current schema cannot be null");
287+
switch (format_version) {
288+
case 1: {
289+
adapter = std::make_unique<ManifestEntryAdapterV1>(
290+
snapshot_id, std::move(partition_spec), std::move(current_schema));
291+
break;
292+
}
293+
case 2: {
294+
adapter = std::make_unique<ManifestEntryAdapterV2>(
295+
snapshot_id, std::move(partition_spec), std::move(current_schema), content);
296+
break;
297+
}
298+
case 3: {
299+
adapter = std::make_unique<ManifestEntryAdapterV3>(
300+
snapshot_id, first_row_id, std::move(partition_spec), std::move(current_schema),
301+
content);
302+
writer_first_row_id = first_row_id;
303+
break;
304+
}
305+
default:
306+
return NotSupported("Format version {} is not supported", format_version);
321307
}
322-
auto adapter = std::make_unique<ManifestEntryAdapterV2>(
323-
snapshot_id, std::move(partition_spec), std::move(current_schema), content);
324-
ICEBERG_RETURN_UNEXPECTED(adapter->Init());
325-
ICEBERG_RETURN_UNEXPECTED(adapter->StartAppending());
326-
327-
auto schema = adapter->schema();
328-
ICEBERG_ASSIGN_OR_RAISE(
329-
auto writer,
330-
OpenFileWriter(manifest_location, std::move(schema), std::move(file_io),
331-
adapter->metadata(), "manifest_entry"));
332-
return std::unique_ptr<ManifestWriter>(new ManifestWriter(
333-
std::move(writer), std::move(adapter), manifest_location, std::nullopt));
334-
}
335308

336-
Result<std::unique_ptr<ManifestWriter>> ManifestWriter::MakeV3Writer(
337-
std::optional<int64_t> snapshot_id, std::optional<int64_t> first_row_id,
338-
std::string_view manifest_location, std::shared_ptr<FileIO> file_io,
339-
std::shared_ptr<PartitionSpec> partition_spec, std::shared_ptr<Schema> current_schema,
340-
ManifestContent content) {
341-
if (manifest_location.empty()) {
342-
return InvalidArgument("Manifest location cannot be empty");
343-
}
344-
if (!file_io) {
345-
return InvalidArgument("FileIO cannot be null");
346-
}
347-
if (!partition_spec) {
348-
return InvalidArgument("PartitionSpec cannot be null");
349-
}
350-
if (!current_schema) {
351-
return InvalidArgument("Current schema cannot be null");
352-
}
353-
auto adapter = std::make_unique<ManifestEntryAdapterV3>(
354-
snapshot_id, first_row_id, std::move(partition_spec), std::move(current_schema),
355-
content);
356309
ICEBERG_RETURN_UNEXPECTED(adapter->Init());
357310
ICEBERG_RETURN_UNEXPECTED(adapter->StartAppending());
358311

@@ -362,28 +315,7 @@ Result<std::unique_ptr<ManifestWriter>> ManifestWriter::MakeV3Writer(
362315
OpenFileWriter(manifest_location, std::move(schema), std::move(file_io),
363316
adapter->metadata(), "manifest_entry"));
364317
return std::unique_ptr<ManifestWriter>(new ManifestWriter(
365-
std::move(writer), std::move(adapter), manifest_location, first_row_id));
366-
}
367-
368-
Result<std::unique_ptr<ManifestWriter>> ManifestWriter::MakeWriter(
369-
int8_t format_version, std::optional<int64_t> snapshot_id,
370-
std::string_view manifest_location, std::shared_ptr<FileIO> file_io,
371-
std::shared_ptr<PartitionSpec> partition_spec, std::shared_ptr<Schema> current_schema,
372-
ManifestContent content, std::optional<int64_t> first_row_id) {
373-
switch (format_version) {
374-
case 1:
375-
return MakeV1Writer(snapshot_id, manifest_location, std::move(file_io),
376-
std::move(partition_spec), std::move(current_schema));
377-
case 2:
378-
return MakeV2Writer(snapshot_id, manifest_location, std::move(file_io),
379-
std::move(partition_spec), std::move(current_schema), content);
380-
case 3:
381-
return MakeV3Writer(snapshot_id, first_row_id, manifest_location,
382-
std::move(file_io), std::move(partition_spec),
383-
std::move(current_schema), content);
384-
default:
385-
return NotSupported("Format version {} is not supported", format_version);
386-
}
318+
std::move(writer), std::move(adapter), manifest_location, writer_first_row_id));
387319
}
388320

389321
ManifestListWriter::ManifestListWriter(std::unique_ptr<Writer> writer,
@@ -420,83 +352,47 @@ std::optional<int64_t> ManifestListWriter::next_row_id() const {
420352
return adapter_->next_row_id();
421353
}
422354

423-
Result<std::unique_ptr<ManifestListWriter>> ManifestListWriter::MakeV1Writer(
424-
int64_t snapshot_id, std::optional<int64_t> parent_snapshot_id,
425-
std::string_view manifest_list_location, std::shared_ptr<FileIO> file_io) {
426-
auto adapter = std::make_unique<ManifestFileAdapterV1>(snapshot_id, parent_snapshot_id);
427-
ICEBERG_RETURN_UNEXPECTED(adapter->Init());
428-
ICEBERG_RETURN_UNEXPECTED(adapter->StartAppending());
429-
430-
auto schema = adapter->schema();
431-
ICEBERG_ASSIGN_OR_RAISE(
432-
auto writer,
433-
OpenFileWriter(manifest_list_location, std::move(schema), std::move(file_io),
434-
adapter->metadata(), "manifest_file"));
435-
return std::unique_ptr<ManifestListWriter>(
436-
new ManifestListWriter(std::move(writer), std::move(adapter)));
437-
}
438-
439-
Result<std::unique_ptr<ManifestListWriter>> ManifestListWriter::MakeV2Writer(
440-
int64_t snapshot_id, std::optional<int64_t> parent_snapshot_id,
441-
int64_t sequence_number, std::string_view manifest_list_location,
442-
std::shared_ptr<FileIO> file_io) {
443-
auto adapter = std::make_unique<ManifestFileAdapterV2>(snapshot_id, parent_snapshot_id,
444-
sequence_number);
445-
ICEBERG_RETURN_UNEXPECTED(adapter->Init());
446-
ICEBERG_RETURN_UNEXPECTED(adapter->StartAppending());
447-
448-
auto schema = adapter->schema();
449-
ICEBERG_ASSIGN_OR_RAISE(
450-
auto writer,
451-
OpenFileWriter(manifest_list_location, std::move(schema), std::move(file_io),
452-
adapter->metadata(), "manifest_file"));
453-
454-
return std::unique_ptr<ManifestListWriter>(
455-
new ManifestListWriter(std::move(writer), std::move(adapter)));
456-
}
457-
458-
Result<std::unique_ptr<ManifestListWriter>> ManifestListWriter::MakeV3Writer(
459-
int64_t snapshot_id, std::optional<int64_t> parent_snapshot_id,
460-
int64_t sequence_number, int64_t first_row_id,
461-
std::string_view manifest_list_location, std::shared_ptr<FileIO> file_io) {
462-
auto adapter = std::make_unique<ManifestFileAdapterV3>(snapshot_id, parent_snapshot_id,
463-
sequence_number, first_row_id);
464-
ICEBERG_RETURN_UNEXPECTED(adapter->Init());
465-
ICEBERG_RETURN_UNEXPECTED(adapter->StartAppending());
466-
467-
auto schema = adapter->schema();
468-
ICEBERG_ASSIGN_OR_RAISE(
469-
auto writer,
470-
OpenFileWriter(manifest_list_location, std::move(schema), std::move(file_io),
471-
adapter->metadata(), "manifest_file"));
472-
return std::unique_ptr<ManifestListWriter>(
473-
new ManifestListWriter(std::move(writer), std::move(adapter)));
474-
}
475-
476355
Result<std::unique_ptr<ManifestListWriter>> ManifestListWriter::MakeWriter(
477356
int8_t format_version, int64_t snapshot_id, std::optional<int64_t> parent_snapshot_id,
478357
std::string_view manifest_list_location, std::shared_ptr<FileIO> file_io,
479358
std::optional<int64_t> sequence_number, std::optional<int64_t> first_row_id) {
359+
std::unique_ptr<ManifestFileAdapter> adapter;
360+
480361
switch (format_version) {
481-
case 1:
482-
return MakeV1Writer(snapshot_id, parent_snapshot_id, manifest_list_location,
483-
std::move(file_io));
484-
case 2:
362+
case 1: {
363+
adapter = std::make_unique<ManifestFileAdapterV1>(snapshot_id, parent_snapshot_id);
364+
break;
365+
}
366+
case 2: {
485367
ICEBERG_PRECHECK(sequence_number.has_value(),
486368
"Sequence number is required for format version 2");
487-
return MakeV2Writer(snapshot_id, parent_snapshot_id, sequence_number.value(),
488-
manifest_list_location, std::move(file_io));
489-
case 3:
369+
adapter = std::make_unique<ManifestFileAdapterV2>(snapshot_id, parent_snapshot_id,
370+
sequence_number.value());
371+
break;
372+
}
373+
case 3: {
490374
ICEBERG_PRECHECK(sequence_number.has_value(),
491375
"Sequence number is required for format version 3");
492376
ICEBERG_PRECHECK(first_row_id.has_value(),
493377
"First row ID is required for format version 3");
494-
return MakeV3Writer(snapshot_id, parent_snapshot_id, sequence_number.value(),
495-
first_row_id.value(), manifest_list_location,
496-
std::move(file_io));
378+
adapter = std::make_unique<ManifestFileAdapterV3>(
379+
snapshot_id, parent_snapshot_id, sequence_number.value(), first_row_id.value());
380+
break;
381+
}
497382
default:
498383
return NotSupported("Format version {} is not supported", format_version);
499384
}
385+
386+
ICEBERG_RETURN_UNEXPECTED(adapter->Init());
387+
ICEBERG_RETURN_UNEXPECTED(adapter->StartAppending());
388+
389+
auto schema = adapter->schema();
390+
ICEBERG_ASSIGN_OR_RAISE(
391+
auto writer,
392+
OpenFileWriter(manifest_list_location, std::move(schema), std::move(file_io),
393+
adapter->metadata(), "manifest_file"));
394+
return std::unique_ptr<ManifestListWriter>(
395+
new ManifestListWriter(std::move(writer), std::move(adapter)));
500396
}
501397

502398
} // namespace iceberg

src/iceberg/manifest/manifest_writer.h

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -117,48 +117,6 @@ class ICEBERG_EXPORT ManifestWriter {
117117
/// \note Only valid after the file is closed.
118118
Result<ManifestFile> ToManifestFile() const;
119119

120-
/// \brief Creates a writer for a manifest file.
121-
/// \param snapshot_id ID of the snapshot.
122-
/// \param manifest_location Path to the manifest file.
123-
/// \param file_io File IO implementation to use.
124-
/// \param partition_spec Partition spec for the manifest.
125-
/// \param current_schema Current table schema.
126-
/// \return A Result containing the writer or an error.
127-
static Result<std::unique_ptr<ManifestWriter>> MakeV1Writer(
128-
std::optional<int64_t> snapshot_id, std::string_view manifest_location,
129-
std::shared_ptr<FileIO> file_io, std::shared_ptr<PartitionSpec> partition_spec,
130-
std::shared_ptr<Schema> current_schema);
131-
132-
/// \brief Creates a writer for a manifest file.
133-
/// \param snapshot_id ID of the snapshot.
134-
/// \param manifest_location Path to the manifest file.
135-
/// \param file_io File IO implementation to use.
136-
/// \param partition_spec Partition spec for the manifest.
137-
/// \param current_schema Schema containing the source fields referenced by partition
138-
/// spec.
139-
/// \param content Content of the manifest.
140-
/// \return A Result containing the writer or an error.
141-
static Result<std::unique_ptr<ManifestWriter>> MakeV2Writer(
142-
std::optional<int64_t> snapshot_id, std::string_view manifest_location,
143-
std::shared_ptr<FileIO> file_io, std::shared_ptr<PartitionSpec> partition_spec,
144-
std::shared_ptr<Schema> current_schema, ManifestContent content);
145-
146-
/// \brief Creates a writer for a manifest file.
147-
/// \param snapshot_id ID of the snapshot.
148-
/// \param first_row_id First row ID of the snapshot.
149-
/// \param manifest_location Path to the manifest file.
150-
/// \param file_io File IO implementation to use.
151-
/// \param partition_spec Partition spec for the manifest.
152-
/// \param current_schema Schema containing the source fields referenced by partition
153-
/// spec.
154-
/// \param content Content of the manifest.
155-
/// \return A Result containing the writer or an error.
156-
static Result<std::unique_ptr<ManifestWriter>> MakeV3Writer(
157-
std::optional<int64_t> snapshot_id, std::optional<int64_t> first_row_id,
158-
std::string_view manifest_location, std::shared_ptr<FileIO> file_io,
159-
std::shared_ptr<PartitionSpec> partition_spec,
160-
std::shared_ptr<Schema> current_schema, ManifestContent content);
161-
162120
/// \brief Factory function to create a writer for a manifest file based on format
163121
/// version.
164122
/// \param format_version The format version (1, 2, 3, etc.).
@@ -226,41 +184,6 @@ class ICEBERG_EXPORT ManifestListWriter {
226184
/// \brief Get the next row id to assign.
227185
std::optional<int64_t> next_row_id() const;
228186

229-
/// \brief Creates a writer for the v1 manifest list.
230-
/// \param snapshot_id ID of the snapshot.
231-
/// \param parent_snapshot_id ID of the parent snapshot.
232-
/// \param manifest_list_location Path to the manifest list file.
233-
/// \param file_io File IO implementation to use.
234-
/// \return A Result containing the writer or an error.
235-
static Result<std::unique_ptr<ManifestListWriter>> MakeV1Writer(
236-
int64_t snapshot_id, std::optional<int64_t> parent_snapshot_id,
237-
std::string_view manifest_list_location, std::shared_ptr<FileIO> file_io);
238-
239-
/// \brief Creates a writer for the manifest list.
240-
/// \param snapshot_id ID of the snapshot.
241-
/// \param parent_snapshot_id ID of the parent snapshot.
242-
/// \param sequence_number Sequence number of the snapshot.
243-
/// \param manifest_list_location Path to the manifest list file.
244-
/// \param file_io File IO implementation to use.
245-
/// \return A Result containing the writer or an error.
246-
static Result<std::unique_ptr<ManifestListWriter>> MakeV2Writer(
247-
int64_t snapshot_id, std::optional<int64_t> parent_snapshot_id,
248-
int64_t sequence_number, std::string_view manifest_list_location,
249-
std::shared_ptr<FileIO> file_io);
250-
251-
/// \brief Creates a writer for the manifest list.
252-
/// \param snapshot_id ID of the snapshot.
253-
/// \param parent_snapshot_id ID of the parent snapshot.
254-
/// \param sequence_number Sequence number of the snapshot.
255-
/// \param first_row_id First row ID of the snapshot.
256-
/// \param manifest_list_location Path to the manifest list file.
257-
/// \param file_io File IO implementation to use.
258-
/// \return A Result containing the writer or an error.
259-
static Result<std::unique_ptr<ManifestListWriter>> MakeV3Writer(
260-
int64_t snapshot_id, std::optional<int64_t> parent_snapshot_id,
261-
int64_t sequence_number, int64_t first_row_id,
262-
std::string_view manifest_list_location, std::shared_ptr<FileIO> file_io);
263-
264187
/// \brief Factory function to create a writer for the manifest list based on format
265188
/// version.
266189
/// \param format_version The format version (1, 2, 3, etc.).

0 commit comments

Comments
 (0)