LCOV - code coverage report
Current view: top level - include/storage - store.hpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 1 1
Test Date: 2025-12-19 03:13:09 Functions: 50.0 % 2 1

            Line data    Source code
       1              : /**
       2              :  * @file store.hpp
       3              :  * @brief 持久化存储抽象接口
       4              :  *
       5              :  * 定义订单、成交、消息、账户、持仓等数据的存储接口。
       6              :  */
       7              : 
       8              : #pragma once
       9              : 
      10              : #include <string>
      11              : #include <vector>
      12              : #include <optional>
      13              : #include <cstdint>
      14              : #include "app/model/order.hpp"
      15              : #include "app/model/account.hpp"
      16              : #include "app/model/position.hpp"
      17              : 
      18              : namespace fix40 {
      19              : 
      20              : /**
      21              :  * @brief 存储用成交记录
      22              :  */
      23              : struct StoredTrade {
      24              :     std::string tradeId;        ///< 成交编号
      25              :     std::string clOrdID;        ///< 客户订单号
      26              :     std::string symbol;         ///< 合约代码
      27              :     OrderSide side;             ///< 买卖方向
      28              :     double price;               ///< 成交价格
      29              :     int64_t quantity;           ///< 成交数量
      30              :     int64_t timestamp;          ///< 成交时间戳 (毫秒)
      31              :     std::string counterpartyOrderId; ///< 对手方订单号
      32              : };
      33              : 
      34              : /**
      35              :  * @brief 会话状态 (用于断线恢复)
      36              :  */
      37              : struct SessionState {
      38              :     std::string senderCompID;
      39              :     std::string targetCompID;
      40              :     int sendSeqNum;             ///< 发送序列号
      41              :     int recvSeqNum;             ///< 接收序列号
      42              :     int64_t lastUpdateTime;     ///< 最后更新时间
      43              : };
      44              : 
      45              : /**
      46              :  * @brief 存储的消息 (用于重传)
      47              :  */
      48              : struct StoredMessage {
      49              :     int seqNum;                 ///< 序列号
      50              :     std::string senderCompID;
      51              :     std::string targetCompID;
      52              :     std::string msgType;        ///< 消息类型
      53              :     std::string rawMessage;     ///< 原始消息
      54              :     int64_t timestamp;          ///< 时间戳
      55              : };
      56              : 
      57              : /**
      58              :  * @class IStore
      59              :  * @brief 存储接口
      60              :  */
      61              : class IStore {
      62              : public:
      63         1136 :     virtual ~IStore() = default;
      64              : 
      65              :     // =========================================================================
      66              :     // 订单存储
      67              :     // =========================================================================
      68              :     
      69              :     /**
      70              :      * @brief 保存订单(不包含订单归属信息)
      71              :      *
      72              :      * 该接口兼容旧代码路径与测试;在需要按用户隔离查询订单历史的场景,
      73              :      * 应优先使用 saveOrderForAccount() 写入 accountId 并使用 loadOrdersByAccount() 查询。
      74              :      */
      75              :     virtual bool saveOrder(const Order& order) = 0;
      76              : 
      77              :     /**
      78              :      * @brief 保存订单并记录订单归属账户
      79              :      *
      80              :      * @param order 订单对象
      81              :      * @param accountId 订单所属账户(由服务端基于 Session 身份绑定得出,不应由客户端消息提供)
      82              :      * @return 保存成功返回 true,失败返回 false
      83              :      */
      84              :     virtual bool saveOrderForAccount(const Order& order, const std::string& accountId) = 0;
      85              : 
      86              :     virtual bool updateOrder(const Order& order) = 0;
      87              :     virtual std::optional<Order> loadOrder(const std::string& clOrdID) = 0;
      88              :     virtual std::vector<Order> loadOrdersBySymbol(const std::string& symbol) = 0;
      89              :     virtual std::vector<Order> loadOrdersByAccount(const std::string& accountId) = 0;
      90              :     virtual std::vector<Order> loadActiveOrders() = 0;
      91              :     virtual std::vector<Order> loadAllOrders() = 0;
      92              : 
      93              :     // =========================================================================
      94              :     // 成交存储
      95              :     // =========================================================================
      96              :     
      97              :     virtual bool saveTrade(const StoredTrade& trade) = 0;
      98              :     virtual std::vector<StoredTrade> loadTradesByOrder(const std::string& clOrdID) = 0;
      99              :     virtual std::vector<StoredTrade> loadTradesBySymbol(const std::string& symbol) = 0;
     100              : 
     101              :     // =========================================================================
     102              :     // 会话状态存储
     103              :     // =========================================================================
     104              :     
     105              :     virtual bool saveSessionState(const SessionState& state) = 0;
     106              :     virtual std::optional<SessionState> loadSessionState(
     107              :         const std::string& senderCompID, const std::string& targetCompID) = 0;
     108              : 
     109              :     // =========================================================================
     110              :     // 消息存储 (用于重传)
     111              :     // =========================================================================
     112              :     
     113              :     virtual bool saveMessage(const StoredMessage& msg) = 0;
     114              :     virtual std::vector<StoredMessage> loadMessages(
     115              :         const std::string& senderCompID, const std::string& targetCompID,
     116              :         int beginSeqNum, int endSeqNum) = 0;
     117              : 
     118              :     /**
     119              :      * @brief 删除指定会话方向的所有已持久化消息
     120              :      *
     121              :      * 典型用途:处理 Logon 的 ResetSeqNumFlag(141)=Y 时,清理旧会话的消息记录,
     122              :      * 避免序列号重置后出现同一 seq_num 的重复历史消息干扰重传。
     123              :      *
     124              :      * @param senderCompID 发送方 CompID
     125              :      * @param targetCompID 接收方 CompID
     126              :      * @return 删除成功返回 true,失败返回 false
     127              :      */
     128              :     virtual bool deleteMessagesForSession(
     129              :         const std::string& senderCompID, const std::string& targetCompID) = 0;
     130              : 
     131              :     virtual bool deleteMessagesOlderThan(int64_t timestamp) = 0;
     132              : 
     133              :     // =========================================================================
     134              :     // 账户存储
     135              :     // =========================================================================
     136              :     
     137              :     /**
     138              :      * @brief 保存账户信息
     139              :      * 
     140              :      * 将账户状态持久化到数据库。如果账户已存在则更新,否则插入新记录。
     141              :      * 
     142              :      * @param account 要保存的账户对象
     143              :      * @return 保存成功返回 true,失败返回 false
     144              :      * 
     145              :      * @note 此方法使用 INSERT OR REPLACE 语义,确保幂等性
     146              :      */
     147              :     virtual bool saveAccount(const Account& account) = 0;
     148              :     
     149              :     /**
     150              :      * @brief 加载账户信息
     151              :      * 
     152              :      * 从数据库加载指定账户的状态。
     153              :      * 
     154              :      * @param accountId 账户ID
     155              :      * @return 如果账户存在返回 Account 对象,否则返回 std::nullopt
     156              :      */
     157              :     virtual std::optional<Account> loadAccount(const std::string& accountId) = 0;
     158              :     
     159              :     /**
     160              :      * @brief 加载所有账户
     161              :      * 
     162              :      * 从数据库加载所有账户信息。
     163              :      * 
     164              :      * @return 账户列表
     165              :      */
     166              :     virtual std::vector<Account> loadAllAccounts() = 0;
     167              :     
     168              :     /**
     169              :      * @brief 删除账户
     170              :      * 
     171              :      * 从数据库删除指定账户。
     172              :      * 
     173              :      * @param accountId 账户ID
     174              :      * @return 删除成功返回 true,失败返回 false
     175              :      * 
     176              :      * @warning 删除账户不会自动删除关联的持仓数据,需要单独处理
     177              :      */
     178              :     virtual bool deleteAccount(const std::string& accountId) = 0;
     179              : 
     180              :     // =========================================================================
     181              :     // 持仓存储
     182              :     // =========================================================================
     183              :     
     184              :     /**
     185              :      * @brief 保存持仓信息
     186              :      * 
     187              :      * 将持仓状态持久化到数据库。如果持仓已存在则更新,否则插入新记录。
     188              :      * 
     189              :      * @param position 要保存的持仓对象
     190              :      * @return 保存成功返回 true,失败返回 false
     191              :      * 
     192              :      * @note 此方法使用 INSERT OR REPLACE 语义,确保幂等性
     193              :      */
     194              :     virtual bool savePosition(const Position& position) = 0;
     195              :     
     196              :     /**
     197              :      * @brief 加载持仓信息
     198              :      * 
     199              :      * 从数据库加载指定账户在指定合约上的持仓。
     200              :      * 
     201              :      * @param accountId 账户ID
     202              :      * @param instrumentId 合约代码
     203              :      * @return 如果持仓存在返回 Position 对象,否则返回 std::nullopt
     204              :      */
     205              :     virtual std::optional<Position> loadPosition(
     206              :         const std::string& accountId, const std::string& instrumentId) = 0;
     207              :     
     208              :     /**
     209              :      * @brief 加载账户的所有持仓
     210              :      * 
     211              :      * 从数据库加载指定账户的所有持仓信息。
     212              :      * 
     213              :      * @param accountId 账户ID
     214              :      * @return 持仓列表
     215              :      */
     216              :     virtual std::vector<Position> loadPositionsByAccount(const std::string& accountId) = 0;
     217              :     
     218              :     /**
     219              :      * @brief 加载所有持仓
     220              :      * 
     221              :      * 从数据库加载所有持仓信息。
     222              :      * 
     223              :      * @return 持仓列表
     224              :      */
     225              :     virtual std::vector<Position> loadAllPositions() = 0;
     226              :     
     227              :     /**
     228              :      * @brief 删除持仓
     229              :      * 
     230              :      * 从数据库删除指定持仓。
     231              :      * 
     232              :      * @param accountId 账户ID
     233              :      * @param instrumentId 合约代码
     234              :      * @return 删除成功返回 true,失败返回 false
     235              :      */
     236              :     virtual bool deletePosition(const std::string& accountId, const std::string& instrumentId) = 0;
     237              :     
     238              :     /**
     239              :      * @brief 删除账户的所有持仓
     240              :      * 
     241              :      * 从数据库删除指定账户的所有持仓。
     242              :      * 
     243              :      * @param accountId 账户ID
     244              :      * @return 删除成功返回 true,失败返回 false
     245              :      */
     246              :     virtual bool deletePositionsByAccount(const std::string& accountId) = 0;
     247              : };
     248              : 
     249              : } // namespace fix40
        

Generated by: LCOV version 2.0-1