query.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #ifndef QUERY_H
  2. #define QUERY_H
  3. #include "path.h"
  4. namespace binom {
  5. enum class QProp : ui8 {
  6. type,
  7. type_class,
  8. value_type,
  9. element_count,
  10. index,
  11. name,
  12. value,
  13. sub_exp,
  14. };
  15. enum class QOper : ui8 {
  16. equal,
  17. not_equal,
  18. highter,
  19. highter_equal,
  20. lower,
  21. lower_equal,
  22. in_range,
  23. out_range,
  24. reg_test, //!< Regular expression test
  25. contains_values,
  26. contains_memory
  27. };
  28. enum class QRel : ui8 {
  29. AND,
  30. OR,
  31. XOR
  32. };
  33. enum class QValType : ui8 {
  34. type,
  35. type_class,
  36. value_type,
  37. number,
  38. range,
  39. string
  40. };
  41. struct Range {
  42. i64 from;
  43. i64 to;
  44. bool in(i64 number) const {return number >= from && number <= to;}
  45. bool out(i64 number) const {return number < from || number > to;}
  46. };
  47. /*
  48. { // Query Initer
  49. {QProp::..., ?{path...}, QOper::..., QValue(...), QRel::...}, // Expression lvl#0
  50. {{ // Expression lvl#0
  51. {QProp::..., ?{path...}, QOper::..., QValue(...), QRel::...}, // SubExpression lvl#1
  52. {QProp::..., ?{path...}, QOper::..., QValue(...), QRel::...}, // SubExpression lvl#1
  53. {QProp::..., ?{path...}, QOper::..., QValue(...), QRel::...}, // SubExpression lvl#1
  54. {{ // SubExpression lvl#1
  55. {QProp::..., ?{path...}, QOper::..., QValue(...), QRel::...}, // SubExpression lvl#2
  56. {QProp::..., ?{path...}, QOper::..., QValue(...), QRel::...}, // SubExpression lvl#2
  57. {QProp::..., ?{path...}, QOper::..., QValue(...), QRel::...}, // SubExpression lvl#2
  58. }, QRel::...}
  59. }, QRel::...}
  60. }
  61. // Data model
  62. [flag][path][value]
  63. [flag][value:
  64. [size]
  65. [flag][path][value]
  66. [flag][path][value]
  67. [flag][path][value]
  68. [flag][value:
  69. [size]
  70. [flag][path][value]
  71. [flag][path][value]
  72. [flag][path][value]
  73. ]
  74. ]
  75. */
  76. class Query {
  77. struct Flag;
  78. struct QValue;
  79. public:
  80. class QueryLiteral;
  81. class QueryEpression;
  82. class iterator;
  83. private:
  84. typedef std::initializer_list<QueryLiteral> QExprInitList;
  85. ByteArray data;
  86. static ByteArray buildSubexpression(QExprInitList& exprs);
  87. Query(ByteArray data);
  88. public:
  89. Query(QExprInitList exprs);
  90. Query(const Query& other);
  91. Query(Query&& other);
  92. Query& operator=(const Query& other);
  93. Query& operator=(Query&& other);
  94. bool isEmpty() const;
  95. iterator begin();
  96. iterator end();
  97. ByteArray toByteArray() const;
  98. static Query fromByteArray(ByteArray data);
  99. static Query fromString(std::string str);
  100. };
  101. struct Query::Flag {
  102. // Octet #1
  103. QProp prop : 3;
  104. QOper oper : 4;
  105. bool has_path : 1;
  106. // Octet #2
  107. QValType val_type : 3;
  108. QRel next_rel : 2;
  109. Flag(QProp prop, QOper oper, bool has_path, QValType val_type, QRel next_rel);
  110. };
  111. struct Query::QValue {
  112. QValType value_type;
  113. union Data {
  114. VarType type;
  115. VarTypeClass type_class;
  116. ValType value_type;
  117. i64 number;
  118. Range range;
  119. BufferArray string; // or subexpression
  120. ~Data();
  121. Data(VarType type);
  122. Data(VarTypeClass type_class);
  123. Data(ValType value_type);
  124. Data(i64 number);
  125. Data(Range range);
  126. Data(BufferArray string);
  127. Data(QValType vtype, const Data& other);
  128. } data;
  129. QValue(VarType type);
  130. QValue(VarTypeClass type_class);
  131. QValue(ValType value_type);
  132. QValue(i64 number);
  133. QValue(Range range);
  134. QValue(BufferArray string);
  135. QValue(const char* c_str);
  136. QValue(QValue&& other);
  137. ~QValue();
  138. };
  139. class Query::QueryLiteral {
  140. Flag flag;
  141. Path path;
  142. QValue value;
  143. friend ByteArray Query::buildSubexpression(Query::QExprInitList& exprs);
  144. public:
  145. QueryLiteral(QProp prop, Path path, QOper oper, QValue val, QRel next_rel = QRel::AND);
  146. QueryLiteral(QProp prop, QOper oper, QValue val, QRel next_rel = QRel::AND);
  147. QueryLiteral(QExprInitList subexprs, QRel next_rel = QRel::AND);
  148. QueryLiteral(const QueryLiteral&) = delete;
  149. QueryLiteral(QueryLiteral&&) = delete;
  150. };
  151. class Query::iterator {
  152. ByteArray::iterator it;
  153. public:
  154. iterator(ByteArray::iterator it);
  155. iterator(iterator& other);
  156. iterator(iterator&& other);
  157. QueryEpression& operator*();
  158. QueryEpression* operator->();
  159. iterator& operator++();
  160. iterator operator++(int);
  161. bool operator==(iterator other) const;
  162. bool operator!=(iterator other) const;
  163. bool operator>(iterator other) const;
  164. bool operator>=(iterator other) const;
  165. bool operator<(iterator other) const;
  166. bool operator<=(iterator other) const;
  167. };
  168. class Query::QueryEpression {
  169. ByteArray::iterator it;
  170. Flag& getFlag();
  171. byte* getDataPointer();
  172. byte* getPathPointer();
  173. public:
  174. // Flag getters
  175. QProp getProp();
  176. QOper getOper();
  177. bool hasPath();
  178. QValType getQValType();
  179. QRel getNextRel();
  180. Path getPath();
  181. // Value getters
  182. VarType getVarType();
  183. VarTypeClass getVarTypeClass();
  184. ValType getValType();
  185. ui64 getUNumber();
  186. i64 getNumber();
  187. Range getRange();
  188. BufferArray getString();
  189. iterator begin();
  190. iterator end();
  191. };
  192. typedef std::initializer_list<Query::QueryLiteral> query;
  193. typedef Query::QueryLiteral qexpr;
  194. }
  195. #endif // QUERY_H