get the solution


Storing hierarchical data structures in repositories

By
on Regards: PHP;

For the project Lenex2html a performance optimization was done for this Lenex library. The performance gain was significant: an example file had a reduced parsing time of 0,41 seconds instead of 7,3 seconds.

The lenex file format has a hierarchical structure. For instance: A Meet element has a subset of Sessions, which have a subset of Events. Events contain Agegroups and these contain Rankings. But the results themselves are stored at the Athlete entries (orange line), which are a subset of the Clubs, which are a subset of the Meet.

This can lead to difficult search operations, when you want to display the data in an other order than the hierarchical data structure. Here I present a proposal to store such data structures without loosing any information.

I created a generic repository called “ObjectRepository” (possibly there exist better names). This repository can also store indexes using a private array named “index”. The addIndex method converts the criteria parameter to a string using json_encode, as PHP does not support any other index data types than int or string. Then the result parameter is associated in the private “index” array of the class to the criteria as string. The findBy method then uses this private array. It looks if there exists a result for the specified criteria in the private array. It can be understood as a cache. If there are no cached results, the method searches for corresponding objects.
This implementation also allows you to create relations of objects on properties, which do not exist at all.

All in all: The simple idea of storing the parent-child relations in an index allows you to store hierarchical data structures simplified without loosing any information.

 


%d bloggers like this: