10 #include <IceUtil/Options.h>
11 #include <IceUtil/FileUtil.h>
13 #include <Ice/ConsoleUtil.h>
16 #include <IceUtil/DisableWarnings.h>
25 class Client :
public Ice::Application
30 virtual int run(
int,
char* []);
36 wmain(
int argc,
wchar_t* argv[])
41 main(
int argc,
char* argv[])
46 return app.main(argc, argv);
52 consoleErr <<
"Usage: " << appName() <<
" <options>\n";
55 "-h, --help Show this message.\n"
56 "-v, --version Display version.\n"
57 "--import FILE Import database from FILE.\n"
58 "--export FILE Export database to FILE.\n"
59 "--dbhome DIR Source or target database environment.\n"
60 "--dbpath DIR Source or target database environment.\n"
61 "--mapsize VALUE Set LMDB map size in MB (optional, import only).\n"
62 "-d, --debug Print debug messages.\n"
69 IceUtilInternal::Options opts;
70 opts.addOpt(
"h",
"help");
71 opts.addOpt(
"v",
"version");
72 opts.addOpt(
"d",
"debug");
73 opts.addOpt(
"",
"import", IceUtilInternal::Options::NeedArg);
74 opts.addOpt(
"",
"export", IceUtilInternal::Options::NeedArg);
75 opts.addOpt(
"",
"dbhome", IceUtilInternal::Options::NeedArg);
76 opts.addOpt(
"",
"dbpath", IceUtilInternal::Options::NeedArg);
77 opts.addOpt(
"",
"mapsize", IceUtilInternal::Options::NeedArg);
82 args = opts.parse(argc,
const_cast<const char**
>(argv));
84 catch (
const IceUtilInternal::BadOptException& e)
86 consoleErr << argv[0] <<
": " << e.reason << endl;
92 consoleErr << argv[0] <<
": too many arguments" << endl;
97 if (opts.isSet(
"help"))
103 if (opts.isSet(
"version"))
105 consoleOut << ICE_STRING_VERSION << endl;
109 if (!(opts.isSet(
"import") ^ opts.isSet(
"export")))
111 consoleErr << argv[0] <<
": either --import or --export must be set" << endl;
116 if (!(opts.isSet(
"dbhome") ^ opts.isSet(
"dbpath")))
118 consoleErr << argv[0] <<
": set the database environment directory with either --dbhome or --dbpath" << endl;
123 bool debug = opts.isSet(
"debug");
124 bool import = opts.isSet(
"import");
125 string dbFile = opts.optArg(
import ?
"import" :
"export");
127 if (opts.isSet(
"dbhome"))
129 dbPath = opts.optArg(
"dbhome");
133 dbPath = opts.optArg(
"dbpath");
136 string mapSizeStr = opts.optArg(
"mapsize");
150 consoleOut <<
"Importing database to directory " << dbPath <<
" from file " << dbFile << endl;
154 consoleErr << argv[0] <<
": output directory does not exist: " << dbPath << endl;
158 if (!IceUtilInternal::isEmptyDirectory(dbPath))
160 consoleErr << argv[0] <<
": output directory is not empty: " << dbPath << endl;
164 ifstream fs(IceUtilInternal::streamFilename(dbFile).c_str(), ios::binary);
167 consoleErr << argv[0] <<
": could not open input file: " << strerror(errno) << endl;
170 fs.unsetf(ios::skipws);
172 fs.seekg(0, ios::end);
173 streampos fileSize = fs.tellg();
178 consoleErr << argv[0] <<
": empty input file" << endl;
182 fs.seekg(0, ios::beg);
184 vector<Ice::Byte> buf;
185 buf.reserve(
static_cast<size_t>(fileSize));
186 buf.insert(buf.begin(), istream_iterator<Ice::Byte>(fs), istream_iterator<Ice::Byte>());
195 if (type !=
"IceStorm")
197 consoleErr << argv[0] <<
": incorrect input file type: " << type << endl;
200 stream.read(version);
209 consoleOut <<
"Writing LLU Map:" << endl;
213 lluMap(txn,
"llu",
dbContext, MDB_CREATE);
215 for (StringLogUpdateDict::const_iterator p =
data.llus.begin(); p !=
data.llus.end(); ++p)
219 consoleOut <<
" KEY = " << p->first << endl;
221 lluMap.put(txn, p->first, p->second);
226 consoleOut <<
"Writing Subscriber Map:" << endl;
230 subscriberMap(txn,
"subscribers",
dbContext, MDB_CREATE);
232 for (SubscriberRecordDict::const_iterator
q =
data.subscribers.begin();
q !=
data.subscribers.end(); ++
q)
236 consoleOut <<
" KEY = TOPIC(" << communicator()->identityToString(
q->first.topic)
237 <<
") ID(" << communicator()->identityToString(
q->first.id) <<
")" << endl;
239 subscriberMap.put(txn,
q->first,
q->second);
248 consoleOut <<
"Exporting database from directory " << dbPath <<
" to file " << dbFile << endl;
256 consoleOut <<
"Reading LLU Map:" << endl;
265 while (lluCursor.get(
s, llu, MDB_NEXT))
269 consoleOut <<
" KEY = " <<
s << endl;
271 data.llus.insert(std::make_pair(
s, llu));
277 consoleOut <<
"Reading Subscriber Map:" << endl;
281 subscriberMap(txn,
"subscribers",
dbContext, 0);
286 subCursor(subscriberMap, txn);
287 while (subCursor.get(key, record, MDB_NEXT))
291 consoleOut <<
" KEY = TOPIC(" << communicator()->identityToString(key.
topic)
292 <<
") ID(" << communicator()->identityToString(key.
id) <<
")" << endl;
294 data.subscribers.insert(std::make_pair(key, record));
303 stream.write(
"IceStorm");
304 stream.write(ICE_INT_VERSION);
307 ofstream fs(IceUtilInternal::streamFilename(dbFile).c_str(), ios::binary);
310 consoleErr << argv[0] <<
": could not open output file: " << strerror(errno) << endl;
313 fs.write(
reinterpret_cast<const char*
>(stream.b.begin()), stream.b.size());
317 catch (
const IceUtil::Exception& ex)
319 consoleErr << argv[0] <<
": " << (
import ?
"import" :
"export") <<
" failed:\n" << ex << endl;