|
|
|
Posted by Aashish Patil (patil_aashish AT emc.com) on January 2008
|
Abstract
The purpose of this article is to provide a short introduction to XHive/DB from a developer's perspective. The information provided is just sufficient to get a reader's interest piqued to explore Xhive/DB in more details.
X-Hive DB
Xhive/DB is a native XML database and not an XML-enabled database(API wrapped over a relational database). Xhive/DB persists XML DOMs and provides fast access to them using XPath and XQuery. It supports some other standards too (check X-Hive documentation for more details).
Session Setup
The main container of data in X-Hive is a Database that stores XML documents. The first step to accessing a database is to setup a session. Any interaction with the database is then done within a transaction. Here is a code snippet that shows setting up a session and performing some hypothetical actions -
XhiveDriverIf driver = XhiveDriverFactory.getDriver("xhive://localhost:1235");
driver.init(1024);
XhiveSessionIf sess = driver.createSession();
sess.connect("aashishp", "aashish","testXmlDb");
sess.begin();
XhiveDatabaseIf db = sess.getDatabase();
//some actions here
sess.commit();
|
This transaction model implies that any the transaction must be started and closed in the outer layer of your API and not within any delegates. That way the delegates can freely call other Xhive APIs, without worrying about starting/stopping a transaction.
Data Organization
In Xhive, XML DOMs (XML Documents) are organized within 'Libraries'. A database consists of a root library and multiple libraries within it. A library can contain any level of libraries (i.e. a library within a library within a library... you get the idea). A library can also store binary data though I won't go into details of that here. Here is an example that prints out the hierarchy -
private static void printLibrary(XhiveLibraryIf lib)
{
System.out.println("lib: " + lib.getName());
Iterator iter = lib.getChildren();
while(iter.hasNext())
{
XhiveNodeIf node = (XhiveNodeIf) iter.next();
if(node instanceof XhiveDocumentIf)
{
XhiveDocumentIf doc = (XhiveDocumentIf) node;
System.out.println("doc id: " + doc.getId());
System.out.println(doc.toString());
}
else if(node instanceof XhiveLibraryIf)
{
printLibrary((XhiveLibraryIf)node);
}
}
}
|
An XhiveLibraryIf is a sub-interface of org.w3c.Node interface. Similarly, XhiveDocumentIf is a subinterface of org.w3c.Document interface.
Querying for Data
It is possible to execute XQueries and XPath expressions on a library or a document node. Here is a snippet that executes a XQuery on a library node.
String query = "for $x in . where $x/doc-item/name='test' return $x";
XhiveLibraryIf docLib = ...
Iterator iterRes = docLib.executeXQuery(query);
while (iterRes.hasNext()) {
XhiveXQueryValueIf val = (XhiveXQueryValueIf) iterRes.next();
XhiveDocumentIf doc = (XhiveDocumentIf) val.asNode();
}
|
There is a lot more to Xhive than what is mentioned above - clustering, failover, user-management, security and access control.
Thoughts
Xhive was very easy to learn. The biggest reason was the excellent documentation available and the numerous code samples provided. It took me about a week to pick up Xhive while working on it in my spare time. Also, because a time limited (one month) evaluation version of Xhive is available any developer can download and explore Xhive. In fact, I had to use this route when I started to learn because it took me some time to get hold of a permanent license key.
Xhive can run as a server process and can also be embedded in your application so that it runs as part of your applications JVM process. When I embedded Xhive, its footprint was ~ 18 megs. In my usage of Xhive, I found it to be extremely fast and light (low memory footprint). Note that my observation is not based on extensive performance testing. I'd recommend attending X-Hive sessions in the EDN Developer Track @ EMC World to get more details.
Resources
Xhive/DB Information Page
Evaluation Copy of X-Hive/DB
Comments / Discussion
|