pipeline/DatabaseAccessor.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // <DatabaseAccessor.hpp> -*- C++ -*- | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include "simdb/Assert.hpp" | ||
| 6 | #include "simdb/pipeline/AsyncDatabaseAccessor.hpp" | ||
| 7 | #include "simdb/sqlite/DatabaseManager.hpp" | ||
| 8 | #include "simdb/sqlite/PreparedINSERT.hpp" | ||
| 9 | #include "simdb/sqlite/Table.hpp" | ||
| 10 | |||
| 11 | namespace simdb::pipeline { | ||
| 12 | |||
| 13 | /*! | ||
| 14 | * \class DatabaseAccessor | ||
| 15 | * | ||
| 16 | * \brief Provides DatabaseManager access and per-App prepared INSERT objects | ||
| 17 | * for DatabaseStage. Used by DatabaseStage<AppT> to get the manager | ||
| 18 | * and getTableInserter<AppT>(table_name) for high-volume inserts. | ||
| 19 | */ | ||
| 20 | class DatabaseAccessor | ||
| 21 | { | ||
| 22 | public: | ||
| 23 | /// \brief Construct with the DatabaseManager used for all operations. | ||
| 24 | /// \param db_mgr Non-null DatabaseManager. | ||
| 25 | 12 | DatabaseAccessor(DatabaseManager* db_mgr) : | |
| 26 | 12 | db_mgr_(db_mgr) | |
| 27 | { | ||
| 28 | 12 | } | |
| 29 | |||
| 30 | /// \brief Return the DatabaseManager. | ||
| 31 | 11172 | DatabaseManager* getDatabaseManager() const { return db_mgr_; } | |
| 32 | |||
| 33 | /// \brief Return a prepared INSERT for the given table; lazily builds inserters from App::defineSchema(). | ||
| 34 | /// \tparam App App type (must have NAME and defineSchema()). | ||
| 35 | /// \param tbl_name Table name in the App's schema. | ||
| 36 | /// \return Raw pointer to the PreparedINSERT (owned by this accessor). | ||
| 37 | /// \throws DBException if the table is not in the App's schema. | ||
| 38 | 33691 | template <typename App> PreparedINSERT* getTableInserter(const std::string& tbl_name) | |
| 39 | { | ||
| 40 | 67382 | auto& inserters = tbl_inserters_by_app_[App::NAME]; | |
| 41 | 33691 | if (inserters.empty()) | |
| 42 | { | ||
| 43 | 11 | Schema schema; | |
| 44 | 11 | App::defineSchema(schema); | |
| 45 | |||
| 46 | 67 | for (const auto& tbl : schema.getTables()) | |
| 47 | { | ||
| 48 | 56 | std::vector<std::string> col_names; | |
| 49 | 191 | for (const auto& col : tbl.getColumns()) | |
| 50 | { | ||
| 51 | 135 | col_names.emplace_back(col->getName()); | |
| 52 | } | ||
| 53 | |||
| 54 | 56 | SqlTable table(tbl.getName()); | |
| 55 | 56 | SqlColumns columns(col_names); | |
| 56 | 56 | auto inserter = db_mgr_->prepareINSERT(std::move(table), std::move(columns)); | |
| 57 | 56 | inserters[tbl.getName()] = std::move(inserter); | |
| 58 | } | ||
| 59 | 11 | } | |
| 60 | |||
| 61 | 33691 | auto& inserter = inserters[tbl_name]; | |
| 62 | 33691 | simdb_assert(inserter, "Table does not exist in schema for app: " << App::NAME); | |
| 63 | |||
| 64 | 33691 | return inserter.get(); | |
| 65 | } | ||
| 66 | |||
| 67 | private: | ||
| 68 | DatabaseManager* db_mgr_ = nullptr; | ||
| 69 | |||
| 70 | using TableInserters = std::unordered_map<std::string, std::unique_ptr<PreparedINSERT>>; | ||
| 71 | std::unordered_map<std::string, TableInserters> tbl_inserters_by_app_; | ||
| 72 | }; | ||
| 73 | |||
| 74 | } // namespace simdb::pipeline | ||
| 75 |