infiniloom_engine/embedding/
limits.rs1use serde::{Deserialize, Serialize};
10
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
13pub struct ResourceLimits {
14 pub max_recursion_depth: u32,
17
18 pub max_file_size: u64,
21
22 pub max_total_chunks: usize,
25
26 pub max_files: usize,
29
30 pub max_concurrent_loads: usize,
33
34 pub max_line_length: usize,
37
38 pub max_chunk_size: usize,
41}
42
43impl Default for ResourceLimits {
44 fn default() -> Self {
45 Self {
46 max_recursion_depth: 500,
47 max_file_size: 10 * 1024 * 1024, max_total_chunks: 1_000_000, max_files: 500_000, max_concurrent_loads: 32, max_line_length: 10_000, max_chunk_size: 1024 * 1024, }
54 }
55}
56
57impl ResourceLimits {
58 pub fn relaxed() -> Self {
60 Self {
61 max_recursion_depth: 1000,
62 max_file_size: 50 * 1024 * 1024, max_total_chunks: 10_000_000, max_files: 1_000_000, max_concurrent_loads: 64, max_line_length: 100_000, max_chunk_size: 5 * 1024 * 1024, }
69 }
70
71 pub fn strict() -> Self {
73 Self {
74 max_recursion_depth: 100,
75 max_file_size: 1024 * 1024, max_total_chunks: 100_000, max_files: 50_000, max_concurrent_loads: 8, max_line_length: 1000, max_chunk_size: 100 * 1024, }
82 }
83
84 pub fn minimal() -> Self {
86 Self {
87 max_recursion_depth: 50,
88 max_file_size: 100 * 1024, max_total_chunks: 1000, max_files: 100, max_concurrent_loads: 4, max_line_length: 500, max_chunk_size: 10 * 1024, }
95 }
96
97 #[inline]
99 pub fn check_file_size(&self, size: u64) -> bool {
100 size <= self.max_file_size
101 }
102
103 #[inline]
105 pub fn check_recursion_depth(&self, depth: u32) -> bool {
106 depth <= self.max_recursion_depth
107 }
108
109 #[inline]
111 pub fn check_chunk_count(&self, count: usize) -> bool {
112 count <= self.max_total_chunks
113 }
114
115 #[inline]
117 pub fn check_file_count(&self, count: usize) -> bool {
118 count <= self.max_files
119 }
120
121 #[inline]
123 pub fn check_line_length(&self, length: usize) -> bool {
124 length <= self.max_line_length
125 }
126
127 #[inline]
129 pub fn check_chunk_size(&self, size: usize) -> bool {
130 size <= self.max_chunk_size
131 }
132
133 pub fn with_max_recursion_depth(mut self, depth: u32) -> Self {
135 self.max_recursion_depth = depth;
136 self
137 }
138
139 pub fn with_max_file_size(mut self, size: u64) -> Self {
141 self.max_file_size = size;
142 self
143 }
144
145 pub fn with_max_total_chunks(mut self, count: usize) -> Self {
147 self.max_total_chunks = count;
148 self
149 }
150
151 pub fn with_max_files(mut self, count: usize) -> Self {
153 self.max_files = count;
154 self
155 }
156
157 pub fn with_max_concurrent_loads(mut self, count: usize) -> Self {
159 self.max_concurrent_loads = count;
160 self
161 }
162
163 pub fn with_max_line_length(mut self, length: usize) -> Self {
165 self.max_line_length = length;
166 self
167 }
168
169 pub fn with_max_chunk_size(mut self, size: usize) -> Self {
171 self.max_chunk_size = size;
172 self
173 }
174}
175
176#[cfg(test)]
177mod tests {
178 use super::*;
179
180 #[test]
181 fn test_default_limits() {
182 let limits = ResourceLimits::default();
183 assert_eq!(limits.max_recursion_depth, 500);
184 assert_eq!(limits.max_file_size, 10 * 1024 * 1024);
185 assert_eq!(limits.max_total_chunks, 1_000_000);
186 assert_eq!(limits.max_files, 500_000);
187 }
188
189 #[test]
190 fn test_strict_limits() {
191 let limits = ResourceLimits::strict();
192 assert_eq!(limits.max_recursion_depth, 100);
193 assert_eq!(limits.max_file_size, 1024 * 1024);
194 assert!(limits.max_total_chunks < ResourceLimits::default().max_total_chunks);
195 }
196
197 #[test]
198 fn test_relaxed_limits() {
199 let limits = ResourceLimits::relaxed();
200 assert!(limits.max_recursion_depth > ResourceLimits::default().max_recursion_depth);
201 assert!(limits.max_file_size > ResourceLimits::default().max_file_size);
202 }
203
204 #[test]
205 fn test_check_file_size() {
206 let limits = ResourceLimits::default();
207 assert!(limits.check_file_size(1024)); assert!(limits.check_file_size(10 * 1024 * 1024)); assert!(!limits.check_file_size(11 * 1024 * 1024)); }
211
212 #[test]
213 fn test_check_recursion_depth() {
214 let limits = ResourceLimits::default();
215 assert!(limits.check_recursion_depth(100));
216 assert!(limits.check_recursion_depth(500)); assert!(!limits.check_recursion_depth(501)); }
219
220 #[test]
221 fn test_builder_pattern() {
222 let limits = ResourceLimits::default()
223 .with_max_file_size(5 * 1024 * 1024)
224 .with_max_recursion_depth(200)
225 .with_max_total_chunks(50_000);
226
227 assert_eq!(limits.max_file_size, 5 * 1024 * 1024);
228 assert_eq!(limits.max_recursion_depth, 200);
229 assert_eq!(limits.max_total_chunks, 50_000);
230 }
231
232 #[test]
233 fn test_serialization() {
234 let limits = ResourceLimits::default();
235 let json = serde_json::to_string(&limits).unwrap();
236 let deserialized: ResourceLimits = serde_json::from_str(&json).unwrap();
237 assert_eq!(limits, deserialized);
238 }
239
240 #[test]
241 fn test_minimal_limits() {
242 let limits = ResourceLimits::minimal();
243 assert!(limits.max_files <= 100);
244 assert!(limits.max_total_chunks <= 1000);
245 }
246}