|
| 1 | +/****************************************************************************** |
| 2 | + * |
| 3 | + * Copyright 2021-2023 Nikolay Shaplov (Postgres Professional) |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + ******************************************************************************/ |
| 18 | + |
| 19 | + |
| 20 | +#include <string> |
| 21 | +#include <vector> |
| 22 | +#include <memory> |
| 23 | + |
| 24 | +#include "stamp_json.h" |
| 25 | +#include "oracle.h" |
| 26 | + |
| 27 | +PoolPickerStamp::PoolPickerStamp(std::vector<std::shared_ptr<StampBaseStr>> new_pool) |
| 28 | + : pool{new_pool} |
| 29 | +{ |
| 30 | + for(auto stamp : pool) |
| 31 | + { |
| 32 | + std::weak_ptr<StampBaseStr> wp = stamp; |
| 33 | + weak_pool.push_back(wp); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +bool |
| 38 | +PoolPickerStamp::isRecursive() |
| 39 | +{ |
| 40 | + if(is_recursive || is_in_recursion) |
| 41 | + return true; |
| 42 | + is_in_recursion = true; |
| 43 | + for(auto stamp : weak_pool) |
| 44 | + { |
| 45 | + if (stamp.lock()->isRecursive()) |
| 46 | + { |
| 47 | + is_recursive = true; // Once recursive -- recursive forever. |
| 48 | + is_in_recursion = false; |
| 49 | + return true; |
| 50 | + } |
| 51 | + } |
| 52 | + is_in_recursion = false; |
| 53 | + return false; |
| 54 | +} |
| 55 | + |
| 56 | +std::string |
| 57 | +PoolPickerStamp::ExtractStr(std::shared_ptr<Blob> blob) |
| 58 | +{ |
| 59 | + static ORACLE_STAMP stamp_oracle; |
| 60 | + ORACLE_TYPE oracle = stamp_oracle.ExtractValue(blob); |
| 61 | + |
| 62 | + std::vector<std::weak_ptr<StampBaseStr>> target_pool; |
| 63 | + std::vector<std::weak_ptr<StampBaseStr>> unbounded_pool; |
| 64 | + |
| 65 | + for(auto stamp_w : weak_pool) |
| 66 | + { |
| 67 | + auto stamp = stamp_w.lock(); |
| 68 | + if (stamp->minSize() <= blob->Size()) |
| 69 | + { |
| 70 | + target_pool.push_back(stamp_w); |
| 71 | + if (stamp->maxSize() == -1 || stamp->maxSize() >= blob->Size()) |
| 72 | + { |
| 73 | + unbounded_pool.push_back(stamp_w); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + if (unbounded_pool.size()>0) |
| 78 | + target_pool = unbounded_pool; |
| 79 | + |
| 80 | + size_t index = OracleProportion(oracle, 0, target_pool.size() - 1); |
| 81 | + return target_pool[index].lock()->ExtractStr(blob); |
| 82 | +} |
| 83 | + |
| 84 | +int |
| 85 | +PoolPickerStamp::minSize() |
| 86 | +{ |
| 87 | + int res = INT_MAX / 2; |
| 88 | + /* Do not check is_recursive here: even if stamp is known to be recursive we |
| 89 | + * still should iterate all his non-recursive children to find real minimal |
| 90 | + * size */ |
| 91 | + if (is_in_recursion) |
| 92 | + return res; |
| 93 | + is_in_recursion = true; /* Do not use isRecursive() inside as it uses same flag*/ |
| 94 | + for(auto stamp : weak_pool) |
| 95 | + { |
| 96 | + int candidat = stamp.lock()->minSize(); |
| 97 | + if (res > candidat) |
| 98 | + res = candidat; |
| 99 | + } |
| 100 | + is_in_recursion = false; |
| 101 | + if (res == INT_MAX / 2) |
| 102 | + return INT_MAX / 2; |
| 103 | + res += ORACLE_SIZE; |
| 104 | + return res; |
| 105 | +} |
| 106 | + |
| 107 | +int |
| 108 | +PoolPickerStamp::maxSize() |
| 109 | +{ |
| 110 | + int res = 0; |
| 111 | + if (is_recursive || is_in_recursion) |
| 112 | + return -1; |
| 113 | + is_in_recursion = true; /* Do not use isRecursive() inside as it uses same flag*/ |
| 114 | + for(auto stamp : weak_pool) |
| 115 | + { |
| 116 | + int candidat = stamp.lock()->maxSize(); |
| 117 | + if (candidat == -1) |
| 118 | + { |
| 119 | + is_in_recursion = false; |
| 120 | + return -1; |
| 121 | + } |
| 122 | + if (res < candidat) |
| 123 | + res = candidat; |
| 124 | + } |
| 125 | + is_in_recursion = false; |
| 126 | + res += ORACLE_SIZE; |
| 127 | + return res; |
| 128 | +} |
| 129 | + |
| 130 | +void |
| 131 | +PoolPickerStamp::add_weak(std::shared_ptr<StampBaseStr> stamp) |
| 132 | +{ |
| 133 | + weak_pool.push_back(stamp); |
| 134 | +} |
| 135 | + |
| 136 | +std::string |
| 137 | +StampJSONString::ExtractStr(std::shared_ptr<Blob> blob) |
| 138 | +{ |
| 139 | + std::string res = "\"" + StampDictT<DictLCAlphaSmall>::ExtractStr(blob) +"\""; |
| 140 | + return res; |
| 141 | +} |
| 142 | + |
| 143 | + |
| 144 | +std::string |
| 145 | +StampJSONHashEl::ExtractStr(std::shared_ptr<Blob> blob) |
| 146 | +{ |
| 147 | + std::string n = stamp_name->ExtractStr(blob); |
| 148 | + std::string v = stamp_value->ExtractStr(blob); |
| 149 | + return n + ": " + v; |
| 150 | +} |
| 151 | + |
| 152 | +void null_deleter(StampJSON *) {} |
| 153 | + |
| 154 | +StampJSON::StampJSON() |
| 155 | + : PoolPickerStamp({}) |
| 156 | +{ |
| 157 | + stamp_i = std::make_shared<StampJSONInt>(); |
| 158 | + stamp_f = std::make_shared<StampJSONFloat>(); |
| 159 | + stamp_s = std::make_shared<StampJSONString>(); |
| 160 | + |
| 161 | + // FIXME Так не надо делеать!!!! null_deleter -- зло. |
| 162 | + stamp_a = std::make_shared<StampJSONArray>(std::shared_ptr<StampJSON>(this, null_deleter)); |
| 163 | + stamp_h = std::make_shared<StampJSONHash>(std::shared_ptr<StampJSON>(this, null_deleter)); |
| 164 | + add_weak(stamp_i); |
| 165 | + add_weak(stamp_f); |
| 166 | + add_weak(stamp_s); |
| 167 | + |
| 168 | + add_weak(stamp_a); |
| 169 | + add_weak(stamp_h); |
| 170 | +} |
| 171 | + |
0 commit comments