Esempio n. 1
0
void putDocWithMyCompression(XmlManager& mgr,
			     const string& containerName,
			     XmlDocument& xdoc,
			     XmlUpdateContext& uc,
			     XmlCompression& myCompression)
{
	// Define an unique name to use for registering the compression
	string compressionName = "myCompression";
	
	// Register custom class
	mgr.registerCompression(compressionName.c_str(), myCompression);

	// Set the container type as WholedocContainer
	// and use the custom compression
	XmlContainerConfig contConf;
	contConf.setAllowCreate(true);
	contConf.setContainerType(XmlContainer::WholedocContainer);
	contConf.setCompressionName(compressionName.c_str());

	// Create container
	XmlContainer cont = mgr.createContainer(containerName, contConf);

	// Put Document
	cont.putDocument(xdoc, uc);
}
Esempio n. 2
0
void RepNode::openContainer()
{
	// reopening a container is forbidden.
	if (!cont_.isNull())
		throwException("Attempt to re-open a container.");

	XmlContainerConfig conf;

	// wait for being elected as a Master or start up done(as a client).
	while (!(isMaster() || appData_.isStartupDone()))
		yield();

	if (isMaster())
		conf.setAllowCreate(true);

	XmlTransaction txn;
	while (true) {
		try {
			txn = mgr_->createTransaction();
			cont_ = mgr_->openContainer(txn, containerName_, conf);
			txn.commit();
			return;
		/*
		 * One thing to watch out for is a case where the databases
		 * you are trying to open do not yet exist. This can happen
		 * for replicas where the databases are being opened
		 * read-only. If this happens, ENOENT is returned by the
		 * open() call.
		 */
		} catch (XmlException &xe) {
			ostringstream o;
			XmlException::ExceptionCode ec = xe.getExceptionCode();
			if (ec == XmlException::DATABASE_ERROR) {
				int dbErr = xe.getDbErrno();
				if (dbErr != ENOENT && dbErr != DB_REP_LOCKOUT) {
					o << envHome_ << ":" << xe.what() << endl
					  << "DB_ERRNO:" << dbErr << endl;
					throwException(o.str());
				}
			} else if (ec != XmlException::CONTAINER_NOT_FOUND) {
				o << envHome_ << ":" << xe.what() << endl
				  << "ExceptionCode :" << ec << endl;
				throwException(o.str());
			}
			yield();
		}
	}
}
Esempio n. 3
0
void putDocWithoutDefaultCompression(XmlManager& mgr,
				     const string& containerName,
				     XmlDocument& xdoc,
				     XmlUpdateContext& uc)
{
	// Set the container type as WholedocContainer and turn off
	// the default compression
	XmlContainerConfig contConf;
	contConf.setAllowCreate(true);
	contConf.setContainerType(XmlContainer::WholedocContainer);
	contConf.setCompressionName(XmlContainerConfig::NO_COMPRESSION);

	// Create container
	XmlContainer cont = mgr.createContainer(containerName, contConf);
	
	// Put Document
	cont.putDocument(xdoc, uc);
}
Esempio n. 4
0
void putDocWithDefaultCompression(XmlManager& mgr,
				  const string& containerName,
				  XmlDocument& xdoc,
				  XmlUpdateContext& uc)
{
	// Set the container type as WholedocContainer and
	// use the default compression
	XmlContainerConfig contConf;
	contConf.setAllowCreate(true);
	contConf.setContainerType(XmlContainer::WholedocContainer);

	// The following line is unnecessary because default compression
	// would take effect if user do not turn off it explicitly.
	contConf.setCompressionName(XmlContainerConfig::DEFAULT_COMPRESSION);

	// Create container
	XmlContainer cont = mgr.createContainer(containerName, contConf);

	// Put Document
	cont.putDocument(xdoc, uc);
}
int main(int argc, char **argv)
{
	std::string path2DbEnv;
	std::string theContainer = "namespaceExampleData.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 mgr(dbEnv, DBXML_ADOPT_DBENV);

	//Configure the container to use transactions
	XmlContainerConfig config;
	config.setTransactional(true);

	//Open a container in the db environment
	XmlContainer container = mgr.openContainer(theContainer, config);

	//create a transaction
	XmlTransaction txn = mgr.createTransaction();

	//create a context and declare the namespaces
	XmlQueryContext context = mgr.createQueryContext();
	context.setNamespace( "fruits", "http://groceryItem.dbxml/fruits");
	context.setNamespace( "vegetables", "http://groceryItem.dbxml/vegetables");
	context.setNamespace( "desserts", "http://groceryItem.dbxml/desserts");

	//Query for documents by their document names.
	doContextQuery( txn, mgr, container.getName(),
		"/*[dbxml:metadata('dbxml:name')='ZuluNut.xml']", context );
	doContextQuery( txn, mgr, container.getName(),
		"/*[dbxml:metadata('dbxml:name')='TrifleOrange.xml']", context );
	doContextQuery( txn, mgr, container.getName(),
		"/*[dbxml:metadata('dbxml:name')='TriCountyProduce.xml']", context );

	//Get the document from the container using the document name
	doGetDocument(txn, container, "ZuluNut.xml");
	doGetDocument(txn, container, "TrifleOrange.xml");
	doGetDocument(txn, container, "TriCountyProduce.xml");

	//commit the transaction
	txn.commit();

	return 0;
}
Esempio n. 6
0
int
main(int argc, char **argv)
{
	// This program uses a named container, which will apear
	// on disk
	std::string containerName = "people.dbxml";
	std::string content = "<people><person><name>joe</name></person><person><name>mary</name></person></people>";
	std::string docName = "people";
	// Note that the query uses a variable, which must be set
	// in the query context
	std::string queryString =
		"collection('people.dbxml')/people/person[name=$name]";
	std::string environmentDir = ".";

	// 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 (25 MB).  The default is quite small
	u_int32_t envCacheSize = 25*1024*1024;

	// argument parsing should really use getopt(), but
	// avoid it for platform-independence
	if (argc == 3) {
		if (std::string(argv[1]) != std::string("-h"))
			usage(argv[0]);
		environmentDir = argv[2];
	} else if (argc != 1)
		usage(argv[0]);

	// 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);
		dbEnv->set_errcall(dbEnv, errcall); // set error callback
		dbEnv->set_lk_detect(dbEnv, DB_LOCK_DEFAULT); // handle deadlocks
		dberr = dbEnv->open(dbEnv, environmentDir.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;
	}

	try {
		// All BDB XML programs require an XmlManager instance.
		// Create it from the DbEnv
		XmlManager mgr(dbEnv, DBXML_ADOPT_DBENV);

		// Because the container will exist on disk, remove it
		// first if it exists
		if (mgr.existsContainer(containerName))
			mgr.removeContainer(containerName);

		/* Create a container that is transactional.  The container
		* type is NodeContainer, which is the default container type,
		* and the index is on nodes, which is the default for a
		* NodeContainer container.  XmlContainerConfig can be used
		* to set the container type and index type.
		*/
		XmlContainerConfig config;
		config.setTransactional(true);
		XmlContainer cont = mgr.createContainer(
			containerName,
			config);

		// All Container modification operations need XmlUpdateContext
		XmlUpdateContext uc = mgr.createUpdateContext();

		// The following putDocument call will auto-transact
		// and will abort/cleanup if the operation deadlocks or
		// otherwise fails
		cont.putDocument(docName, content, uc);

		// Querying requires an XmlQueryContext
		XmlQueryContext qc = mgr.createQueryContext();

		// Add a variable to the query context, used by the query
		qc.setVariableValue("name", "mary");

		// Use try/catch and while to handle deadlock/retry
		int retry = 0;
		while (retry < 5) { // hard-code 5 retries
			// Create a new transaction for the query
			XmlTransaction txn = mgr.createTransaction();

			try {
				// o Note the passing of txn to both methods
				// o Often the XmlQueryExpression object will be created and
				// saved for reuse in order to amortize the cost of parsing a query
				XmlQueryExpression expr = mgr.prepare(txn, queryString, qc);
				XmlResults res = expr.execute(txn, qc);

				// Note use of XmlQueryExpression::getQuery() and
				// XmlResults::size()
				std::cout << "The query, '" << expr.getQuery() << "' returned " <<
					(unsigned int)res.size() << " result(s)" << std::endl;

				// Process results -- just print them
				XmlValue value;
				std::cout << "Result: " << std::endl;
				while (res.next(value)) {
					std::cout << "\t" << value.asString() << std::endl;
				}

				// done with the transaction
				txn.commit();
				break;
			} catch (XmlException &x) {
				txn.abort(); // unconditional
				if ((x.getExceptionCode() == XmlException::DATABASE_ERROR) &&
					(x.getDbErrno() == DB_LOCK_DEADLOCK)) {
						++retry;
						continue; // try again
				}
				throw; // re-throw -- not deadlock
			}
		}
		// In C++, resources are released as objects go out
		// of scope.
	} catch (XmlException &xe) {
		std::cout << "XmlException: " << xe.what() << std::endl;
	}

	return 0;
}
Esempio n. 7
0
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;
}
Esempio n. 8
0
int main(int argc, char **argv)
{
	std::string path2DbEnv;
	std::string theContainer = "namespaceExampleData.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);
	XmlContainer container = db.openContainer(theContainer, config);

	//Get a transaction
	XmlTransaction txn = db.createTransaction();
	XmlUpdateContext uc = db.createUpdateContext();

	//Add an string equality index for the "product" element node.
	addIndex( container, "", "product", "node-element-equality-string", txn, uc );
	//Add an edge presence index for the product node
	addIndex( container, "", "product", "edge-element-presence", txn, uc );

	//commit the index adds
	txn.commit();

	return 0;
}
Esempio n. 9
0
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);

	//Create a transaction
	XmlTransaction txn = db.createTransaction();

	//perform the queries

	//find all the Vendor documents in the database
	doQuery( txn, db, container, "/vendor" );

	//find all the vendors that are wholesale shops
	doQuery( txn, db, container, "/vendor[@type=\"wholesale\"]");

	//find the product document for "Lemon Grass"
	doQuery( txn, db, container, "/product/item[.=\"Lemon Grass\"]");

	//find all the products where the price is less than or equal to 0.11
	doQuery( txn, db, container, "/product/inventory[price<=0.11]");

	//find all the vegetables where the price is less than or equal to 0.11
	doQuery( txn, db, container, "/product[inventory/price<=0.11 and category=\"vegetables\"]");

	//commit the transaction
	txn.commit();

	return 0;
}