Skip to content

Commit 830f013

Browse files
author
ldn-softdev
committed
reinstated NS propagation interleaved way (was lost after NS redesign)
1 parent 16204be commit 830f013

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

jtc.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,11 @@ class map_jnse: public Json::map_jne {
192192
public:
193193

194194
#define NSOPT \
195-
NsReferAll, /* reference all ns values - local and remote */\
196-
NsMove, /* move local ns values, reference remote entriess */\
197-
NsUpdate, /* same as NsMove, but for non-existent (new) entries only*/\
198-
NsMoveAll /* move all ns values - local & remote */
195+
NsUpdateRef, /* reference all ns values - only non-existent ones */\
196+
NsReferAll, /* reference all ns values - local and remote */\
197+
NsMove, /* move local ns values, reference remote entriess */\
198+
NsUpdate, /* same as NsMove, but for non-existent (new) entries only*/\
199+
NsMoveAll /* move all ns values - local & remote */
199200
ENUM(NsOpType, NSOPT)
200201

201202
void sync_out(Json::map_jne &to, NsOpType nsopt);
@@ -208,11 +209,16 @@ class map_jnse: public Json::map_jne {
208209

209210
void map_jnse::sync_out(Json::map_jne &to, NsOpType nsopt) {
210211
// sync all entries from this map to Json namespace `to``
211-
if(nsopt == NsOpType::NsReferAll) { // reference all entries
212+
if(nsopt <= NsOpType::NsReferAll) { // reference all entries
212213
for(auto &ns: *this) { // for all entries in this map
213214
if(ns.KEY.front() < ' ' and ns.KEY.front() > '\0') continue; // sync only valid namespaces
214-
auto ip = to.emplace(ns.KEY, Json::JnEntry{}); // ip: insertion pair
215-
ip.first->VALUE.ptr(&ns.VALUE.ref()); // override existing or refer new
215+
auto found = to.find(ns.KEY);
216+
if(found == to.end())
217+
to.emplace(ns.KEY, Json::JnEntry{}).first->VALUE.ptr(&ns.VALUE.ref());
218+
else {
219+
if(nsopt == NsOpType::NsUpdateRef) continue;
220+
found->VALUE.ptr(&ns.VALUE.ref());
221+
}
216222
}
217223
return;
218224
}
@@ -236,7 +242,7 @@ void map_jnse::sync_out(Json::map_jne &to, NsOpType nsopt) {
236242

237243
void map_jnse::sync_in(Json::map_jne &from, NsOpType nsopt) {
238244
// sync all entries `from` map according to given handling type `nsopt`
239-
if(nsopt == NsOpType::NsReferAll) { // reference all entries
245+
if(nsopt <= NsOpType::NsReferAll) { // reference all entries
240246
for(auto &ns: from) { // for all entries in from
241247
if(ns.KEY.front() < ' ' and ns.KEY.front() > '\0') continue; // sync only valid namespaces
242248
auto mp = emplace(ns.KEY, Json::JnEntry{}); // mp: my pair
@@ -479,6 +485,8 @@ class Jtc {
479485

480486
size_t group{0};
481487
size_t counter{0};
488+
489+
COUTABLE(Grouping, group, counter) // for debugs only
482490
};
483491

484492

@@ -634,6 +642,7 @@ class Jtc {
634642
size_t jscur_{0}; // current walk in jsrc_
635643
map<Json::iterator*, map_jnse>
636644
wns_; // namespaces for walked (-w) paths
645+
map_jnse ins_; // holds interleaved namespaces
637646
Json::iterator * last_dwi_ptr_{nullptr};
638647
Jnode hwlk_{ARY{STR{}}}; // last walked value (interpolated)
639648
set<string> c2a_; // used in jsonized_output_obj_()
@@ -1947,6 +1956,7 @@ void Jtc::output_by_iterator(Json::iterator &wi, Grouping grp) {
19471956
wns_[&wi][WLK_HPFX] = move(hwlk_[0]);
19481957
hwlk_[0].type(Jnode::Neither);
19491958
}
1959+
19501960
if(opt()[CHR(OPT_TMP)].hits() > 0) { // interpolate each tmp per walk
19511961
// in all other cases - templates are round-robin applied onto each walk iteration
19521962
if(is_tpw_) // template per walk:
@@ -1958,10 +1968,13 @@ void Jtc::output_by_iterator(Json::iterator &wi, Grouping grp) {
19581968
opt()[CHR(OPT_TMP)].str(tpw_.size() + 1));
19591969
size_t tmp_idx = is_tpw_?
19601970
wi.walk_uid(): key_++ % (opt()[CHR(OPT_TMP)].size() - 1);
1961-
tmp = Json::interpolate(tpw_[tmp_idx], wi, wns_[&wi]);
1971+
ins_.sync_out(wns_[&wi], map_jnse::NsUpdateRef); // this is done so that walks could
1972+
tmp = Json::interpolate(tpw_[tmp_idx], wi, wns_[&wi]); // propagate namespaces interleaved
1973+
ins_.sync_in(wns_[&wi], map_jnse::NsReferAll); // way rather than sequentially
19621974
if(use_hpfx_) hwlk_ = move(ARY{tmp.root()});
19631975
}
19641976

1977+
19651978
if(use_hpfx_ and hwlk_[0].is_neither()) { // templating failed or was none
19661979
if(wi->has_label()) hwlk_ = move(OBJ{LBL{wi->label(), *wi}}); // preserve also the label if exist
19671980
else hwlk_ = move(ARY{*wi});
@@ -2618,9 +2631,10 @@ void Jtc::walk_interleaved_(wlk_subscr Jtc::* subscriber) {
26182631
subscriber_ = subscriber;
26192632
deque<deq_jit> wpi; // wpi holds queues of iterators
26202633

2621-
cr_.global_ns().sync_out(json().ns(), map_jnse::NsOpType::NsReferAll);
26222634

26232635
for(const auto &walk_str: opt()[CHR(OPT_WLK)]) { // process each -w arguments
2636+
cr_.global_ns().sync_out(json().clear_ns().ns(), map_jnse::NsOpType::NsReferAll);
2637+
26242638
// wpi: push back deq_jit init'ed with -w inited walk
26252639
wpi.push_back( deq_jit{json().walk(walk_str.find_first_not_of(" ") == string::npos?
26262640
"": walk_str, Json::Keep_cache)} );
@@ -2630,9 +2644,7 @@ void Jtc::walk_interleaved_(wlk_subscr Jtc::* subscriber) {
26302644
}
26312645
auto & dwi = wpi.back(); // dwi: deque<Json::iterator>
26322646
while(dwi.back() != dwi.back().end()) { // extend all iterators until end
2633-
//cerr << " extending interators" << endl;
2634-
if(wns_.empty()) wns_[&dwi.back()].sync_in(json().ns(), map_jnse::NsOpType::NsMoveAll);
2635-
else wns_[&dwi.back()].sync_in(json().ns(), map_jnse::NsOpType::NsMove);
2647+
wns_[&dwi.back()].sync_in(json().ns(), map_jnse::NsMove);
26362648
if(use_hpfx_) {
26372649
wns_[&dwi.back()][WLK_RSTH] = BUL{wns_[&dwi.back()].erase(WLK_HPFX)? true: false};
26382650
if(dwi.back()->has_label()) hwlk_ = move(OBJ{ LBL{dwi.back()->label(), *dwi.back()} });
@@ -2644,11 +2656,11 @@ void Jtc::walk_interleaved_(wlk_subscr Jtc::* subscriber) {
26442656
++dwi.back(); // json.ns now is partial
26452657
}
26462658
dwi.pop_back(); // remove last (->end()) iterator
2659+
// json.ns() may hold now the latest updated namespace while wns_ might not even have been
2660+
// updated (e.g.: when walk has ended as <>F, or out of iterations), thus require syncing
2661+
wns_[last_dwi_ptr_].sync_in(json().ns(), map_jnse::NsOpType::NsUpdate);
26472662
}
26482663
hwlk_ = move(ARY{STR{}}); // reset hwlk_ to init value
2649-
// json.ns() may hold now the latest updated namespace while wns_ might not even have been
2650-
// updated (e.g.: when walk has ended as <>F, or out of iterations), thus require syncing
2651-
wns_[last_dwi_ptr_].sync_in(json().ns(), map_jnse::NsOpType::NsUpdate);
26522664

26532665
is_multi_walk_ = opt()[CHR(OPT_WLK)].hits() > 1 or // i.e. -w.. -w.., else (one -w..)
26542666
wpi.size() > 1 or // wpi.size > 1, otherwise:

lib/extensions.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ bool operator==(const std::string &__a__, std::vector<const char *> __b__) {
280280
#define __STITCH_2TKNS__(X,Y) X ## Y
281281
#define STITCH_2TKNS(X,Y) __STITCH_2TKNS__(X, Y)
282282

283+
283284
#define __GUARD_1_ARG__(X) \
284285
__Guard_X__<decltype(X)> STITCH_2TKNS(__my_Guard_X__, __LINE__)(X);
285286
#define __GUARD_2_ARG__(X, Y) \
@@ -376,7 +377,6 @@ class __Guard_X__ {
376377

377378

378379

379-
380380
/*
381381
* A couple of definitions for mutexes:
382382
* ULOCK - declared a unique_lock
@@ -401,6 +401,8 @@ class __Guard_X__ {
401401

402402

403403

404+
405+
404406
/*
405407
* Provide a shorthand for containers with find method (e.g. std::map)
406408
* - quite often a temp variable found is created only for the purpose of the next check only
@@ -433,4 +435,3 @@ class __Guard_X__ {
433435

434436

435437

436-

0 commit comments

Comments
 (0)