local keys = redis.call("KEYS", "*")
local hash_count = 0

for _, key in ipairs(keys) do
    if redis.call("TYPE", key).ok == "hash" then
        hash_count = hash_count + 1
        local metadata = redis.call("HGET", key, "metadata")
        if metadata then
            local success, json = pcall(cjson.decode, metadata)
            if success and json.user_canister_id then
                redis.call("SET", json.user_canister_id, key)
            end
        end
    end
end

return hash_count
