jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
local_storage.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2011 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_STORAGE_LOCAL_STORAGE_HPP_
18 #define JUBATUS_CORE_STORAGE_LOCAL_STORAGE_HPP_
19 
20 #include <map>
21 #include <string>
22 #include <vector>
23 #include "jubatus/util/data/unordered_map.h"
24 #include "jubatus/util/concurrent/mutex.h"
25 #include "jubatus/util/concurrent/lock.h"
26 #include "storage_base.hpp"
27 #include "../common/key_manager.hpp"
28 #include "../common/version.hpp"
29 
30 namespace jubatus {
31 namespace core {
32 namespace storage {
33 
34 typedef jubatus::util::data::unordered_map<uint64_t, val3_t> id_feature_val3_t;
35 typedef jubatus::util::data::unordered_map<std::string, id_feature_val3_t>
37 
38 class local_storage : public storage_base {
39  public:
40  local_storage();
42 
43  void get(const std::string &feature, feature_val1_t& ret) const;
44  void get_nolock(const std::string &feature, feature_val1_t& ret) const;
45  void get2(const std::string &feature, feature_val2_t& ret) const;
46  void get2_nolock(const std::string &feature, feature_val2_t& ret) const;
47  void get3(const std::string &feature, feature_val3_t& ret) const;
48  void get3_nolock(const std::string &feature, feature_val3_t& ret) const;
49 
50  // inner product
51  void inp(const common::sfv_t& sfv, map_feature_val1_t& ret) const;
52 
53  void set(
54  const std::string& feature,
55  const std::string& klass,
56  const val1_t& w);
57  void set_nolock(
58  const std::string& feature,
59  const std::string& klass,
60  const val1_t& w);
61  void set2(
62  const std::string& feature,
63  const std::string& klass,
64  const val2_t& w);
65  void set2_nolock(
66  const std::string& feature,
67  const std::string& klass,
68  const val2_t& w);
69  void set3(
70  const std::string& feature,
71  const std::string& klass,
72  const val3_t& w);
73  void set3_nolock(
74  const std::string& feature,
75  const std::string& klass,
76  const val3_t& w);
77 
78  void get_status(std::map<std::string, std::string>& status) const;
79 
80  void update(
81  const std::string& feature,
82  const std::string& inc_class,
83  const std::string& dec_class,
84  const val1_t& v);
85  void bulk_update(
86  const common::sfv_t& sfv,
87  float step_width,
88  const std::string& inc_class,
89  const std::string& dec_class);
90 
91  util::concurrent::mutex& get_lock() const;
92 
93  void register_label(const std::string& label);
94  bool delete_label(const std::string& label);
95  bool delete_label_nolock(const std::string& label);
96 
97  void clear();
98  std::vector<std::string> get_labels() const;
99  bool set_label(const std::string& label);
100 
101  void pack(framework::packer& packer) const;
102  void unpack(msgpack::object o);
104  return storage::version();
105  }
106  std::string type() const;
107 
109 
110  private:
111  // map_features3_t tbl_;
112  mutable util::concurrent::mutex mutex_;
115 
116  // used for dump data
117  friend std::ostream& operator<<(std::ostream& os, const local_storage& ls) {
118  util::concurrent::scoped_lock lk(ls.mutex_);
119  os << "{" << std::endl;
120  for (id_features3_t::const_iterator it = ls.tbl_.begin();
121  it != ls.tbl_.end();
122  ++it) {
123  os << " {" << it->first << ": " << "{" << std::endl;
124  const id_feature_val3_t val = it->second;
125  for (id_feature_val3_t::const_iterator jt = val.begin();
126  jt != val.end();
127  ++jt) {
128  os << " " << ls.class2id_.get_key(jt->first) << ": " << jt->second;
129 
130  {
131  id_feature_val3_t::const_iterator jt_next = jt;
132  ++jt_next;
133  if (jt_next != val.end()) {
134  os << ", ";
135  }
136  }
137  os << std::endl;
138  }
139  os << " }";
140 
141  {
142  id_features3_t::const_iterator it_next = it;
143  ++it_next;
144  if (it_next != ls.tbl_.end()) {
145  os << ",";
146  }
147  }
148  os << std::endl;
149  }
150  os << "}";
151  return os;
152  }
153 };
154 
155 } // namespace storage
156 } // namespace core
157 } // namespace jubatus
158 
159 #endif // JUBATUS_CORE_STORAGE_LOCAL_STORAGE_HPP_
void set3(const std::string &feature, const std::string &klass, const val3_t &w)
void set2(const std::string &feature, const std::string &klass, const val2_t &w)
bool set_label(const std::string &label)
void set2_nolock(const std::string &feature, const std::string &klass, const val2_t &w)
void get_status(std::map< std::string, std::string > &status) const
void update(const std::string &feature, const std::string &inc_class, const std::string &dec_class, const val1_t &v)
jubatus::util::data::unordered_map< std::string, val1_t > map_feature_val1_t
void get_nolock(const std::string &feature, feature_val1_t &ret) const
jubatus::util::data::unordered_map< std::string, id_feature_val3_t > id_features3_t
void inp(const common::sfv_t &sfv, map_feature_val1_t &ret) const
void register_label(const std::string &label)
void get2_nolock(const std::string &feature, feature_val2_t &ret) const
void pack(framework::packer &packer) const
void bulk_update(const common::sfv_t &sfv, float step_width, const std::string &inc_class, const std::string &dec_class)
std::vector< std::pair< std::string, val1_t > > feature_val1_t
void set(const std::string &feature, const std::string &klass, const val1_t &w)
void get3(const std::string &feature, feature_val3_t &ret) const
const std::string & get_key(const uint64_t id) const
Definition: key_manager.cpp:78
storage::version get_version() const
void set3_nolock(const std::string &feature, const std::string &klass, const val3_t &w)
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
bool delete_label_nolock(const std::string &label)
std::vector< T > v(size)
std::vector< std::pair< std::string, val3_t > > feature_val3_t
friend std::ostream & operator<<(std::ostream &os, const local_storage &ls)
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
bool delete_label(const std::string &label)
jubatus::util::data::unordered_map< uint64_t, val3_t > id_feature_val3_t
void set_nolock(const std::string &feature, const std::string &klass, const val1_t &w)
std::vector< std::string > get_labels() const
std::vector< std::pair< std::string, val2_t > > feature_val2_t
void get3_nolock(const std::string &feature, feature_val3_t &ret) const
util::concurrent::mutex & get_lock() const
void get2(const std::string &feature, feature_val2_t &ret) const