MCPcopy Create free account
hub / github.com/ClickHouse/ClickHouse / getModule

Method getModule

src/Interpreters/WasmModuleManager.cpp:220–279  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

218}
219
220std::pair<std::shared_ptr<WasmModule>, UInt256> WasmModuleManager::getModule(std::string_view module_name, FuelMode fuel_mode)
221{
222 const bool requires_fuel_specialization = engine->requiresFuelSpecialization();
223 const size_t cache_idx = requires_fuel_specialization ? fuelModeIndex(fuel_mode) : 0;
224
225 {
226 SharedLockGuard lock(modules_mutex);
227
228 auto it = modules.find(module_name);
229 if (it != modules.end())
230 {
231 if (auto module = it->second.ptrs[cache_idx].lock())
232 return {module, it->second.hash};
233 }
234 }
235
236 UniqueLock lock(modules_mutex);
237 auto it = modules.find(module_name);
238 if (it != modules.end())
239 {
240 if (auto module = it->second.ptrs[cache_idx].lock())
241 return {module, it->second.hash};
242 }
243
244 auto module_path = getFilePath(module_name);
245 if (!user_scripts_disk->existsFile(module_path))
246 throw Exception(ErrorCodes::RESOURCE_NOT_FOUND, "WebAssembly module '{}' not found", module_name);
247 if (auto res = validateModuleFile(user_scripts_disk, module_path); !res)
248 throw Exception(ErrorCodes::INCORRECT_DATA, "Cannot load WebAssembly module '{}': {}", module_name, res.error().text);
249
250 auto wasm_code = loadModuleImpl(module_name);
251 UInt256 local_hash = calculateHash(wasm_code);
252
253 if (it != modules.end())
254 {
255 if (it->second.hash && local_hash != it->second.hash)
256 throw Exception(
257 ErrorCodes::INCORRECT_DATA,
258 "Hash mismatch for WebAssembly module '{}', expected {}, got {}",
259 module_name,
260 hashToHex(it->second.hash),
261 hashToHex(local_hash));
262 }
263
264 std::shared_ptr<WasmModule> local_module = engine->compileModule(module_name, wasm_code, fuel_mode);
265 linkHostFunctions(*local_module);
266
267 if (it == modules.end())
268 {
269 auto [inserted_it, _] = modules.emplace(String(module_name), ModuleRef{{}, local_hash});
270 it = inserted_it;
271 }
272 else if (!it->second.hash)
273 {
274 it->second.hash = local_hash;
275 }
276
277 it->second.ptrs[cache_idx] = local_module;

Callers 2

compileComparatorMethod · 0.45
compileComparatorMethod · 0.45

Calls 15

fuelModeIndexFunction · 0.85
validateModuleFileFunction · 0.85
calculateHashFunction · 0.85
hashToHexFunction · 0.85
linkHostFunctionsFunction · 0.85
ExceptionClass · 0.70
StringClass · 0.50
findMethod · 0.45
endMethod · 0.45
lockMethod · 0.45
existsFileMethod · 0.45

Tested by

no test coverage detected