jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
bit_vector_nearest_neighbor_base.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2012 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 
18 
19 #include <string>
20 #include <utility>
21 #include <vector>
22 #include "../common/type.hpp"
23 #include "bit_vector_ranking.hpp"
24 #include "jubatus/util/concurrent/rwmutex.h"
25 
26 using std::string;
27 using std::pair;
28 using std::vector;
34 
35 namespace jubatus {
36 namespace core {
37 namespace nearest_neighbor {
38 
40  uint32_t bitnum,
41  jubatus::util::lang::shared_ptr<storage::column_table> table,
42  const std::string& id)
43  : nearest_neighbor_base(table, id),
44  bitnum_(bitnum) {
45  vector<column_type> schema;
46  fill_schema(schema);
47  table->init(schema);
48 }
49 
50 bit_vector_nearest_neighbor_base::bit_vector_nearest_neighbor_base(
51  uint32_t bitnum,
52  jubatus::util::lang::shared_ptr<column_table> table,
53  vector<column_type>& schema,
54  const std::string& id)
55  : nearest_neighbor_base(table, id),
56  bitnum_(bitnum) {
57  fill_schema(schema);
58 }
59 
60 void bit_vector_nearest_neighbor_base::set_row(
61  const string& id,
62  const common::sfv_t& sfv) {
63  // TODO(beam2d): support nested algorithm, e.g. when used by lof and then
64  // we cannot suppose that the first column is assigned
65  // to bit_vector_nearest_neighbor_base.
66  get_table()->add(id, owner(my_id_), hash(sfv));
67 }
68 
69 void bit_vector_nearest_neighbor_base::neighbor_row(
70  const common::sfv_t& query,
71  vector<pair<string, float> >& ids,
72  uint64_t ret_num) const {
73  util::concurrent::scoped_rlock lk(get_const_table()->get_mutex());
74  neighbor_row_from_hash(hash(query), ids, ret_num);
75 }
76 
77 void bit_vector_nearest_neighbor_base::neighbor_row(
78  const string& query_id,
79  vector<pair<string, float> >& ids,
80  uint64_t ret_num) const {
81  const storage::column_table& table = *get_const_table();
82  util::concurrent::scoped_rlock lk(table.get_mutex());
83  const pair<bool, uint64_t> maybe_index = table.exact_match(query_id);
84  if (!maybe_index.first) {
85  ids.clear();
86  return;
87  }
88 
90  neighbor_row_from_hash(col[maybe_index.second], ids, ret_num);
91 }
92 
93 void bit_vector_nearest_neighbor_base::fill_schema(
94  vector<column_type>& schema) {
95  bit_vector_column_id_ = schema.size();
96  schema.push_back(column_type(column_type::bit_vector_type, bitnum_));
97 }
98 
100  const {
101  return get_const_table()->get_bit_vector_column(bit_vector_column_id_);
102 }
103 
104 void bit_vector_nearest_neighbor_base::neighbor_row_from_hash(
105  const bit_vector& query,
106  vector<pair<string, float> >& ids,
107  uint64_t ret_num) const {
108  vector<pair<uint64_t, float> > scores;
109  ranking_hamming_bit_vectors(query, bit_vector_column(), scores, ret_num);
110 
111  jubatus::util::lang::shared_ptr<const column_table> table = get_const_table();
112  ids.clear();
113  for (size_t i = 0; i < scores.size(); ++i) {
114  ids.push_back(make_pair(table->get_key_nolock(scores[i].first),
115  scores[i].second));
116  }
117 }
118 
119 } // namespace nearest_neighbor
120 } // namespace core
121 } // namespace jubatus
void ranking_hamming_bit_vectors(const bit_vector &query, const const_bit_vector_column &bvs, vector< pair< uint64_t, float > > &ret, uint64_t ret_num)
std::pair< bool, uint64_t > exact_match(const std::string &prefix) const
const bit_vector_column const_bit_vector_column
bit_vector_base< uint64_t > bit_vector
util::concurrent::rw_mutex & get_mutex() const
typed_column< bit_vector > bit_vector_column
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
bit_vector_nearest_neighbor_base(uint32_t bitnum, jubatus::util::lang::shared_ptr< storage::column_table > table, const std::string &id)