Adding convenience cache entries getter and iterator; Simplifying and refactoring code
Publish PHP Package / docker (push) Successful in 6s Details

This commit is contained in:
Pavel Shevaev 2025-04-22 20:47:49 +03:00
parent 04fd79645c
commit 71acf66d82
2 changed files with 74 additions and 14 deletions

View File

@ -95,6 +95,13 @@ class ConfigCache
throw new Exception("Failed to find entry by strid '$strid'"); throw new Exception("Failed to find entry by strid '$strid'");
return $ce; return $ce;
} }
function update(ConfigCacheEntry $ce)
{
$this->by_path[$ce->file] = $ce;
$this->by_id[$ce->id] = $ce;
$this->by_alias[$ce->strid] = $ce;
}
} }
/** /**
@ -350,7 +357,6 @@ class ConfigCacheUpdateParams
public ConfigGlobals $globals; public ConfigGlobals $globals;
public ConfigDirFiles $affected_files; public ConfigDirFiles $affected_files;
public bool $verbose = false; public bool $verbose = false;
public bool $check_junk = true;
public int $max_errors_num = 15; public int $max_errors_num = 15;
public bool $return_affected_entries = false; public bool $return_affected_entries = false;
@ -445,7 +451,7 @@ function _config_merge_update_results(ConfigUpdateResult $result, array $results
$result->fast_jsons = $total_fast_jsons; $result->fast_jsons = $total_fast_jsons;
} }
function _config_update_cache_entry(ConfigCacheUpdateParams $params, $base_dir, string $file, function _config_update_cache_entry(ConfigGlobals $globals, $base_dir, string $file, string $cache_file,
?int &$parser_type = null) : ConfigCacheEntry ?int &$parser_type = null) : ConfigCacheEntry
{ {
list($conf_id, $conf_strid) = config_ensure_header($base_dir, $file); list($conf_id, $conf_strid) = config_ensure_header($base_dir, $file);
@ -457,7 +463,7 @@ function _config_update_cache_entry(ConfigCacheUpdateParams $params, $base_dir,
$GLOBALS['CONFIG_CURRENT_PROTO_ID'] = $conf_id; $GLOBALS['CONFIG_CURRENT_PROTO_ID'] = $conf_id;
$GLOBALS['CONFIG_EXTRAS'] = ConfigCacheEntryExtras::create(); $GLOBALS['CONFIG_EXTRAS'] = ConfigCacheEntryExtras::create();
$pres = config_parse($params->globals->base_dirs, $file); $pres = config_parse($globals->base_dirs, $file);
if($pres->error !== 0) if($pres->error !== 0)
throw new Exception("Parse error({$pres->error}):\n" . $pres->error_descr); throw new Exception("Parse error({$pres->error}):\n" . $pres->error_descr);
@ -467,11 +473,9 @@ function _config_update_cache_entry(ConfigCacheUpdateParams $params, $base_dir,
$parsed_arr['id'] = $conf_id; $parsed_arr['id'] = $conf_id;
$parsed_arr['strid'] = $conf_strid; $parsed_arr['strid'] = $conf_strid;
list($class, $class_id, $normalized_data) list($class, $class_id, $normalized_data)
= config_apply_class_normalization($parsed_arr, $params->check_junk); = config_apply_class_normalization($parsed_arr, check_junk: true);
$cache_payload_file = config_get_cache_payload_path($params->globals, $file); $cache_payload_file = config_get_cache_payload_path($globals, $file);
$cache_file = config_get_cache_path($params->globals, $file);
$cache_entry = new ConfigCacheEntry(); $cache_entry = new ConfigCacheEntry();
$cache_entry->id = $conf_id; $cache_entry->id = $conf_id;
@ -489,8 +493,8 @@ function _config_update_cache_entry(ConfigCacheUpdateParams $params, $base_dir,
ensure_write($cache_file, ConfigCacheEntry::serialize($cache_entry)); ensure_write($cache_file, ConfigCacheEntry::serialize($cache_entry));
ensure_write($cache_payload_file, config_msgpack_pack($normalized_data)); ensure_write($cache_payload_file, config_msgpack_pack($normalized_data));
ensure_write(config_get_cache_id_path($params->globals, $conf_id), $cache_entry->file); ensure_write(config_get_cache_id_path($globals, $conf_id), $cache_entry->file);
ensure_write(config_get_cache_strid_path($params->globals, $conf_strid), $cache_entry->file); ensure_write(config_get_cache_strid_path($globals, $conf_strid), $cache_entry->file);
return $cache_entry; return $cache_entry;
} }
@ -541,12 +545,12 @@ function _config_update_worker_func(ConfigCacheUpdateParams $params, array $job,
$cache_file = config_get_cache_path($params->globals, $file); $cache_file = config_get_cache_path($params->globals, $file);
$parser_type = null; $parser_type = null;
$entry = _config_update_cache_entry($params, $base_dir, $file, $parser_type); $entry = _config_update_cache_entry($params->globals, $base_dir, $file, $cache_file, $parser_type);
$results[] = [ $results[] = [
'base_dir' => $base_dir, 'base_dir' => $base_dir,
'file' => $file, 'file' => $file,
'cache_file' => $entry->cache_file, 'cache_file' => $cache_file,
'parser_type' => $parser_type, 'parser_type' => $parser_type,
'error' => null 'error' => null
]; ];

View File

@ -16,13 +16,15 @@ class ConfigGlobals
public readonly int $workers_num; public readonly int $workers_num;
public readonly string $base_class; public readonly string $base_class;
public readonly string $build_dir; public readonly string $build_dir;
public readonly mixed $files_overrider;
function __construct( function __construct(
array $base_dirs, array $base_dirs,
string $build_dir, string $build_dir,
?string $worker_init_fn = null, ?string $worker_init_fn = null,
int $workers_num = 1, int $workers_num = 1,
string $base_class = '\ConfBase' string $base_class = '\ConfBase',
callable $files_overrider = null
) )
{ {
$this->base_dirs = array_map(fn($path) => normalize_path($path), $base_dirs); $this->base_dirs = array_map(fn($path) => normalize_path($path), $base_dirs);
@ -30,6 +32,7 @@ class ConfigGlobals
$this->worker_init_fn = $worker_init_fn; $this->worker_init_fn = $worker_init_fn;
$this->workers_num = $workers_num; $this->workers_num = $workers_num;
$this->base_class = $base_class; $this->base_class = $base_class;
$this->files_overrider = $files_overrider;
} }
function initWorker(bool $is_master_proc) function initWorker(bool $is_master_proc)
@ -44,6 +47,13 @@ class ConfigGlobals
} }
} }
enum ConfigGetMode : int
{
case ForceUpdate = 1;
case Auto = 2;
case CacheOnly = 3;
}
enum ConfigUpdateMode : int enum ConfigUpdateMode : int
{ {
case Force = 1; case Force = 1;
@ -141,8 +151,14 @@ class ConfigManager
return $this->globals; return $this->globals;
} }
function getCache() : ConfigCache function getCache(ConfigGetMode $mode = ConfigGetMode::CacheOnly) : ConfigCache
{ {
if($mode != ConfigGetMode::CacheOnly)
{
foreach($this->getIterator($mode) as $ce)
{}
}
return $this->cache; return $this->cache;
} }
@ -165,6 +181,43 @@ class ConfigManager
return $this->file_map; return $this->file_map;
} }
function getByPath(string $file, ConfigGetMode $mode = ConfigGetMode::Auto, bool $normalize_path = true) : ConfigCacheEntry
{
if($normalize_path)
$file = normalize_path($file);
if($mode != ConfigGetMode::CacheOnly)
{
$cache_file = config_get_cache_path($this->globals, $file);
if($mode == ConfigGetMode::ForceUpdate || need_to_regen($cache_file, [$file]))
{
$base_dir = config_map_base_dir($this->globals->base_dirs, $file);
$parser_type = null;
$ce = _config_update_cache_entry($this->globals, $base_dir, $file, $cache_file, $parser_type);
$this->cache->update($ce);
}
}
return $this->cache->getOrLoadByPath($file);
}
function getIterator(ConfigGetMode $mode = ConfigGetMode::Auto, mixed $files = null, bool $normalize_path = false) : \Generator
{
if($files === null)
{
$files = $this->scanFiles();
$normalize_path = false;
}
$cache_entries = [];
foreach($files as $file)
{
$ce = $this->getByPath($file, $mode, normalize_path: $normalize_path);
yield $ce;
}
}
function update(ConfigUpdateRequest $req) : ConfigUpdateResult function update(ConfigUpdateRequest $req) : ConfigUpdateResult
{ {
config_log("Updating configs, mode '{$req->mode->name}'..."); config_log("Updating configs, mode '{$req->mode->name}'...");
@ -357,7 +410,10 @@ class ConfigManager
function scanFiles(string $extension = '.conf.js', bool $verbose = false) : ConfigDirFiles function scanFiles(string $extension = '.conf.js', bool $verbose = false) : ConfigDirFiles
{ {
return config_scan_files($this->globals->base_dirs, $extension, $verbose); $res = config_scan_files($this->globals->base_dirs, $extension, $verbose);
if($this->globals->files_overrider != null)
$res = call_user_func($this->globals->files_overrider, $res);
return $res;
} }
private function _getMapPath() : string private function _getMapPath() : string