MongoType
MongoDB Collection Data Dump with BSON Types
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mongotype.cpp
Go to the documentation of this file.
1 
102 //----------------------------------------------------------------------------
103 #include <mongotype.hpp>
104 #include <Parameters.hpp>
105 #include <BSONTypeMap.hpp>
106 #include <BSONObjectTypeDump.hpp>
107 #include <BSONDotNotationDump.hpp>
108 #include <JSONDump.hpp>
109 
110 //----------------------------------------------------------------------------
111 
117 namespace mongotype {
118 
119 //----------------------------------------------------------------------------
120 
121 const string VERSION("2.4.1");
122 const string COPYRIGHT("Copyright (c) 2013 by Mark Deazley");
123 const string LICENSE(
124  "Free Software Foundation’s GNU AGPL v3.0.\n"
125  "This program is free software: you can redistribute it and/or modify\n"
126  "it under the terms of the GNU Affero General Public License as\n"
127  "published by the Free Software Foundation, either version 3 of the\n"
128  "License, or (at your option) any later version.\n"
129  "\n"
130  "This program is distributed in the hope that it will be useful,\n"
131  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
132  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
133  "GNU Affero General Public License for more details.\n"
134  "\n"
135  "You should have received a copy of the GNU Affero General Public License\n"
136  "along with this program. If not, see http://www.gnu.org/licenses/ .\n");
137 
138 //----------------------------------------------------------------------------
139 
140 void dumpCollection(Parameters& params) {
141  DBClientConnection c;
142  string hostPort(params.getHost());
143  if (hostPort.find(':') == string::npos) {
144  hostPort += ":";
145  hostPort += to_string(params.getPort());
146  }
147  c.connect(hostPort);
148  time_t t;
149  time(&t);
150 
151  int documentCount = c.count(params.getDbCollection());
152 
153  if (params.isDebug()) {
154  cout << "{ " << params.getDbCollection() << ".count: "
155  << documentCount << " }\n";
156  }
157 
158  string docPrefixString(params.getDbCollection());
159 // docIndex += "{";
160 // docIndex += to_string(i++);
161 // docIndex += "}";
162 
163  unique_ptr<DBClientCursor> cursor = c.query(params.getDbCollection(), BSONObj());
164 
165  unique_ptr<IBSONRenderer> renderer;
166  switch (params.getStyle()) {
167  case STYLE_DOTTED:
168  renderer = unique_ptr<IBSONRenderer>(new BSONDotNotationDump(params, docPrefixString));
169  break;
170  case STYLE_TREE:
171  renderer = unique_ptr<IBSONRenderer>(new BSONObjectTypeDump(params, docPrefixString));
172  break;
173  case STYLE_JSON:
174  case STYLE_JSONPACKED:
175  renderer = unique_ptr<IBSONRenderer>(new JSONDump(params, " "));
176  break;
177  default:
178  throw std::logic_error("ISE: Undefined STYLE!");
179  break;
180  }
181  if (renderer) {
182  renderer->setOutputStream(cout);
183  renderer->begin(NULL);
184  int documentIndex = 0;
185  while (cursor->more()) {
186  const BSONObj& o = cursor->next(); // Get the BSON Object
187  renderer->render(o, documentIndex++, documentCount);
188  }
189  renderer->end(NULL);
190  } else {
191  throw std::logic_error("ISE: Undefined renderer!");
192  }
193 }
194 
195 //----------------------------------------------------------------------------
196 
197 } // End of namespace mongotype
198 
199 //----------------------------------------------------------------------------
200 
201 int main(int argc, char* argv[]) {
202  try {
203  mongotype::Parameters params;
204  params.parse(argc, argv);
206  } catch (const mongo::DBException &e) {
207  cerr << "mongotype MongoDB Error: \"" << e.what() << "\"" << endl;
208  exit(2);
209  } catch (std::logic_error &e) {
210  cerr << "mongotype Generic Error: \"" << e.what() << "\"" << endl;
211  exit(2);
212  }
213  return EXIT_SUCCESS;
214 }
215