|
| 1 | +// |
| 2 | +// Created by giacomo on 16/01/24. |
| 3 | +// |
| 4 | + |
| 5 | +#ifndef GSM2_SCHEMAINDEXER_H |
| 6 | +#define GSM2_SCHEMAINDEXER_H |
| 7 | + |
| 8 | +#include <unordered_map> |
| 9 | +#include <set> |
| 10 | +#include <string> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#include "utility.h" |
| 14 | +#include "scriptv2/java_types.h" |
| 15 | +#include "scriptv2/ScriptAST.h" |
| 16 | + |
| 17 | +struct SchemaIndexer { |
| 18 | + std::unordered_map<std::string, size_t> field_to_schema; |
| 19 | + std::unordered_map<std::string, std::unordered_map<std::string, size_t>> nested_schema; |
| 20 | + std::set<size_t> isNull, cow; |
| 21 | + std::vector<DPtr<script::structures::ScriptAST>> values; |
| 22 | + std::set<size_t> strings; |
| 23 | + const std::vector<value>* row; |
| 24 | + const std::vector<std::string>* schema; |
| 25 | + |
| 26 | + SchemaIndexer(size_t n) : values(n, std::make_shared<script::structures::ScriptAST>()){ |
| 27 | + fullyIndexed = false; |
| 28 | + row = nullptr; |
| 29 | + } |
| 30 | + void setUp(const std::vector<std::string>* schema) { |
| 31 | + this->schema = {schema}; |
| 32 | + for (size_t i = 0, N = schema->size(); i<N; i++) { |
| 33 | + field_to_schema.emplace(schema->at(i), i); |
| 34 | + } |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + void initialize(const std::vector<value>* nestedRow) { |
| 39 | + if (row == nestedRow) return; |
| 40 | + row = nestedRow; |
| 41 | + isNull.clear(); |
| 42 | + cow.clear(); |
| 43 | + for (size_t i = 0, N = std::min(nestedRow->size(), field_to_schema.size()); i<N; i++) { |
| 44 | + const auto& cell = nestedRow->at(i); |
| 45 | + if (!cell.isNested) { |
| 46 | + if (std::holds_alternative<bool>(cell.val)) |
| 47 | + isNull.insert(i); |
| 48 | + else if (!std::holds_alternative<size_t>(cell.val)) { |
| 49 | + strings.emplace(i); |
| 50 | + } |
| 51 | + } else { |
| 52 | + if (!fullyIndexed) { |
| 53 | + for (size_t j = 0, M = cell.table.Schema.size(); j<M; j++) { |
| 54 | + nested_schema[schema->at(i)].emplace(cell.table.Schema.at(j), j); |
| 55 | + } |
| 56 | + } |
| 57 | + // TODO: efficient nested values on demand (COW) |
| 58 | + } |
| 59 | + } |
| 60 | + fullyIndexed = true; |
| 61 | + } |
| 62 | + |
| 63 | + bool hasKey(const std::string& key) const { |
| 64 | + return field_to_schema.contains(key); |
| 65 | + } |
| 66 | + |
| 67 | + size_t getNativeInt(const std::string& key) const { |
| 68 | + auto it = field_to_schema.find(key); |
| 69 | + if ((!row) || it == field_to_schema.end() || (isNull.contains(it->second))) |
| 70 | + return -1; |
| 71 | + else |
| 72 | + return std::get<size_t>(row->at(it->second).val); |
| 73 | + } |
| 74 | + |
| 75 | + DPtr<script::structures::ScriptAST> get(const std::string& key); |
| 76 | + |
| 77 | +private: |
| 78 | + bool fullyIndexed; |
| 79 | +}; |
| 80 | + |
| 81 | +struct TemplateIndexer { |
| 82 | + std::unordered_map<size_t, SchemaIndexer> map; |
| 83 | + size_t current_template; |
| 84 | + |
| 85 | + bool contains(const std::string& key) const { |
| 86 | + auto it = map.find(current_template); |
| 87 | + return it != map.end() && (it->second.hasKey(key)); |
| 88 | + } |
| 89 | + |
| 90 | + DPtr<script::structures::ScriptAST> get(const std::string&key) { |
| 91 | + auto it = map.find(current_template); |
| 92 | + if (it == map.end() ) |
| 93 | + return script::structures::ScriptAST::null_(); |
| 94 | + return it->second.get(key); |
| 95 | + } |
| 96 | + |
| 97 | + size_t getNativeInt(const std::string&key) const { |
| 98 | + auto it = map.find(current_template); |
| 99 | + if (it == map.end() ) |
| 100 | + return -1; |
| 101 | + return it->second.getNativeInt(key); |
| 102 | + } |
| 103 | + |
| 104 | + SchemaIndexer& attemptInitialise(size_t i, const std::vector<std::string> *vector) { |
| 105 | + if (!map.contains(i)) { |
| 106 | + auto& ref = map.emplace(i, vector->size()).first->second; |
| 107 | + ref.setUp(vector); |
| 108 | + return ref; |
| 109 | + } else { |
| 110 | + return map.at(i); |
| 111 | + } |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +#endif //GSM2_SCHEMAINDEXER_H |
0 commit comments