perfsonar-dev - r1719 - branches/yi-udel/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/bdbxml
Subject: perfsonar development work
List archive
r1719 - branches/yi-udel/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/bdbxml
Chronological Thread
- From:
- To:
- Subject: r1719 - branches/yi-udel/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/bdbxml
- Date: Fri, 27 Oct 2006 13:55:27 -0400
Author: yi
Date: 2006-10-27 13:55:26 -0400 (Fri, 27 Oct 2006)
New Revision: 1719
Modified:
branches/yi-udel/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/bdbxml/BerkeleyDbXmlStorageManager.java
Log:
BDB API
Modified:
branches/yi-udel/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/bdbxml/BerkeleyDbXmlStorageManager.java
===================================================================
---
branches/yi-udel/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/bdbxml/BerkeleyDbXmlStorageManager.java
2006-10-27 11:36:30 UTC (rev 1718)
+++
branches/yi-udel/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/bdbxml/BerkeleyDbXmlStorageManager.java
2006-10-27 17:55:26 UTC (rev 1719)
@@ -26,6 +26,7 @@
*/
public class BerkeleyDbXmlStorageManager implements XmlDbStorageManager
{
+ // determine the query return type
private int resultFlag = XmlDbConstants.RETURN_STRING_ARRAY;
protected int getResultType() { return resultFlag; }
@@ -47,47 +48,95 @@
}
}
- final static String XML_DB_HOME = "/home/gabyfox/xmldb";
- public final static String BDBXML_DEFAULT_CONTAINER = "LSStore.dbxml";
+ //-----------------------------------------------------------------------
- private XmlManager myManager;
- private XmlContainer myContainer;
+ // Just don't know what are these 3 method used for
+ // but have to implement because they are the abstract
+ // method from XmlStorage manager
- // Just don't know what are these 3 method used for
public String getType() { return "Berkeley DB XML"; }
public void setType(String type) {}
public void initStorage(DataRepository dataRepository) {}
+ //-----------------------------------------------------------------------
+
+ // debug
+ final static boolean DEBUG = false;
+
+ // default Berkeley database environment path
+ final static String XML_DB_HOME = "/home/gabyfox/xmldb";
+
//------------------------------------------------------------------------
+
+ private Environment myEnv;
+ private XmlManager myManager;
+ private XmlContainer myContainer;
+
public BerkeleyDbXmlStorageManager ()
{
+ myEnv = null;
myManager = null;
myContainer = null;
}
+ private void openManager () throws DatabaseException,
FileNotFoundException
+ {
+ // Environment
+ File envHome = new File (XML_DB_HOME);
+ EnvironmentConfig envConf = new EnvironmentConfig();
+
+ envConf.setCacheSize(50 * 1024 * 1024);
+ envConf.setAllowCreate(true);
+ envConf.setInitializeCache(true);
+ envConf.setTransactional(true);
+ envConf.setInitializeLocking(true);
+ envConf.setInitializeLogging(true);
+
+ if (DEBUG)
+ {
+ envConf.setErrorStream (System.err);
+ }
+
+ myEnv = new Environment(envHome, envConf);
+
+ // XmlManager
+ XmlManagerConfig managerConf = new XmlManagerConfig ();
+ //avoid using setAdoptEnvironment when you set
setAllowAutoOpen to be true
+ //managerConf.setAdoptEnvironment(true);
+ managerConf.setAllowAutoOpen(true);
+ myManager = new XmlManager (myEnv, managerConf);
+
+ if (DEBUG)
+ {
+ XmlManager.setLogLevel(XmlManager.LEVEL_ALL, true);
+ XmlManager.setLogCategory(XmlManager.CATEGORY_ALL,
true);
+ }
+ }
+
public void open() throws SystemException
{
try {
- // Environment
- Environment myEnv = null;
- EnvironmentConfig envConf = new EnvironmentConfig();
-
- envConf.setCacheSize(50 * 1024 * 1024);
- envConf.setAllowCreate(true);
- envConf.setInitializeCache(true);
- envConf.setTransactional(true);
- envConf.setInitializeLocking(true);
- envConf.setInitializeLogging(true);
-
- File envHome = new File (XML_DB_HOME);
- myEnv = new Environment(envHome, envConf);
-
- // XmlManager
+ openManager ();
+ }
+ catch (DatabaseException e)
+ {
+ throw new SystemException("error.common.storage.xmldb.query",
+ "Can't find Berkeley DB: "+e.getMessage());
+ }
+ catch (FileNotFoundException e)
+ {
+ throw new SystemException("error.common.storage.xmldb.query",
+ "Can't find Berkeley DB evironment:
"+e.getMessage());
+ }
+ }
+
+ public void open(String container) throws SystemException
+ {
+ try {
+ openManager ();
- myManager = new XmlManager (myEnv, new XmlManagerConfig ());
-
// XmlContainer
- myContainer =
myManager.openContainer(BDBXML_DEFAULT_CONTAINER);
-
+ XmlContainerConfig containerConf = new XmlContainerConfig();
+ myContainer = myManager.openContainer(container,
containerConf);
}
catch (DatabaseException e)
{
@@ -101,13 +150,13 @@
}
}
-
public void close() throws SystemException
{
try
{
- if (myContainer != null)
+ if (myContainer != null)
myContainer.close();
+
if (myManager != null)
myManager.close();
@@ -121,29 +170,26 @@
}
}
- /**
- * Fetch data. dataQuery input parameter MUST be a String
- * @param dataQuery Must be String that contain XPath/XQuery expression
- * @return always StringArrayDataSet object
- */
- public Object fetch(Object dataQuery) throws SystemException,
RequestException, DataFormatException
+ public Object fetch(Object dataQuery) throws SystemException,
RequestException, DataFormatException
{
- String myQuery = null;
- try
+ String queryString = null;
+ try {
+ queryString = (String)dataQuery;
+ } catch (ClassCastException ex) {
+ throw new
DataFormatException("error.common.storage.xmldb.wrong_query",
+ "BerkelyDbXmlStorageManager: This method accepts " +
+ "StringData data format only");
+ }
+
+ return fetch (queryString);
+ }
+
+ public Object fetch(String queryString) throws SystemException,
RequestException, DataFormatException
+ {
+ if (queryString == null)
{
- myQuery = (String) dataQuery;
- }
- catch (ClassCastException e)
- {
- throw new
DataFormatException("error.common.storage.xmldb.wrong_query",
- "ExistDbXmlrpcXmlStorageManager: This method accepts " +
- "StringData data format only");
- }
-
- if (myQuery == null)
- {
throw new
DataFormatException("error.common.storage.xmldb.empty_query",
- "ExistDbXmlrpcXmlStorageManager: Input DataQuery " +
+ "BerkelyDbXmlStorageManager: Input DataQuery " +
"contains no data");
}
@@ -153,9 +199,9 @@
// XmlQueryContext
XmlQueryContext qc = myManager.createQueryContext();
-
+
// Query
- XmlResults results= myManager.query (myQuery, qc);
+ XmlResults results= myManager.query (queryString, qc);
// Put result into String []
XmlValue value;
@@ -183,78 +229,140 @@
}
}
- /**
- * insert data input parameter MUST be a String
- * @param dataQuery Must be String that contain XPath/XQuery expression
- * @return always StringArrayDataSet object
- */
- public void insertElement (String namespaceDecl, String docName, String
path, String insertContent) throws SystemException, RequestException,
DataFormatException
+ public void insertDocument (String container, String insertData) throws
SystemException, RequestException, DataFormatException
{
- if (insertContent == null)
+ if (insertData == null)
{
throw new
DataFormatException("error.common.storage.xmldb.empty_query",
- "ExistDbXmlrpcXmlStorageManager: Input DataQuery " +
+ "BerkelyDbXmlStorageManager: Input DataQuery " +
"contains no data");
}
try
{
- this.open();
-
- // XmlQueryContext and XmlUpdateContext
- XmlQueryContext qc = myManager.createQueryContext();
+ this.open(container);
+
+ // XmlUpdateContext
XmlUpdateContext uc = myManager.createUpdateContext();
+ // Put the document into the container
+ XmlDocument theDoc = myManager.createDocument();
+ theDoc.setContent(insertData);
+ XmlDocumentConfig documentConf = new
XmlDocumentConfig();
+ documentConf.setGenerateName(true);
+
+ myContainer.putDocument(theDoc, uc, documentConf);
+
+ this.close();
+ }
+ catch (XmlException e)
+ {
+ throw new SystemException("error.common.storage.xmldb.query",
+ "Can't perform XQuery: "+e.getMessage());
+ }
+ }
+
+ public void insert (String container, String namespaceDecl, String
xPath, String insertData) throws SystemException, RequestException,
DataFormatException
+ {
+ if (insertData == null)
+ {
+ throw new
DataFormatException("error.common.storage.xmldb.empty_query",
+ "BerkelyDbXmlStorageManager: Input DataQuery " +
+ "contains no data");
+ }
+
+ try
+ {
+ this.open(container);
+
+ // Find the result set to be inserted
+ XmlQueryContext qc = myManager.createQueryContext();
+ String queryString = namespaceDecl + "collection(\""
+ container + "\")" + xPath;
+ XmlResults results = myManager.query(queryString, qc);
+
// XmlModify
XmlModify mod = myManager.createModify();
- String queryString = namespaceDecl + path;
- XmlQueryExpression select =
myManager.prepare(queryString, qc);
- mod.addAppendStep (select, XmlModify.Element, "",
insertContent);
+ XmlQueryExpression select =
myManager.prepare(namespaceDecl + xPath, qc);
+ mod.addAppendStep (select, XmlModify.Element, "",
insertData);
- // Retrieve the document we want to modify
- XmlDocument doc = myContainer.getDocument (docName);
- XmlValue docValue = new XmlValue (doc);
- mod.execute(docValue, qc, uc);
+ // Modify everything in result set
+ XmlUpdateContext uc = myManager.createUpdateContext();
+ XmlDocument theDoc = myManager.createDocument();
+ while (results.next(theDoc))
+ {
+ XmlValue docValue = new XmlValue (theDoc);
+ mod.execute(docValue, qc, uc);
+ }
this.close();
}
catch (XmlException e)
+ {
+ throw new SystemException("error.common.storage.xmldb.query",
+ "Can't perform XQuery: "+e.getMessage());
+ }
+ }
+
+ public void deleteDocument (String container, String namespaceDecl,
String xPath) throws SystemException, RequestException, DataFormatException
+ {
+ if (xPath == null)
+ {
+ throw new
DataFormatException("error.common.storage.xmldb.empty_query",
+ "BerkelyDbXmlStorageManager: Input DataQuery " +
+ "contains no data");
+ }
+
+ try
+ {
+ this.open(container);
+
+ // Perform the query
+ XmlQueryContext qc = myManager.createQueryContext();
+ String queryString = namespaceDecl + "collection(\"" +
container + "\")" + xPath;
+ XmlResults results = myManager.query(queryString, qc);
+
+ // Delete everything in the result set
+ XmlUpdateContext uc = myManager.createUpdateContext();
+ XmlDocument theDoc = myManager.createDocument();
+ while (results.next(theDoc))
+ {
+ myContainer.deleteDocument(theDoc, uc);
+ }
+
+ this.close();
+ }
+ catch (XmlException e)
{
throw new SystemException("error.common.storage.xmldb.query",
"Can't perform XQuery: "+e.getMessage());
}
}
- /**
- * delete data input parameter MUST be a String
- * @param dataQuery Must be String that contain XPath/XQuery expression
- * @return always StringArrayDataSet object
- */
- public void deleteElement (String namespaceDecl, String deleteNodes)
throws SystemException, RequestException, DataFormatException
+ public void delete (String container, String namespaceDecl, String
xPath) throws SystemException, RequestException, DataFormatException
{
- if (deleteNodes == null)
+ if (xPath == null)
{
throw new
DataFormatException("error.common.storage.xmldb.empty_query",
- "ExistDbXmlrpcXmlStorageManager: Input DataQuery " +
+ "BerkelyDbXmlStorageManager: Input DataQuery " +
"contains no data");
}
try
{
- this.open();
-
- // XmlQueryContext and XmlUpdateContext
+ this.open(container);
+
+ // Perform the query
XmlQueryContext qc = myManager.createQueryContext();
- XmlUpdateContext uc = myManager.createUpdateContext();
+ String queryString = namespaceDecl + "collection(\"" +
container + "\")" + xPath;
+ XmlResults results = myManager.query(queryString, qc);
// XmlModify
XmlModify mod = myManager.createModify();
- String queryString = namespaceDecl + deleteNodes;
- XmlQueryExpression select =
myManager.prepare(queryString, qc);
+ XmlQueryExpression select =
myManager.prepare(namespaceDecl + xPath, qc);
mod.addRemoveStep (select);
- // Retrieve the document we want to modify
- XmlResults results = myContainer.getAllDocuments(new
XmlDocumentConfig ());
+ // Delete everything in the result set
+ XmlUpdateContext uc = myManager.createUpdateContext();
XmlDocument theDoc = myManager.createDocument();
while (results.next(theDoc))
{
@@ -270,4 +378,45 @@
"Can't perform XQuery: "+e.getMessage());
}
}
+
+ public void update (String container, String namespaceDecl, String
xPath, String updateData) throws SystemException, RequestException,
DataFormatException
+ {
+ if (updateData == null)
+ {
+ throw new
DataFormatException("error.common.storage.xmldb.empty_query",
+ "BerkelyDbXmlStorageManager: Input DataQuery " +
+ "contains no data");
+ }
+
+ try
+ {
+ this.open(container);
+
+ // Find the result set to be updated
+ XmlQueryContext qc = myManager.createQueryContext();
+ String queryString = namespaceDecl + "collection(\""
+ container + "\")" + xPath;
+ XmlResults results = myManager.query(queryString, qc);
+
+ // XmlModify
+ XmlModify mod = myManager.createModify();
+ XmlQueryExpression select =
myManager.prepare(namespaceDecl + xPath, qc);
+ mod.addUpdateStep (select, updateData);
+
+ // Modify everything in result set
+ XmlUpdateContext uc = myManager.createUpdateContext();
+ XmlDocument theDoc = myManager.createDocument();
+ while (results.next(theDoc))
+ {
+ XmlValue docValue = new XmlValue (theDoc);
+ mod.execute(docValue, qc, uc);
+ }
+
+ this.close();
+ }
+ catch (XmlException e)
+ {
+ throw new SystemException("error.common.storage.xmldb.query",
+ "Can't perform XQuery: "+e.getMessage());
+ }
+ }
}
- r1719 - branches/yi-udel/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/bdbxml, svnlog, 10/27/2006
Archive powered by MHonArc 2.6.16.