Skip to content

Commit ba7f4f6

Browse files
JSON Stamp. Work started
1 parent 2faa3fa commit ba7f4f6

File tree

5 files changed

+298
-13
lines changed

5 files changed

+298
-13
lines changed

blobstamper/galley.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ GalleyVectorBase::extract_internal(std::shared_ptr<Blob> blob)
152152
int el_size = el_size_f;
153153
remainder = el_size_f - el_size;
154154

155-
std::shared_ptr<Blob> blob2 = blob->Chop(el_size);
155+
std::shared_ptr<Blob> blob2 = blob->Chop(el_size);
156156
res.push_back(blob2);
157157
}
158158
}
@@ -161,6 +161,8 @@ GalleyVectorBase::extract_internal(std::shared_ptr<Blob> blob)
161161
/* Stamp is variated size */
162162
int fixed_size = stamp->minSize();
163163
int var_size = stamp->maxSize() - fixed_size;
164+
fprintf(stderr, "fixed = %i, var_size = %i\n", fixed_size, var_size);
165+
164166
ORACLE_STAMP stamp_oracle;
165167
while(1)
166168
{
@@ -172,7 +174,8 @@ GalleyVectorBase::extract_internal(std::shared_ptr<Blob> blob)
172174
int size = (double) oracle / ORACLE_MAX * (var_size + 1); /* +1 -- это грубая эмуляция округления вверх. oracle == ORACLE_MAX-1 == 65534 должен дать count_max*/
173175
if (size > var_size) size = var_size; // In case we've hit oracle == ORACLE_MAX boundary
174176
size += fixed_size;
175-
std::shared_ptr<Blob> blob2 = blob->Chop(size);
177+
fprintf(stderr,"---- %i %i\n", size, blob->Size());
178+
std::shared_ptr<Blob> blob2 = blob->Chop(size);
176179
res.push_back(blob2);
177180
}
178181
}

blobstamper/stamp.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,27 @@ class StampBase
3232
{
3333
protected:
3434
std::shared_ptr<Blob> bitten_blob;
35+
bool is_recursive = 0;
36+
bool is_in_recursion = 0;
3537
public:
36-
virtual int minSize() = 0;
37-
virtual int maxSize() = 0;
38+
virtual int minSize() = 0;
39+
virtual int maxSize() = 0;
3840

3941
void Load(std::shared_ptr<Blob> blob);
4042

4143
bool isFixedSize() {return minSize() == maxSize();}
4244
bool isVariated() {return ! isFixedSize() && ! isUnbounded();}
4345
bool isUnbounded() {return maxSize() == -1;}
46+
virtual bool isRecursive() {return is_recursive;}
47+
};
48+
49+
class StampRecursive: public virtual StampBase
50+
{
51+
protected:
52+
// bool is_in_recursion = 0;
53+
public:
54+
StampRecursive() {is_recursive = 1;}
55+
virtual ssize_t minSizeNR() = 0;
4456
};
4557

4658

blobstamper/stamp_dict.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,14 @@ class StampDictLCAlphaSmall : public StampDict
5353
StampDictLCAlphaSmall (): StampDict(std::make_shared<DictLCAlphaSmall>()) {};
5454
};
5555

56+
57+
template<class T> class StampDictT: public StampDict
58+
{
59+
protected:
60+
std::shared_ptr<T> dict;
61+
public:
62+
StampDictT<T>(): StampDict(dict = std::make_shared<T>()) {};
63+
};
64+
65+
5666
#endif /* STAMP_DICT_H */

t/300-galley.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,31 @@ main()
123123
but at least here we check that it work the same way it worked before. May be this test should be improved later*/
124124

125125
char sample[]="z1234567*89abcde&fghijklm";
126-
126+
char sample2[]="1234";
127127
std::string expected1 = "234";
128128
std::string expected2 = "7*8";
129129
std::string expected3 = "bcde";
130130
std::string expected4 = "ghij";
131131

