Commit 87d686d7 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

SeriesAdapter WIP...

parent c0331479
Loading
Loading
Loading
Loading
+7 −6
Original line number Diff line number Diff line
@@ -53,8 +53,8 @@ public:
    return type_;
  }

  void addSeries(ResultList* series) {
    series_.push_back(series);
  void addSelectStatement(Executable* select_stmt) {
    select_stmts_.push_back(select_stmt);
  }

  void addAxisStatement(AxisStatement* axis_stmt) {
@@ -65,9 +65,10 @@ public:

  template <typename T>
  void executeDrawable(T* drawable) {
    for (auto series : series_) {
      SeriesAdapter<T> series_adapter(drawable);
      series_adapter.addSeries(series);
    for (const auto& stmt : select_stmts_) {
      SeriesAdapter<T> series_adapter(drawable, stmt);
      stmt->setTarget(&series_adapter);
      stmt->execute();
    }

    for (const auto& axis_stmt : axis_stmts_) {
@@ -76,7 +77,7 @@ public:
  }

protected:
  std::vector<ResultList*> series_;
  std::vector<Executable*> select_stmts_;
  std::vector<AxisStatement*> axis_stmts_;
  kDrawStatementType type_;
};
+12 −0
Original line number Diff line number Diff line
@@ -26,5 +26,17 @@ bool Executable::emitRow(SValue* row, int row_len) {
  return target_->nextRow(row, row_len);
}

int Executable::getColumnIndex(const std::string& column_name) const {
  const auto& columns = getColumns();

  for (int i = 0; i < columns.size(); ++i) {
    if (columns[i] == column_name) {
      return i;
    }
  }

  return -1;
}

}
}
+1 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ public:

  virtual size_t getNumCols() const = 0;
  virtual const std::vector<std::string>& getColumns() const = 0;
  int getColumnIndex(const std::string& column_name) const;

  void setTarget(RowSink* target);

+5 −3
Original line number Diff line number Diff line
@@ -67,12 +67,14 @@ void Query::execute() {

    auto target = new ResultList();
    target->addHeader(stmt->getColumns());
    stmt->setTarget(target);
    stmt->execute();
    results_.emplace_back(target);

    if (current_draw_statement != nullptr) {
      current_draw_statement->addSeries(target);
      // FIXPAUL copy results into target...
      current_draw_statement->addSelectStatement(stmt.get());
    } else {
      stmt->setTarget(target);
      stmt->execute();
    }
  }

+98 −14
Original line number Diff line number Diff line
@@ -18,42 +18,121 @@ namespace fnordmetric {
namespace query {

template <typename T>
class SeriesAdapter {
public:
class SeriesAdapter2D : public RowSink {

  SeriesAdapter(T* drawable) : drawable_(drawable) {}

  void addSeries(ResultList* series) {
    if (series->getNumRows() == 0) {
      return;
    }
};

    int x_ind = series->getColumnIndex("x");
    int y_ind = series->getColumnIndex("y");
    int z_ind = series->getColumnIndex("z");
    int name_ind = series->getColumnIndex("series");
template <typename T>
class SeriesAdapter : public RowSink {
public:

    if (name_ind < 0) {
  SeriesAdapter(
      T* drawable,
      Executable* stmt) :
      drawable_(drawable),
      stmt_(stmt) {
    x_ind_ = stmt->getColumnIndex("x");
    y_ind_ = stmt->getColumnIndex("y");
    z_ind_ = stmt->getColumnIndex("z");
    name_ind_ = stmt->getColumnIndex("series");

    if (name_ind_ < 0) {
      RAISE(
          util::RuntimeException,
          "can't draw SELECT because it has no 'series' column");
    }

    if (x_ind < 0) {
    if (x_ind_ < 0) {
      RAISE(
          util::RuntimeException,
          "can't draw SELECT because it has no 'x' column");
    }

    if (y_ind < 0) {
    if (y_ind_ < 0) {
      RAISE(
          util::RuntimeException,
          "can't draw SELECT because it has no 'y' column");
    }
  }

  bool nextRow(SValue* row, int row_len) {

  }

/*
  void addSeries(ResultList* series) {
    if (z_ind < 0) {
      addSeries2D(series, name_ind, x_ind, y_ind);
    }
  }

protected:

  void addSeries2D(
      ResultList* series,
      int name_ind,
      int x_ind,
      int y_ind) {
    auto first_row = series->getRow(0);

    if (testSeriesSchema2D<double, double>(
            first_row[x_ind],
            first_row[y_ind])) {
      return copySeries2D<double, double>(series, name_ind, x_ind, y_ind);
    }
  }

  template <typename TX, typename TY>
  bool testSeriesSchema2D(std::string x, std::string y) const {
    return (testType<TX>(x) && testType<TY>(y));
  }

  template <typename TX, typename TY>
  void copySeries2D(
      ResultList* series,
      int name_ind,
      int x_ind,
      int y_ind) {
    std::unordered_map<std::string, Series2D<D1, D2>*> series_map;
    std::vector<Series2D<D1, D2>*> series_list;

    for (int i = 0; i < series->getNumRows(); ++i) {
      const auto& row = series->getRow(i);
      std::string name = "unnamed";

      if (name_ind >= 0) {
        name = row[name_ind].getValue<std::string>();
      }

      Series2D<D1, D2>* series = nullptr;
      const auto& series_iter = series_map.find(name);
      if (series_iter == series_map.end()) {
        series = new Series2D<D1, D2>(name);
        series_map[name] = series;
        series_list.push_back(series);
      } else {
        series = series_iter->second;
      }

      series->addDatum(
          row[x_ind].getValue<D1>(),
          row[y_ind].getValue<D2>());
    }

    for (auto series : series_list) {
      drawable->addSeries(series);
    }
  }

  template <typename TV>
  bool testType(std::string value) const {
    return true;
  }

  template <typename TV>
  TV castType(std::string value) const;
*/
/*

  void executeDrawable( {
@@ -236,6 +315,11 @@ public:
*/
protected:
  T* drawable_;
  Executable* stmt_;
  int name_ind_;
  int x_ind_;
  int y_ind_;
  int z_ind_;
};

}