jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
unlearner_base.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_UNLEARNER_BASE_HPP_
18 #define JUBATUS_CORE_UNLEARNER_UNLEARNER_BASE_HPP_
19 
20 #include <string>
21 #include "jubatus/util/lang/function.h"
22 
23 namespace jubatus {
24 namespace core {
25 namespace unlearner {
26 
27 typedef jubatus::util::lang::function<void (std::string)> unlearning_callback;
28 
29 // Base class of unlearners.
30 //
31 // Unlearning is a functionality to keep the model size up to constant.
32 // Unlearner is a strategy of unlearning that decides which part of the model to
33 // be removed. User should register a callback that actually removes something
34 // on unlearning.
36  public:
37  virtual ~unlearner_base() {}
38 
39  void set_callback(const unlearning_callback& callback) {
40  callback_ = callback;
41  }
42 
43  virtual std::string type() const = 0;
44  virtual void clear() = 0;
45 
46  // Tests if the given id can be touched.
47  //
48  // This function returns true if the given |id| can be touched (i.e., the
49  // number of registered unique ids is less than the limit, or the number of
50  // registered unique ids reached the limit but there are at least one ID that
51  // is available for unlearning), and false otherwise.
52  virtual bool can_touch(const std::string& id) = 0;
53 
54  // Informs that the item of given id is updated in the model.
55  //
56  // If the number of unique ids reaches to the predefined upper bound, one or
57  // more ids are unlearned. In that case, when there are no IDs available for
58  // unlearning, this function returns false. On unlearning an id X, a callback
59  // given by |set_callback| is called by passing X as an argument.
60  virtual bool touch(const std::string& id) = 0;
61 
62  // Informs that the item of given id is removed from the model.
63  //
64  // This function returns true when succeed to remove |id|. If |id| does not
65  // exist in memory, it returns false.
66  virtual bool remove(const std::string& id) = 0;
67 
68  // Checks that a given id is still begin in memory.
69  //
70  // If |id| has not been touched, or touched but unlearned at some point and
71  // not been touched since then, this function returns false. If |id| has been
72  // touched and not unlearned since then, it returns true.
73  virtual bool exists_in_memory(const std::string& id) const = 0;
74 
75  protected:
76  void unlearn(const std::string& id) const {
77  callback_(id);
78  }
79 
80  private:
81  unlearning_callback callback_;
82 };
83 
84 } // namespace unlearner
85 } // namespace core
86 } // namespace jubatus
87 
88 #endif // JUBATUS_CORE_UNLEARNER_UNLEARNER_BASE_HPP_
virtual bool exists_in_memory(const std::string &id) const =0
void set_callback(const unlearning_callback &callback)
virtual std::string type() const =0
void unlearn(const std::string &id) const
jubatus::util::data::optional< std::string > unlearner
virtual bool touch(const std::string &id)=0
virtual bool can_touch(const std::string &id)=0
jubatus::util::lang::function< void(std::string)> unlearning_callback