bool Plan::fromFile(const char* fileName) { std::ifstream file(fileName); if (!file.is_open()) return false; std::string token; file >> token; root = std::move(buildOperator(token)); parse(*root, file); return true; }
//iterate through stringstream from its end and parse operands(simple predicates) and operators(composite logical operations) Predicate* PredicateBuilder::build(string predicateLine) { predicate_stack = new stack<Predicate*>(); stringstream saver(""); bool operand = true; try { for (int i = predicateLine.length() - 1; i >= 0; i--) { if (predicateLine[i] == ')') continue; if (predicateLine[i] == ',' || predicateLine[i] == '(') { string op = saver.str(); /*rotiranje stringa*/ strRotate(op); if (operand) buildOperand(op); else buildOperator(op); saver.str(""); /*resetujem stream*/ if (predicateLine[i] == '(') operand = false; else operand = true; continue; } if (predicateLine[i] == ' ' && !operand) { continue; } saver << predicateLine[i]; } string op = saver.str(); strRotate(op); if (!operand) buildOperator(op); else buildOperand(op); root = predicate_stack->top(); predicate_stack->pop(); } catch (EXCEPTION* e) { root = nullptr; } delete predicate_stack; // delete memory allocated for stack return root; // return pointer to first operator in composite predicate }
static void parse(PlanOperator& op, std::ifstream& i) { switch (op.getOperatorType()) { case OperatorType::TableScan: { TableScan& t = static_cast<TableScan&>(op); i >> t.name; break; } case OperatorType::MergeJoin: { MergeJoin& m = static_cast<MergeJoin&>(op); readIntList(m.attributeIdsLeft, i); m.cmp = readComparator(i); readIntList(m.attributeIdsRight, i); std::string opCode; i >> opCode; std::unique_ptr<PlanOperator> l(std::move(buildOperator(opCode))); parse(*l, i); m.setLeft(l); i >> opCode; std::unique_ptr<PlanOperator> r(std::move(buildOperator(opCode))); parse(*r, i); m.setRight(r); break; } case OperatorType::Select: { Select& s = static_cast<Select&>(op); readIntList(s.attributeIds, i); s.cmp = readComparator(i); std::string constant; for (unsigned j=0, limit=s.attributeIds.size(); j<limit; ++j) { i >> constant; s.constants.push_back(constant); } std::string opCode; i >> opCode; std::unique_ptr<PlanOperator> c(std::move(buildOperator(opCode))); parse(*c, i); s.setChild(c); break; } case OperatorType::Project: { Project& p = static_cast<Project&>(op); readIntList(p.attributeIds, i); std::string opCode; i >> opCode; std::unique_ptr<PlanOperator> c(std::move(buildOperator(opCode))); parse(*c, i); p.setChild(c); break; } case OperatorType::Sort: { Sort& s = static_cast<Sort&>(op); readIntList(s.attributeIds, i); s.order = readSortOrder(i); std::string opCode; i >> opCode; std::unique_ptr<PlanOperator> c(std::move(buildOperator(opCode))); parse(*c, i); s.setChild(c); break; } default: throw; } }