|
| 1 | +#ifndef SQL_PARSER_STMT_CACHE_H |
| 2 | +#define SQL_PARSER_STMT_CACHE_H |
| 3 | + |
| 4 | +#include "sql_parser/ast.h" |
| 5 | +#include "sql_parser/common.h" |
| 6 | +#include "sql_parser/parse_result.h" |
| 7 | +#include <cstdlib> |
| 8 | +#include <cstring> |
| 9 | +#include <unordered_map> |
| 10 | +#include <list> |
| 11 | + |
| 12 | +namespace sql_parser { |
| 13 | + |
| 14 | +// Deep-copy an AST tree from arena to heap memory. |
| 15 | +// The returned tree must be freed with free_ast(). |
| 16 | +inline AstNode* deep_copy_ast(const AstNode* src) { |
| 17 | + if (!src) return nullptr; |
| 18 | + |
| 19 | + AstNode* dst = static_cast<AstNode*>(std::malloc(sizeof(AstNode))); |
| 20 | + if (!dst) return nullptr; |
| 21 | + |
| 22 | + dst->type = src->type; |
| 23 | + dst->flags = src->flags; |
| 24 | + dst->first_child = nullptr; |
| 25 | + dst->next_sibling = nullptr; |
| 26 | + |
| 27 | + // Deep-copy value string to heap |
| 28 | + if (src->value_ptr && src->value_len > 0) { |
| 29 | + char* val_copy = static_cast<char*>(std::malloc(src->value_len)); |
| 30 | + if (val_copy) { |
| 31 | + std::memcpy(val_copy, src->value_ptr, src->value_len); |
| 32 | + } |
| 33 | + dst->value_ptr = val_copy; |
| 34 | + dst->value_len = src->value_len; |
| 35 | + } else { |
| 36 | + dst->value_ptr = nullptr; |
| 37 | + dst->value_len = 0; |
| 38 | + } |
| 39 | + |
| 40 | + // Recursively copy children |
| 41 | + const AstNode* src_child = src->first_child; |
| 42 | + AstNode* prev_dst_child = nullptr; |
| 43 | + while (src_child) { |
| 44 | + AstNode* dst_child = deep_copy_ast(src_child); |
| 45 | + if (dst_child) { |
| 46 | + if (!dst->first_child) { |
| 47 | + dst->first_child = dst_child; |
| 48 | + } else if (prev_dst_child) { |
| 49 | + prev_dst_child->next_sibling = dst_child; |
| 50 | + } |
| 51 | + prev_dst_child = dst_child; |
| 52 | + } |
| 53 | + src_child = src_child->next_sibling; |
| 54 | + } |
| 55 | + |
| 56 | + return dst; |
| 57 | +} |
| 58 | + |
| 59 | +// Free a heap-allocated AST tree (produced by deep_copy_ast). |
| 60 | +inline void free_ast(AstNode* node) { |
| 61 | + if (!node) return; |
| 62 | + // Free children first |
| 63 | + AstNode* child = node->first_child; |
| 64 | + while (child) { |
| 65 | + AstNode* next = child->next_sibling; |
| 66 | + free_ast(child); |
| 67 | + child = next; |
| 68 | + } |
| 69 | + // Free value string |
| 70 | + if (node->value_ptr) { |
| 71 | + std::free(const_cast<char*>(node->value_ptr)); |
| 72 | + } |
| 73 | + std::free(node); |
| 74 | +} |
| 75 | + |
| 76 | +// Cached entry for a prepared statement. |
| 77 | +struct CachedStmt { |
| 78 | + uint32_t stmt_id; |
| 79 | + StmtType stmt_type; |
| 80 | + AstNode* ast; // heap-allocated deep copy |
| 81 | + |
| 82 | + ~CachedStmt() { |
| 83 | + free_ast(ast); |
| 84 | + } |
| 85 | + |
| 86 | + // Non-copyable |
| 87 | + CachedStmt(const CachedStmt&) = delete; |
| 88 | + CachedStmt& operator=(const CachedStmt&) = delete; |
| 89 | + CachedStmt(CachedStmt&& o) noexcept |
| 90 | + : stmt_id(o.stmt_id), stmt_type(o.stmt_type), ast(o.ast) { |
| 91 | + o.ast = nullptr; |
| 92 | + } |
| 93 | + CachedStmt& operator=(CachedStmt&& o) noexcept { |
| 94 | + if (this != &o) { |
| 95 | + free_ast(ast); |
| 96 | + stmt_id = o.stmt_id; |
| 97 | + stmt_type = o.stmt_type; |
| 98 | + ast = o.ast; |
| 99 | + o.ast = nullptr; |
| 100 | + } |
| 101 | + return *this; |
| 102 | + } |
| 103 | + |
| 104 | + CachedStmt() : stmt_id(0), stmt_type(StmtType::UNKNOWN), ast(nullptr) {} |
| 105 | + CachedStmt(uint32_t id, StmtType type, AstNode* a) |
| 106 | + : stmt_id(id), stmt_type(type), ast(a) {} |
| 107 | +}; |
| 108 | + |
| 109 | +// Fixed-capacity LRU cache for prepared statements. |
| 110 | +class StmtCache { |
| 111 | +public: |
| 112 | + explicit StmtCache(size_t capacity = 128) : capacity_(capacity) {} |
| 113 | + |
| 114 | + ~StmtCache() { clear(); } |
| 115 | + |
| 116 | + // Non-copyable |
| 117 | + StmtCache(const StmtCache&) = delete; |
| 118 | + StmtCache& operator=(const StmtCache&) = delete; |
| 119 | + |
| 120 | + // Store a prepared statement. Deep-copies the AST from the arena. |
| 121 | + // Evicts LRU entry if at capacity. |
| 122 | + bool store(uint32_t stmt_id, StmtType stmt_type, const AstNode* ast) { |
| 123 | + // If already exists, remove old entry |
| 124 | + evict(stmt_id); |
| 125 | + |
| 126 | + AstNode* copy = deep_copy_ast(ast); |
| 127 | + if (!copy && ast) return false; |
| 128 | + |
| 129 | + // Evict LRU if at capacity |
| 130 | + if (lru_.size() >= capacity_) { |
| 131 | + auto& oldest = lru_.back(); |
| 132 | + map_.erase(oldest.stmt_id); |
| 133 | + lru_.pop_back(); |
| 134 | + } |
| 135 | + |
| 136 | + lru_.emplace_front(stmt_id, stmt_type, copy); |
| 137 | + map_[stmt_id] = lru_.begin(); |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + // Look up a cached statement. Returns nullptr if not found. |
| 142 | + // Moves the entry to front of LRU. |
| 143 | + const CachedStmt* lookup(uint32_t stmt_id) { |
| 144 | + auto it = map_.find(stmt_id); |
| 145 | + if (it == map_.end()) return nullptr; |
| 146 | + // Move to front (most recently used) |
| 147 | + lru_.splice(lru_.begin(), lru_, it->second); |
| 148 | + return &(*it->second); |
| 149 | + } |
| 150 | + |
| 151 | + // Evict a specific statement. |
| 152 | + void evict(uint32_t stmt_id) { |
| 153 | + auto it = map_.find(stmt_id); |
| 154 | + if (it != map_.end()) { |
| 155 | + lru_.erase(it->second); |
| 156 | + map_.erase(it); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // Clear all entries. |
| 161 | + void clear() { |
| 162 | + lru_.clear(); |
| 163 | + map_.clear(); |
| 164 | + } |
| 165 | + |
| 166 | + size_t size() const { return map_.size(); } |
| 167 | + size_t capacity() const { return capacity_; } |
| 168 | + |
| 169 | +private: |
| 170 | + size_t capacity_; |
| 171 | + std::list<CachedStmt> lru_; |
| 172 | + std::unordered_map<uint32_t, std::list<CachedStmt>::iterator> map_; |
| 173 | +}; |
| 174 | + |
| 175 | +} // namespace sql_parser |
| 176 | + |
| 177 | +#endif // SQL_PARSER_STMT_CACHE_H |
0 commit comments