Skip to content

Commit e3435de

Browse files
Move StampJSON related code to proper place
1 parent c13f237 commit e3435de

File tree

3 files changed

+285
-254
lines changed

3 files changed

+285
-254
lines changed

blobstamper/stamp_json.cpp

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+

blobstamper/stamp_json.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
#ifndef STAMP_JSON_H
20+
#define STAMP_JSON_H
21+
22+
#include <string>
23+
#include <limits>
24+
25+
#include "stamp.h"
26+
#include "stamp_arithm.h"
27+
#include "stamp_dict.h"
28+
#include "stamp_enumerator.h"
29+
30+
class PoolPickerStamp : public virtual StampBaseStr
31+
{
32+
protected:
33+
std::vector<std::shared_ptr<StampBaseStr>> pool;
34+
std::vector<std::weak_ptr<StampBaseStr>> weak_pool;
35+
36+
public:
37+
PoolPickerStamp(std::vector<std::shared_ptr<StampBaseStr>> new_pool);
38+
// ~PoolPickerStamp() {fprintf(stderr, "DESTROY!\n");};
39+
40+
std::string ExtractStr(std::shared_ptr<Blob> blob) override;
41+
virtual void add_weak(std::shared_ptr<StampBaseStr> stamp);
42+
virtual bool isRecursive();
43+
virtual int minSize() override;
44+
virtual int maxSize() override;
45+
};
46+
47+
class StampJSONInt : public virtual StampArithm<long int>
48+
{
49+
};
50+
51+
class StampJSONFloat : public virtual StampArithm<double>
52+
{
53+
};
54+
55+
class StampJSONString : public virtual StampDictT<DictLCAlphaSmall>
56+
{
57+
protected:
58+
public:
59+
std::string ExtractStr(std::shared_ptr<Blob> blob) override;
60+
virtual int minSize() override {return 8;};
61+
virtual int maxSize() override {return 8;};
62+
63+
};
64+
65+
class StampJSONArray: public StampStrEnumerator
66+
{
67+
private:
68+
public:
69+
StampJSONArray(std::shared_ptr<PoolPickerStamp> picker)
70+
:StampStrEnumerator(picker, ", ", "[", "]") {};
71+
};
72+
73+
class StampJSONHashEl: public StampBaseStr
74+
{
75+
private:
76+
std::shared_ptr<StampJSONString> stamp_name;
77+
std::shared_ptr<PoolPickerStamp> stamp_value;
78+
public:
79+
StampJSONHashEl(std::shared_ptr<PoolPickerStamp> picker)
80+
:stamp_value(picker), stamp_name(std::make_shared<StampJSONString>()) {};
81+
virtual int minSize() override {return stamp_name->minSize() + stamp_value->minSize();};
82+
virtual int maxSize() override {return -1;};
83+
std::string ExtractStr(std::shared_ptr<Blob> blob) override;
84+
};
85+
86+
class StampJSONHash: public StampStrEnumerator
87+
{
88+
private:
89+
std::shared_ptr<StampJSONHashEl> stamp_el;
90+
public:
91+
StampJSONHash(std::shared_ptr<PoolPickerStamp> picker)
92+
:StampStrEnumerator(stamp_el = std::make_shared<StampJSONHashEl>(picker), ", ", "{", "}") {};
93+
};
94+
95+
96+
class StampJSON: public PoolPickerStamp
97+
{
98+
private:
99+
std::shared_ptr<StampJSONString> stamp_s;
100+
std::shared_ptr<StampJSONInt> stamp_i;
101+
std::shared_ptr<StampJSONFloat> stamp_f;
102+
std::shared_ptr<StampJSONArray> stamp_a;
103+
std::shared_ptr<StampJSONHash> stamp_h;
104+
105+
public:
106+
StampJSON();
107+
};
108+
109+
#endif /* STAMP_DICT_H */
110+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy