jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
Public Member Functions | Private Attributes | Friends | List of all members
jubatus::core::storage::local_storage Class Reference

#include <local_storage.hpp>

Inheritance diagram for jubatus::core::storage::local_storage:
Inheritance graph
Collaboration diagram for jubatus::core::storage::local_storage:
Collaboration graph

Public Member Functions

void bulk_update (const common::sfv_t &sfv, float step_width, const std::string &inc_class, const std::string &dec_class)
 
void clear ()
 
bool delete_label (const std::string &label)
 
bool delete_label_nolock (const std::string &label)
 
void get (const std::string &feature, feature_val1_t &ret) const
 
void get2 (const std::string &feature, feature_val2_t &ret) const
 
void get2_nolock (const std::string &feature, feature_val2_t &ret) const
 
void get3 (const std::string &feature, feature_val3_t &ret) const
 
void get3_nolock (const std::string &feature, feature_val3_t &ret) const
 
std::vector< std::string > get_labels () const
 
util::concurrent::mutex & get_lock () const
 
void get_nolock (const std::string &feature, feature_val1_t &ret) const
 
void get_status (std::map< std::string, std::string > &status) const
 
storage::version get_version () const
 
void inp (const common::sfv_t &sfv, map_feature_val1_t &ret) const
 
 local_storage ()
 
 MSGPACK_DEFINE (tbl_, class2id_)
 
void pack (framework::packer &packer) const
 
void register_label (const std::string &label)
 
void set (const std::string &feature, const std::string &klass, const val1_t &w)
 
void set2 (const std::string &feature, const std::string &klass, const val2_t &w)
 
void set2_nolock (const std::string &feature, const std::string &klass, const val2_t &w)
 
void set3 (const std::string &feature, const std::string &klass, const val3_t &w)
 
void set3_nolock (const std::string &feature, const std::string &klass, const val3_t &w)
 
bool set_label (const std::string &label)
 
void set_nolock (const std::string &feature, const std::string &klass, const val1_t &w)
 
std::string type () const
 
void unpack (msgpack::object o)
 
void update (const std::string &feature, const std::string &inc_class, const std::string &dec_class, const val1_t &v)
 
 ~local_storage ()
 
- Public Member Functions inherited from jubatus::core::storage::storage_base
virtual void get_diff (diff_t &) const
 
virtual bool set_average_and_clear_diff (const diff_t &)
 
virtual ~storage_base ()
 
- Public Member Functions inherited from jubatus::core::framework::model
virtual ~model ()
 

Private Attributes

common::key_manager class2id_
 
util::concurrent::mutex mutex_
 
id_features3_t tbl_
 

Friends

std::ostream & operator<< (std::ostream &os, const local_storage &ls)
 

Detailed Description

Definition at line 38 of file local_storage.hpp.

Constructor & Destructor Documentation

jubatus::core::storage::local_storage::local_storage ( )

Definition at line 34 of file local_storage.cpp.

34  {
35 }
jubatus::core::storage::local_storage::~local_storage ( )

Definition at line 37 of file local_storage.cpp.

37  {
38 }

Member Function Documentation

void jubatus::core::storage::local_storage::bulk_update ( const common::sfv_t sfv,
float  step_width,
const std::string &  inc_class,
const std::string &  dec_class 
)
virtual

Reimplemented from jubatus::core::storage::storage_base.

Definition at line 191 of file local_storage.cpp.

References class2id_, jubatus::core::common::key_manager::get_id(), mutex_, and tbl_.

195  {
196  scoped_lock lk(mutex_);
197  uint64_t inc_id = class2id_.get_id(inc_class);
198  typedef common::sfv_t::const_iterator iter_t;
199  if (dec_class != "") {
200  uint64_t dec_id = class2id_.get_id(dec_class);
201  for (iter_t it = sfv.begin(); it != sfv.end(); ++it) {
202  float val = it->second * step_width;
203  id_feature_val3_t& feature_row = tbl_[it->first];
204  feature_row[inc_id].v1 += val;
205  feature_row[dec_id].v1 -= val;
206  }
207  } else {
208  for (iter_t it = sfv.begin(); it != sfv.end(); ++it) {
209  float val = it->second * step_width;
210  id_feature_val3_t& feature_row = tbl_[it->first];
211  feature_row[inc_id].v1 += val;
212  }
213  }
214 }
uint64_t get_id(const std::string &key)
Definition: key_manager.cpp:48
jubatus::util::data::unordered_map< uint64_t, val3_t > id_feature_val3_t

