JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
ISetProjector.h
Go to the documentation of this file.
1 //
2 // Created by Nico Schäfer on 11/14/17.
3 //
4 
5 #ifndef JODA_ISetPROJECTOR_H
6 #define JODA_ISetPROJECTOR_H
7 #include <rapidjson/pointer.h>
8 #include <vector>
10 
11 namespace joda::query {
17  public:
23  ISetProjector(const std::string &to) : ptr(to.c_str()), ptr_str(to){};
24 
25  virtual ~ISetProjector() = default;
26 
33  virtual void project(const RapidJsonDocument &json,
34  std::vector<std::unique_ptr<RJDocument>> &newDocs,
35  bool view = false) = 0;
36 
41  virtual std::string getToPointer() const final { return ptr_str; }
42 
47  RJPointer getRawToPointer() const { return ptr; }
48 
53  virtual std::string getType() = 0;
54 
60  virtual std::string toString() { return "'" + ptr_str + "':"; }
61 
66  virtual std::vector<std::string> getAttributes() const = 0;
67 
68  protected:
70  std::string ptr_str;
71 
72  unsigned long multiplicate(std::vector<std::unique_ptr<RJDocument>> &newDocs,
73  int times) {
74  auto origDocs = newDocs.size();
75  newDocs.reserve(origDocs * times);
76  // Duplicate existing documents |VAL|-1 times
77  for (auto i = 0; i < times - 1; ++i) {
78  for (size_t j = 0; j < origDocs; ++j) {
79  assert(j < newDocs.size() && j >= 0 && "Stay in range of array");
80  auto doc = std::make_unique<RJDocument>(&newDocs[j]->GetAllocator());
81  doc->CopyFrom(*newDocs[j], doc->GetAllocator());
82  newDocs.push_back(std::move(doc));
83  }
84  }
85  return origDocs;
86  }
87 
88  void fillArrayRangeWithVal(std::vector<std::unique_ptr<RJDocument>> &newDocs,
89  unsigned long origDocs, int i, RJValue &val) {
90  for (unsigned long j = origDocs * i; j < origDocs * (i + 1); ++j) {
91  assert(j < newDocs.size() && j >= 0 && "Stay in range of array");
92  RJValue newval;
93  newval.CopyFrom(val, newDocs.front()->GetAllocator());
94  ptr.Set(*newDocs[j], newval);
95  }
96  }
97 };
98 } // namespace joda::query
99 #endif // JODA_ISetPROJECTOR_H
rapidjson::GenericPointer< RJValue, RJBaseAlloc > RJPointer
Definition: RJFwd.h:30
rapidjson::GenericValue< RJChar, RJMemoryPoolAlloc > RJValue
Definition: RJFwd.h:29
Definition: RapidJsonDocument.h:22
Definition: ISetProjector.h:16
ISetProjector(const std::string &to)
Definition: ISetProjector.h:23
RJPointer getRawToPointer() const
Definition: ISetProjector.h:47
virtual ~ISetProjector()=default
virtual std::vector< std::string > getAttributes() const =0
void fillArrayRangeWithVal(std::vector< std::unique_ptr< RJDocument >> &newDocs, unsigned long origDocs, int i, RJValue &val)
Definition: ISetProjector.h:88
virtual std::string getToPointer() const final
Definition: ISetProjector.h:41
virtual std::string getType()=0
unsigned long multiplicate(std::vector< std::unique_ptr< RJDocument >> &newDocs, int times)
Definition: ISetProjector.h:72
RJPointer ptr
Definition: ISetProjector.h:69
std::string ptr_str
Definition: ISetProjector.h:70
virtual std::string toString()
Definition: ISetProjector.h:60
virtual void project(const RapidJsonDocument &json, std::vector< std::unique_ptr< RJDocument >> &newDocs, bool view=false)=0
Definition: AttributeStatAggregator.h:12