jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
storage.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2013 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 #include "storage.hpp"
18 
19 #include <algorithm>
20 #include <iterator>
21 #include <string>
22 #include "util.hpp"
23 
24 namespace jubatus {
25 namespace core {
26 namespace clustering {
27 
28 storage::storage(const std::string& name, const clustering_config& config)
29  : revision_(0),
30  name_(name),
31  config_(config) {
32 }
33 
35  wplist ret = get_mine();
36  wplist common = get_common();
37  concat(common, ret);
38  return ret;
39 }
40 
42  wplist ret;
43  for (diff_t::const_iterator it = common_.begin();
44  it != common_.end(); ++it) {
45  concat(it->second, ret);
46  }
47  return ret;
48 }
49 
50 void storage::get_diff(diff_t& d) const {
51  d.clear();
52  wplist coreset = get_mine();
53  d.push_back(make_pair(name_, coreset));
54 }
55 
56 bool storage::put_diff(const diff_t& diff) {
57  common_.clear();
58  for (diff_t::const_iterator it = diff.begin(); it != diff.end(); ++it) {
59  if (it->first != name_) {
60  common_.push_back(*it);
61  }
62  }
64 
65  // TODO(kumagi): return false if we want to reject the diff
66  return true;
67 }
68 
69 void storage::mix(const diff_t& lhs, diff_t& ret) {
70  diff_t::const_iterator lb = lhs.begin(), le = lhs.end();
71  diff_t::iterator b = ret.begin(), e = ret.end();
72  while (lb != le && b != e) {
73  if ((*lb).first < (*b).first) {
74  b = ret.insert(b, (*lb));
75  ++lb;
76  } else if ((*lb).first > (*b).first) {
77  ++b;
78  } else {
79  std::copy((*lb).second.begin(), (*lb).second.end(),
80  std::back_inserter((*b).second));
81  ++b;
82  ++lb;
83  }
84  }
85  std::copy(lb, le, std::back_inserter(ret));
86 };
87 
89  pack_impl_(packer);
90 }
91 
92 void storage::unpack(msgpack::object o) {
93  unpack_impl_(o);
95 }
96 
98  // TODO(gintenlabo): consider revisions
99  clear_impl_();
101 }
102 
104  return revision_;
105 }
106 
108  ++revision_;
110 }
111 
113  packer.pack(*this);
114 }
115 void storage::unpack_impl_(msgpack::object o) {
116  o.convert(this);
117 }
119  common_.clear();
120 }
121 
122 
123 } // namespace clustering
124 } // namespace core
125 } // namespace jubatus
virtual wplist get_common() const
Definition: storage.cpp:41
void unpack(msgpack::object o)
Definition: storage.cpp:92
virtual wplist get_all() const
Definition: storage.cpp:34
void mix(const diff_t &, diff_t &ret)
Definition: storage.cpp:69
std::vector< std::pair< std::string, wplist > > common_
Definition: storage.hpp:76
storage(const std::string &name, const clustering_config &config)
Definition: storage.cpp:28
void concat(const wplist &src, wplist &dst)
Definition: util.cpp:33
void dispatch(const storage_event_type &type, const wplist &data) const
void pack(framework::packer &packer) const
Definition: storage.cpp:88
void get_diff(diff_t &d) const
Definition: storage.cpp:50
virtual wplist get_mine() const =0
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
std::vector< std::pair< std::string, wplist > > diff_t
Definition: types.hpp:57
virtual void unpack_impl_(msgpack::object o)
Definition: storage.cpp:115
virtual void pack_impl_(framework::packer &packer) const
Definition: storage.cpp:112
std::vector< weighted_point > wplist
Definition: types.hpp:55
bool put_diff(const diff_t &d)
Definition: storage.cpp:56