Here is the call graph for this function:

void jubatus::core::storage::local_storage::clear ( )
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 271 of file local_storage.cpp.

References class2id_, mutex_, jubatus::core::common::key_manager::swap(), and tbl_.

271  {
272  scoped_lock lk(mutex_);
273  // Clear and minimize
274  id_features3_t().swap(tbl_);
275  common::key_manager().swap(class2id_);
276 }
jubatus::util::data::unordered_map< std::string, id_feature_val3_t > id_features3_t

Here is the call graph for this function:

bool jubatus::core::storage::local_storage::delete_label ( const std::string &  label)
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 247 of file local_storage.cpp.

References delete_label_nolock(), and mutex_.

247  {
248  scoped_lock lk(mutex_);
249  return delete_label_nolock(label);
250 }
bool delete_label_nolock(const std::string &label)

Here is the call graph for this function:

bool jubatus::core::storage::local_storage::delete_label_nolock ( const std::string &  label)
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 252 of file local_storage.cpp.

References class2id_, jubatus::core::common::key_manager::delete_key(), jubatus::core::common::key_manager::get_id_const(), jubatus::core::common::key_manager::NOTFOUND, and tbl_.

Referenced by delete_label().

252  {
253  uint64_t delete_id = class2id_.get_id_const(label);
254  if (delete_id == common::key_manager::NOTFOUND) {
255  return false;
256  }
257  for (id_features3_t::iterator it = tbl_.begin();
258  it != tbl_.end();
259  ) {
260  const bool deleted = it->second.erase(delete_id);
261  if (deleted && it->second.empty()) {
262  it = tbl_.erase(it);
263  } else {
264  ++it;
265  }
266  }
267  class2id_.delete_key(label);
268  return true;
269 }
uint64_t get_id_const(const std::string &key) const
Definition: key_manager.cpp:67
void delete_key(const std::string &name)

Here is the call graph for this function:

Here is the caller graph for this function:

void jubatus::core::storage::local_storage::get ( const std::string &  feature,
feature_val1_t ret 
) const
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 40 of file local_storage.cpp.

References get_nolock(), and mutex_.

40  {
41  scoped_lock lk(mutex_);
42  get_nolock(feature, ret);
43 }
void get_nolock(const std::string &feature, feature_val1_t &ret) const

Here is the call graph for this function:

void jubatus::core::storage::local_storage::get2 ( const std::string &  feature,
feature_val2_t ret 
) const
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 58 of file local_storage.cpp.

References get2_nolock(), and mutex_.

58  {
59  scoped_lock lk(mutex_);
60  get2_nolock(feature, ret);
61 }
void get2_nolock(const std::string &feature, feature_val2_t &ret) const

Here is the call graph for this function:

void jubatus::core::storage::local_storage::get2_nolock ( const std::string &  feature,
feature_val2_t ret 
) const
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 63 of file local_storage.cpp.

References class2id_, jubatus::core::common::key_manager::get_key(), and tbl_.

Referenced by get2().

64  {
65  ret.clear();
66  id_features3_t::const_iterator cit = tbl_.find(feature);
67  if (cit == tbl_.end()) {
68  return;
69  }
70  const id_feature_val3_t& m = cit->second;
71  for (id_feature_val3_t::const_iterator it = m.begin(); it != m.end(); ++it) {
72  ret.push_back(make_pair(class2id_.get_key(it->first),
73  val2_t(it->second.v1, it->second.v2)));
74  }
75 }
unordered_map< string, uint64_t >::const_iterator cit
Definition: key_manager.cpp:33
const std::string & get_key(const uint64_t id) const
Definition: key_manager.cpp:78
jubatus::util::data::unordered_map< uint64_t, val3_t > id_feature_val3_t