132-
std::shared_ptr<Blob> blob = std::make_shared<Blob>(sample, strlen(sample));
133-
std::shared_ptr<StampSeveralChars> stamp = std::make_shared<StampSeveralChars>();
132+
auto blob = std::make_shared<Blob>(sample, strlen(sample));
133+
auto stamp = std::make_shared<StampSeveralChars>();
134134
GalleyVectorStr galley(stamp);
135135

136136
std::vector<std::string> res = galley.ExtractStrVector(blob);
137-
std::string str;
137+
// std::string str;
138138

139-
is(res[0], expected1, "GalleyVector, unlimited size string stamp: First element of shifted list is ok");
140-
is(res[1], expected2, "GalleyVector, unlimited size string stamp: Second element of shifted list is ok");
141-
is(res[2], expected3, "GalleyVector, unlimited size string stamp: Third element of shifted list is ok");
142-
is(res[3], expected4, "GalleyVector, unlimited size string stamp: Fourth element of shifted list is ok");
139+
is(res[0], expected1, "GalleyVector, variated size string stamp: First element of shifted list is ok");
140+
is(res[1], expected2, "GalleyVector, variated size string stamp: Second element of shifted list is ok");
141+
is(res[2], expected3, "GalleyVector, variated size string stamp: Third element of shifted list is ok");
142+
is(res[3], expected4, "GalleyVector, variated size string stamp: Fourth element of shifted list is ok");
143+
144+
is(res.size(), 4, "GalleyVector, variated size string stamp: The list has only 4 members");
143145

144-
is(res.size(), 4, "GalleyVector, unlimited size string stamp: The list has only 4 members");
146+
auto blob2 = std::make_shared<Blob>(sample2, strlen(sample2));
147+
res = galley.ExtractStrVector(blob2);
148+
145149

150+
146151
}
147152

