Planning on building a Folder based structure in Java.
I will be using a jquery plugin for the GUI, so I don’t need need information on how to display the folder structure.
I am looking for the backend logic on how folder information is stored, such that it can be retrieved in a quick and efficient manner.
Each folder will have multiple subfolders.
From a leaf folder, we should be able access the root quickly and efficiently
Example:
+Folder1
|__SubFolder1_1
|__SubFolder1_2
|_SubSubFolder1_2_1
|_
+Folder2
|__SubFolder2_1
|_SubFolder2_1_1
|_SubFolder2_1_2
|_SubFolder2_1_2_1
New folders could be added randomly.
Folder can be renamed.
Folders can be deleted.
My Question is:
How would these folder details be stored in the database?
Again, I am looking for a fast and efficient means of storing and retrieving this information.
For storing in DB, the easiest and most straight forward way is to have an parent_folder_id for each folder/node. This should be good enough in most scenario, especially you are going to construct the folder object structure and do manipulation base on the object model.
Depends on your requirement, there is a quite common case that you need to
If it is what you are looking for, then there is a interesting method that you may have a look:
Each DB record will have 2 extra number field, let’s call it LEFT and RIGHT
assume a tree like this:
What is going to be stored in DB is
When you need to find all nodes under certain node (N) by SQL, simply find out all nodes with LEFT > N.LEFT and RIGHT < N.RIGHT
You can easily perform insert/delete by bulk updating related nodes by (not a difficult task, leave it to you 😛 )
This is probably not very OO friendly but in case the requirement I mentioned is what you need, u may consider using this method.