jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
mixable_helper.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2011,2012,2014 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #ifndef JUBATUS_CORE_FRAMEWORK_MIXABLE_HELPER_HPP_
18 #define JUBATUS_CORE_FRAMEWORK_MIXABLE_HELPER_HPP_
19 
20 #include <string>
21 #include <vector>
22 
23 #include <msgpack.hpp>
24 #include "jubatus/util/lang/shared_ptr.h"
25 #include "../common/exception.hpp"
26 #include "model.hpp"
27 #include "linear_mixable.hpp"
28 #include "push_mixable.hpp"
29 
30 namespace jubatus {
31 namespace core {
32 namespace framework {
33 
34 // This CRTP delegation helper makes a class linear mixable
35 // which has get_diff and put_diff functions.
36 // This helper doesn't inherit `model` interface.
37 // MUST NOT create inheritance the `model`.
38 template <typename Model, typename Diff>
40  public:
41  typedef Model model_type;
42  typedef Diff diff_type;
43  typedef jubatus::util::lang::shared_ptr<Model> model_ptr;
44 
45  explicit linear_mixable_helper(model_ptr model)
46  : model_(model) {
47  if (!model) {
49  }
50  }
51 
52  void set_model(model_ptr m) {
53  if (!m) {
55  }
56  model_ = m;
57  }
58 
60  }
61 
62  model_ptr get_model() const {
63  return model_;
64  }
65 
66  diff_object convert_diff_object(const msgpack::object& obj) const {
67  internal_diff_object* diff = new internal_diff_object;
68  diff_object diff_obj(diff);
69  obj.convert(&diff->diff_);
70  return diff_obj;
71  }
72 
73  void mix(const msgpack::object& obj, diff_object ptr) const {
74  Diff diff;
75  internal_diff_object* diff_obj =
76  dynamic_cast<internal_diff_object*>(ptr.get());
77  if (!diff_obj) {
78  throw JUBATUS_EXCEPTION(
79  core::common::exception::runtime_error("bad diff_object"));
80  }
81  obj.convert(&diff);
82  model_->mix(diff, diff_obj->diff_);
83  }
84 
85  void get_diff(packer& pk) const {
86  Diff diff;
87  model_->get_diff(diff);
88  pk.pack(diff);
89  }
90 
91  bool put_diff(const diff_object& ptr) {
92  internal_diff_object* diff_obj =
93  dynamic_cast<internal_diff_object*>(ptr.get());
94  if (!diff_obj) {
95  throw JUBATUS_EXCEPTION(
96  core::common::exception::runtime_error("bad diff_object"));
97  }
98  return model_->put_diff(diff_obj->diff_);
99  }
100 
102  return model_->get_version();
103  }
104 
105  private:
107  void convert_binary(packer& pk) const {
108  pk.pack(diff_);
109  }
110 
111  Diff diff_;
112  };
113 
114  model_ptr model_;
115 };
116 
117 } // namespace framework
118 } // namespace core
119 } // namespace jubatus
120 
121 #endif // JUBATUS_CORE_FRAMEWORK_MIXABLE_HELPER_HPP_
void mix(const msgpack::object &obj, diff_object ptr) const
jubatus::util::lang::shared_ptr< diff_object_raw > diff_object
diff_object convert_diff_object(const msgpack::object &obj) const
jubatus::util::lang::shared_ptr< Model > model_ptr
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79