Tales From Domain51 Content Repository

For any of you who following my Subversion repository, and from the looks of the stats on that site, that would appear to be none of you, you would know that I’ve been working on implementing my Domain51 Content Repository (d51CR). I’ve got a lot of the basics out of the way and have started implementing node types and definitions. I’ll save you from a full explanation, but a quick understanding of what they are is necessary for the rest of this…

d51CR implements a PHP version of Java’s JSR-170, or the Java Content Repository. The idea is to provide a universal API for accessing data within a repository, be it a file system, forum, or blog. All of them accomplish the same goals, creating nodes that contain properties (or content), JSR-170 just codifies a means of retrieving that data. With d51CR, my goal is to provide a system that abstracts data in such a way that it can contain any type of data and work just fine. Think of it as a universal object persistence system.

Now, the node types… Each node could have a node type which defines how that node will behave. As an example, a folder might implement the nt:folder type which would say that it could have children nodes, while a file might implement nt:file which would say that it couldn’t have children nodes. That’s the bird’s eye view anyway.

So I got to the point toward the end of last week and into the weekend that I was implementing d51NodeTypes (my code for handling node types) and I went about it the normal way. Created a nodeTypes table in the database, added the fields I needed, and away I went. Then I started reading the JSR-170 Spec which talks about implementing them inside the actual repository. Well duh. Here I am building a content repository to store content, and I’m putting some of that content in other tables.

I’ve had a few discussions over the past few months with various other developers about my plans and the one thing I’ve heard time and time again is that it probably won’t perform as well as creating straight tables storing the data you want. As I thought about rewriting the node types to be stored inside the repository, it dawned on me that I had an opportunity to test these theories and see how they panned out. The results… Well, they surprised me.

For starters, I had to create a new test database that contained all the new nodes I needed to store. I got that up and running, finding a few bugs along the way, squashed them, and put unit tests in to make sure they don’t pop up again. By the time I got done the new data in the repository had caused the number of tests being run to grow from 510 in the old system to 621 in the new. Each test can reset the database and since the new system will take longer to reset, I factored in a way for guestimating how long the database reset takes. Each test starts off with two resets and it times the second reset figuring it will be approximately the same as subsequent resets. It calculates the total time to run the tests and subtracts out what the approximate time spent resetting the database.

Those numbers are very close, but you’ll notice that the first test of the new system seems abnormally low. Figuring that something may have thrown it off, I decided to pull the highest and lowest times from each and recalculate the averages:

Average per test with abstract system: 0.00178 per test

Average per test with “standard” system: 0.00180 per test

I did a double take when I saw those the first time, too. What makes this that much more impressive is that the new system has to parse a quasi-XPath query and translate it into an executable SQL statement prior to executing it while the older system executes it directly against the database access object. I know this is a small sampling of how this will actually work in the real-world, but it does bode well seeing that it can take on a handful of node types and come out to the good.

Just FYI, the query it is performing is to see if a node type has the property of “isMixin” (does it play well with other node types, basically). In the standard system the query was along the lines of:

Needless to say, the second should be the slower of the two. If the second SQL query odd and you’ve never seen “lft” or “rgt” in an SQL statement before, check out this article on SitePoint for an explanation of the modified preorder tree traversal algorithm.

Anyhow, it looks like I’m going to rewrite a few other parts of d51CR to store their data inside the items table as well.