Here is the call graph for this function:

Here is the caller graph for this function:

void jubatus::core::storage::local_storage::get3 ( const std::string &  feature,
feature_val3_t ret 
) const
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 77 of file local_storage.cpp.

References get3_nolock(), and mutex_.

77  {
78  scoped_lock lk(mutex_);
79  get3_nolock(feature, ret);
80 }
void get3_nolock(const std::string &feature, feature_val3_t &ret) const

Here is the call graph for this function:

void jubatus::core::storage::local_storage::get3_nolock ( const std::string &  feature,
feature_val3_t ret 
) const
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 82 of file local_storage.cpp.

References class2id_, jubatus::core::common::key_manager::get_key(), and tbl_.

Referenced by get3().

83  {
84  ret.clear();
85  id_features3_t::const_iterator cit = tbl_.find(feature);
86  if (cit == tbl_.end()) {
87  return;
88  }
89  const id_feature_val3_t& m = cit->second;
90  for (id_feature_val3_t::const_iterator it = m.begin(); it != m.end(); ++it) {
91  ret.push_back(make_pair(class2id_.get_key(it->first), it->second));
92  }
93 }
unordered_map< string, uint64_t >::const_iterator cit
Definition: key_manager.cpp:33
const std::string & get_key(const uint64_t id) const
Definition: key_manager.cpp:78
jubatus::util::data::unordered_map< uint64_t, val3_t > id_feature_val3_t

Here is the call graph for this function:

Here is the caller graph for this function:

vector< string > jubatus::core::storage::local_storage::get_labels ( ) const
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 237 of file local_storage.cpp.

References class2id_, jubatus::core::common::key_manager::get_all_id2key(), and mutex_.

237  {
238  scoped_lock lk(mutex_);
239  return class2id_.get_all_id2key();
240 }
std::vector< std::string > get_all_id2key() const
Definition: key_manager.cpp:87

Here is the call graph for this function:

util::concurrent::mutex & jubatus::core::storage::local_storage::get_lock ( ) const
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 227 of file local_storage.cpp.

References mutex_.

227  {
228  return mutex_;
229 }
void jubatus::core::storage::local_storage::get_nolock ( const std::string &  feature,
feature_val1_t ret 
) const
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 45 of file local_storage.cpp.

References class2id_, jubatus::core::common::key_manager::get_key(), and tbl_.

Referenced by get().

46  {
47  ret.clear();
48  id_features3_t::const_iterator cit = tbl_.find(feature);
49  if (cit == tbl_.end()) {
50  return;
51  }
52  const id_feature_val3_t& m = cit->second;
53  for (id_feature_val3_t::const_iterator it = m.begin(); it != m.end(); ++it) {
54  ret.push_back(make_pair(class2id_.get_key(it->first), it->second.v1));
55  }
56 }
unordered_map< string, uint64_t >::const_iterator cit
Definition: key_manager.cpp:33
const std::string & get_key(const uint64_t id) const
Definition: key_manager.cpp:78
jubatus::util::data::unordered_map< uint64_t, val3_t > id_feature_val3_t

Here is the call graph for this function:

Here is the caller graph for this function:

void jubatus::core::storage::local_storage::get_status ( std::map< std::string, std::string > &  status) const
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 175 of file local_storage.cpp.

References class2id_, mutex_, jubatus::core::common::key_manager::size(), and tbl_.

175  {
176  scoped_lock lk(mutex_);
177  status["num_features"] =
178  jubatus::util::lang::lexical_cast<std::string>(tbl_.size());
179  status["num_classes"] =
180  jubatus::util::lang::lexical_cast<std::string>(class2id_.size());
181 }

Here is the call graph for this function:

storage::version jubatus::core::storage::local_storage::get_version ( ) const
inlinevirtual

Implements jubatus::core::storage::storage_base.

Definition at line 103 of file local_storage.hpp.

103  {
104  return storage::version();
105  }
void jubatus::core::storage::local_storage::inp ( const common::sfv_t sfv,
map_feature_val1_t ret 
) const
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 95 of file local_storage.cpp.

