Input:
- potentially large file with text lines
- lines count is unknown, naive assumption gives range maybe more than 100K lines, but less than 400K
- it's unknown if lines are sorted or not
- it's fine to keep in memory at most 10.000 lines
Limitations:
- read lines one by one, there is no possibility to read file at once into memory.
- read file once and sample lines at the same time, it's way too long to read all lines first to get lines count.
Goal:
Sample the minimum count of lines from file to get maximum variance
Example: Here is the file:
1,Tom,22,3000000
3,Bob,34,4000000
...
10,Tim,18,500000.05
Lines are ordered, I expect to sample
1,Tom,22,3000000
10,Tim,18,500000.05
Solution:
Does it make sense?
- read line by line
- keep tail buffer for the recent 1000 lines
- keep sample buffer for 9000 lines
- add every line with index XXX (what is the right formula here?) to sample buffer
- union sample buffer and tail buffer once end of file reached.
that it sounds like the intent is to preserve the input order- seems like poor explanation. It' a caveat, if lines are ordered, than I can't just take first records for sampling. Sample will be skewed since first lines will be similar due to sorting. I don't know in advance if lines are sorted or not. – Capacytron May 31 '20 at 09:27