GENIE
serialization.cc
Go to the documentation of this file.
1 #include <fstream>
2 #include <memory>
3 
4 #include <boost/archive/binary_oarchive.hpp>
5 #include <boost/archive/binary_iarchive.hpp>
6 #include <boost/iostreams/filtering_streambuf.hpp>
7 #include <boost/iostreams/filter/bzip2.hpp>
8 #include <boost/serialization/base_object.hpp>
9 #include <boost/serialization/export.hpp>
10 #include <boost/serialization/map.hpp>
11 #include <boost/serialization/unordered_map.hpp>
12 #include <boost/serialization/vector.hpp>
13 
16 #include <genie/table/inv_table.h>
17 #include <genie/utility/Logger.h>
18 
19 #include "serialization.h"
20 
21 using namespace genie::utility;
22 
23 void
24 genie::utility::SaveTable(const std::string &filename, const std::shared_ptr<const genie::table::inv_table> &table)
25 {
26  // Cannot save more than one table using this function
27  if (table->get_table_index() != 0 || table->get_total_num_of_table() != 1)
28  throw genie::exception::cpu_runtime_error("Saving multiple tables not supported");
29 
30  std::ofstream ofs(filename.c_str(), std::ios::out | std::ios::binary | std::ios::trunc);
31 
32  boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
33  out.push(boost::iostreams::bzip2_compressor());
34  out.push(ofs);
35 
36  boost::archive::binary_oarchive oa(ofs);
37  // oa.register_type<genie::table::inv_compr_table>();
38  oa << table.get();
39 }
40 
41 std::shared_ptr<genie::table::inv_table>
42 genie::utility::LoadTable(const std::string &filename)
43 {
44  std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary);
45  if (!ifs.good())
46  throw genie::exception::cpu_runtime_error("Loading from file failed (fstream not good)");
47 
48  boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
49  in.push(boost::iostreams::bzip2_decompressor());
50  in.push(ifs);
51 
52  boost::archive::binary_iarchive ia(ifs);
53  // ia.register_type<genie::table::inv_compr_table>();
54 
55  genie::table::inv_table *loaded_table = nullptr;
56  ia >> loaded_table;
57  return std::shared_ptr<genie::table::inv_table>(loaded_table);
58 }
59 
60 // Macro BOOST_CLASS_VERSION() only works for version < 256. Use the following for higher versions:
61 // namespace boost { namespace serialization {
62 // template<> struct version<gps_position> {BOOST_STATIC_CONSTANT(int, value = APP_INT_VERSION); };
63 // }}
66 
67 
68 template <class Archive>
69 void genie::table::inv_table::load(Archive &ar, const unsigned int version)
70 {
71  ar.register_type(static_cast<genie::table::inv_compr_table*>(nullptr));
72  Logger::log(Logger::DEBUG, "Loading inv_table archive of version %d", version);
73  _build_status = builded;
74  // General structure
75  ar >> _size;
76  ar >> _dim_size;
77  // Inverted Index
78  ar >> _inv;
79  ar >> _inv_pos;
80  ar >> _inv_index_map;
81  ar >> inv_list_lowerbound;
82  ar >> inv_list_upperbound;
83  ar >> posting_list_size;
84  // Subsequence related fields
85  ar >> shift_bits_subsequence;
86  ar >> min_value_sequence;
87  ar >> max_value_sequence;
88  ar >> gram_length_sequence;
89  ar >> shift_bits_sequence;
90  ar >> _distinct_map;
91 
92 }
93 
94 template <class Archive>
95 void genie::table::inv_table::save(Archive &ar, const unsigned int version) const
96 {
97  ar.register_type(static_cast<genie::table::inv_compr_table*>(nullptr));
98  Logger::log(Logger::DEBUG, "Saving inv_table archive of version %d", version);
99  if (_build_status != builded)
100  throw genie::exception::cpu_runtime_error("Cannot serialize inv::table that has not yet been built.");
101  // General structure
102  ar << _size;
103  ar << _dim_size;
104  // Inverted Index
105  ar << _inv;
106  ar << _inv_pos;
107  ar << _inv_index_map;
108  ar << inv_list_lowerbound;
109  ar << inv_list_upperbound;
110  ar << posting_list_size;
111  // Subsequence related fields
112  ar << shift_bits_subsequence;
113  ar << min_value_sequence;
114  ar << max_value_sequence;
115  ar << gram_length_sequence;
116  ar << shift_bits_sequence;
117  ar << _distinct_map;
118 }
119 
120 
121 template <class Archive>
122 void genie::table::inv_compr_table::load(Archive &ar, const unsigned int version)
123 {
124  Logger::log(Logger::DEBUG, "Loading inv_compr_table archive of version %d", version);
125  ar >> boost::serialization::base_object<inv_table>(*this);
126 
127  ar >> m_isCompressed;
128  ar >> m_compression;
129  ar >> m_uncompressedInvListsMaxLength;
130  ar >> m_comprInv;
131  ar >> m_comprInvPos;
132 
133 }
134 
135 template <class Archive>
136 void genie::table::inv_compr_table::save(Archive &ar, const unsigned int version) const
137 {
138  Logger::log(Logger::DEBUG, "Saving inv_compr_table archive of version %d", version);
139  ar << boost::serialization::base_object<inv_table>(*this);
140 
141  ar << m_isCompressed;
142  ar << m_compression;
143  ar << m_uncompressedInvListsMaxLength;
144  ar << m_comprInv;
145  ar << m_comprInvPos;
146 }
147 
The declaration for class inv_table.
Definition: inv_table.h:41
std::shared_ptr< genie::table::inv_table > LoadTable(const std::string &filename)
void load(Archive &ar, const unsigned int version)
define class inv_table
static const int DEBUG
Definition: Logger.h:53
BOOST_CLASS_VERSION(genie::table::inv_table, 0)
static int log(int level, const char *fmt,...)
Record and print a message into log file.
Definition: Logger.cc:99
Record run-time information.
void save(Archive &ar, const unsigned int version) const
void load(Archive &ar, const unsigned int version)
void SaveTable(const std::string &filename, const std::shared_ptr< const genie::table::inv_table > &table)
void save(Archive &ar, const unsigned int version) const
define class inv_compre_table