Recently, I am working on a project which will have thousands of small files to manage. The whole system will randomly read/write those files. And parts of them are downloaded from networks at the moment that it is needed. For performance and system limitations, I could only keep 10~20 file descriptors/handles. So I decided to create a large single file as my 'disk'. And then dynamic read and write files into it.
Here is my design:
- A file header for serialization of meta-info
- An index to save offset of every little file
- A bunch of 'pre-allocate segments' to save files
- Support multi-thread accessing
Finally, It looks like a virtual file system. But I'm not sure is it safe to read/write the same file at the same time on Android and iOS. And I'm totally no idea how to manage the fragment of my 'pre-allocate segments'.
Is there any best practice or frameworks that I can reference? Am I on the right way?