#pragma once /// @file cloud_native.h 云原生协同设计基础设施 /// @ingroup cloud #include #include #include #include #include #include #include #include #include #include #include namespace vde::cloud { // ═══════════════════════════════════════════════════════ // 基础数据结构 // ═══════════════════════════════════════════════════════ /// 操作类型枚举 enum class OpType : uint8_t { INSERT, DELETE, MODIFY, MOVE, ROTATE, SCALE }; /// 增量操作单位(DeltaSync 同步单元) struct DeltaOp { OpType type; uint64_t entity_id; // 面/边/体 ID uint64_t timestamp; // UTC 微秒时间戳 std::vector data; // 序列化增量数据 }; /// 模型变更集(一次同步的增量集合) struct ChangeSet { uint64_t base_version; // 基于哪个版本 uint64_t new_version; // 新的版本号 std::string user_id; std::vector ops; std::chrono::system_clock::time_point t; }; // ═══════════════════════════════════════════════════════ // OT (Operation Transform) // ═══════════════════════════════════════════════════════ /** * @brief 操作变换 —— 实时协作冲突解决引擎 * * 实现 OT 算法核心:对并发的操作对进行变换, * 确保多用户编辑最终一致。 * * @ingroup cloud */ class OperationalTransform { public: OperationalTransform() = default; /// 变换 op1 使其与 op2 兼容(op1' = OT(op1, op2)) DeltaOp transform(const DeltaOp& op1, const DeltaOp& op2) const; /// 组合变换:将整个 changeset 变换到另一个 changeset 之后 ChangeSet transform_set(const ChangeSet& cs1, const ChangeSet& cs2) const; /// 检查两个操作是否冲突 [[nodiscard]] bool conflicts(const DeltaOp& a, const DeltaOp& b) const; /// 合并两个并发变更集 ChangeSet merge(const ChangeSet& cs1, const ChangeSet& cs2) const; private: bool same_entity(const DeltaOp& a, const DeltaOp& b) const; DeltaOp rebase_insert(const DeltaOp& op, const DeltaOp& against) const; DeltaOp rebase_delete(const DeltaOp& op, const DeltaOp& against) const; DeltaOp rebase_modify(const DeltaOp& op, const DeltaOp& against) const; }; // ═══════════════════════════════════════════════════════ // DeltaSync // ═══════════════════════════════════════════════════════ /** * @brief 增量同步引擎 * * 仅同步被修改的面/边/体,而非完整模型。 * 基于版本链的变更日志实现高效增量传输。 * * @ingroup cloud */ class DeltaSync { public: DeltaSync() = default; /// 生成相对于 base_version 的增量 ChangeSet diff(uint64_t base_version) const; /// 应用远程增量 bool apply_remote(const ChangeSet& cs); /// 提交本地变更 void commit_local(const std::vector& ops, const std::string& user_id); /// 获取当前版本号 [[nodiscard]] uint64_t version() const { return current_version_; } /// 获取所有已追踪的 entity_id [[nodiscard]] std::vector dirty_entities() const; /// 检查指定实体是否已修改 [[nodiscard]] bool is_dirty(uint64_t entity_id) const; /// 回滚到指定版本 bool rollback(uint64_t target_version); private: uint64_t current_version_ = 0; std::vector change_log_; std::set dirty_set_; std::map entity_version_; // entity_id → last changed version mutable std::shared_mutex mutex_; }; // ═══════════════════════════════════════════════════════ // CloudSession // ═══════════════════════════════════════════════════════ /// 用户信息 struct UserInfo { std::string user_id; std::string display_name; uint32_t color = 0xFF0000; // 协同光标颜色 bool online = false; }; /** * @brief 多用户协作会话 * * 管理多个用户的实时协同编辑会话。 * 集成 OT 冲突解决与 DeltaSync 增量同步。 * * @ingroup cloud */ class CloudSession { public: explicit CloudSession(std::string session_id); ~CloudSession(); /// 加入会话 bool join(const UserInfo& user); /// 离开会话 bool leave(const std::string& user_id); /// 提交操作(经 OT 变换后合并) bool submit_operation(const std::string& user_id, const std::vector& ops); /// 拉取当前用户未接收的变更 ChangeSet pull_changes(const std::string& user_id); /// 获取会话基本信息 [[nodiscard]] const std::string& session_id() const { return session_id_; } /// 在线用户列表 [[nodiscard]] std::vector online_users() const; /// 当前模型版本 [[nodiscard]] uint64_t version() const; /// 获取用户的版本游标(该用户已拉到哪个版本) [[nodiscard]] uint64_t user_cursor(const std::string& user_id) const; private: std::string session_id_; OperationalTransform ot_; DeltaSync sync_; std::map users_; // user_id → UserInfo std::map user_cursor_; // user_id → 已拉取版本 std::map pending_seq_; // user_id → 待合并 seq mutable std::shared_mutex mutex_; }; // ═══════════════════════════════════════════════════════ // Serverless Function (AWS Lambda / Cloud Functions) // ═══════════════════════════════════════════════════════ /// 函数调用请求 struct FunctionRequest { std::string function_name; std::vector payload; std::map headers; std::chrono::milliseconds timeout{30000}; }; /// 函数调用响应 struct FunctionResponse { int status_code = 200; std::vector body; std::map headers; std::chrono::milliseconds duration{0}; bool ok = false; }; /** * @brief Serverless 函数部署与调用接口 * * 支持 AWS Lambda / Google Cloud Functions 等无服务器平台。 * 用于部署设计验证规则、模型转换管道等无状态函数。 * * @ingroup cloud */ class ServerlessFunction { public: ServerlessFunction() = default; virtual ~ServerlessFunction() = default; /// 调用远程函数 virtual FunctionResponse invoke(const FunctionRequest& req); /// 异步调用(fire-and-forget) void invoke_async(const FunctionRequest& req); /// 部署函数到云端 virtual bool deploy(const std::string& function_name, const std::string& code_path, const std::string& runtime = "provided.al2023"); /// 更新函数配置 virtual bool update_config(const std::string& function_name, int memory_mb = 256, std::chrono::seconds timeout = std::chrono::seconds(30)); /// 列出已部署的函数 virtual std::vector list_functions() const; /// 删除函数 virtual bool remove_function(const std::string& function_name); /// 设置云端端点 void set_endpoint(const std::string& url) { endpoint_ = url; } protected: std::string endpoint_; std::string region_ = "us-east-1"; std::string credentials_path_; }; // ═══════════════════════════════════════════════════════ // ObjectStorage (S3 / MinIO) // ═══════════════════════════════════════════════════════ /// 对象元信息 struct ObjectMeta { std::string key; uint64_t size_bytes = 0; std::string content_type; std::string etag; std::chrono::system_clock::time_point last_modified; }; /** * @brief 对象存储 —— S3/MinIO 模型仓库 * * 管理 3D 模型文件(STEP/IGES/glTF/OBJ 等)的云端存储。 * 支持分片上传、断点续传、版本管理和预签名 URL。 * * @ingroup cloud */ class ObjectStorage { public: ObjectStorage() = default; virtual ~ObjectStorage() = default; /// 配置存储后端 void configure(const std::string& endpoint, const std::string& bucket, const std::string& access_key, const std::string& secret_key); /// 上传对象 virtual bool put_object(const std::string& key, const std::vector& data, const std::string& content_type = "application/octet-stream"); /// 分片上传(大文件自动分片) bool multipart_upload(const std::string& key, const std::vector& data, size_t part_size_mb = 5); /// 下载对象 virtual std::optional> get_object(const std::string& key); /// 检查对象是否存在 virtual bool object_exists(const std::string& key) const; /// 列出前缀下的对象 virtual std::vector list_objects(const std::string& prefix = "", int max_keys = 1000) const; /// 删除对象 virtual bool delete_object(const std::string& key); /// 生成预签名下载 URL virtual std::string presigned_get_url(const std::string& key, std::chrono::seconds expires); /// 生成预签名上传 URL virtual std::string presigned_put_url(const std::string& key, std::chrono::seconds expires); /// 复制对象 bool copy_object(const std::string& src_key, const std::string& dst_key); /// 获取对象元信息 virtual std::optional head_object(const std::string& key); /// 模型文件快捷存储(.stp / .igs / .gltf 等) bool store_model(const std::string& model_name, const std::vector& model_data, const std::string& format); /// 模型文件快捷下载 std::optional> load_model(const std::string& model_name, const std::string& format); /// 删除模型 bool delete_model(const std::string& model_name, const std::string& format); protected: std::string endpoint_; std::string bucket_; std::string access_key_; std::string secret_key_; std::string region_ = "us-east-1"; bool use_ssl_ = true; }; } // namespace vde::cloud