| 646 | } |
| 647 | |
| 648 | void Dataset::SaveBinaryFile(const char* bin_filename) { |
| 649 | if (bin_filename != nullptr |
| 650 | && std::string(bin_filename) == data_filename_) { |
| 651 | Log::Warning("Bianry file %s already exists", bin_filename); |
| 652 | return; |
| 653 | } |
| 654 | // if not pass a filename, just append ".bin" of original file |
| 655 | std::string bin_filename_str(data_filename_); |
| 656 | if (bin_filename == nullptr || bin_filename[0] == '\0') { |
| 657 | bin_filename_str.append(".bin"); |
| 658 | bin_filename = bin_filename_str.c_str(); |
| 659 | } |
| 660 | bool is_file_existed = false; |
| 661 | |
| 662 | if (VirtualFileWriter::Exists(bin_filename)) { |
| 663 | is_file_existed = true; |
| 664 | Log::Warning("File %s exists, cannot save binary to it", bin_filename); |
| 665 | } |
| 666 | |
| 667 | if (!is_file_existed) { |
| 668 | auto writer = VirtualFileWriter::Make(bin_filename); |
| 669 | if (!writer->Init()) { |
| 670 | Log::Fatal("Cannot write binary data to %s ", bin_filename); |
| 671 | } |
| 672 | Log::Info("Saving data to binary file %s", bin_filename); |
| 673 | size_t size_of_token = std::strlen(binary_file_token); |
| 674 | writer->Write(binary_file_token, size_of_token); |
| 675 | // get size of header |
| 676 | size_t size_of_header = sizeof(num_data_) + sizeof(num_features_) + sizeof(num_total_features_) |
| 677 | + sizeof(int) * num_total_features_ + sizeof(label_idx_) + sizeof(num_groups_) + sizeof(sparse_threshold_) |
| 678 | + 3 * sizeof(int) * num_features_ + sizeof(uint64_t) * (num_groups_ + 1) + 2 * sizeof(int) * num_groups_ + sizeof(int8_t) * num_features_ |
| 679 | + sizeof(double) * num_features_ + sizeof(int32_t) * num_total_features_ + sizeof(int) * 3 + sizeof(bool) * 2; |
| 680 | // size of feature names |
| 681 | for (int i = 0; i < num_total_features_; ++i) { |
| 682 | size_of_header += feature_names_[i].size() + sizeof(int); |
| 683 | } |
| 684 | // size of forced bins |
| 685 | for (int i = 0; i < num_total_features_; ++i) { |
| 686 | size_of_header += forced_bin_bounds_[i].size() * sizeof(double) + sizeof(int); |
| 687 | } |
| 688 | writer->Write(&size_of_header, sizeof(size_of_header)); |
| 689 | // write header |
| 690 | writer->Write(&num_data_, sizeof(num_data_)); |
| 691 | writer->Write(&num_features_, sizeof(num_features_)); |
| 692 | writer->Write(&num_total_features_, sizeof(num_total_features_)); |
| 693 | writer->Write(&label_idx_, sizeof(label_idx_)); |
| 694 | writer->Write(&max_bin_, sizeof(max_bin_)); |
| 695 | writer->Write(&bin_construct_sample_cnt_, sizeof(bin_construct_sample_cnt_)); |
| 696 | writer->Write(&min_data_in_bin_, sizeof(min_data_in_bin_)); |
| 697 | writer->Write(&use_missing_, sizeof(use_missing_)); |
| 698 | writer->Write(&zero_as_missing_, sizeof(zero_as_missing_)); |
| 699 | writer->Write(&sparse_threshold_, sizeof(sparse_threshold_)); |
| 700 | writer->Write(used_feature_map_.data(), sizeof(int) * num_total_features_); |
| 701 | writer->Write(&num_groups_, sizeof(num_groups_)); |
| 702 | writer->Write(real_feature_idx_.data(), sizeof(int) * num_features_); |
| 703 | writer->Write(feature2group_.data(), sizeof(int) * num_features_); |
| 704 | writer->Write(feature2subfeature_.data(), sizeof(int) * num_features_); |
| 705 | writer->Write(group_bin_boundaries_.data(), sizeof(uint64_t) * (num_groups_ + 1)); |
no test coverage detected