hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SphinxFilters.cpp
Go to the documentation of this file.
1 #include <Poco/JSON/Object.h>
2 #include <Poco/JSON/Array.h>
3 #include <Poco/JSON/JSON.h>
4 #include <Poco/JSON/Stringifier.h>
5 #include <Poco/JSON/Parser.h>
6 #include <Poco/JSON/JSONException.h>
7 #include <Poco/Dynamic/Var.h>
8 
9 #include "SphinxError.hpp"
10 #include "SphinxFilters.hpp"
12 
13 namespace HCE
14 {
15 namespace sphinx
16 {
17 //-----------------------------------------------------------------------------
20 {
21 }
22 //-----------------------------------------------------------------------------
23 void SphinxFilter::addAttributeValue(const std::string& value)
24 {
25  attributeValues.push_back(value);
26 }
27 //-----------------------------------------------------------------------------
28 std::ostream& operator << (std::ostream& os, const SphinxFilter& filter)
29 {
30  os << "Filter type = " << (unsigned int)(filter.getFilterType()) << " AttributeName: '" << filter.attributeName << "' attributeValues: '";
31  for (size_t i=0;i<filter.attributeValues.size();++i)
32  {
33  os << filter.attributeValues[i];
34  if (i<filter.attributeValues.size()-1)
35  os << ", ";
36  }
37  os << " exclude: '" << std::boolalpha << static_cast<bool>(filter.excludeValue) << "'";
38  return os << std::endl;
39 }
40 //-----------------------------------------------------------------------------
42 {
43  filterType = sphinxFilter.getFilterType();
44  attributeName = sphinxFilter.getAttributeName();
45  attributeValues = const_cast<SphinxFilter&>(sphinxFilter).getAttributeValues();
46  excludeValue = sphinxFilter.getExcludeValue();
47  return *this;
48 }
49 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 :inherited(), vecFilters()
53 {
54  if (!json.empty())
55  unserialize(json);
56 }
57 //-----------------------------------------------------------------------------
59 {
60  vecFilters.clear();
61 }
62 //-----------------------------------------------------------------------------
63 void SphinxFiltersArray::addFilter(Poco::SharedPtr<SphinxFilter> pFilter)
64 {
65  vecFilters.push_back(pFilter);
66 }
67 //-----------------------------------------------------------------------------
69 {
70  vecFilters = const_cast<SphinxFiltersArray&>(filtersArray).getFilters();
71  return *this;
72 }
73 //-----------------------------------------------------------------------------
74 bool SphinxFiltersArray::serialize(std::string& json)
75 {
76  _isError = false;
77  errorMsg.clear();
78  try
79  {
80  Poco::JSON::Array::Ptr pArrayFilters = new Poco::JSON::Array();
81  for (size_t i=0;i<vecFilters.size();++i)
82  {
83  Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
84  obj->set(input_json_message_const::filterType, static_cast<unsigned int>(vecFilters[i]->getFilterType()));
85  obj->set(input_json_message_const::attributeName, vecFilters[i]->getAttributeName());
86 
87  Poco::JSON::Array::Ptr pArray = new Poco::JSON::Array();
89  for (size_t k=0;k<vecFilters[i]->getAttributeValues().size();++k)
90  pArray->add(vecFilters[i]->getAttributeValues()[k]);
91 
92  obj->set(input_json_message_const::excludeValue, static_cast<unsigned int>(vecFilters[i]->getExcludeValue()));
93  pArrayFilters->add(obj);
94  }
95 
96  std::stringstream ostr;
97  Poco::JSON::Stringifier::stringify(pArrayFilters, ostr);
98  json = ostr.str();
99 
100  }
101  catch(std::exception& e)
102  {
103  errorMsg = e.what();
105  _isError = true;
106  }
107  return !_isError;
108 }
109 //-----------------------------------------------------------------------------
110 bool SphinxFiltersArray::unserialize(const std::string& json)
111 {
112  vecFilters.clear();
113  errorMsg.clear();
114  _isError = false;
115  try
116  {
117  Poco::JSON::Parser parser;
118  Poco::Dynamic::Var res = parser.parse(json);
119  Poco::JSON::Array::Ptr pArray = res.extract<Poco::JSON::Array::Ptr>();
120 
121  for (size_t i=0;i<pArray->size();++i)
122  {
123  Poco::Dynamic::Var tmp = pArray->get(i);
124  if (tmp.type()==typeid(Poco::JSON::Object::Ptr))
125  {
126  Poco::SharedPtr<SphinxFilter> pFilter = new SphinxFilter;
127 
128  Poco::JSON::Object::Ptr obj = tmp.extract<Poco::JSON::Object::Ptr>();
129 
130  tmp = obj->get(input_json_message_const::filterType);
131  if(tmp.isInteger() && !tmp.isSigned())
132  {
133  unsigned long filterType = tmp.convert<unsigned long>();
134 
135  if(filterType > 3)
136  throw Poco::JSON::JSONException("Wrong filter type param. Input value = "+std::to_string(filterType));
137 
138  pFilter->setFilterType(static_cast<SphinxFilter::FilterType>(filterType));
139  }
140 
142  if (tmp.isString())
143  {
144  std::string attributeName = tmp.convert<std::string>();
145  if (attributeName.empty())
146  throw Poco::JSON::JSONException("Attribute name is empty.");
147  pFilter->setAttributeName(attributeName);
148  }
149 
150  Poco::JSON::Array::Ptr pArrayVal = obj->getArray(input_json_message_const::attributeValues);
151  if (!pArrayVal.isNull())
152  {
153  for (size_t j = 0;j <pArrayVal->size();++j)
154  {
155  tmp = pArrayVal->get(j);
156  if (tmp.isString())
157  pFilter->addAttributeValue(tmp.convert<std::string>());
158 
159  if(tmp.isInteger() && !tmp.isSigned())
160  pFilter->addAttributeValue(std::to_string(tmp.convert<Poco::UInt64>()));
161  }
162  if (pFilter->getAttributeValues().empty())
163  throw Poco::JSON::JSONException("Array attribute values is empty.");
164  }
165  else
166  throw Poco::JSON::JSONException (std::string("array '").append(input_json_message_const::attributeValues).append("' not found"));
167 
168  SphinxFilter::ExcludeType excludeValue = SphinxFilter::ExcludeType::etFalse;
170  if(tmp.isInteger() && !tmp.isSigned())
171  excludeValue = static_cast<SphinxFilter::ExcludeType>(tmp.convert<unsigned int>());
172 
173  if (tmp.isString())
174  excludeValue = static_cast<SphinxFilter::ExcludeType>(std::stoul(tmp.convert<std::string>()));
175 
176  pFilter->setExcludeValue(excludeValue);
177  addFilter(pFilter);
178  }
179  }
180  }
181  catch(Poco::JSON::JSONException& e)
182  {
183  errorMsg = e.displayText();
185  _isError = true;
186  }
187  catch(std::exception& e)
188  {
189  errorMsg = e.what();
191  _isError = true;
192  }
193  return !_isError;
194 }
195 //-----------------------------------------------------------------------------
196 std::ostream& operator << (std::ostream& os, const SphinxFiltersArray& filtersArray)
197 {
198  for (size_t i=0;i<filtersArray.vecFilters.size();++i)
199  os << *(filtersArray.vecFilters[i]);
200  return os << std::endl;
201 }
202 //-----------------------------------------------------------------------------
203 //-----------------------------------------------------------------------------
204 } /* namespace sphinx */
205 } /* namespace HCE */