3D Repo Bouncer  1.4
repo_bson_builder.h
1 /*
2 * Copyright(C) 2015 3D Repo Ltd
3 *
4 * This program is free software : you can redistribute it and / or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or(at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 *but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program.If not, see <http://www.gnu.org/licenses/>.
16 */
17 
24 #pragma once
25 
26 #if defined(_WIN32) || defined(_WIN64)
27 #include <WinSock2.h>
28 #include <Windows.h>
29 
30 #define strcasecmp _stricmp
31 #endif
32 
33 #include <string>
34 #include <mongo/bson/bson.h>
35 #include "../repo_node_utils.h"
36 #include "repo_bson.h"
37 
38 namespace repo {
39  namespace core {
40  namespace model {
41  class RepoBSONBuilder : public mongo::BSONObjBuilder
42  {
43  public:
45  ~RepoBSONBuilder();
46 
53  template <class T>
55  const std::string &label,
56  const std::vector<T> &vec)
57  {
58  RepoBSONBuilder array;
59  for (unsigned int i = 0; i < vec.size(); ++i)
60  array.append(std::to_string(i), vec[i]);
61  mongo::BSONObjBuilder::appendArray(label, array.obj());
62  }
63 
64  void appendArray(
65  const std::string &label,
66  const RepoBSON &bson)
67  {
68  mongo::BSONObjBuilder::appendArray(label, bson);
69  }
70 
71  template<class T>
72  void append(
73  const std::string &label,
74  const T &item)
75  {
76  mongo::BSONObjBuilder::append(label, item);
77  }
78 
86  void appendArrayPair(
87  const std::string &label,
88  const std::list<std::pair<std::string, std::string> > &list,
89  const std::string &fstLabel,
90  const std::string &sndLabel
91  );
92 
104  template <class T>
106  const std::string &label,
107  const T *data,
108  const uint32_t &byteCount)
109  {
110  if (data && 0 < byteCount)
111  {
112  // Store data as a binary blob
113  try{
114  appendBinData(
115  label, byteCount, mongo::BinDataGeneral,
116  (void *)data);
117  }
118  catch (std::exception &e)
119  {
120  repoError << "Failed: " << e.what();
121  exit(-1);
122  }
123  }
124  else
125  {
126  repoWarning << "Trying to append a binary of size 0 into a bson. Skipping..";
127  }
128  }
129 
130  void appendTimeStamp(std::string label){
131  appendTime(label, time(NULL) * 1000);
132  }
133 
134  void appendTime(std::string label, const int64_t &ts){
135  mongo::Date_t date = mongo::Date_t(ts);
136  mongo::BSONObjBuilder::append(label, date);
137  }
138 
143  RepoBSON obj();
144 
145  private:
153  void appendUUID(
154  const std::string &label,
155  const repoUUID &uuid);
156  };
157 
158  // Template specialization
159  template<> void RepoBSONBuilder::append < repoUUID >
160  (
161  const std::string &label,
162  const repoUUID &uuid
163  );
164 
165  template<> void RepoBSONBuilder::append < repo_vector_t >
166  (
167  const std::string &label,
168  const repo_vector_t &vec
169  );
170  }// end namespace model
171  } // end namespace core
172 } // end namespace repo
Definition: repo_connection_pool_mongo.h:32
RepoBSON obj()
Definition: repo_bson_builder.cpp:46
void appendArray(const std::string &label, const std::vector< T > &vec)
Definition: repo_bson_builder.h:54
Definition: repo_node_utils.h:67
Definition: repo_bson.h:53
Definition: repo_bson_builder.h:41
void appendArrayPair(const std::string &label, const std::list< std::pair< std::string, std::string > > &list, const std::string &fstLabel, const std::string &sndLabel)
Definition: repo_bson_builder.cpp:21
void appendBinary(const std::string &label, const T *data, const uint32_t &byteCount)
Definition: repo_bson_builder.h:105