TODO(zongheng): on metadata write failure or !status_.ok(), consider removing the orphaned data file.
| 595 | // TODO(zongheng): on metadata write failure or !status_.ok(), consider removing |
| 596 | // the orphaned data file. |
| 597 | Status BundleWriter::Finish() { |
| 598 | if (out_) { |
| 599 | status_.Update(out_->Close()); |
| 600 | out_ = nullptr; |
| 601 | if (status_.ok()) { |
| 602 | status_ = Env::Default()->RenameFile(tmp_data_path_, |
| 603 | DataFilename(prefix_, 0, 1)); |
| 604 | } else { |
| 605 | Env::Default()->DeleteFile(tmp_data_path_).IgnoreError(); |
| 606 | } |
| 607 | } |
| 608 | if (!status_.ok()) return status_; |
| 609 | // Build key -> BundleEntryProto table. |
| 610 | std::unique_ptr<WritableFile> file; |
| 611 | status_ = env_->NewWritableFile(tmp_metadata_path_, &file); |
| 612 | if (!status_.ok()) return status_; |
| 613 | { |
| 614 | // N.B.: the default use of Snappy compression may not be supported on all |
| 615 | // platforms (e.g. Android). The metadata file is small, so this is fine. |
| 616 | table::Options options; |
| 617 | options.compression = table::kNoCompression; |
| 618 | table::TableBuilder builder(options, file.get()); |
| 619 | // Header entry. |
| 620 | BundleHeaderProto header; |
| 621 | header.set_num_shards(1); |
| 622 | header.set_endianness(BundleHeaderProto::LITTLE); |
| 623 | if (!port::kLittleEndian) header.set_endianness(BundleHeaderProto::BIG); |
| 624 | VersionDef* version = header.mutable_version(); |
| 625 | version->set_producer(kTensorBundleVersion); |
| 626 | version->set_min_consumer(kTensorBundleMinConsumer); |
| 627 | |
| 628 | builder.Add(kHeaderEntryKey, header.SerializeAsString()); |
| 629 | |
| 630 | // All others. |
| 631 | for (const auto& p : entries_) { |
| 632 | builder.Add(p.first, p.second.SerializeAsString()); |
| 633 | } |
| 634 | status_ = builder.Finish(); |
| 635 | } |
| 636 | status_.Update(file->Close()); |
| 637 | if (!status_.ok()) { |
| 638 | Env::Default()->DeleteFile(tmp_metadata_path_).IgnoreError(); |
| 639 | return status_; |
| 640 | } else { |
| 641 | status_ = |
| 642 | Env::Default()->RenameFile(tmp_metadata_path_, MetaFilename(prefix_)); |
| 643 | if (!status_.ok()) return status_; |
| 644 | } |
| 645 | status_ = errors::Internal("BundleWriter is closed"); |
| 646 | return Status::OK(); |
| 647 | } |
| 648 | |
| 649 | // Merging tensor bundles. |
| 650 |