MCPcopy Create free account
hub / github.com/ddd/gpb / sample_file

Function sample_file

src/utils/estimation.rs:81–135  ·  view source on GitHub ↗

Sample a file to get an estimate of matching lines

(
    input_file_path: &str, 
    sample_size: u64,
    prefix: &str,
    suffix: &str,
    infix: Option<&str>
)

Source from the content-addressed store, hash-verified

79
80/// Sample a file to get an estimate of matching lines
81async fn sample_file(
82 input_file_path: &str,
83 sample_size: u64,
84 prefix: &str,
85 suffix: &str,
86 infix: Option<&str>
87) -> Result<(u64, u64, u64), Error> {
88 let sample_file = File::open(input_file_path).await?;
89 let sample_reader = BufReader::new(sample_file);
90 let mut sample_lines = sample_reader.lines();
91
92 let mut sample_count = 0;
93 let mut sample_matched = 0;
94 let mut sample_bytes_read = 0;
95 let check_suffix = !suffix.is_empty();
96 let check_prefix = !prefix.is_empty();
97 let check_infix = infix.is_some();
98
99 // Read the sample
100 while let Some(line) = sample_lines.next_line().await? {
101 if sample_count >= sample_size {
102 break;
103 }
104
105 sample_bytes_read += line.len() as u64 + 1; // +1 for newline
106
107 if !line.trim().is_empty() {
108 sample_count += 1;
109
110 let phone = line.trim();
111 let suffix_match = !check_suffix || phone.ends_with(suffix);
112 let prefix_match = !check_prefix || phone.starts_with(prefix);
113
114 // Check infix if needed
115 let infix_match = if check_infix {
116 let infix_val = infix.unwrap();
117 if phone.len() >= 6 {
118 // Extract the infix (6th and 5th characters from the end)
119 let potential_infix = &phone[phone.len() - 6..phone.len() - 4];
120 potential_infix == infix_val
121 } else {
122 false // Phone number too short for infix
123 }
124 } else {
125 true // No infix check needed
126 };
127
128 if suffix_match && prefix_match && infix_match {
129 sample_matched += 1;
130 }
131 }
132 }
133
134 Ok((sample_matched, sample_count, sample_bytes_read))
135}
136
137/// Estimate workload for full scan (number generation) mode
138fn estimate_fullscan_workload(

Callers 1

estimate_file_workloadFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected