jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
jubatus::core::unlearner::lru_unlearner Class Reference

#include <lru_unlearner.hpp>

Inheritance diagram for jubatus::core::unlearner::lru_unlearner:
Inheritance graph
Collaboration diagram for jubatus::core::unlearner::lru_unlearner:
Collaboration graph

Classes

struct  config
 

Public Member Functions

bool can_touch (const std::string &id)
 
void clear ()
 
bool exists_in_memory (const std::string &id) const
 
 lru_unlearner (const config &conf)
 
bool remove (const std::string &id)
 
bool touch (const std::string &id)
 
std::string type () const
 
- Public Member Functions inherited from jubatus::core::unlearner::unlearner_base
void set_callback (const unlearning_callback &callback)
 
virtual ~unlearner_base ()
 

Private Types

typedef jubatus::util::data::unordered_map< std::string, lru::iterator > entry_map
 
typedef std::list< std::string > lru
 

Private Member Functions

void rebuild_entry_map ()
 

Private Attributes

entry_map entry_map_
 
lru lru_
 
size_t max_size_
 
jubatus::util::data::unordered_set< std::string > sticky_ids_
 
jubatus::util::lang::shared_ptr< jubatus::core::fv_converter::key_matchersticky_matcher_
 

Additional Inherited Members

- Protected Member Functions inherited from jubatus::core::unlearner::unlearner_base
void unlearn (const std::string &id) const
 

Detailed Description

Definition at line 38 of file lru_unlearner.hpp.

Member Typedef Documentation

typedef jubatus::util::data::unordered_map<std::string, lru::iterator> jubatus::core::unlearner::lru_unlearner::entry_map
private

Definition at line 70 of file lru_unlearner.hpp.

typedef std::list<std::string> jubatus::core::unlearner::lru_unlearner::lru
private

Definition at line 68 of file lru_unlearner.hpp.

Constructor & Destructor Documentation

jubatus::core::unlearner::lru_unlearner::lru_unlearner ( const config conf)
explicit

Definition at line 34 of file lru_unlearner.cpp.

References jubatus::core::fv_converter::key_matcher_factory::create_matcher(), entry_map_, JUBATUS_EXCEPTION, jubatus::core::unlearner::lru_unlearner::config::max_size, max_size_, sticky_matcher_, and jubatus::core::unlearner::lru_unlearner::config::sticky_pattern.

35  : max_size_(conf.max_size) {
36  if (conf.max_size <= 0) {
37  throw JUBATUS_EXCEPTION(
38  common::config_exception() << common::exception::error_message(
39  "max_size must be a positive integer"));
40  }
41  entry_map_.reserve(max_size_);
42 
43  if (conf.sticky_pattern) {
45  sticky_matcher_ = f.create_matcher(*conf.sticky_pattern);
46  }
47 }
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
error_info< struct error_message_, std::string > error_message
Definition: exception.hpp:59
jubatus::util::lang::shared_ptr< jubatus::core::fv_converter::key_matcher > sticky_matcher_
jubatus::util::lang::shared_ptr< key_matcher > create_matcher(const std::string &matcher)

Here is the call graph for this function:

Member Function Documentation

bool jubatus::core::unlearner::lru_unlearner::can_touch ( const std::string &  id)
virtual

Implements jubatus::core::unlearner::unlearner_base.

Definition at line 49 of file lru_unlearner.cpp.

References exists_in_memory(), max_size_, and sticky_ids_.

49  {
50  return (exists_in_memory(id) || sticky_ids_.size() < max_size_);
51 }
bool exists_in_memory(const std::string &id) const
jubatus::util::data::unordered_set< std::string > sticky_ids_

Here is the call graph for this function:

void jubatus::core::unlearner::lru_unlearner::clear ( )
inlinevirtual

Implements jubatus::core::unlearner::unlearner_base.

Definition at line 54 of file lru_unlearner.hpp.

References entry_map_, lru_, and sticky_ids_.

54  {
55  lru_.clear();
56  entry_map_.clear();
57  sticky_ids_.clear();
58  }
jubatus::util::data::unordered_set< std::string > sticky_ids_
bool jubatus::core::unlearner::lru_unlearner::exists_in_memory ( const std::string &  id) const
virtual

Implements jubatus::core::unlearner::unlearner_base.

Definition at line 125 of file lru_unlearner.cpp.

References entry_map_, and sticky_ids_.

Referenced by can_touch().

125  {
126  return entry_map_.count(id) > 0 || sticky_ids_.count(id) > 0;
127 }
jubatus::util::data::unordered_set< std::string > sticky_ids_

Here is the caller graph for this function:

void jubatus::core::unlearner::lru_unlearner::rebuild_entry_map ( )
private

