jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
key_manager.cpp
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 #include "key_manager.hpp"
18 
19 #include <algorithm>
20 #include <string>
21 #include <vector>
22 
23 #include "assert.hpp"
24 
25 using std::string;
26 using std::vector;
27 using jubatus::util::data::unordered_map;
28 
29 namespace jubatus {
30 namespace core {
31 namespace common {
32 
33 typedef unordered_map<string, uint64_t>::const_iterator cit;
34 
36  : next_id_(0llu) {
37 }
38 
39 uint64_t key_manager::append_key(const string& key) {
40  JUBATUS_ASSERT_EQ(true, // must not exist
41  key2id_.end() == key2id_.find(key),
42  "existing key appended");
43  key2id_[key] = next_id_;
44  id2key_[next_id_] = key;
45  return next_id_++;
46 }
47 
48 uint64_t key_manager::get_id(const string& key) {
49  cit it = key2id_.find(key);
50  if (it != key2id_.end()) {
51  return it->second;
52  }
53  // TODO(beam2d): Make it exception safe.
54  return append_key(key);
55 }
56 
57 bool key_manager::set_key(const string& key) {
58  cit it = key2id_.find(key);
59  if (it != key2id_.end()) {
60  return false;
61  }
62  // TODO(kumagi): Make it exception safe.
63  append_key(key);
64  return true;
65 }
66 
67 uint64_t key_manager::get_id_const(const string& key) const {
68  cit it = key2id_.find(key);
69  if (it != key2id_.end()) {
70  return it->second;
71  } else {
72  return NOTFOUND;
73  }
74 }
75 
76 const std::string key_not_found = ""; // const object has internal linkage
77 
78 const string& key_manager::get_key(const uint64_t id) const {
79  unordered_map<uint64_t, string>::const_iterator it = id2key_.find(id);
80  if (it != id2key_.end()) {
81  return it->second;
82  } else {
83  return key_not_found;
84  }
85 }
86 
87 std::vector<std::string> key_manager::get_all_id2key() const {
88  std::vector<std::string> ret;
89  ret.reserve(id2key_.size());
90  for (unordered_map<uint64_t, string>::const_iterator it = id2key_.begin();
91  it != id2key_.end();
92  ++it) {
93  ret.push_back(it->second);
94  }
95  return ret;
96 }
97 
99  jubatus::util::data::unordered_map<std::string, uint64_t>().swap(key2id_);
100  jubatus::util::data::unordered_map<uint64_t, std::string>().swap(id2key_);
101  next_id_ = 0u;
102 }
103 
104 void key_manager::init_by_id2key(const std::vector<std::string>& id2key) {
105  key2id_.clear();
106  id2key_.clear();
107 
108  for (size_t i = 0; i < id2key.size(); ++i) {
109  key2id_[id2key[i]] = i;
110  id2key_[i] = id2key[i];
111  }
112  next_id_ = id2key.size();
113 }
114 
115 void key_manager::delete_key(const std::string& name) {
116  const uint64_t id = get_id_const(name);
117  if (id != NOTFOUND) {
118  key2id_.erase(name);
119  id2key_.erase(id);
120  }
121 }
122 
123 uint64_t key_manager::get_max_id() const {
124  return next_id_ - 1;
125 }
126 
127 } // namespace common
128 } // namespace core
129 } // namespace jubatus
uint64_t get_id_const(const std::string &key) const
Definition: key_manager.cpp:67
#define JUBATUS_ASSERT_EQ(a, b, messages)
Definition: assert.hpp:63
void init_by_id2key(const std::vector< std::string > &id2key)
const std::string key_not_found
Definition: key_manager.cpp:76
unordered_map< string, uint64_t >::const_iterator cit
Definition: key_manager.cpp:33
bool set_key(const std::string &key)
Definition: key_manager.cpp:57
util::data::unordered_map< uint64_t, std::string > id2key_
Definition: key_manager.hpp:84
const std::string & get_key(const uint64_t id) const
Definition: key_manager.cpp:78
util::data::unordered_map< std::string, uint64_t > key2id_
Definition: key_manager.hpp:83
uint64_t get_id(const std::string &key)
Definition: key_manager.cpp:48
uint64_t append_key(const std::string &key)
Definition: key_manager.cpp:39
void delete_key(const std::string &name)
std::vector< std::string > get_all_id2key() const
Definition: key_manager.cpp:87