Showing posts with label berkely db xml. Show all posts
Showing posts with label berkely db xml. Show all posts

2008-01-09

Using Threads in Twisted

This Twisted thingy is seriously hard stuff to understand... In addition to that threading doc (post link), see howtos about deferreds and async programming.

What I'm trying to do here is plug Berkeley DB XML into a Twisted.Web2 application. This involves delegating DB access to worker threads outside the Twisted event loop ("reactor" pattern). A lot more stuff to figure out before coding starts...

2008-01-07

Berkeley DB XML Python basics

In an earlier post a C++ snippet can be found where a DB XML container was created (or opened if already exists) and a document read from stdin was put into that container. That same snippet done in Python is pretty much identical:

from bsddb3.db import *
from dbxml import *

mgr = XmlManager(DBXML_ALLOW_EXTERNAL_ACCESS)
uc = mgr.createUpdateContext()

try:
        cont = mgr.openContainer("testcontainer.dbxml", DB_CREATE|DBXML_ALLOW_VALIDATION, XmlContainer.WholedocContainer)
        doc = mgr.createDocument()
        input = mgr.createStdInInputStream()
        doc.setContentAsXmlInputStream(input)
        cont.putDocument(doc, uc, DBXML_GEN_NAME)

except XmlException, inst:
        print "XmlException (", inst.ExceptionCode,"): ", inst.What
        if inst.ExceptionCode == DATABASE_ERROR:
                print "Database error code:",inst.DBError

Berkeley DB XML Python module building

A glitch appears when trying to build out of the box as instructed in src/python/README. Minor tweaking with types was necessary in the dbxml_python_wrap.cpp file. Here's output from diff(1):


2058c2058
<     const char *doc = (((PyCFunctionObject *)obj) -> m_ml -> ml_doc);
---
>     char *doc = (((PyCFunctionObject *)obj) -> m_ml -> ml_doc);
3001c3001
<     char *cstr; Py_ssize_t len;
---
>     char *cstr; int len;
36695c36695
<       const char *c = methods[i].ml_doc;
---
>       char *c = methods[i].ml_doc;
36699c36699
<         const char *name = c + 10;
---
>         char *name = c + 10;


This same problem was discussed in DB XML forums (which, for some reason, seem to be off line a lot of time; what's wrong, Oracle?). After those minor modifications, build went as advertised:


cd bsddb3-4.5.0

python setup.dbxml.py build

sudo python setup.dbxml.py install

cd ..

python setup.py build

sudo python setup.py install

After the build these basic tests passed:


>>> from bsddb3.db import *
>>> from dbxml import *
>>> print version()
(4, 5, 20)
>>> mgr=XmlManager()
>>> print mgr.get_version_string()
Oracle: Berkeley DB XML 2.3.10: (January 30, 2007)

Running the tests (python examples.py test) also passed just fine. Great!

2006-10-06

Sarissa: XML parsing (take 2)

Use the .getText() method goddamnit! Bit of a glitch there earlier.. Parsing and then (re-)serializing does not make that much sense in retrospect. 8-]

var oDomDoc = Sarissa.getDomDocument( );
oDomDoc = ( new DOMParser()).parseFromString( responseText, "text/xml");
if( oDomDoc.parseError != 0)
  {
    alert( "Virhe. Palvelin lähetti epäkelvon vastauksen. (parse error)");
    critical( );
  }

oDomDoc.setProperty( "SelectionLanguage", "XPath");
var oElem = oDomDoc.selectSingleNode( "//@some_attribute");

var str =Sarissa.getText( oElem);

...

2006-05-24

BDB XML & document insertion

try {    
  XmlManager mgr( DBXML_ALLOW_EXTERNAL_ACCESS);  // flag is for validation
  XmlContainer cont =mgr.openContainer( "clientlog.dbxml",
                                        DB_CREATE |
                                        DBXML_ALLOW_VALIDATION,
                                        XmlContainer::WholedocContainer);

  XmlDocument newdoc =mgr.createDocument();

  XmlUpdateContext uc =mgr.createUpdateContext();

  XmlInputStream *in =mgr.createStdInInputStream();

  newdoc.setContentAsXmlInputStream( in);

  cont.putDocument(newdoc, uc, DBXML_GEN_NAME);

  ...

BDB XML & Metadata

time_t now = time( 0 );
char timeString[100];

strftime(timeString, 100, "%Y-%m-%dT%H:%M:%S", localtime( &now ) );

newdoc.setMetaData( "", "created", timeString);

Sarissa & XML parsing

function callback( responseText)
{
  var oDomDoc = Sarissa.getDomDocument( );

  oDomDoc = ( new DOMParser()).parseFromString( responseText, "text/xml");
  if( oDomDoc.parseError != 0)
    handle_error( ); // or something...

  oDomDoc.setProperty( "SelectionLanguage", "XPath");  //  for IE
  var oElem = oDomDoc.selectSingleNode( "//some_element");
  var str =Sarissa.serialize( oElem);

  ...
}

Sarissa & XMLHttpRequest

function gettime( ) { return (new Date()).getTime(); }

...

if( !Sarissa.IS_ENABLED_XMLHTTP) { critical_error( ); return; }

// uniq is needed for IE, otherwise we get a cached copy
var r ="/do?uniq=" + gettime( ) + "&";

r +='foo=' +bar; // ...

var xmlhttp =new XMLHttpRequest();  // Sarissa makes this cross-browser

xmlhttp.onreadystatechange =function( )
{
  if (xmlhttp.readyState ==4)
  {
    if (xmlhttp.status ==200)
    {
      callback( xmlhttp.responseText);  // or something...
    }
    else
    {
      resolve_error( xmlhttp.status);  // or something...
    }
  }
}

xmlhttp.open( "GET", r, true);
xmlhttp.send( null);