0

I have a text file which follows exactly the same type:

**Unique-Key_1**
Value1
Value2

**Unique-Key_2**
Value1

**Unique_Key_3**
Value1

(Please note that the keys and values are not fixed.They might grow in time but one thing is confirmed: It will always follow this structure)

My program wants to search for a key and then retrive all values under it.

I have some viable solutions for this.

1) Should I use a dictionary type and then when my app loads read all keys and values and populate that list?

2) Can I use file access/search methods during run-time and based upon a key , search it and then retrieve values ?

3) Which is the optimum method or is there any methods or any other ways to achieve the same ?

2 Answers2

4

Things to consider:

  1. Does the application have time to load in and parse the file before data is searched? If so, consider parsing the file into a Dictionary. If not, parse the file as needed.
  2. Will the file be very large? If so, parsing it into a Dictionary may take up too much memory. Consider an LRU Cache like Object cache for C#.
  3. Are the keys in the file sorted? If so, a binary search on the file may be possible to speed up the file parses.
  4. Does the data change frequently? If so, parsing the file would guarantee the data is up to date at the cost of slower data access.

Another alternative is to load the values into database tables or a key/value store. This allows the data to be updated piece meal or completely with reasonable access speed if needed at the expense of maintaining and running the database.

Community
  • 1
  • 1
akton
  • 14,148
  • 3
  • 43
  • 47
0

Okay, if the file isn't that large I would recommend the Dictionary approach because it's going to make accessing it easier and more efficient at runtime. However, if the file is too large to hold in memory you can use the algorithm provided in this answer to search it.

Community
  • 1
  • 1
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232