Data representation issues

Posted by Chris on October 25th, 2007 filed in Uncategorized

So I, like everyone else in the world, wrote my own RSS aggregator. And I think it’s pretty good. It does everything the way I like to do things. If I want a feature, I add it. If I realize I never use something, I remove it. It’s also got a couple secret things that you don’t usually find in an aggregator. For example, some authors only show a short blurb in their feeds. I have an option that lets you select if you want to show the whole page right there in the reader. Another one: I work at a place that has a somewhat moronic firewall filter on it. So there’s an option to use a ssl web-based proxy right there in the window. And to take the link to a new window, etc. Just little things like that that make my day a lot easier that I haven’t seen in other readers.

I want to add a new feature. When in an individual article in a potentially huge list of them, I want a “Next” and “Previous” button. All this has to do is

1- Show the next/previous article (obviously).

2- If the list of articles is displayed, mark the new article as read (if not already marked so).

3- Decrement the Category and Feed count by one, marking empty if needed.

You could go through and do fancy stuff like caching the next and previous, etc. But this is the minimal amount that needs to be done to maintain the current look.

The problem is the data representation. I’m outputting JSON, and can parse through it easily enough obviously. The problem is the next and previous. As the root of the data array, I have the id of the articles. Easy enough, everything else is under it. But….

How do you increment and decrement the current index (go back and forward) in a non-contiguous integer array? If it’s a normal array, it goes 0,1,2,3,etc… But this one could be 2,3,9,13,15,235, etc…

I’ve thought of a few ways. I could push that under a contiguous array so that the first in the array goes to the first one, etc. But that just seems so inelegant for some reason. Then you have two layers of abstraction before anything interesting. Could use a function to iterate through the array to find the one you’re currently on and from there compute next and previous, but that’s definitely a hack. Could create a new array on loading of the array to keep an index, but that also seems like a hack. The one I’m leaning towards is going back to the old school datastructures and creating a linked list. Next and previous and the like. Inside of a javascript array. Less “hackey” sounding than the other ones, no iterating through every time, etc. It’s a little duplication of data (2x more than the first idea), but in the grand scheme it’s not a ton. And json doesn’t have too heavy an overhead. And if you’re getting a bunch of them anyway complete with descriptions, links, ids of category and feed, etc, then it’s not too much extra relative-wise.

It’s also not something I’ve seen in other readers yet. So I can’t just steal other peoples’ good ideas.

Leave a Comment