Definition at line 131 of file lru_unlearner.cpp.

References entry_map_, and lru_.

131  {
132  entry_map_.clear();
133  for (lru::iterator it = lru_.begin(); it != lru_.end(); ++it) {
134  entry_map_[*it] = it;
135  }
136 }
bool jubatus::core::unlearner::lru_unlearner::remove ( const std::string &  id)
virtual

Implements jubatus::core::unlearner::unlearner_base.

Definition at line 102 of file lru_unlearner.cpp.

References entry_map_, lru_, and sticky_ids_.

102  {
103  // Try to erase from non-sticky ID.
104  {
105  entry_map::iterator it = entry_map_.find(id);
106  if (it != entry_map_.end()) {
107  lru_.erase(it->second);
108  entry_map_.erase(it);
109  return true;
110  }
111  }
112 
113  // Try to erase from sticky ID.
114  {
115  unordered_set<std::string>::iterator it = sticky_ids_.find(id);
116  if (it != sticky_ids_.end()) {
117  sticky_ids_.erase(it);
118  return true;
119  }
120  }
121 
122  return false;
123 }
jubatus::util::data::unordered_set< std::string > sticky_ids_
bool jubatus::core::unlearner::lru_unlearner::touch ( const std::string &  id)
virtual

Implements jubatus::core::unlearner::unlearner_base.

Definition at line 53 of file lru_unlearner.cpp.

References entry_map_, lru_, max_size_, sticky_ids_, sticky_matcher_, and jubatus::core::unlearner::unlearner_base::unlearn().

53  {
54  // When sticky pattern is specified, unlearner excludes IDs matching
55  // the pattern from unlearning.
56  bool is_sticky = sticky_matcher_ && sticky_matcher_->match(id);
57 
58  if (is_sticky) {
59  unordered_set<std::string>::const_iterator it = sticky_ids_.find(id);
60  if (it != sticky_ids_.end()) {
61  // Sticky ID that is already on memory; nothing to do.
62  return true;
63  }
64  } else {
65  entry_map::iterator it = entry_map_.find(id);
66  if (it != entry_map_.end()) {
67  // Non-sticky ID that is already on memory; mark the ID
68  // as most recently used.
69  lru_.push_front(id);
70  lru_.erase(it->second);
71  it->second = lru_.begin();
72  return true;
73  }
74  }
75 
76  // Touched ID is not on memory; need to secure a space for it.
77  if ((entry_map_.size() + sticky_ids_.size()) >= max_size_) {
78  // No more space; sticky_ids_ is the list of IDs that cannot be
79  // unlearned, so try to unlearn from entry_map_.
80  if (entry_map_.size() == 0) {
81  // entry_map_ is empty; nothing can be unlearned.
82  return false;
83  }
84 
85  // Unlearn the least recently used entry.
86  unlearn(lru_.back());
87  entry_map_.erase(lru_.back());
88  lru_.pop_back();
89  }
90 
91  // Register the new ID.
92  if (is_sticky) {
93  sticky_ids_.insert(id);
94  } else {
95  lru_.push_front(id);
96  entry_map_[id] = lru_.begin();
97  }
98 
99  return true;
100 }
void unlearn(const std::string &id) const
jubatus::util::lang::shared_ptr< jubatus::core::fv_converter::key_matcher > sticky_matcher_
jubatus::util::data::unordered_set< std::string > sticky_ids_

Here is the call graph for this function:

std::string jubatus::core::unlearner::lru_unlearner::type ( ) const
inlinevirtual

Implements jubatus::core::unlearner::unlearner_base.

Definition at line 50 of file lru_unlearner.hpp.

50  {
51  return "lru_unlearner";
52  }

Member Data Documentation

entry_map jubatus::core::unlearner::lru_unlearner::entry_map_
private
lru jubatus::core::unlearner::lru_unlearner::lru_
private

Definition at line 74 of file lru_unlearner.hpp.

Referenced by clear(), rebuild_entry_map(), remove(), and touch().

size_t jubatus::core::unlearner::lru_unlearner::max_size_
private

Definition at line 77 of file lru_unlearner.hpp.

Referenced by can_touch(), lru_unlearner(), and touch().

jubatus::util::data::unordered_set<std::string> jubatus::core::unlearner::lru_unlearner::sticky_ids_
private

Definition at line 76 of file lru_unlearner.hpp.

Referenced by can_touch(), clear(), exists_in_memory(), remove(), and touch().

jubatus::util::lang::shared_ptr<jubatus::core::fv_converter::key_matcher> jubatus::core::unlearner::lru_unlearner::sticky_matcher_
private

Definition at line 79 of file lru_unlearner.hpp.

Referenced by lru_unlearner(), and touch().


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