References class2id_, jubatus::core::common::key_manager::get_all_id2key(), jubatus::core::common::key_manager::get_id_const(), mutex_, jubatus::core::common::key_manager::NOTFOUND, and tbl_.

96  {
97  ret.clear();
98 
99  scoped_lock lk(mutex_);
100  // Use uin64_t map instead of string map as hash function for string is slow
101  jubatus::util::data::unordered_map<uint64_t, float> ret_id;
102  for (common::sfv_t::const_iterator it = sfv.begin(); it != sfv.end(); ++it) {
103  const string& feature = it->first;
104  const float val = it->second;
105  id_features3_t::const_iterator it2 = tbl_.find(feature);
106  if (it2 == tbl_.end()) {
107  continue;
108  }
109  const id_feature_val3_t& m = it2->second;
110  for (id_feature_val3_t::const_iterator it3 = m.begin(); it3 != m.end();
111  ++it3) {
112  ret_id[it3->first] += it3->second.v1 * val;
113  }
114  }
115 
116  std::vector<std::string> labels = class2id_.get_all_id2key();
117  for (size_t i = 0; i < labels.size(); ++i) {
118  const std::string& label = labels[i];
119  uint64_t id = class2id_.get_id_const(label);
120  if (id == common::key_manager::NOTFOUND || ret_id.count(id) == 0) {
121  ret[label] = 0.0;
122  } else {
123  ret[label] = ret_id[id];
124  }
125  }
126 }
uint64_t get_id_const(const std::string &key) const
Definition: key_manager.cpp:67
jubatus::util::data::unordered_map< uint64_t, val3_t > id_feature_val3_t
std::vector< std::string > get_all_id2key() const
Definition: key_manager.cpp:87

Here is the call graph for this function:

jubatus::core::storage::local_storage::MSGPACK_DEFINE ( tbl_  ,
class2id_   
)
void jubatus::core::storage::local_storage::pack ( framework::packer packer) const
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 278 of file local_storage.cpp.

References mutex_.

278  {
279  scoped_lock lk(mutex_);
280  packer.pack(*this);
281 }
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
void jubatus::core::storage::local_storage::register_label ( const std::string &  label)
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 231 of file local_storage.cpp.

References class2id_, jubatus::core::common::key_manager::get_id(), and mutex_.

231  {
232  scoped_lock lk(mutex_);
233  // get_id method creates an entry when the label doesn't exist
234  class2id_.get_id(label);
235 }
uint64_t get_id(const std::string &key)
Definition: key_manager.cpp:48

Here is the call graph for this function:

void jubatus::core::storage::local_storage::set ( const std::string &  feature,
const std::string &  klass,
const val1_t w 
)
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 128 of file local_storage.cpp.

References class2id_, jubatus::core::common::key_manager::get_id(), mutex_, set_nolock(), and tbl_.

131  {
132  scoped_lock lk(mutex_);
133  set_nolock(feature, klass, w);
134  tbl_[feature][class2id_.get_id(klass)].v1 = w;
135 }
uint64_t get_id(const std::string &key)
Definition: key_manager.cpp:48
void set_nolock(const std::string &feature, const std::string &klass, const val1_t &w)

Here is the call graph for this function:

void jubatus::core::storage::local_storage::set2 ( const std::string &  feature,
const std::string &  klass,
const val2_t w 
)
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 143 of file local_storage.cpp.

References mutex_, and set2_nolock().

146  {
147  scoped_lock lk(mutex_);
148  set2_nolock(feature, klass, w);
149 }
void set2_nolock(const std::string &feature, const std::string &klass, const val2_t &w)

Here is the call graph for this function:

void jubatus::core::storage::local_storage::set2_nolock ( const std::string &  feature,
const std::string &  klass,
const val2_t w 
)
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 151 of file local_storage.cpp.

References class2id_, jubatus::core::common::key_manager::get_id(), tbl_, jubatus::core::storage::val2_t::v1, jubatus::core::storage::val3_t::v1, jubatus::core::storage::val2_t::v2, and jubatus::core::storage::val3_t::v2.

