If you have a non-Java / SQLite related answer, I would be happy to read it.
Environment
I store items in a database with the following schema:
Problem
I have to filter elements according to geocontext and date. It would be an easy task if all the objects were on the same level, but the trick is recursive. EG:
root |_item 1 |_item 2 | |_item 4 | |_item 5 | |_item 6 |_item 3 | |_item 8 | |_item 10 |_item 11 | |_item 12 |_item 7
There is no explicit limit to recursive depth.
Now, if we are in any node and filter with the date "April 1", we should not only see the elements directly contained in the node that correspond to the date, but we should see the elements containing the elements corresponding to the date as well .
EG: We are in "Items 2", if "item 6" corresponds to a date, then we consider that "element 5" also corresponds to a date, and we must save it. If we are at the root, then element 2 should be displayed.
The same applies to the geocontext, but it is even more complicated because:
- It is stored in another table.
- Matching the context is an expensive math calculation.
Of course, crude compliance forcing will slow down the software and lead to a very bad user.
NOTE. I do not need to display the tree . I am showing a list of filtered data from a tree. We should see only a flat list of the top elements. The challenge is to decide whether to display each element or not, depending on all the children. Hierachy.
How I tried to solve it
I thought I could alleviate the problem a bit by using more tables to cache flat data:
This seems reasonable, but I have not tried it yet. However, this should have the following disadvantages:
I shifted the expensive process to / set / create / delete, which would have to manage the cached date. This will be nasty code to write and maintain. The five depths level element will be a trigger that will hit five parents recursively.
The size of the database can be HUGE. Five levels of depth caches data for five parents. I don’t know if this matters, since it is a single-user application with manual input. I don’t think anyone will introduce more than 1000 items with more than 10 depth levels.
Now the good news: we go from the bottom of the pyramid at the top, and not otherwise as it seems. When I have to deal with the parent element deletion, this will be another nice headache, but I keep it for another question; -).
Now my question
How can you store data and handle filtering in the most optimal way?
Additionally:
Should I define an explicit recursive depth limit? Should I perform filtering using SQL or Java? SQL will certainly be faster, but geocontext mapping is much easier to do in Java.
Since I work on the Android platform, I have the following limitations:
Java is the only language available, not with the entire standard library.
SQLite is the only DBMS available.
Performance and memory are important issues. If you need to choose, battery life and therefore performance is a priority.
Exotics external libraries may not be available for use.
PS: I burst into SO and found interesting pieces of information (espacially What is the most efficient / elegant way to parse a flat table into a tree? ), This is a hint, but not a problem.