-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoperations.cpp
More file actions
160 lines (139 loc) · 5.61 KB
/
operations.cpp
File metadata and controls
160 lines (139 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "operations.h"
int Upsert(couchbase::collection& col, const std::string& doc_id, const std::string& value, bool file_flag){
tao::json::value v;
if(file_flag){
v = tao::json::from_file(value);
}
else{
v = tao::json::from_string(value);
}
auto [up_error, up_res] = col.upsert(doc_id, v).get();
if(up_error.ec()){
std::cout << "Problem Upserting doc_id:\t" << doc_id << std::endl;
return 0;
}
else return 1;
}
int Insert(couchbase::collection& col, const std::string& doc_id, const std::string& value, bool file_flag){
tao::json::value v;
if(file_flag){
v = tao::json::from_file(value);
}
else{
v = tao::json::from_string(value);
}
auto [in_error, in_res] = col.insert(doc_id, v).get();
if(in_error.ec()){
std::cout << "Problem Insert doc_id:\t" << doc_id << std::endl;
return 0;
}
else return 1;
}
tao::json::value Read(couchbase::collection& col, const std::string& doc_id){
auto [ex_err, ex_res] = col.exists(doc_id).get();
if(ex_err.ec()){
std::cout << "Problem Checking existence\t" << ex_err.message() << std::endl;
return tao::json::value({});
}
auto [get_err, get_res] = col.get(doc_id).get();
if(get_err.ec()){
std::cout << "Problem Reading doc_id:\t" << doc_id << "\t" << get_err.ec() << std::endl;
return tao::json::value({});
}
auto doc = get_res.content_as<tao::json::value>();
return doc;
}
int Delete(couchbase::collection& col, const std::string& doc_id){
auto [delete_err, delete_res] = col.remove(doc_id).get();
if(delete_err.ec()){
std::cout << "Problem Deleting doc_id:\t" << doc_id << std::endl;
return 0;
}
return 1;
}
std::vector<std::string> Query(couchbase::scope& scope){
std::string query{ R"(
SELECT META(h).id, h AS doc,
AVG(r.ratings.Overall) AS avg_rating
FROM hotel h
UNNEST h.reviews r
WHERE h.country IN $1 AND h.description LIKE "%cheap%"
GROUP BY META(h).id, h
ORDER BY avg_rating DESC
LIMIT 5;
)" };
auto [q_err, q_res] = scope.query(query, couchbase::query_options{}.positional_parameters(std::vector<std::string>{"United States", "United Kingdom"})).get();
if(q_err.ec()){
std::cout << "Error executing query: " << q_err.message() << std::endl;
return std::vector<std::string>();
}
std::vector<std::string> res;
auto v = q_res.rows_as<couchbase::codec::tao_json_serializer>();
for(auto x:v){
res.push_back(x["id"].as<std::string>() + " " + x["doc"]["country"].as<std::string>() + "\n" + std::to_string(x["avg_rating"].as<double>()) + " " + x["doc"]["title"].as<std::string>());
}
return res;
}
bool searchIndexExists(couchbase::scope_search_index_manager& sc_manager, const std::string& index_name){
auto [all_err, all_ind] = sc_manager.get_all_indexes().get();
for(auto x:all_ind){
if(x.name == index_name) return true;
}
return false;
}
std::string CreateSearchIndex(couchbase::scope& scope, const std::string& index_file){
auto scope_index_manager = scope.search_indexes();
tao::json::value v = tao::json::from_file(index_file);
if(searchIndexExists(scope_index_manager, v["name"].get_string())){
std::cout << "Index with same name already exists." << std::endl;
return v["name"].get_string();
}
// Need to build up members manually
couchbase::management::search::index i;
i.name = v["name"].get_string();
i.params_json = tao::json::to_string(v["params"]);
i.plan_params_json = tao::json::to_string(v["planParams"]);
i.source_name = v["sourceName"].get_string();
i.source_type = v["sourceType"].get_string();
auto err = scope_index_manager.upsert_index(i).get();
if(err.ec()){
std::cout << "Error Upserting Index\t" << err.message() << std::endl;
return "";
}
return i.name;
}
std::vector<std::string> SearchByName(couchbase::scope& scope, const std::string& index, const std::string& name, const std::string& field, int limit){
couchbase::search_request searchQ(couchbase::match_query(name).field(field));
couchbase::search_options opts = couchbase::search_options().limit(limit);
auto [s_err, s_res] = scope.search(index, searchQ, opts).get();
if(s_err.ec()){
std::cout << "Error during search\t" << s_err.message() << std::endl;
return std::vector<std::string>{};
}
std::vector<std::string> rows_res{};
// Reference is important since the copy constructor is deleted
for(auto &row:s_res.rows()){
rows_res.push_back(row.id());
}
return rows_res;
}
std::vector<std::string> Filter(couchbase::scope& scope, const std::string& index_name, int limit, int offset){
auto query = couchbase::conjunction_query{
couchbase::match_query("United States").field("country"),
couchbase::term_query("San Diego").field("city")
};
auto opts = couchbase::search_options().fields({"*"});
if(offset != -1) opts = opts.skip(offset);
if(limit != -1) opts = opts.limit(limit);
auto [err,res] = scope.search(index_name, couchbase::search_request(query), opts).get();
if(err.ec()){
std::cout << "Problem in performing search:\t" << err.message() << std::endl;
}
std::vector<std::string> rows_res{};
// Reference is important since the copy constructor is deleted
for(auto &row:res.rows()){
auto fields = row.fields_as<couchbase::codec::tao_json_serializer>();
rows_res.push_back(fields["name"].as<std::string>());
}
return rows_res;
}