void AddDocumentCommand::execute(Args &args, Environment &env) { if ((args.size() < 3) || (args.size() > 4)){ throw CommandException("Wrong number of arguments"); } env.testContainer(); env.deleteResults(); if ((args.size() == 4) && (args[3] == "q")) { if(env.txn()) { XmlTransaction myTxn = env.childTransaction(); env.results() = new XmlResults( env.db().query(myTxn, args[2], env.context())); XmlValue value; while(env.results()->next(value)) { string v = value.asString(); string name = env.container()-> putDocument(myTxn, args[1], v, env.uc(), DBXML_GEN_NAME); if(env.verbose()) cout << "Document added, name = " << name << endl; } myTxn.commit(); } else { env.results() = new XmlResults(env.db().query(args[2], env.context())); XmlValue value; while(env.results()->next(value)) { string v = value.asString(); string name = env.container()-> putDocument(args[1], v, env.uc(), DBXML_GEN_NAME); if(env.verbose()) cout << "Document added, name = " << name << endl; } } } else { // by string or by file bool byString = true; if (args.size() == 4 && args[3] == "f") byString = false; XmlDocument doc = env.db().createDocument(); doc.setName(args[1]); if (byString) doc.setContent(args[2]); else { XmlInputStream *is = env.db().createLocalFileInputStream(args[2]); doc.setContentAsXmlInputStream(is); } if(env.txn()) { XmlTransaction myTxn = env.childTransaction(); env.container()->putDocument(myTxn, doc, env.uc()); myTxn.commit(); } else { env.container()->putDocument(doc, env.uc()); } // put doc content in last results XmlResults res = env.db().createResults(); res.add(XmlValue(doc)); env.results() = new XmlResults(res); if(env.verbose()) cout << "Document added, name = " << args[1] << endl; } }
int main(int argc, char **argv) { std::string path2DbEnv; std::string theContainer = "simpleExampleData.dbxml"; for ( int i=1; i<argc; i++ ) { if ( argv[i][0] == '-' ) { switch(argv[i][1]) { case 'h': path2DbEnv = argv[++i]; break; default: usage(); } } } if (! path2DbEnv.length() ) usage(); // Berkeley DB environment flags u_int32_t envFlags = DB_RECOVER|DB_CREATE|DB_INIT_MPOOL| DB_INIT_LOCK|DB_INIT_TXN|DB_INIT_LOG; // Berkeley DB cache size (64 MB). The default is quite small u_int32_t envCacheSize = 64*1024*1024; // Create and open a Berkeley DB Transactional Environment. int dberr; DB_ENV *dbEnv = 0; dberr = db_env_create(&dbEnv, 0); if (dberr == 0) { dbEnv->set_cachesize(dbEnv, 0, envCacheSize, 1); dberr = dbEnv->open(dbEnv, path2DbEnv.c_str(), envFlags, 0); } if (dberr) { std::cout << "Unable to create environment handle due to the following error: " << db_strerror(dberr) << std::endl; if (dbEnv) dbEnv->close(dbEnv, 0); return -1; } //Have the XmlManager adopt the db environment XmlManager db(dbEnv, DBXML_ADOPT_DBENV); //Configure the container to use transactions XmlContainerConfig config; config.setTransactional(true); //Open a container in the db environment XmlContainer container = db.openContainer(theContainer, config); // Get an XmlUpdateContext. Useful from a performance perspective. XmlUpdateContext updateContext = db.createUpdateContext(); //Get a transaction XmlTransaction txn = db.createTransaction(); std::string document1 = "<aDoc><title>doc1</title><color>green</color></aDoc>"; std::string document2 = "<aDoc><title>doc2</title><color>yellow</color></aDoc>"; //Add the documents XmlDocument myXMLDoc = db.createDocument(); /* Set the XmlDocument to the relevant string and then put it into the container. * Using the flag DBXML_GEN_NAME means that a generated name will be assigned * to the document if it does not have one. An exception will be thrown if * a document is inserted without a name or the DBXML_GEN_NAME flag. */ myXMLDoc.setContent( document1 ); container.putDocument(txn, myXMLDoc, updateContext, DBXML_GEN_NAME); //do it again for the second document myXMLDoc.setContent( document2 ); container.putDocument(txn, myXMLDoc, updateContext, DBXML_GEN_NAME); //Normally we would use a try/catch block to trap any exceptions. // In the catch, we should call txn->abort() to avoid leaving the // database in an indeterminate state in the event of an error. // However, this simple example avoids error handling so as to // highlite basic concepts, so that step if omitted here as well. //Commit the writes. This causes the container write operations // to be saved to the container. txn.commit(); return 0; }