148153
/* Test GalleySet with fixed size stamps*/
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/******************************************************************************
2+
*
3+
* Copyright 2021 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+
#include <string.h>
20+
21+
#include <exception>
22+
#include <string>
23+
#include <cstdlib>
24+
#include <vector>
25+
#include <cassert>
26+
#define WANT_TEST_EXTRAS
27+
#include <tap++/tap++.h>
28+
29+
#include "blobstamper/blobstamper.h"
30+
#include "blobstamper/dict.h"
31+
32+
#include "test-chars-stamps.h"
33+
34+
using namespace TAP;
35+
36+
char short_sample[]="1234567";
37+
char longer_sample[]="z1234567*89abcde&fghijklmnopqrstuvwxyzAB%CDEFGHIJKLMNOPQRSTUVWXYZ!";
38+
unsigned char bin_sample[]= {49, 22, 152, 226, 89, 119, 247, 115, 43, 42, 243, 71, 234, 231, 91, 35, 78, 108, 115, 39, 181, 248, 211, 52, 47, 94, 60, 237, 18, 39, 148, 62, 205, 214, 156, 184, 18, 201, 84, 183, 74, 134, 94, 72, 14, 116, 145, 109, 1, 230, 17, 95, 154, 100, 60, 15, 12, 102, 20, 115, 35, 183, 47, 176, 78, 176, 189, 113, 131, 93, 206, 62, 158, 166, 131, 95, 232, 217, 32, 171, 87, 31, 172, 160, 66, 160, 222, 134, 253, 1, 7, 191, 91, 125, 81, 148, 41, 46, 38, 171, 83, 215, 79, 34, 23, 215, 183, 118, 236, 191, 59, 160, 135, 58, 32, 199, 170, 183, 213, 53, 162, 138, 178, 118, 23, 202, 133, 8, 192, 183, 195, 199, 250, 29, 230, 34, 159, 10, 145, 74, 121, 85, 168, 204, 192, 25, 232, 88, 85, 76, 61, 168, 247, 128, 141, 176, 112, 113, 100, 201, 82, 183, 219, 236, 226, 171, 85, 97, 160, 1, 50, 250, 161, 51, 61, 220, 167, 227, 195, 17, 164, 211, 189, 130, 52, 167, 169, 42, 17, 29, 95, 15, 178, 165, 110, 87, 149, 214, 55, 12, 236, 138, 2, 245, 158, 84, 140, 24, 225, 169, 115, 16, 130, 253, 237, 182, 95, 109, 4, 28, 249, 4, 254, 166, 62, 107, 228, 113, 130, 127, 70, 79, 140, 41, 84, 218, 134, 146, 88, 65, 24, 174, 252, 253, 226, 214, 22, 92, 248, 14, 29, 60, 180, 94, 30, 186, 0};
39+
40+
41+
42+
//class SimpeRecursionNode;
43+
//class TestRNode1;
44+
//class TestRNode2;
45+
//class TestRNode3;
46+
47+
size_t
48+
Proportion(ORACLE_TYPE oracle, size_t min, size_t max)
49+
{
50+
/* Sorry explanation is in Russian. PR translation if you can */
51+
/* Считаем пропорацию, с округлением вниз.
52+
* Диапазон увиличиваем на единицу, чтобы хвост диапазона пресказания при
53+
* округлении вниз как раз попадал в последний элемент целевого диапозона.
54+
* Разброс предсказания увеличиваем на единицу, так чтобы ровно на конец
55+
* хвоста диапазона предсказания не попасть никогда и тогда он не округлиться
56+
* в max + 1*/
57+
size_t delta = max - min + 1;
58+
size_t res = floor(((float) oracle) / ((float) ORACLE_MAX + 1) * delta );
59+
return min + res;
60+
}
61+
62+
class PoolPickerStamp : public virtual StampBaseStr
63+
{
64+
protected:
65+
std::vector<std::shared_ptr<StampBaseStr>> pool;
66+
std::vector<std::weak_ptr<StampBaseStr>> weak_pool;
67+
68+
public:
69+
PoolPickerStamp(std::vector<std::shared_ptr<StampBaseStr>> new_pool);
70+
~PoolPickerStamp() {fprintf(stderr, "DESTROY!\n");};
71+
72+
std::string ExtractStr(std::shared_ptr<Blob> blob) override;
73+
virtual void add_weak(std::shared_ptr<StampBaseStr> stamp);
74+
virtual bool isRecursive();
75+
virtual int minSize() override;
76+
virtual int maxSize() override;
77+
};
78+
79+
80+
PoolPickerStamp::PoolPickerStamp(std::vector<std::shared_ptr<StampBaseStr>> new_pool)
81+
: pool{new_pool}
82+
{
83+
for(auto stamp : pool)
84+
{
85+
std::weak_ptr<StampBaseStr> wp = stamp;
86+
weak_pool.push_back(wp);
87+
}
88+
}
89+
90+
bool
91+
PoolPickerStamp::isRecursive()
92+
{
93+
if(is_recursive || is_in_recursion)
94+
return true;
95+
is_in_recursion = true;
96+
for(auto stamp : pool)
97+
{
98+
if (stamp->isRecursive())
99+
{
100+
is_recursive = true; // Once recursive -- recursive forever.
101+
is_in_recursion = false;
102+
return true;
103+
}
104+
}
105+
is_in_recursion = false;
106+
return false;
107+
}
108+
109+
std::string
110+
PoolPickerStamp::ExtractStr(std::shared_ptr<Blob> blob)
111+
{
112+
fprintf(stderr, "*");
113+
static ORACLE_STAMP stamp_oracle;
114+
ORACLE_TYPE oracle = stamp_oracle.ExtractValue(blob);
115+
116+
std::vector<std::weak_ptr<StampBaseStr>> target_pool;
117+
for(auto stamp : weak_pool)
118+
{
119+
if (stamp.lock()->minSize() <= blob->Size())
120+
{
121+
target_pool.push_back(stamp);
122+
}
123+
}
124+
125+
size_t index = Proportion(oracle, 0, target_pool.size() - 1);
126+
return target_pool[index].lock()->ExtractStr(blob);
127+
}
128+
129+
int
130+
PoolPickerStamp::minSize()
131+
{
132+
int res = INT_MAX;
133+
/* Do not check is_recursive here: even if stamp is known to be recursive we
134+
* still should iterate all his non-recursive children to find real minimal
135+
* size */
136+
if (is_in_recursion)
137+
return res;
138+
is_in_recursion = 1; /* Do not use isRecursive() inside as it uses same flag*/
139+
for(auto stamp : pool)
140+
{
141+
int candidat = stamp->minSize();
142+
if (res > candidat)
143+
res = candidat;
144+
}
145+
is_in_recursion = 0;
146+
res += ORACLE_SIZE;
147+
return res;
148+
}
149+
150+
int
151+
PoolPickerStamp::maxSize()
152+
{
153+
int res = 0;
154+
if (is_recursive || is_in_recursion)
155+
return -1;
156+
is_in_recursion = 1; /* Do not use isRecursive() inside as it uses same flag*/
157+
for(auto stamp : pool)
158+
{
159+
int candidat = stamp->maxSize();
160+
if (candidat == -1)
161+
return -1;
162+
if (res < candidat)
163+
res = candidat;
164+
}
165+
is_in_recursion = 0;
166+
res += ORACLE_SIZE;
167+
return res;
168+
}
169+
170+
void
171+
PoolPickerStamp::add_weak(std::shared_ptr<StampBaseStr> stamp)
172+
{
173+
weak_pool.push_back(stamp);
174+
}
175+
176+
class StampJSONInt : public virtual StampArithm<long int>
177+
{
178+
};
179+
180+
class StampJSONFloat : public virtual StampArithm<double>
181+
{
182+
};
183+
184+
class StampJSONString : public virtual StampDictT<DictLCAlphaSmall>
185+
{
186+
protected:
187+
public:
188+
std::string ExtractStr(std::shared_ptr<Blob> blob) override;
189+
};
190+
191+
192+
std::string
193+
StampJSONString::ExtractStr(std::shared_ptr<Blob> blob)
194+
{
195+
std::string res = "\"" + StampDictT<DictLCAlphaSmall>::ExtractStr(blob) +"\"";
196+
return res;
197+
}
198+
199+
200+
class StampJSONArray: public StampStrEnumerator
201+
{
202+
private:
203+
public:
204+
StampJSONArray(std::shared_ptr<PoolPickerStamp> picker)
205+
:StampStrEnumerator(picker, ", ", "[", "]") {};
206+
};
207+
208+
209+
210+
int
211+
main()
212+
{
213+
// auto dict =std::make_shared<DictLCAlphaSmall>();
214+
// auto stamp_d = std::make_shared<StampDict>(dict);
215+
// auto stamp_d = std::make_shared<StampDictT<DictLCAlphaSmall>>();
216+
// auto stamp_i = std::make_shared<StampArithm<long int>>();
217+
// auto stamp_f = std::make_shared<StampArithm<float>>();
218+
//
219+
auto stamp_d = std::make_shared<StampJSONString>();
220+
auto stamp_i = std::make_shared<StampJSONInt>();
221+
auto stamp_f = std::make_shared<StampJSONFloat>();
222+
223+
224+
// PoolPickerStamp stamp({stamp_i, stamp_f, stamp_d});
225+
std::shared_ptr<PoolPickerStamp> picker(new PoolPickerStamp({stamp_i, stamp_f, stamp_d}));
226+
// picker->add_weak(picker);
227+
auto stamp_a = std::make_shared<StampJSONArray>(picker);
228+
picker->add_weak(stamp_a);
229+
230+
231+
std::shared_ptr<Blob> blob = std::make_shared<Blob>((char *)bin_sample, strlen((char *)bin_sample));
232+
233+
234+
fprintf(stderr,"%i %i \n",stamp_a->minSize(), stamp_a->maxSize());
235+
// for(int i =0; i<25; i++)
236+
{
237+
std::string s = stamp_a->ExtractStr(blob);
238+
239+
fprintf(stderr,"%i %s\n",picker->isRecursive(), s.c_str());
240+
}
241+
242+
243+
244+
TEST_START(6);
245+
{
246+
is(Proportion(0,0,255), 0);
247+
is(Proportion(255,0,255), 0);
248+
is(Proportion(256,0,255), 1);
249+
is(Proportion(65535,0,255), 255);
250+
is(Proportion(65535-255,0,255), 255);
251+
is(Proportion(65535-256,0,255), 254);
252+
253+
}
254+
TEST_END;
255+
}

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