11#include "foundation/index_policy.h"
22
3+ #include <stddef.h>
34#include <stdio.h>
45#include <string.h>
56
6- static const char * const INDEX_POLICY_KEYS [] = {
7- CBM_INDEX_CONFIG_MAX_FILES ,
8- CBM_INDEX_CONFIG_MAX_SOURCE_MB ,
7+ typedef struct {
8+ const char * key ;
9+ size_t field_offset ;
10+ uint64_t minimum ;
11+ uint64_t maximum ;
12+ uint64_t multiplier ;
13+ } index_policy_metadata_t ;
14+
15+ static const index_policy_metadata_t INDEX_POLICY_METADATA [] = {
16+ {CBM_INDEX_CONFIG_MAX_FILES , offsetof(cbm_index_resource_policy_t , max_files ), 1 ,
17+ CBM_INDEX_MAX_FILES_VALUE , 1 },
18+ {CBM_INDEX_CONFIG_MAX_SOURCE_MB , offsetof(cbm_index_resource_policy_t , max_source_bytes ), 1 ,
19+ CBM_INDEX_MAX_SOURCE_MB_VALUE , CBM_INDEX_MIB_BYTES },
20+ {CBM_INDEX_CONFIG_MAX_RSS_MB , offsetof(cbm_index_resource_policy_t , max_rss_bytes ),
21+ CBM_INDEX_MIN_RSS_MB_VALUE , CBM_INDEX_MAX_RSS_MB_VALUE , CBM_INDEX_MIB_BYTES },
22+ {CBM_INDEX_CONFIG_MAX_DURATION_SECONDS , offsetof(cbm_index_resource_policy_t , max_duration_ms ),
23+ 1 , CBM_INDEX_MAX_DURATION_SECONDS_VALUE , UINT64_C (1000 )},
924};
1025
11- static void set_error (char * error , size_t error_size , const char * key , uint64_t maximum ) {
26+ static void set_error (char * error , size_t error_size , const char * key , uint64_t minimum ,
27+ uint64_t maximum ) {
1228 if (error && error_size > 0 ) {
13- (void )snprintf (error , error_size , "%s must be off or an integer from 1 to %llu" , key ,
14- (unsigned long long )maximum );
29+ (void )snprintf (error , error_size , "%s must be off or an integer from %llu to %llu" , key ,
30+ (unsigned long long )minimum , ( unsigned long long ) maximum );
1531 }
1632}
1733
18- static bool parse_bounded_uint64 (const char * value , uint64_t maximum , uint64_t * parsed ) {
34+ static bool parse_bounded_uint64 (const char * value , uint64_t minimum , uint64_t maximum ,
35+ uint64_t * parsed ) {
1936 if (!value || !value [0 ] || !parsed ) {
2037 return false;
2138 }
@@ -30,7 +47,7 @@ static bool parse_bounded_uint64(const char *value, uint64_t maximum, uint64_t *
3047 }
3148 result = result * 10U + digit ;
3249 }
33- if (result == 0 ) {
50+ if (result < minimum ) {
3451 return false;
3552 }
3653 * parsed = result ;
@@ -44,20 +61,39 @@ void cbm_index_policy_init(cbm_index_resource_policy_t *policy) {
4461}
4562
4663bool cbm_index_policy_enabled (const cbm_index_resource_policy_t * policy ) {
64+ if (!policy ) {
65+ return false;
66+ }
67+ for (size_t index = 0 ; index < cbm_index_policy_key_count (); index ++ ) {
68+ const cbm_index_limit_u64_t * limit =
69+ (const cbm_index_limit_u64_t * )((const unsigned char * )policy +
70+ INDEX_POLICY_METADATA [index ].field_offset );
71+ if (limit -> enabled ) {
72+ return true;
73+ }
74+ }
75+ return false;
76+ }
77+
78+ bool cbm_index_policy_discovery_enabled (const cbm_index_resource_policy_t * policy ) {
4779 return policy && (policy -> max_files .enabled || policy -> max_source_bytes .enabled );
4880}
4981
82+ bool cbm_index_policy_worker_enabled (const cbm_index_resource_policy_t * policy ) {
83+ return policy && (policy -> max_rss_bytes .enabled || policy -> max_duration_ms .enabled );
84+ }
85+
5086size_t cbm_index_policy_key_count (void ) {
51- return sizeof (INDEX_POLICY_KEYS ) / sizeof (INDEX_POLICY_KEYS [0 ]);
87+ return sizeof (INDEX_POLICY_METADATA ) / sizeof (INDEX_POLICY_METADATA [0 ]);
5288}
5389
5490const char * cbm_index_policy_key_at (size_t index ) {
55- return index < cbm_index_policy_key_count () ? INDEX_POLICY_KEYS [index ] : NULL ;
91+ return index < cbm_index_policy_key_count () ? INDEX_POLICY_METADATA [index ]. key : NULL ;
5692}
5793
5894const char * cbm_index_policy_default_value (const char * key ) {
5995 for (size_t index = 0 ; index < cbm_index_policy_key_count (); index ++ ) {
60- if (key && strcmp (key , INDEX_POLICY_KEYS [index ]) == 0 ) {
96+ if (key && strcmp (key , INDEX_POLICY_METADATA [index ]. key ) == 0 ) {
6197 return "off" ;
6298 }
6399 }
@@ -70,34 +106,33 @@ bool cbm_index_policy_set(cbm_index_resource_policy_t *policy, const char *key,
70106 error [0 ] = '\0' ;
71107 }
72108 if (!policy || !key || !value ) {
73- set_error (error , error_size , key ? key : "index resource limit" , 0 );
109+ set_error (error , error_size , key ? key : "index resource limit" , 0 , 0 );
74110 return false;
75111 }
76112
77- cbm_index_limit_u64_t * target = NULL ;
78- uint64_t maximum = 0 ;
79- uint64_t multiplier = 1 ;
80- if (strcmp (key , CBM_INDEX_CONFIG_MAX_FILES ) == 0 ) {
81- target = & policy -> max_files ;
82- maximum = CBM_INDEX_MAX_FILES_VALUE ;
83- } else if (strcmp (key , CBM_INDEX_CONFIG_MAX_SOURCE_MB ) == 0 ) {
84- target = & policy -> max_source_bytes ;
85- maximum = CBM_INDEX_MAX_SOURCE_MB_VALUE ;
86- multiplier = CBM_INDEX_MIB_BYTES ;
87- } else {
88- set_error (error , error_size , key , 0 );
113+ const index_policy_metadata_t * metadata = NULL ;
114+ for (size_t index = 0 ; index < cbm_index_policy_key_count (); index ++ ) {
115+ if (strcmp (key , INDEX_POLICY_METADATA [index ].key ) == 0 ) {
116+ metadata = & INDEX_POLICY_METADATA [index ];
117+ break ;
118+ }
119+ }
120+ if (!metadata ) {
121+ set_error (error , error_size , key , 0 , 0 );
89122 return false;
90123 }
124+ cbm_index_limit_u64_t * target =
125+ (cbm_index_limit_u64_t * )((unsigned char * )policy + metadata -> field_offset );
91126
92127 cbm_index_limit_u64_t candidate = {0 };
93128 if (strcmp (value , "off" ) != 0 ) {
94129 uint64_t parsed = 0 ;
95- if (!parse_bounded_uint64 (value , maximum , & parsed )) {
96- set_error (error , error_size , key , maximum );
130+ if (!parse_bounded_uint64 (value , metadata -> minimum , metadata -> maximum , & parsed )) {
131+ set_error (error , error_size , key , metadata -> minimum , metadata -> maximum );
97132 return false;
98133 }
99134 candidate .enabled = true;
100- candidate .value = parsed * multiplier ;
135+ candidate .value = parsed * metadata -> multiplier ;
101136 }
102137 * target = candidate ;
103138 return true;
@@ -108,18 +143,21 @@ bool cbm_index_policy_format(const cbm_index_resource_policy_t *policy, const ch
108143 if (!policy || !key || !out || out_size == 0 ) {
109144 return false;
110145 }
111- const cbm_index_limit_u64_t * limit = NULL ;
112- uint64_t divisor = 1 ;
113- if (strcmp (key , CBM_INDEX_CONFIG_MAX_FILES ) == 0 ) {
114- limit = & policy -> max_files ;
115- } else if ( strcmp ( key , CBM_INDEX_CONFIG_MAX_SOURCE_MB ) == 0 ) {
116- limit = & policy -> max_source_bytes ;
117- divisor = CBM_INDEX_MIB_BYTES ;
118- } else {
146+ const index_policy_metadata_t * metadata = NULL ;
147+ for ( size_t index = 0 ; index < cbm_index_policy_key_count (); index ++ ) {
148+ if (strcmp (key , INDEX_POLICY_METADATA [ index ]. key ) == 0 ) {
149+ metadata = & INDEX_POLICY_METADATA [ index ] ;
150+ break ;
151+ }
152+ }
153+ if (! metadata ) {
119154 return false;
120155 }
156+ const cbm_index_limit_u64_t * limit =
157+ (const cbm_index_limit_u64_t * )((const unsigned char * )policy + metadata -> field_offset );
121158 int length = limit -> enabled
122- ? snprintf (out , out_size , "%llu" , (unsigned long long )(limit -> value / divisor ))
159+ ? snprintf (out , out_size , "%llu" ,
160+ (unsigned long long )(limit -> value / metadata -> multiplier ))
123161 : snprintf (out , out_size , "off" );
124162 return length >= 0 && (size_t )length < out_size ;
125163}
@@ -130,14 +168,24 @@ const char *cbm_index_resource_name(cbm_index_resource_t resource) {
130168 return "files" ;
131169 case CBM_INDEX_RESOURCE_SOURCE_BYTES :
132170 return "source_bytes" ;
171+ case CBM_INDEX_RESOURCE_RSS_BYTES :
172+ return "rss_bytes" ;
173+ case CBM_INDEX_RESOURCE_DURATION_MS :
174+ return "duration_ms" ;
133175 case CBM_INDEX_RESOURCE_NONE :
134176 default :
135177 return "unknown" ;
136178 }
137179}
138180
139181const char * cbm_index_resource_unit (cbm_index_resource_t resource ) {
140- return resource == CBM_INDEX_RESOURCE_FILES ? "files" : "bytes" ;
182+ if (resource == CBM_INDEX_RESOURCE_FILES ) {
183+ return "files" ;
184+ }
185+ if (resource == CBM_INDEX_RESOURCE_DURATION_MS ) {
186+ return "milliseconds" ;
187+ }
188+ return "bytes" ;
141189}
142190
143191const char * cbm_index_resource_config_key (cbm_index_resource_t resource ) {
@@ -146,6 +194,10 @@ const char *cbm_index_resource_config_key(cbm_index_resource_t resource) {
146194 return CBM_INDEX_CONFIG_MAX_FILES ;
147195 case CBM_INDEX_RESOURCE_SOURCE_BYTES :
148196 return CBM_INDEX_CONFIG_MAX_SOURCE_MB ;
197+ case CBM_INDEX_RESOURCE_RSS_BYTES :
198+ return CBM_INDEX_CONFIG_MAX_RSS_MB ;
199+ case CBM_INDEX_RESOURCE_DURATION_MS :
200+ return CBM_INDEX_CONFIG_MAX_DURATION_SECONDS ;
149201 case CBM_INDEX_RESOURCE_NONE :
150202 default :
151203 return "index_resource_limit" ;
0 commit comments