Page 1 of 1

Data Structure Selection

Posted: Fri Jul 18, 2008 3:58 pm
by baileylo
I'm writing a photo gallery:
My orginal approach is to store all the images information and what not in xml files, considering I'm the only one using this photo gallery robustness isn't the biggest of problems.
So I've written to parse the xml file and is able to extract all the information and stores it in a class called image.
My question now is how should i store all the images in the application.

Recent thoughts have been since the gallery size shouldn't be very large I can just store the whole gallery in an array.

Then I was thinking, if I do have large galleries should I store the entire gallery in some large random access data structure such as an array thus only having to parse the xml once, and then create a smaller data structure to hold a small amount of photos that will query the larger data structure for additional images when necessary.

But basically what datastructure should I use if I play on doing no removes or adds and lots of lookups, with a large initial load. I'm sort of thinking binary tree, though since they're not searches an array seems most logical, any suggestions on any of these points.

Re: Data Structure Selection

Posted: Fri Jul 18, 2008 4:05 pm
by allspiritseve
baileylo wrote:But basically what datastructure should I use if I play on doing no removes or adds and lots of lookups, with a large initial load. I'm sort of thinking binary tree, though since they're not searches an array seems most logical, any suggestions on any of these points.
Why can't you just store the image locations in a database?

Re: Data Structure Selection

Posted: Fri Jul 18, 2008 4:29 pm
by baileylo
I dunno just using xml.
The problem still remains on the site how do i hold the images. Not how they're stored on the server.

Re: Data Structure Selection

Posted: Fri Jul 18, 2008 4:39 pm
by VirtuosiMedia
Why wouldn't you just upload them into a directory called images? It doesn't matter if you're using a database or XML, you can still just store the image path. It'll be a lot easier, IMHO.

Re: Data Structure Selection

Posted: Fri Jul 18, 2008 4:48 pm
by baileylo
That's what I am doing. I'm wondering how I should store all the path, and other information associated with that image in the script.

Re: Data Structure Selection

Posted: Fri Jul 18, 2008 5:07 pm
by allspiritseve
baileylo wrote:That's what I am doing. I'm wondering how I should store all the path, and other information associated with that image in the script.
That's what I mean. Why can't you store the path, image name, etc. in the database?

Re: Data Structure Selection

Posted: Fri Jul 18, 2008 6:07 pm
by baileylo
I think that might be more efficient but i'm up in the air. In the backend instead of storing url and file names and other information to go with the image in a databse, i'm storing them in xml.

So now that I have everything stored, whether or not it's in a database or xml file, we have to get it off that storage system. Here are my pro's on cons from these database and xml

Pros:
XML
  • Requires nothing else on server except php.
    Simple to setup.
    easy to move from server to server(backup).
MySQL
  • Random Access
    simple to update
    no initial load
Cons:
XML
  • No Random Accessible
    initial load(parse)
MySQL
  • required on server
    extra overhead for look ups
So based off this list I've deduced to two ways of storing my data in the script.
xml
One large (array?) That stores all the information in the gallery. This array is assigned values when the gallery is opened, this is the long part for xml.
Then one smaller array, holding maybe 25 entries, that has access to the larger array and the ability to get items off of it, but this is fast.

MySQL-No preprocessing
Database backend. So no huge array needs to be created.
Then one smaller array, holding maybe 25 entries, that opens up a connection to the database and retrieves new image information close connection.
With this mysql away I eliminate the need to have the large preprocessing of xml, but I occasionally have to make calls to the database, which if they exceed preprocessing time of xml makes since to use xml.

MySql - with preprocessing
Same as with xml, but you'd assume that it takes longer to parse an xml file than to open up a connection to a database query the database and close the connection.

So after writing this all up I think the third Mysql way would be most efficient. Next to buy's somebodys already written gallery.

Re: Data Structure Selection

Posted: Fri Jul 18, 2008 6:13 pm
by Eran
Cons:
Mysql
...
extra overhead for look ups
Just to be clear - Mysql queries in general take much less resources and overhead than parsing XML files in PHP.

Re: Data Structure Selection

Posted: Fri Jul 18, 2008 6:30 pm
by allspiritseve
pytrin wrote:
Cons:
Mysql
...
extra overhead for look ups
Just to be clear - Mysql queries in general take much less resources and overhead than parsing XML files in PHP.
Not to mention you can make queries and return only a subset of the data-- selecting images by number of views, have the same image in different galleries, etc. Overall a database is a much more flexible solution.