jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
lru_unlearner.hpp
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 #ifndef JUBATUS_CORE_UNLEARNER_LRU_UNLEARNER_HPP_
18 #define JUBATUS_CORE_UNLEARNER_LRU_UNLEARNER_HPP_
19 
20 #include <stdint.h>
21 #include <list>
22 #include <string>
23 #include "jubatus/util/data/serialization.h"
24 #include "jubatus/util/data/unordered_map.h"
25 #include "jubatus/util/data/unordered_set.h"
26 #include "jubatus/util/data/optional.h"
27 #include "jubatus/util/lang/shared_ptr.h"
28 #include "unlearner_base.hpp"
29 
30 namespace jubatus {
31 namespace core {
32 namespace fv_converter {
33 class key_matcher;
34 }
35 namespace unlearner {
36 
37 // Unlearner based on Least Recently Used algorithm.
38 class lru_unlearner : public unlearner_base {
39  public:
40  struct config {
41  int32_t max_size;
42  jubatus::util::data::optional<std::string> sticky_pattern;
43 
44  template<typename Ar>
45  void serialize(Ar& ar) {
46  ar & JUBA_MEMBER(max_size) & JUBA_MEMBER(sticky_pattern);
47  }
48  };
49 
50  std::string type() const {
51  return "lru_unlearner";
52  }
53 
54  void clear() {
55  lru_.clear();
56  entry_map_.clear();
57  sticky_ids_.clear();
58  }
59 
60  explicit lru_unlearner(const config& conf);
61 
62  bool can_touch(const std::string& id);
63  bool touch(const std::string& id);
64  bool remove(const std::string& id);
65  bool exists_in_memory(const std::string& id) const;
66 
67  private:
68  typedef std::list<std::string> lru;
69  typedef jubatus::util::data::unordered_map<std::string, lru::iterator>
71 
72  void rebuild_entry_map();
73 
74  lru lru_;
76  jubatus::util::data::unordered_set<std::string> sticky_ids_;
77  size_t max_size_;
78  jubatus::util::lang::shared_ptr<jubatus::core::fv_converter::key_matcher>
80 };
81 
82 } // namespace unlearner
83 } // namespace core
84 } // namespace jubatus
85 
86 #endif // JUBATUS_CORE_UNLEARNER_LRU_UNLEARNER_HPP_
bool can_touch(const std::string &id)
jubatus::util::data::unordered_map< std::string, lru::iterator > entry_map
bool exists_in_memory(const std::string &id) const
bool touch(const std::string &id)
jubatus::util::data::optional< std::string > unlearner
jubatus::util::lang::shared_ptr< jubatus::core::fv_converter::key_matcher > sticky_matcher_
jubatus::util::data::unordered_set< std::string > sticky_ids_
jubatus::util::data::optional< std::string > sticky_pattern