# Efficient implementation of saving search state

**URL:** <https://www.ruby-forum.com/t/efficient-implementation-of-saving-search-state/184306>\
**Category:** Ruby\
**Created:** [February 28, 2010, 4:47pm UTC](https://www.ruby-forum.com/t/efficient-implementation-of-saving-search-state/184306 "2010-02-28T16:47:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zer0x90](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zer0x90](https://www.ruby-forum.com/u/zer0x90)\
**Post date:** [February 28, 2010, 4:47pm UTC](https://www.ruby-forum.com/t/efficient-implementation-of-saving-search-state/184306/1 "2010-02-28T16:47:02Z")

</div>

Hi everyone, this is my first post to this forum and a new Ruby  
hobbyist. I have a newb question that I am hoping you more experienced  
folks can help me out with.

I am currently developing some ruby code that uses the File.find method  
to perform a depth first (by design) search of a directory tree. No big  
issues with performing that; however, I want to build in the ability to  
save application state if the app is forced close or on error.  
Specifically, if I have to search an extremely large directory  
structure; I would like to start at the location within the directory  
tree that was last reached prior to application shutdown. Also, the  
directory structure is projected to be millions of files, so I need to  
take that into consideration, as well.

Initially, I was looking into Marshal and Pstore, but I am leaning  
towards potentially implementing a database to index the search paths. I  
am asking for any guidance to tutorials, design patterns, and anything  
else that may be relevant.

Any and all comments are welcome. Thanks in advance for everyone’s help.

–Chris

---

<div class="post-metadata">

**Author:** ![zer0x90](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zer0x90](https://www.ruby-forum.com/u/zer0x90)\
**Post date:** [March 1, 2010, 4:34pm UTC](https://www.ruby-forum.com/t/efficient-implementation-of-saving-search-state/184306/2 "2010-03-01T16:34:56Z")

</div>

2010/2/28 User B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> tree that was last reached prior to application shutdown. Also, the  
> directory structure is projected to be millions of files, so I need to  
> take that into consideration, as well.
> 
> Initially, I was looking into Marshal and Pstore, but I am leaning  
> towards potentially implementing a database to index the search paths. I  
> am asking for any guidance to tutorials, design patterns, and anything  
> else that may be relevant.
> 
> Any and all comments are welcome. Thanks in advance for everyone’s help.

Welcome! I am not sure what you think is wrong with using Marshal.  
Wouldn’t it be sufficient to store a stack of currently processed  
directories to a file and overwrite that file every time you have  
finished reading one directory? I believe the volume should be  
sufficiently small to be handled with Marshal.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![zer0x90](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zer0x90](https://www.ruby-forum.com/u/zer0x90)\
**Post date:** [March 4, 2010, 4:10pm UTC](https://www.ruby-forum.com/t/efficient-implementation-of-saving-search-state/184306/3 "2010-03-04T16:10:56Z")

</div>

Robert K. wrote:

> 2010/2/28 User B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > tree that was last reached prior to application shutdown. Also, the  
> > directory structure is projected to be millions of files, so I need to  
> > take that into consideration, as well.
> > 
> > Initially, I was looking into Marshal and Pstore, but I am leaning  
> > towards potentially implementing a database to index the search paths. I  
> > am asking for any guidance to tutorials, design patterns, and anything  
> > else that may be relevant.
> > 
> > Any and all comments are welcome. Thanks in advance for everyone’s help.
> 
> Welcome! I am not sure what you think is wrong with using Marshal.  
> Wouldn’t it be sufficient to store a stack of currently processed  
> directories to a file and overwrite that file every time you have  
> finished reading one directory? I believe the volume should be  
> sufficiently small to be handled with Marshal.
> 
> Kind regards
> 
> robert

Robert, thanks for your response. Your comment makes perfect sense. I am  
saving the string of the last directory searched, then Marshaling to a  
file. Everything appears to be working as it should. Thanks again.

---

<div class="post-metadata">

**Author:** ![zer0x90](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zer0x90](https://www.ruby-forum.com/u/zer0x90)\
**Post date:** [March 4, 2010, 4:30pm UTC](https://www.ruby-forum.com/t/efficient-implementation-of-saving-search-state/184306/4 "2010-03-04T16:30:50Z")

</div>

2010/3/4 User B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > Any and all comments are welcome. Thanks in advance for everyone’s help.
> > 
> > Welcome! I am not sure what you think is wrong with using Marshal.  
> > Wouldn’t it be sufficient to store a stack of currently processed  
> > directories to a file and overwrite that file every time you have  
> > finished reading one directory? I believe the volume should be  
> > sufficiently small to be handled with Marshal.

> Robert, thanks for your response. Your comment makes perfect sense. I am  
> saving the string of the last directory searched, then Marshaling to a  
> file. Everything appears to be working as it should. Thanks again.

Just for the fun of it, here’s my solution:

> <https://gist.github.com/rklemme/321803>

Note, somehow I assumed from your posting that you want to process  
folders _after_ their contained files and sub folders in the same way  
as find’s option “-depth” does it.

Kind regards

robert