Referenced by set2().

154  {
155  val3_t& val3 = tbl_[feature][class2id_.get_id(klass)];
156  val3.v1 = w.v1;
157  val3.v2 = w.v2;
158 }
uint64_t get_id(const std::string &key)
Definition: key_manager.cpp:48

Here is the call graph for this function:

Here is the caller graph for this function:

void jubatus::core::storage::local_storage::set3 ( const std::string &  feature,
const std::string &  klass,
const val3_t w 
)
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 160 of file local_storage.cpp.

References mutex_, and set3_nolock().

163  {
164  scoped_lock lk(mutex_);
165  set3_nolock(feature, klass, w);
166 }
void set3_nolock(const std::string &feature, const std::string &klass, const val3_t &w)

Here is the call graph for this function:

void jubatus::core::storage::local_storage::set3_nolock ( const std::string &  feature,
const std::string &  klass,
const val3_t w 
)
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 168 of file local_storage.cpp.

References class2id_, jubatus::core::common::key_manager::get_id(), and tbl_.

Referenced by set3().

171  {
172  tbl_[feature][class2id_.get_id(klass)] = w;
173 }
uint64_t get_id(const std::string &key)
Definition: key_manager.cpp:48

Here is the call graph for this function:

Here is the caller graph for this function:

bool jubatus::core::storage::local_storage::set_label ( const std::string &  label)
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 242 of file local_storage.cpp.

References class2id_, mutex_, and jubatus::core::common::key_manager::set_key().

242  {
243  scoped_lock lk(mutex_);
244  return class2id_.set_key(label);
245 }
bool set_key(const std::string &key)
Definition: key_manager.cpp:57

Here is the call graph for this function:

void jubatus::core::storage::local_storage::set_nolock ( const std::string &  feature,
const std::string &  klass,
const val1_t w 
)
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 136 of file local_storage.cpp.

References class2id_, jubatus::core::common::key_manager::get_id(), and tbl_.

Referenced by set().

139  {
140  tbl_[feature][class2id_.get_id(klass)].v1 = w;
141 }
uint64_t get_id(const std::string &key)
Definition: key_manager.cpp:48

Here is the call graph for this function:

Here is the caller graph for this function:

std::string jubatus::core::storage::local_storage::type ( ) const
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 288 of file local_storage.cpp.

288  {
289  return "local_storage";
290 }
void jubatus::core::storage::local_storage::unpack ( msgpack::object  o)
virtual

Implements jubatus::core::storage::storage_base.

Definition at line 283 of file local_storage.cpp.

References mutex_.

283  {
284  scoped_lock lk(mutex_);
285  o.convert(this);
286 }
void jubatus::core::storage::local_storage::update ( const std::string &  feature,
const std::string &  inc_class,
const std::string &  dec_class,
const val1_t v 
)
virtual

Reimplemented from jubatus::core::storage::storage_base.

Definition at line 216 of file local_storage.cpp.

References class2id_, jubatus::core::common::key_manager::get_id(), mutex_, tbl_, and jubatus::core::common::jsonconfig::v().

220  {
221  scoped_lock lk(mutex_);
222  id_feature_val3_t& feature_row = tbl_[feature];
223  feature_row[class2id_.get_id(inc_class)].v1 += v;
224  feature_row[class2id_.get_id(dec_class)].v1 -= v;
225 }
std::vector< T > v(size)
uint64_t get_id(const std::string &key)
Definition: key_manager.cpp:48
jubatus::util::data::unordered_map< uint64_t, val3_t > id_feature_val3_t

Here is the call graph for this function:

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const local_storage ls 
)
friend

Definition at line 117 of file local_storage.hpp.

117  {
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  }
jubatus::util::data::unordered_map< uint64_t, val3_t > id_feature_val3_t

Member Data Documentation

common::key_manager jubatus::core::storage::local_storage::class2id_
private
util::concurrent::mutex jubatus::core::storage::local_storage::mutex_
mutableprivate
id_features3_t jubatus::core::storage::local_storage::tbl_
private

The documentation for this class was generated from the following files: