<commit_before>#include <QMainWindow>
#include <QGridLayout>
#include <QButtonGroup>
#include <QHBoxLayout>
#include <QPushButton>
#include <QComboBox>
#include <QSpinBox>
#include <QScrollArea>
#include <QLabel>
#include <QFutureWatcher>
#include <QtConcurrent>
#include <QDir>
#include <QDate>
#include <QGroupBox>
#include <iterator>
#include <QProgressBar>
#include <QFileDialog>
#include <QStandardPaths>
#include <QSplitter>
#include <QSettings>

#include "protocol.h"
#include "heart_cell_sim.h"
#include "varmenu.h"
#include "graph.h"
#include "runWidget.h"
#include "chooseProtoWidget.h"

Simulation::Simulation(QWidget* parent){
//setup class variables
    this->parent = parent;
    date_time = QDate::currentDate().toString("MMddyy") + "-" + QTime::currentTime().toString("hhmm");
//    bool was_grid = false;
//create layouts
    main_layout = new QGridLayout(this);
//organizational widgets
    menu = new QStackedWidget();
    menu_options = new QListWidget();
//create Widgets
    QSplitter* main_splitter = new QSplitter();
    about_button = new QPushButton(QIcon::fromTheme("help-about"),"About");
    next_button = new QPushButton("Next");
    cancel_button = new QPushButton("Cancel");
    chooseProtoWidget* choose = new chooseProtoWidget(this);
    this->proto = choose->getCurrentProto();
    proto->datadir = (QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first() + "/data" + date_time).toStdString();
    proto->cellStateDir = proto->datadir;
    simvarMenu* sims = new simvarMenu(proto,QDir(proto->datadir.c_str()), this);
    dvarMenu* dvars = new dvarMenu(proto,QDir(proto->datadir.c_str()) , this);
    mvarMenu* mvars =  new mvarMenu(proto,QDir(proto->datadir.c_str()), this);
    pvarMenu* pvars =  new pvarMenu(proto,QDir(proto->datadir.c_str()), this);
    runWidget* run = new runWidget(proto,QDir(proto->datadir.c_str()));
//add items menu_list
    menu_list.append(choose);
    menu_list.append(sims);
    menu_list.append(pvars);
    menu_list.append(dvars);
    menu_list.append(mvars);
    menu_list.append(run);

    connect(choose, SIGNAL(protocolChanged(Protocol*)), sims, SLOT(changeProto(Protocol*)));
    connect(choose, SIGNAL(protocolChanged(Protocol*)), dvars, SLOT(changeProto(Protocol*)));
    connect(choose, SIGNAL(protocolChanged(Protocol*)), pvars, SLOT(changeProto(Protocol*)));
    connect(choose, SIGNAL(protocolChanged(Protocol*)), mvars, SLOT(changeProto(Protocol*)));
    connect(choose, SIGNAL(protocolChanged(Protocol*)), run, SLOT(setProto(Protocol*)));
    connect(choose, SIGNAL(protocolChanged(Protocol*)), this, SLOT(changeProto(Protocol*)));
    connect(choose, SIGNAL(cell_type_changed()), this, SIGNAL(cell_type_changed()));
    connect(this, SIGNAL(cell_type_changed()), choose, SLOT(cellChangedSlot()));
    connect(sims, SIGNAL(working_dir_changed(QDir&)), this, SIGNAL(working_dir_changed(QDir&)));
    connect(sims, SIGNAL(cell_type_changed()), this, SIGNAL(cell_type_changed()));
    connect(this, SIGNAL(working_dir_changed(QDir&)), sims, SLOT(setWorkingDir(QDir&)));
    connect(this, SIGNAL(working_dir_changed(QDir&)), dvars, SLOT(setWorkingDir(QDir&)));
    connect(this, SIGNAL(working_dir_changed(QDir&)), mvars, SLOT(setWorkingDir(QDir&)));
    connect(this, SIGNAL(working_dir_changed(QDir&)), pvars, SLOT(setWorkingDir(QDir&)));
    connect(this, SIGNAL(working_dir_changed(QDir&)), run, SLOT(setWorkingDir(QDir&)));
    connect(this, SIGNAL(cell_type_changed()), sims, SLOT(changeCellType()));
    connect(this, SIGNAL(cell_type_changed()), dvars, SLOT(reset()));
    connect(this, SIGNAL(cell_type_changed()), mvars, SLOT(reset()));
    connect(this, SIGNAL(cell_type_changed()), pvars, SLOT(reset()));
/*    connect(this, static_cast<void(Simulation::*)()>(&Simulation::cell_type_changed), [this,&was_grid,pvars,run] () {
        if((proto->cell->type == string("gridCell"))&&(!was_grid)) {
            int pvarsPos = menu_list.indexOf(pvars);
            menu_list.removeAt(pvarsPos);
            menu->removeWidget(pvars);
            menu_options->takeItem(pvarsPos);
            was_grid = true;
        } else if((proto->cell->type != string("gridCell"))&&was_grid) {
            int pvarsPos = menu_list.indexOf(run)-1;
            menu_list.insert(pvarsPos,pvars);
            menu->insertWidget(pvarsPos,pvars);
            menu_options->insertItem(pvarsPos,"Cell Initializers");
            was_grid = false;
        }
    });*/
    connect(run, SIGNAL(canceled()), this, SLOT(canceled()));
//    connect(run, SIGNAL(finished()), choose, SLOT(resetProto()));
    connect(run, SIGNAL(finished()), this, SLOT(finished()));
    connect(run, SIGNAL(running()), this, SLOT(running()));
    connect(run, static_cast<void(runWidget::*)()>(&runWidget::running), sims ,static_cast<void(simvarMenu::*)()>(&simvarMenu::write_file));
    connect(run, static_cast<void(runWidget::*)()>(&runWidget::running), dvars ,static_cast<void(dvarMenu::*)()>(&dvarMenu::write_file));
    connect(run, static_cast<void(runWidget::*)()>(&runWidget::running), mvars ,static_cast<void(mvarMenu::*)()>(&mvarMenu::write_file));
    connect(run, static_cast<void(runWidget::*)()>(&runWidget::running), pvars ,static_cast<void(pvarMenu::*)()>(&pvarMenu::write_file));
    connect(cancel_button, SIGNAL(clicked()),run, SLOT(cancel()));
//set button/combo box inital values
    cancel_button->hide();
//menu
    menu_options->addItem("Set Protocol");
    menu_options->item(menu_options->count()-1)->setToolTip("");
    menu_options->addItem("Set Sim. Parameters");
    menu_options->item(menu_options->count()-1)->setToolTip("Change model parameters (e.g. change an ion channel conductance)");
    menu_options->addItem("Set Model Parameters");
    menu_options->item(menu_options->count()-1)->setToolTip("");
    menu_options->addItem("Select Output");
    menu_options->item(menu_options->count()-1)->setToolTip("");
    menu_options->addItem("Select Measured Props.");
    menu_options->item(menu_options->count()-1)->setToolTip("Measure properties related to output variables (e.g. action potential duration, calcium amplitude)");
    menu_options->addItem("Run Simulation");
    menu_options->item(menu_options->count()-1)->setToolTip("");
    for(auto it = menu_list.begin(); it != menu_list.end(); it++) {
        QScrollArea* scrollWraper = new QScrollArea();
        scrollWraper->setWidget(*it);
        scrollWraper->setWidgetResizable(true);
        menu->addWidget(scrollWraper);
    }
//main_splitter
    main_splitter->addWidget(menu_options);
    main_splitter->addWidget(menu);
    QList<int> tempList = QList<int>();
    tempList << 10 << 200;
    main_splitter->setSizes(tempList);
//main_layout
    main_layout->addWidget(main_splitter, 0,0,-1,1);
    main_layout->addWidget(about_button, 0, 2);
    main_layout->addWidget(next_button, 2, 2);
    main_layout->addWidget(cancel_button, 2, 2);

    this->setWindowTitle("LongQt");
    this->showMaximized();
//connect buttons
    connect(about_button, &QPushButton::clicked, [this] () {
        if(QMessageBox::RestoreDefaults == QMessageBox::information(NULL,tr("LongQt Information"),tr("Copyright© 2016 Thomas J. Hund\nLicense: Revised BSD License\nEmail: thomas.hund@osumc.edu\nVersion 0.1\nContributers:\n\tThomas Hund\n\tBirce Ela Onal\n\tDanielle Beckley\n\tDaniel Gratz\n"),QMessageBox::Close|QMessageBox::RestoreDefaults,QMessageBox::Close)) {
            QSettings settings;
            settings.setValue("showHelp",true);
        }
    });
    connect(menu_options, SIGNAL(currentRowChanged(int)), this, SLOT(list_click_aciton(int)));
    connect(next_button, SIGNAL(clicked()), this, SLOT(next_button_aciton()));
};
Simulation::~Simulation(){};
void Simulation::changeProto(Protocol* proto) {
    this->proto = proto;
}
void Simulation::list_click_aciton (int next_row) {
    menu->setCurrentIndex(next_row);
    menu_options->setCurrentRow(next_row);
}
void Simulation::next_button_aciton () {
    int current_row = menu->currentIndex();
    if(menu->count() > current_row +1) {
        menu->setCurrentIndex(current_row +1);
        menu_options->setCurrentRow(current_row +1);
    }
}
void Simulation::canceled() {
    qDebug()<<"canceled!";
    QMessageBox::critical(this,"Cancel","Simulation canceled!");
    cancel_button->hide();
    next_button->show();
}
void Simulation::finished() {
    QMessageBox::information(0,"Folder", "Simulation is finished!\n The folder is named: " + QString(proto->datadir.c_str()));
    try {
        menu_list.append(new Dialog(QDir(proto->datadir.c_str()), this));
        menu->addWidget(menu_list.last());
        menu_options->addItem("Graph " + QFileInfo(proto->datadir.c_str()).baseName());
    } catch(badFile& e) {
        cerr << e.what() << ": " << "data files not readable" << endl;
    }
    date_time = QDate::currentDate().toString("MMddyy") + "-" + QTime::currentTime().toString("hhmm");
    QDir working_dir = (QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first() + "/data" + date_time);
    for(int i = 1; working_dir.exists(); i++) {
        working_dir = (QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first() + "/data" + date_time + "_" + QString::number(i));
    }
    proto->datadir = working_dir.absolutePath().toStdString();
    proto->cellStateDir = proto->datadir;
    emit working_dir_changed(working_dir);
    cancel_button->hide();
    next_button->show();
}
void Simulation::running() {
    next_button->hide();
    cancel_button->show();
}
<commit_msg>added hover texts for all list options<commit_after>#include <QMainWindow>
#include <QGridLayout>
#include <QButtonGroup>
#include <QHBoxLayout>
#include <QPushButton>
#include <QComboBox>
#include <QSpinBox>
#include <QScrollArea>
#include <QLabel>
#include <QFutureWatcher>
#include <QtConcurrent>
#include <QDir>
#include <QDate>
#include <QGroupBox>
#include <iterator>
#include <QProgressBar>
#include <QFileDialog>
#include <QStandardPaths>
#include <QSplitter>
#include <QSettings>

#include "protocol.h"
#include "heart_cell_sim.h"
#include "varmenu.h"
#include "graph.h"
#include "runWidget.h"
#include "chooseProtoWidget.h"

Simulation::Simulation(QWidget* parent){
//setup class variables
    this->parent = parent;
    date_time = QDate::currentDate().toString("MMddyy") + "-" + QTime::currentTime().toString("hhmm");
//    bool was_grid = false;
//create layouts
    main_layout = new QGridLayout(this);
//organizational widgets
    menu = new QStackedWidget();
    menu_options = new QListWidget();
//create Widgets
    QSplitter* main_splitter = new QSplitter();
    about_button = new QPushButton(QIcon::fromTheme("help-about"),"About");
    next_button = new QPushButton("Next");
    cancel_button = new QPushButton("Cancel");
    chooseProtoWidget* choose = new chooseProtoWidget(this);
    this->proto = choose->getCurrentProto();
    proto->datadir = (QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first() + "/data" + date_time).toStdString();
    proto->cellStateDir = proto->datadir;
    simvarMenu* sims = new simvarMenu(proto,QDir(proto->datadir.c_str()), this);
    dvarMenu* dvars = new dvarMenu(proto,QDir(proto->datadir.c_str()) , this);
    mvarMenu* mvars =  new mvarMenu(proto,QDir(proto->datadir.c_str()), this);
    pvarMenu* pvars =  new pvarMenu(proto,QDir(proto->datadir.c_str()), this);
    runWidget* run = new runWidget(proto,QDir(proto->datadir.c_str()));
//add items menu_list
    menu_list.append(choose);
    menu_list.append(sims);
    menu_list.append(pvars);
    menu_list.append(dvars);
    menu_list.append(mvars);
    menu_list.append(run);

    connect(choose, SIGNAL(protocolChanged(Protocol*)), sims, SLOT(changeProto(Protocol*)));
    connect(choose, SIGNAL(protocolChanged(Protocol*)), dvars, SLOT(changeProto(Protocol*)));
    connect(choose, SIGNAL(protocolChanged(Protocol*)), pvars, SLOT(changeProto(Protocol*)));
    connect(choose, SIGNAL(protocolChanged(Protocol*)), mvars, SLOT(changeProto(Protocol*)));
    connect(choose, SIGNAL(protocolChanged(Protocol*)), run, SLOT(setProto(Protocol*)));
    connect(choose, SIGNAL(protocolChanged(Protocol*)), this, SLOT(changeProto(Protocol*)));
    connect(choose, SIGNAL(cell_type_changed()), this, SIGNAL(cell_type_changed()));
    connect(this, SIGNAL(cell_type_changed()), choose, SLOT(cellChangedSlot()));
    connect(sims, SIGNAL(working_dir_changed(QDir&)), this, SIGNAL(working_dir_changed(QDir&)));
    connect(sims, SIGNAL(cell_type_changed()), this, SIGNAL(cell_type_changed()));
    connect(this, SIGNAL(working_dir_changed(QDir&)), sims, SLOT(setWorkingDir(QDir&)));
    connect(this, SIGNAL(working_dir_changed(QDir&)), dvars, SLOT(setWorkingDir(QDir&)));
    connect(this, SIGNAL(working_dir_changed(QDir&)), mvars, SLOT(setWorkingDir(QDir&)));
    connect(this, SIGNAL(working_dir_changed(QDir&)), pvars, SLOT(setWorkingDir(QDir&)));
    connect(this, SIGNAL(working_dir_changed(QDir&)), run, SLOT(setWorkingDir(QDir&)));
    connect(this, SIGNAL(cell_type_changed()), sims, SLOT(changeCellType()));
    connect(this, SIGNAL(cell_type_changed()), dvars, SLOT(reset()));
    connect(this, SIGNAL(cell_type_changed()), mvars, SLOT(reset()));
    connect(this, SIGNAL(cell_type_changed()), pvars, SLOT(reset()));
/*    connect(this, static_cast<void(Simulation::*)()>(&Simulation::cell_type_changed), [this,&was_grid,pvars,run] () {
        if((proto->cell->type == string("gridCell"))&&(!was_grid)) {
            int pvarsPos = menu_list.indexOf(pvars);
            menu_list.removeAt(pvarsPos);
            menu->removeWidget(pvars);
            menu_options->takeItem(pvarsPos);
            was_grid = true;
        } else if((proto->cell->type != string("gridCell"))&&was_grid) {
            int pvarsPos = menu_list.indexOf(run)-1;
            menu_list.insert(pvarsPos,pvars);
            menu->insertWidget(pvarsPos,pvars);
            menu_options->insertItem(pvarsPos,"Cell Initializers");
            was_grid = false;
        }
    });*/
    connect(run, SIGNAL(canceled()), this, SLOT(canceled()));
//    connect(run, SIGNAL(finished()), choose, SLOT(resetProto()));
    connect(run, SIGNAL(finished()), this, SLOT(finished()));
    connect(run, SIGNAL(running()), this, SLOT(running()));
    connect(run, static_cast<void(runWidget::*)()>(&runWidget::running), sims ,static_cast<void(simvarMenu::*)()>(&simvarMenu::write_file));
    connect(run, static_cast<void(runWidget::*)()>(&runWidget::running), dvars ,static_cast<void(dvarMenu::*)()>(&dvarMenu::write_file));
    connect(run, static_cast<void(runWidget::*)()>(&runWidget::running), mvars ,static_cast<void(mvarMenu::*)()>(&mvarMenu::write_file));
    connect(run, static_cast<void(runWidget::*)()>(&runWidget::running), pvars ,static_cast<void(pvarMenu::*)()>(&pvarMenu::write_file));
    connect(cancel_button, SIGNAL(clicked()),run, SLOT(cancel()));
//set button/combo box inital values
    cancel_button->hide();
//menu
    menu_options->addItem("Set Protocol");
    menu_options->item(menu_options->count()-1)->setToolTip("Choose the type of protocol to follow in the simulation (e.g. fix the cell membrane voltage)");
    menu_options->addItem("Set Sim. Parameters");
    menu_options->item(menu_options->count()-1)->setToolTip("Change model parameters (e.g. change an ion channel conductance)");
    menu_options->addItem("Set Model Parameters");
    menu_options->item(menu_options->count()-1)->setToolTip("Set model constants or have them randomly choosen");
    menu_options->addItem("Select Output");
    menu_options->item(menu_options->count()-1)->setToolTip("Select which model variables should be written to a file (e.g. Ca Concentration)");
    menu_options->addItem("Select Measured Props.");
    menu_options->item(menu_options->count()-1)->setToolTip("Measure properties related to output variables (e.g. action potential duration, calcium amplitude)");
    menu_options->addItem("Run Simulation");
    menu_options->item(menu_options->count()-1)->setToolTip("Optionally write a description of the simulation and run the simulation");
    for(auto it = menu_list.begin(); it != menu_list.end(); it++) {
        QScrollArea* scrollWraper = new QScrollArea();
        scrollWraper->setWidget(*it);
        scrollWraper->setWidgetResizable(true);
        menu->addWidget(scrollWraper);
    }
//main_splitter
    main_splitter->addWidget(menu_options);
    main_splitter->addWidget(menu);
    QList<int> tempList = QList<int>();
    tempList << 10 << 200;
    main_splitter->setSizes(tempList);
//main_layout
    main_layout->addWidget(main_splitter, 0,0,-1,1);
    main_layout->addWidget(about_button, 0, 2);
    main_layout->addWidget(next_button, 2, 2);
    main_layout->addWidget(cancel_button, 2, 2);

    this->setWindowTitle("LongQt");
    this->showMaximized();
//connect buttons
    connect(about_button, &QPushButton::clicked, [this] () {
        if(QMessageBox::RestoreDefaults == QMessageBox::information(NULL,tr("LongQt Information"),tr("Copyright© 2016 Thomas J. Hund\nLicense: Revised BSD License\nEmail: thomas.hund@osumc.edu\nVersion 0.1\nContributers:\n\tThomas Hund\n\tBirce Ela Onal\n\tDanielle Beckley\n\tDaniel Gratz\n"),QMessageBox::Close|QMessageBox::RestoreDefaults,QMessageBox::Close)) {
            QSettings settings;
            settings.setValue("showHelp",true);
        }
    });
    connect(menu_options, SIGNAL(currentRowChanged(int)), this, SLOT(list_click_aciton(int)));
    connect(next_button, SIGNAL(clicked()), this, SLOT(next_button_aciton()));
};
Simulation::~Simulation(){};
void Simulation::changeProto(Protocol* proto) {
    this->proto = proto;
}
void Simulation::list_click_aciton (int next_row) {
    menu->setCurrentIndex(next_row);
    menu_options->setCurrentRow(next_row);
}
void Simulation::next_button_aciton () {
    int current_row = menu->currentIndex();
    if(menu->count() > current_row +1) {
        menu->setCurrentIndex(current_row +1);
        menu_options->setCurrentRow(current_row +1);
    }
}
void Simulation::canceled() {
    qDebug()<<"canceled!";
    QMessageBox::critical(this,"Cancel","Simulation canceled!");
    cancel_button->hide();
    next_button->show();
}
void Simulation::finished() {
    QMessageBox::information(0,"Folder", "Simulation is finished!\n The folder is named: " + QString(proto->datadir.c_str()));
    try {
        menu_list.append(new Dialog(QDir(proto->datadir.c_str()), this));
        menu->addWidget(menu_list.last());
        menu_options->addItem("Graph " + QFileInfo(proto->datadir.c_str()).baseName());
    } catch(badFile& e) {
        cerr << e.what() << ": " << "data files not readable" << endl;
    }
    date_time = QDate::currentDate().toString("MMddyy") + "-" + QTime::currentTime().toString("hhmm");
    QDir working_dir = (QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first() + "/data" + date_time);
    for(int i = 1; working_dir.exists(); i++) {
        working_dir = (QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first() + "/data" + date_time + "_" + QString::number(i));
    }
    proto->datadir = working_dir.absolutePath().toStdString();
    proto->cellStateDir = proto->datadir;
    emit working_dir_changed(working_dir);
    cancel_button->hide();
    next_button->show();
}
void Simulation::running() {
    next_button->hide();
    cancel_button->show();
}
<|endoftext|>


<commit_before>// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/page_info_model.h"

#include <string>

#include "app/l10n_util.h"
#include "base/callback.h"
#include "base/i18n/time_formatting.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/cert_store.h"
#include "chrome/browser/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/ssl/ssl_manager.h"
#include "chrome/common/pref_names.h"
#include "grit/generated_resources.h"
#include "net/base/cert_status_flags.h"
#include "net/base/x509_certificate.h"

namespace {
  // Returns a name that can be used to represent the issuer.  It tries in this
  // order CN, O and OU and returns the first non-empty one found.
  std::string GetIssuerName(const net::X509Certificate::Principal& issuer) {
    if (!issuer.common_name.empty())
      return issuer.common_name;
    if (!issuer.organization_names.empty())
      return issuer.organization_names[0];
    if (!issuer.organization_unit_names.empty())
      return issuer.organization_unit_names[0];

    return std::string();
  }
}

PageInfoModel::PageInfoModel(Profile* profile,
                             const GURL& url,
                             const NavigationEntry::SSLStatus& ssl,
                             bool show_history,
                             PageInfoModelObserver* observer)
    : observer_(observer) {
  bool state = true;
  string16 head_line;
  string16 description;
  scoped_refptr<net::X509Certificate> cert;

  // Identity section.
  string16 subject_name(UTF8ToUTF16(url.host()));
  bool empty_subject_name = false;
  if (subject_name.empty()) {
    subject_name.assign(
        l10n_util::GetStringUTF16(IDS_PAGE_INFO_SECURITY_TAB_UNKNOWN_PARTY));
    empty_subject_name = true;
  }
  if (ssl.cert_id() &&
      CertStore::GetSharedInstance()->RetrieveCert(ssl.cert_id(), &cert) &&
      !net::IsCertStatusError(ssl.cert_status())) {
    // OK HTTPS page.
    if ((ssl.cert_status() & net::CERT_STATUS_IS_EV) != 0) {
      DCHECK(!cert->subject().organization_names.empty());
      head_line =
          l10n_util::GetStringFUTF16(IDS_PAGE_INFO_EV_IDENTITY_TITLE,
              UTF8ToUTF16(cert->subject().organization_names[0]),
              UTF8ToUTF16(url.host()));
      // An EV Cert is required to have a city (localityName) and country but
      // state is "if any".
      DCHECK(!cert->subject().locality_name.empty());
      DCHECK(!cert->subject().country_name.empty());
      string16 locality;
      if (!cert->subject().state_or_province_name.empty()) {
        locality = l10n_util::GetStringFUTF16(
            IDS_PAGEINFO_ADDRESS,
            UTF8ToUTF16(cert->subject().locality_name),
            UTF8ToUTF16(cert->subject().state_or_province_name),
            UTF8ToUTF16(cert->subject().country_name));
      } else {
        locality = l10n_util::GetStringFUTF16(
            IDS_PAGEINFO_PARTIAL_ADDRESS,
            UTF8ToUTF16(cert->subject().locality_name),
            UTF8ToUTF16(cert->subject().country_name));
      }
      DCHECK(!cert->subject().organization_names.empty());
      description.assign(l10n_util::GetStringFUTF16(
          IDS_PAGE_INFO_SECURITY_TAB_SECURE_IDENTITY_EV,
          UTF8ToUTF16(cert->subject().organization_names[0]),
          locality,
          UTF8ToUTF16(GetIssuerName(cert->issuer()))));
    } else {
      // Non EV OK HTTPS.
      if (empty_subject_name)
        head_line.clear();  // Don't display any title.
      else
        head_line.assign(subject_name);
      string16 issuer_name(UTF8ToUTF16(GetIssuerName(cert->issuer())));
      if (issuer_name.empty()) {
        issuer_name.assign(l10n_util::GetStringUTF16(
            IDS_PAGE_INFO_SECURITY_TAB_UNKNOWN_PARTY));
      } else {
        description.assign(l10n_util::GetStringFUTF16(
            IDS_PAGE_INFO_SECURITY_TAB_SECURE_IDENTITY, issuer_name));
      }
    }
  } else {
    // Bad HTTPS.
    description.assign(l10n_util::GetStringUTF16(
        IDS_PAGE_INFO_SECURITY_TAB_INSECURE_IDENTITY));
    state = false;
  }
  sections_.push_back(SectionInfo(
      state,
      l10n_util::GetStringUTF16(IDS_PAGE_INFO_SECURITY_TAB_IDENTITY_TITLE),
      head_line,
      description));

  // Connection section.
  // We consider anything less than 80 bits encryption to be weak encryption.
  // TODO(wtc): Bug 1198735: report mixed/unsafe content for unencrypted and
  // weakly encrypted connections.
  state = true;
  head_line.clear();
  description.clear();
  if (ssl.security_bits() <= 0) {
    state = false;
    description.assign(l10n_util::GetStringFUTF16(
        IDS_PAGE_INFO_SECURITY_TAB_NOT_ENCRYPTED_CONNECTION_TEXT,
        subject_name));
  } else if (ssl.security_bits() < 80) {
    state = false;
    description.assign(l10n_util::GetStringFUTF16(
        IDS_PAGE_INFO_SECURITY_TAB_WEAK_ENCRYPTION_CONNECTION_TEXT,
        subject_name));
  } else {
    description.assign(l10n_util::GetStringFUTF16(
        IDS_PAGE_INFO_SECURITY_TAB_ENCRYPTED_CONNECTION_TEXT,
        subject_name,
        IntToString16(ssl.security_bits())));
    if (ssl.displayed_insecure_content() || ssl.ran_insecure_content()) {
      state = false;
      description.assign(l10n_util::GetStringFUTF16(
          IDS_PAGE_INFO_SECURITY_TAB_ENCRYPTED_SENTENCE_LINK,
          description,
          l10n_util::GetStringUTF16(ssl.ran_insecure_content() ?
              IDS_PAGE_INFO_SECURITY_TAB_ENCRYPTED_INSECURE_CONTENT_ERROR :
              IDS_PAGE_INFO_SECURITY_TAB_ENCRYPTED_INSECURE_CONTENT_WARNING)));
    }
  }
  sections_.push_back(SectionInfo(
      state,
      l10n_util::GetStringUTF16(IDS_PAGE_INFO_SECURITY_TAB_CONNECTION_TITLE),
      head_line,
      description));

  // Request the number of visits.
  HistoryService* history = profile->GetHistoryService(
      Profile::EXPLICIT_ACCESS);
  if (show_history && history) {
    history->GetVisitCountToHost(
        url,
        &request_consumer_,
        NewCallback(this, &PageInfoModel::OnGotVisitCountToHost));
  }
}

int PageInfoModel::GetSectionCount() {
  return sections_.size();
}

PageInfoModel::SectionInfo PageInfoModel::GetSectionInfo(int index) {
  DCHECK(index < static_cast<int>(sections_.size()));
  return sections_[index];
}

void PageInfoModel::OnGotVisitCountToHost(HistoryService::Handle handle,
                                          bool found_visits,
                                          int count,
                                          base::Time first_visit) {
  if (!found_visits) {
    // This indicates an error, such as the page wasn't http/https; do nothing.
    return;
  }

  bool visited_before_today = false;
  if (count) {
    base::Time today = base::Time::Now().LocalMidnight();
    base::Time first_visit_midnight = first_visit.LocalMidnight();
    visited_before_today = (first_visit_midnight < today);
  }

  if (!visited_before_today) {
    sections_.push_back(SectionInfo(
        false,
        l10n_util::GetStringUTF16(
            IDS_PAGE_INFO_SECURITY_TAB_PERSONAL_HISTORY_TITLE),
        string16(),
        l10n_util::GetStringUTF16(
            IDS_PAGE_INFO_SECURITY_TAB_FIRST_VISITED_TODAY)));
  } else {
    sections_.push_back(SectionInfo(
        true,
        l10n_util::GetStringUTF16(
            IDS_PAGE_INFO_SECURITY_TAB_PERSONAL_HISTORY_TITLE),
        string16(),
        l10n_util::GetStringFUTF16(
            IDS_PAGE_INFO_SECURITY_TAB_VISITED_BEFORE_TODAY,
            WideToUTF16(base::TimeFormatShortDate(first_visit)))));
  }
  observer_->ModelChanged();
}

// static
void PageInfoModel::RegisterPrefs(PrefService* prefs) {
  prefs->RegisterDictionaryPref(prefs::kPageInfoWindowPlacement);
}
<commit_msg>Trivial change: update a comment.<commit_after>// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/page_info_model.h"

#include <string>

#include "app/l10n_util.h"
#include "base/callback.h"
#include "base/i18n/time_formatting.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/cert_store.h"
#include "chrome/browser/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/ssl/ssl_manager.h"
#include "chrome/common/pref_names.h"
#include "grit/generated_resources.h"
#include "net/base/cert_status_flags.h"
#include "net/base/x509_certificate.h"

namespace {
  // Returns a name that can be used to represent the issuer.  It tries in this
  // order CN, O and OU and returns the first non-empty one found.
  std::string GetIssuerName(const net::X509Certificate::Principal& issuer) {
    if (!issuer.common_name.empty())
      return issuer.common_name;
    if (!issuer.organization_names.empty())
      return issuer.organization_names[0];
    if (!issuer.organization_unit_names.empty())
      return issuer.organization_unit_names[0];

    return std::string();
  }
}

PageInfoModel::PageInfoModel(Profile* profile,
                             const GURL& url,
                             const NavigationEntry::SSLStatus& ssl,
                             bool show_history,
                             PageInfoModelObserver* observer)
    : observer_(observer) {
  bool state = true;
  string16 head_line;
  string16 description;
  scoped_refptr<net::X509Certificate> cert;

  // Identity section.
  string16 subject_name(UTF8ToUTF16(url.host()));
  bool empty_subject_name = false;
  if (subject_name.empty()) {
    subject_name.assign(
        l10n_util::GetStringUTF16(IDS_PAGE_INFO_SECURITY_TAB_UNKNOWN_PARTY));
    empty_subject_name = true;
  }
  if (ssl.cert_id() &&
      CertStore::GetSharedInstance()->RetrieveCert(ssl.cert_id(), &cert) &&
      !net::IsCertStatusError(ssl.cert_status())) {
    // OK HTTPS page.
    if ((ssl.cert_status() & net::CERT_STATUS_IS_EV) != 0) {
      DCHECK(!cert->subject().organization_names.empty());
      head_line =
          l10n_util::GetStringFUTF16(IDS_PAGE_INFO_EV_IDENTITY_TITLE,
              UTF8ToUTF16(cert->subject().organization_names[0]),
              UTF8ToUTF16(url.host()));
      // An EV Cert is required to have a city (localityName) and country but
      // state is "if any".
      DCHECK(!cert->subject().locality_name.empty());
      DCHECK(!cert->subject().country_name.empty());
      string16 locality;
      if (!cert->subject().state_or_province_name.empty()) {
        locality = l10n_util::GetStringFUTF16(
            IDS_PAGEINFO_ADDRESS,
            UTF8ToUTF16(cert->subject().locality_name),
            UTF8ToUTF16(cert->subject().state_or_province_name),
            UTF8ToUTF16(cert->subject().country_name));
      } else {
        locality = l10n_util::GetStringFUTF16(
            IDS_PAGEINFO_PARTIAL_ADDRESS,
            UTF8ToUTF16(cert->subject().locality_name),
            UTF8ToUTF16(cert->subject().country_name));
      }
      DCHECK(!cert->subject().organization_names.empty());
      description.assign(l10n_util::GetStringFUTF16(
          IDS_PAGE_INFO_SECURITY_TAB_SECURE_IDENTITY_EV,
          UTF8ToUTF16(cert->subject().organization_names[0]),
          locality,
          UTF8ToUTF16(GetIssuerName(cert->issuer()))));
    } else {
      // Non EV OK HTTPS.
      if (empty_subject_name)
        head_line.clear();  // Don't display any title.
      else
        head_line.assign(subject_name);
      string16 issuer_name(UTF8ToUTF16(GetIssuerName(cert->issuer())));
      if (issuer_name.empty()) {
        issuer_name.assign(l10n_util::GetStringUTF16(
            IDS_PAGE_INFO_SECURITY_TAB_UNKNOWN_PARTY));
      } else {
        description.assign(l10n_util::GetStringFUTF16(
            IDS_PAGE_INFO_SECURITY_TAB_SECURE_IDENTITY, issuer_name));
      }
    }
  } else {
    // HTTP or bad HTTPS.
    description.assign(l10n_util::GetStringUTF16(
        IDS_PAGE_INFO_SECURITY_TAB_INSECURE_IDENTITY));
    state = false;
  }
  sections_.push_back(SectionInfo(
      state,
      l10n_util::GetStringUTF16(IDS_PAGE_INFO_SECURITY_TAB_IDENTITY_TITLE),
      head_line,
      description));

  // Connection section.
  // We consider anything less than 80 bits encryption to be weak encryption.
  // TODO(wtc): Bug 1198735: report mixed/unsafe content for unencrypted and
  // weakly encrypted connections.
  state = true;
  head_line.clear();
  description.clear();
  if (ssl.security_bits() <= 0) {
    state = false;
    description.assign(l10n_util::GetStringFUTF16(
        IDS_PAGE_INFO_SECURITY_TAB_NOT_ENCRYPTED_CONNECTION_TEXT,
        subject_name));
  } else if (ssl.security_bits() < 80) {
    state = false;
    description.assign(l10n_util::GetStringFUTF16(
        IDS_PAGE_INFO_SECURITY_TAB_WEAK_ENCRYPTION_CONNECTION_TEXT,
        subject_name));
  } else {
    description.assign(l10n_util::GetStringFUTF16(
        IDS_PAGE_INFO_SECURITY_TAB_ENCRYPTED_CONNECTION_TEXT,
        subject_name,
        IntToString16(ssl.security_bits())));
    if (ssl.displayed_insecure_content() || ssl.ran_insecure_content()) {
      state = false;
      description.assign(l10n_util::GetStringFUTF16(
          IDS_PAGE_INFO_SECURITY_TAB_ENCRYPTED_SENTENCE_LINK,
          description,
          l10n_util::GetStringUTF16(ssl.ran_insecure_content() ?
              IDS_PAGE_INFO_SECURITY_TAB_ENCRYPTED_INSECURE_CONTENT_ERROR :
              IDS_PAGE_INFO_SECURITY_TAB_ENCRYPTED_INSECURE_CONTENT_WARNING)));
    }
  }
  sections_.push_back(SectionInfo(
      state,
      l10n_util::GetStringUTF16(IDS_PAGE_INFO_SECURITY_TAB_CONNECTION_TITLE),
      head_line,
      description));

  // Request the number of visits.
  HistoryService* history = profile->GetHistoryService(
      Profile::EXPLICIT_ACCESS);
  if (show_history && history) {
    history->GetVisitCountToHost(
        url,
        &request_consumer_,
        NewCallback(this, &PageInfoModel::OnGotVisitCountToHost));
  }
}

int PageInfoModel::GetSectionCount() {
  return sections_.size();
}

PageInfoModel::SectionInfo PageInfoModel::GetSectionInfo(int index) {
  DCHECK(index < static_cast<int>(sections_.size()));
  return sections_[index];
}

void PageInfoModel::OnGotVisitCountToHost(HistoryService::Handle handle,
                                          bool found_visits,
                                          int count,
                                          base::Time first_visit) {
  if (!found_visits) {
    // This indicates an error, such as the page wasn't http/https; do nothing.
    return;
  }

  bool visited_before_today = false;
  if (count) {
    base::Time today = base::Time::Now().LocalMidnight();
    base::Time first_visit_midnight = first_visit.LocalMidnight();
    visited_before_today = (first_visit_midnight < today);
  }

  if (!visited_before_today) {
    sections_.push_back(SectionInfo(
        false,
        l10n_util::GetStringUTF16(
            IDS_PAGE_INFO_SECURITY_TAB_PERSONAL_HISTORY_TITLE),
        string16(),
        l10n_util::GetStringUTF16(
            IDS_PAGE_INFO_SECURITY_TAB_FIRST_VISITED_TODAY)));
  } else {
    sections_.push_back(SectionInfo(
        true,
        l10n_util::GetStringUTF16(
            IDS_PAGE_INFO_SECURITY_TAB_PERSONAL_HISTORY_TITLE),
        string16(),
        l10n_util::GetStringFUTF16(
            IDS_PAGE_INFO_SECURITY_TAB_VISITED_BEFORE_TODAY,
            WideToUTF16(base::TimeFormatShortDate(first_visit)))));
  }
  observer_->ModelChanged();
}

// static
void PageInfoModel::RegisterPrefs(PrefService* prefs) {
  prefs->RegisterDictionaryPref(prefs::kPageInfoWindowPlacement);
}
<|endoftext|>


<commit_before>#include "lexer.h"
#include "token.h"
#include "utils.h"
#include "exceptions.h"

namespace sota {

    Token SotaLexer::eol() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        if (IsCurrAnyOf({ '\r', '\n' })) {
            ++token.Length;
            token.Type = TokenType::EndOfLine;
            if (IsCurrSeqOf({ '\r', '\n' }))
                ++token.Length;
            Next(token.Length);
        }
        while (auto type = dent())
            _cache.push_back(type);
        return token;
    }

    Token SotaLexer::dent() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        if (IsPrevSeqOf({ '\r', '\n' }, 2) || IsPrevAnyOf({ '\r', '\n' })) {
            auto t = ws();
            auto indent = t ? t.Length : 0;

            if (!_stride)
                _stride = indent;

            if (indent > _indents.top()) {
                if (indent == _indents.top() + _stride) {
                    _indents.push(indent);
                    token.Type = TokenType::Indent;
                }
                else
                    throw SotaException("indent didnt meet previously established stride of");
            }
            else if (indent < _indents.top()) {
                _indents.pop();
                token.Type = TokenType::Dedent;
                token.Length = 0;
            }
        }
        return token;
    }
    Token SotaLexer::ws() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        if (IsCurrAnyOf({ ' ', '\t' })) {
            ++token.Length;
            token.Type = TokenType::WhiteSpace;
            while (IsNextAnyOf({ ' ', '\t' }))
                ++token.Length;
        }
        return token;
    }
    Token SotaLexer::comment() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        if (IsCurr('#')) {
            ++token.Length;
            token.Type = TokenType::Comment;
            while (!IsNextAnyOf({ '\r', '\n' }))
                ++token.Length;
        }
        return token;
    }
    Token SotaLexer::lit() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        char c = Curr;
        if ('\'' == c || '\"' == c) {
            ++token.Length;
            token.Type = TokenType::Str;
            while (!IsNextAnyOf({ c, '\r', '\n', '\0' }))
                ++token.Length;

            if (IsCurr(c)) {
                ++token.Length;
                Next();
            }
            else
                throw SotaException("missing end quote on string literal");
        }
        return token;
    }
    Token SotaLexer::sym() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        std::string sym = "";
        auto symkeys = keys(SymbolValue2Type);

        while (startofany(sym + Curr, symkeys)) {
            sym += Curr;
            ++token.Length;
            Next();
        }

        token.Type = SymbolValue2Type[sym];

        if (!token.Type) {
            token.Index = Index; //backtrack
            token.Length = 0;
        }
        return token;
    }
    Token SotaLexer::id_num_kw() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        bool loop = true;
        while (loop) {
            switch (Curr) {
            case 'a':   case 'n':   case 'A':   case 'N':
            case 'b':   case 'o':   case 'B':   case 'O':
            case 'c':   case 'p':   case 'C':   case 'P':
            case 'd':   case 'q':   case 'D':   case 'Q':
            case 'e':   case 'r':   case 'E':   case 'R':
            case 'f':   case 's':   case 'F':   case 'S':
            case 'g':   case 't':   case 'G':   case 'T':
            case 'h':   case 'u':   case 'H':   case 'U':
            case 'i':   case 'v':   case 'I':   case 'V':
            case 'j':   case 'w':   case 'J':   case 'W':
            case 'k':   case 'x':   case 'K':   case 'X':
            case 'l':   case 'y':   case 'L':   case 'Y':
            case 'm':   case 'z':   case 'M':   case 'Z':
            case '_':
                token.Type = TokenType::Id;
                break;

            case '0':   case '5':
            case '1':   case '6':
            case '2':   case '7':
            case '3':   case '8':
            case '4':   case '9':
                if (token.Type != TokenType::Id)
                    token.Type = TokenType::Num;
                break;

            default:
                loop = false;
                continue;
            }
            ++token.Length;
            Next();

        }
        auto value = Value(token);
        if (KeywordValue2Type.count(value))
            token.Type = KeywordValue2Type[value];
        return token;
    }
    Token SotaLexer::eof() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        return token;
    }

    /* public */

    SotaLexer::~SotaLexer() {

    }
    SotaLexer::SotaLexer(std::vector<char> chars) : SotaStream(chars) {
        _stride = 0;
        _indents.push(0);
    }

    unsigned int SotaLexer::Line(const Token &token) {
        /*
        regex const pattern("\r?\n");
        string head = string(Items.begin(), Items.begin() + token.Index);
        ptrdiff_t const count(distance(
            sregex_iterator(head.begin(), head.end(), pattern),
            sregex_iterator()));
        return count + 1;
        */
        return 0;
    }

    unsigned int SotaLexer::Column(const Token &token) {
        unsigned int count = token.Index;
        for (; count > 0; --count) {
            char c = Items[count];
            if ('\n' == c || '\r' == c) {
                break;
            }
        }
        return token.Index - count;
    }

    std::string SotaLexer::Value(const Token &token) {
        return std::string(Items.begin() + token.Index, Items.begin() + token.Index + token.Length);
    }

    std::string SotaLexer::Pretty(const Token &token) {
        std::string result;
        auto value = Value(token);
        auto tokenvalue = Type2Value[token.Type];
        switch (token.Type) {
        case TokenType::WhiteSpace:
        case TokenType::EndOfFile:
        case TokenType::EndOfLine:
        case TokenType::Indent:
        case TokenType::Dedent:
            result = tokenvalue;
            break;
        default:
            result = value != tokenvalue ? tokenvalue + ":" + value : value;
            break;
        }
        return result;
    }

    Token SotaLexer::Scan() {

        //Length = 0;

        if (_cache.size()) {
            auto token = _cache.front();
            _cache.pop_front();
            return token;
        }

        if (auto token = eol())
            return token;

        if (auto token = ws())
            return token;

        if (auto token = comment())
            return token;

        if (auto token = lit())
            return token;

        if (auto token = sym())
            return token;

        if (auto token = id_num_kw())
            return token;

        return eof();
    }
    std::vector<Token> SotaLexer::Tokenize() {
        auto tokens = std::vector<Token>();
        return tokens;
    }
}
<commit_msg>fixed backtracking bug for values starting with m and s due to the keyword symbols m/ and s/ used for regex matching and replacing... -sai<commit_after>#include "lexer.h"
#include "token.h"
#include "utils.h"
#include "exceptions.h"

namespace sota {

    Token SotaLexer::eol() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        if (IsCurrAnyOf({ '\r', '\n' })) {
            ++token.Length;
            token.Type = TokenType::EndOfLine;
            if (IsCurrSeqOf({ '\r', '\n' }))
                ++token.Length;
            Next(token.Length);
        }
        while (auto type = dent())
            _cache.push_back(type);
        return token;
    }

    Token SotaLexer::dent() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        if (IsPrevSeqOf({ '\r', '\n' }, 2) || IsPrevAnyOf({ '\r', '\n' })) {
            auto t = ws();
            auto indent = t ? t.Length : 0;

            if (!_stride)
                _stride = indent;

            if (indent > _indents.top()) {
                if (indent == _indents.top() + _stride) {
                    _indents.push(indent);
                    token.Type = TokenType::Indent;
                }
                else
                    throw SotaException("indent didnt meet previously established stride of");
            }
            else if (indent < _indents.top()) {
                _indents.pop();
                token.Type = TokenType::Dedent;
                token.Length = 0;
            }
        }
        return token;
    }
    Token SotaLexer::ws() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        if (IsCurrAnyOf({ ' ', '\t' })) {
            ++token.Length;
            token.Type = TokenType::WhiteSpace;
            while (IsNextAnyOf({ ' ', '\t' }))
                ++token.Length;
        }
        return token;
    }
    Token SotaLexer::comment() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        if (IsCurr('#')) {
            ++token.Length;
            token.Type = TokenType::Comment;
            while (!IsNextAnyOf({ '\r', '\n' }))
                ++token.Length;
        }
        return token;
    }
    Token SotaLexer::lit() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        char c = Curr;
        if ('\'' == c || '\"' == c) {
            ++token.Length;
            token.Type = TokenType::Str;
            while (!IsNextAnyOf({ c, '\r', '\n', '\0' }))
                ++token.Length;

            if (IsCurr(c)) {
                ++token.Length;
                Next();
            }
            else
                throw SotaException("missing end quote on string literal");
        }
        return token;
    }
    Token SotaLexer::sym() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        std::string sym = "";
        auto symkeys = keys(SymbolValue2Type);

        while (startofany(sym + Curr, symkeys)) {
            sym += Curr;
            Next();
        }

        token.Type = SymbolValue2Type[sym];
        token.Length = Index - token.Index;

        if (!token.Type) {
            Index = token.Index; //backtrack
            token.Length = 0;
        }
        return token;
    }
    Token SotaLexer::id_num_kw() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        bool loop = true;
        while (loop) {
            switch (Curr) {
            case 'a':   case 'n':   case 'A':   case 'N':
            case 'b':   case 'o':   case 'B':   case 'O':
            case 'c':   case 'p':   case 'C':   case 'P':
            case 'd':   case 'q':   case 'D':   case 'Q':
            case 'e':   case 'r':   case 'E':   case 'R':
            case 'f':   case 's':   case 'F':   case 'S':
            case 'g':   case 't':   case 'G':   case 'T':
            case 'h':   case 'u':   case 'H':   case 'U':
            case 'i':   case 'v':   case 'I':   case 'V':
            case 'j':   case 'w':   case 'J':   case 'W':
            case 'k':   case 'x':   case 'K':   case 'X':
            case 'l':   case 'y':   case 'L':   case 'Y':
            case 'm':   case 'z':   case 'M':   case 'Z':
            case '_':
                token.Type = TokenType::Id;
                break;

            case '0':   case '5':
            case '1':   case '6':
            case '2':   case '7':
            case '3':   case '8':
            case '4':   case '9':
                if (token.Type != TokenType::Id)
                    token.Type = TokenType::Num;
                break;

            default:
                loop = false;
                continue;
            }
            ++token.Length;
            Next();

        }
        auto value = Value(token);
        if (KeywordValue2Type.count(value))
            token.Type = KeywordValue2Type[value];
        return token;
    }
    Token SotaLexer::eof() {
        Token token = { TokenType::EndOfFile, Index, 0 };
        return token;
    }

    /* public */

    SotaLexer::~SotaLexer() {

    }
    SotaLexer::SotaLexer(std::vector<char> chars) : SotaStream(chars) {
        _stride = 0;
        _indents.push(0);
    }

    unsigned int SotaLexer::Line(const Token &token) {
        /*
        regex const pattern("\r?\n");
        string head = string(Items.begin(), Items.begin() + token.Index);
        ptrdiff_t const count(distance(
            sregex_iterator(head.begin(), head.end(), pattern),
            sregex_iterator()));
        return count + 1;
        */
        return 0;
    }

    unsigned int SotaLexer::Column(const Token &token) {
        unsigned int count = token.Index;
        for (; count > 0; --count) {
            char c = Items[count];
            if ('\n' == c || '\r' == c) {
                break;
            }
        }
        return token.Index - count;
    }

    std::string SotaLexer::Value(const Token &token) {
        return std::string(Items.begin() + token.Index, Items.begin() + token.Index + token.Length);
    }

    std::string SotaLexer::Pretty(const Token &token) {
        std::string result;
        auto value = Value(token);
        auto tokenvalue = Type2Value[token.Type];
        switch (token.Type) {
        case TokenType::WhiteSpace:
        case TokenType::EndOfFile:
        case TokenType::EndOfLine:
        case TokenType::Indent:
        case TokenType::Dedent:
            result = tokenvalue;
            break;
        default:
            result = value != tokenvalue ? tokenvalue + ":" + value : value;
            break;
        }
        return result;
    }

    Token SotaLexer::Scan() {

        //Length = 0;

        if (_cache.size()) {
            auto token = _cache.front();
            _cache.pop_front();
            return token;
        }

        if (auto token = eol())
            return token;

        if (auto token = ws())
            return token;

        if (auto token = comment())
            return token;

        if (auto token = lit())
            return token;

        if (auto token = sym())
            return token;

        if (auto token = id_num_kw())
            return token;

        return eof();
    }
    std::vector<Token> SotaLexer::Tokenize() {
        auto tokens = std::vector<Token>();
        return tokens;
    }
}
<|endoftext|>


<commit_before>/**
 * @file   consolidator.cc
 *
 * @section LICENSE
 *
 * The MIT License
 *
 * @copyright Copyright (c) 2017-2018 TileDB, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @section DESCRIPTION
 *
 * This file implements the Consolidator class.
 */

#include "tiledb/sm/storage_manager/consolidator.h"
#include "tiledb/sm/misc/logger.h"
#include "tiledb/sm/misc/utils.h"
#include "tiledb/sm/storage_manager/storage_manager.h"

#include <iostream>
#include <sstream>

/* ****************************** */
/*             MACROS             */
/* ****************************** */

namespace tiledb {
namespace sm {

/* ****************************** */
/*   CONSTRUCTORS & DESTRUCTORS   */
/* ****************************** */

Consolidator::Consolidator(StorageManager* storage_manager)
    : storage_manager_(storage_manager) {
}

Consolidator::~Consolidator() = default;

/* ****************************** */
/*               API              */
/* ****************************** */

Status Consolidator::consolidate(const char* array_name) {
  std::vector<URI> old_fragment_uris;
  URI array_uri = URI(array_name);

  // Open array
  auto open_array_for_reads = (OpenArray*)nullptr;
  auto open_array_for_writes = (OpenArray*)nullptr;
  uint64_t snapshot;
  RETURN_NOT_OK(storage_manager_->array_open(
      array_uri, QueryType::READ, &open_array_for_reads, &snapshot));
  RETURN_NOT_OK_ELSE(
      storage_manager_->array_open(
          array_uri, QueryType::WRITE, &open_array_for_writes, &snapshot),
      storage_manager_->array_close(array_uri, QueryType::READ));
  auto array_schema = open_array_for_reads->array_schema();

  // Create subarray
  void* subarray = nullptr;
  auto st = create_subarray(open_array_for_reads, snapshot, &subarray);
  if (!st.ok()) {
    storage_manager_->array_close(array_uri, QueryType::READ);
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    return st;
  }

  // Prepare buffers
  void** buffers;
  uint64_t* buffer_sizes;
  unsigned int buffer_num;
  st = create_buffers(array_schema, &buffers, &buffer_sizes, &buffer_num);
  if (!st.ok()) {
    storage_manager_->array_close(array_uri, QueryType::READ);
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    return st;
  }

  // Create queries
  unsigned int fragment_num;
  auto query_r = (Query*)nullptr;
  auto query_w = (Query*)nullptr;
  URI new_fragment_uri;
  st = create_queries(
      &query_r,
      &query_w,
      subarray,
      open_array_for_reads,
      open_array_for_writes,
      snapshot,
      buffers,
      buffer_sizes,
      &fragment_num,
      &new_fragment_uri);
  if (!st.ok()) {
    storage_manager_->array_close(array_uri, QueryType::READ);
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    return st;
  }

  // Check number of fragments
  if (fragment_num <= 1) {  // Nothing to consolidate
    storage_manager_->array_close(array_uri, QueryType::READ);
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    return Status::Ok();
  }

  // Read from one array and write to the other
  st = copy_array(query_r, query_w);
  if (!st.ok()) {
    storage_manager_->array_close(array_uri, QueryType::READ);
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    return st;
  }

  // Get old fragment uris
  old_fragment_uris = query_r->fragment_uris();

  // Close array for reading
  st = storage_manager_->array_close(array_uri, QueryType::READ);
  if (!st.ok()) {
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    storage_manager_->vfs()->remove_dir(new_fragment_uri);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    return st;
  }

  // Lock the array exclusively
  st = storage_manager_->array_xlock(array_uri);
  if (!st.ok()) {
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    storage_manager_->vfs()->remove_dir(new_fragment_uri);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    return st;
  }

  // Finalize both queries
  st = query_w->finalize();
  if (!st.ok()) {
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    storage_manager_->array_xunlock(array_uri);
    storage_manager_->vfs()->remove_dir(new_fragment_uri);
    return st;
  }

  // Close array
  st = storage_manager_->array_close(array_uri, QueryType::WRITE);
  if (!st.ok()) {
    storage_manager_->array_xunlock(array_uri);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    storage_manager_->vfs()->remove_dir(new_fragment_uri);
    return st;
  }

  // Delete old fragment metadata. This makes the old fragments invisible
  st = delete_old_fragment_metadata(old_fragment_uris);
  if (!st.ok()) {
    delete_old_fragments(old_fragment_uris);
    storage_manager_->array_xunlock(array_uri);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    return st;
  }

  // Unlock the array
  st = storage_manager_->array_xunlock(array_uri);
  if (!st.ok()) {
    delete_old_fragments(old_fragment_uris);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    return st;
  }

  // Delete old fragments. The array does not need to be locked.
  st = delete_old_fragments(old_fragment_uris);

  // Clean up
  clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);

  return st;
}

/* ****************************** */
/*        PRIVATE METHODS         */
/* ****************************** */

Status Consolidator::copy_array(Query* query_r, Query* query_w) {
  do {
    RETURN_NOT_OK(storage_manager_->query_submit(query_r));
    RETURN_NOT_OK(storage_manager_->query_submit(query_w));
  } while (query_r->status() == QueryStatus::INCOMPLETE);

  return Status::Ok();
}

void Consolidator::clean_up(
    void* subarray,
    unsigned buffer_num,
    void** buffers,
    uint64_t* buffer_sizes,
    Query* query_r,
    Query* query_w) const {
  if (subarray != nullptr)
    std::free(subarray);
  free_buffers(buffer_num, buffers, buffer_sizes);
  delete query_r;
  delete query_w;
}

Status Consolidator::create_buffers(
    const ArraySchema* array_schema,
    void*** buffers,
    uint64_t** buffer_sizes,
    unsigned int* buffer_num) {
  // For easy reference
  auto attribute_num = array_schema->attribute_num();
  auto dense = array_schema->dense();

  // Calculate number of buffers
  *buffer_num = 0;
  for (unsigned int i = 0; i < attribute_num; ++i)
    *buffer_num += (array_schema->var_size(i)) ? 2 : 1;
  *buffer_num += (dense) ? 0 : 1;

  // Create buffers
  *buffers = (void**)std::malloc(*buffer_num * sizeof(void*));
  if (*buffers == nullptr) {
    return LOG_STATUS(Status::ConsolidationError(
        "Cannot create consolidation buffers; Memory allocation failed"));
  }
  *buffer_sizes = new uint64_t[*buffer_num];
  if (*buffer_sizes == nullptr) {
    return LOG_STATUS(Status::ConsolidationError(
        "Cannot create consolidation buffer sizes; Memory allocation failed"));
  }

  // Allocate space for each buffer
  bool error = false;
  for (unsigned int i = 0; i < *buffer_num; ++i) {
    (*buffers)[i] = std::malloc(constants::consolidation_buffer_size);
    if ((*buffers)[i] == nullptr)  // The loop should continue to
      error = true;                // allocate nullptr to each buffer
    (*buffer_sizes)[i] = constants::consolidation_buffer_size;
  }

  // Clean up upon error
  if (error) {
    free_buffers(*buffer_num, *buffers, *buffer_sizes);
    *buffers = nullptr;
    *buffer_sizes = nullptr;
    return LOG_STATUS(Status::ConsolidationError(
        "Cannot create consolidation buffers; Memory allocation failed"));
  }

  // Success
  return Status::Ok();
}

Status Consolidator::create_queries(
    Query** query_r,
    Query** query_w,
    void* subarray,
    OpenArray* open_array_for_reads,
    OpenArray* open_array_for_writes,
    uint64_t snapshot,
    void** buffers,
    uint64_t* buffer_sizes,
    unsigned int* fragment_num,
    URI* new_fragment_uri) {
  // Create read query
  RETURN_NOT_OK(
      storage_manager_->query_create(query_r, open_array_for_reads, snapshot));
  if (!(*query_r)->array_schema()->is_kv())
    RETURN_NOT_OK((*query_r)->set_layout(Layout::GLOBAL_ORDER));
  RETURN_NOT_OK(set_query_buffers(*query_r, buffers, buffer_sizes));

  // Get fragment num and terminate with success if it is <=1
  *fragment_num = (*query_r)->fragment_num();
  if (*fragment_num <= 1)
    return Status::Ok();

  // Get last fragment URI, which will be the URI of the consolidated fragment
  *new_fragment_uri = (*query_r)->last_fragment_uri();
  RETURN_NOT_OK(rename_new_fragment_uri(new_fragment_uri));

  // Create write query
  RETURN_NOT_OK(storage_manager_->query_create(
      query_w, open_array_for_writes, 0, *new_fragment_uri));
  if (!(*query_w)->array_schema()->is_kv())
    RETURN_NOT_OK((*query_w)->set_layout(Layout::GLOBAL_ORDER));
  RETURN_NOT_OK((*query_w)->set_subarray(subarray));
  RETURN_NOT_OK(set_query_buffers(*query_w, buffers, buffer_sizes));

  return Status::Ok();
}

Status Consolidator::create_subarray(
    OpenArray* open_array, uint64_t snapshot, void** subarray) const {
  assert(open_array != nullptr);
  auto array_schema = open_array->array_schema();

  // Create subarray only for the dense case
  if (array_schema->dense()) {
    *subarray = std::malloc(2 * array_schema->coords_size());
    if (*subarray == nullptr)
      return LOG_STATUS(Status::ConsolidationError(
          "Cannot create subarray; Failed to allocate memory"));
    bool is_empty;
    RETURN_NOT_OK_ELSE(
        storage_manager_->array_get_non_empty_domain(
            open_array, snapshot, *subarray, &is_empty),
        std::free(subarray));
    assert(!is_empty);
    array_schema->domain()->expand_domain(*subarray);
  }

  return Status::Ok();
}

Status Consolidator::delete_old_fragment_metadata(
    const std::vector<URI>& uris) {
  for (auto& uri : uris) {
    auto meta_uri = uri.join_path(constants::fragment_metadata_filename);
    RETURN_NOT_OK(storage_manager_->vfs()->remove_file(meta_uri));
  }

  return Status::Ok();
}

Status Consolidator::delete_old_fragments(const std::vector<URI>& uris) {
  for (auto& uri : uris)
    RETURN_NOT_OK(storage_manager_->vfs()->remove_dir(uri));

  return Status::Ok();
}

void Consolidator::free_buffers(
    unsigned int buffer_num, void** buffers, uint64_t* buffer_sizes) const {
  for (unsigned int i = 0; i < buffer_num; ++i) {
    if (buffers[i] != nullptr)
      std::free(buffers[i]);
  }
  std::free(buffers);
  delete[] buffer_sizes;
}

Status Consolidator::rename_new_fragment_uri(URI* uri) const {
  // Get timestamp
  std::string name = uri->last_path_part();
  auto timestamp_str = name.substr(name.find_last_of('_') + 1);

  // Get current time
  uint64_t ms = utils::timestamp_ms();

  std::stringstream ss;
  ss << uri->parent().to_string() << "/__" << std::this_thread::get_id() << "_"
     << ms << "_" << timestamp_str;

  *uri = URI(ss.str());
  return Status::Ok();
}

Status Consolidator::set_query_buffers(
    Query* query, void** buffers, uint64_t* buffer_sizes) const {
  auto dense = query->array_schema()->dense();
  auto attributes = query->array_schema()->attributes();
  unsigned bid = 0;
  for (const auto& attr : attributes) {
    if (!attr->var_size()) {
      RETURN_NOT_OK(query->set_buffer(
          attr->name().c_str(), buffers[bid], &buffer_sizes[bid]));
      ++bid;
    } else {
      RETURN_NOT_OK(query->set_buffer(
          attr->name().c_str(),
          (uint64_t*)buffers[bid],
          &buffer_sizes[bid],
          buffers[bid + 1],
          &buffer_sizes[bid + 1]));
      bid += 2;
    }
  }
  if (!dense)
    RETURN_NOT_OK(query->set_buffer(
        constants::coords.c_str(), buffers[bid], &buffer_sizes[bid]));

  return Status::Ok();
}

}  // namespace sm
}  // namespace tiledb
<commit_msg>Use uuid instead of tid in consolidated fragment names. Closes #771<commit_after>/**
 * @file   consolidator.cc
 *
 * @section LICENSE
 *
 * The MIT License
 *
 * @copyright Copyright (c) 2017-2018 TileDB, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @section DESCRIPTION
 *
 * This file implements the Consolidator class.
 */

#include "tiledb/sm/storage_manager/consolidator.h"
#include "tiledb/sm/misc/logger.h"
#include "tiledb/sm/misc/utils.h"
#include "tiledb/sm/misc/uuid.h"
#include "tiledb/sm/storage_manager/storage_manager.h"

#include <iostream>
#include <sstream>

/* ****************************** */
/*             MACROS             */
/* ****************************** */

namespace tiledb {
namespace sm {

/* ****************************** */
/*   CONSTRUCTORS & DESTRUCTORS   */
/* ****************************** */

Consolidator::Consolidator(StorageManager* storage_manager)
    : storage_manager_(storage_manager) {
}

Consolidator::~Consolidator() = default;

/* ****************************** */
/*               API              */
/* ****************************** */

Status Consolidator::consolidate(const char* array_name) {
  std::vector<URI> old_fragment_uris;
  URI array_uri = URI(array_name);

  // Open array
  auto open_array_for_reads = (OpenArray*)nullptr;
  auto open_array_for_writes = (OpenArray*)nullptr;
  uint64_t snapshot;
  RETURN_NOT_OK(storage_manager_->array_open(
      array_uri, QueryType::READ, &open_array_for_reads, &snapshot));
  RETURN_NOT_OK_ELSE(
      storage_manager_->array_open(
          array_uri, QueryType::WRITE, &open_array_for_writes, &snapshot),
      storage_manager_->array_close(array_uri, QueryType::READ));
  auto array_schema = open_array_for_reads->array_schema();

  // Create subarray
  void* subarray = nullptr;
  auto st = create_subarray(open_array_for_reads, snapshot, &subarray);
  if (!st.ok()) {
    storage_manager_->array_close(array_uri, QueryType::READ);
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    return st;
  }

  // Prepare buffers
  void** buffers;
  uint64_t* buffer_sizes;
  unsigned int buffer_num;
  st = create_buffers(array_schema, &buffers, &buffer_sizes, &buffer_num);
  if (!st.ok()) {
    storage_manager_->array_close(array_uri, QueryType::READ);
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    return st;
  }

  // Create queries
  unsigned int fragment_num;
  auto query_r = (Query*)nullptr;
  auto query_w = (Query*)nullptr;
  URI new_fragment_uri;
  st = create_queries(
      &query_r,
      &query_w,
      subarray,
      open_array_for_reads,
      open_array_for_writes,
      snapshot,
      buffers,
      buffer_sizes,
      &fragment_num,
      &new_fragment_uri);
  if (!st.ok()) {
    storage_manager_->array_close(array_uri, QueryType::READ);
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    return st;
  }

  // Check number of fragments
  if (fragment_num <= 1) {  // Nothing to consolidate
    storage_manager_->array_close(array_uri, QueryType::READ);
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    return Status::Ok();
  }

  // Read from one array and write to the other
  st = copy_array(query_r, query_w);
  if (!st.ok()) {
    storage_manager_->array_close(array_uri, QueryType::READ);
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    return st;
  }

  // Get old fragment uris
  old_fragment_uris = query_r->fragment_uris();

  // Close array for reading
  st = storage_manager_->array_close(array_uri, QueryType::READ);
  if (!st.ok()) {
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    storage_manager_->vfs()->remove_dir(new_fragment_uri);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    return st;
  }

  // Lock the array exclusively
  st = storage_manager_->array_xlock(array_uri);
  if (!st.ok()) {
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    storage_manager_->vfs()->remove_dir(new_fragment_uri);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    return st;
  }

  // Finalize both queries
  st = query_w->finalize();
  if (!st.ok()) {
    storage_manager_->array_close(array_uri, QueryType::WRITE);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    storage_manager_->array_xunlock(array_uri);
    storage_manager_->vfs()->remove_dir(new_fragment_uri);
    return st;
  }

  // Close array
  st = storage_manager_->array_close(array_uri, QueryType::WRITE);
  if (!st.ok()) {
    storage_manager_->array_xunlock(array_uri);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    storage_manager_->vfs()->remove_dir(new_fragment_uri);
    return st;
  }

  // Delete old fragment metadata. This makes the old fragments invisible
  st = delete_old_fragment_metadata(old_fragment_uris);
  if (!st.ok()) {
    delete_old_fragments(old_fragment_uris);
    storage_manager_->array_xunlock(array_uri);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    return st;
  }

  // Unlock the array
  st = storage_manager_->array_xunlock(array_uri);
  if (!st.ok()) {
    delete_old_fragments(old_fragment_uris);
    clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);
    return st;
  }

  // Delete old fragments. The array does not need to be locked.
  st = delete_old_fragments(old_fragment_uris);

  // Clean up
  clean_up(subarray, buffer_num, buffers, buffer_sizes, query_r, query_w);

  return st;
}

/* ****************************** */
/*        PRIVATE METHODS         */
/* ****************************** */

Status Consolidator::copy_array(Query* query_r, Query* query_w) {
  do {
    RETURN_NOT_OK(storage_manager_->query_submit(query_r));
    RETURN_NOT_OK(storage_manager_->query_submit(query_w));
  } while (query_r->status() == QueryStatus::INCOMPLETE);

  return Status::Ok();
}

void Consolidator::clean_up(
    void* subarray,
    unsigned buffer_num,
    void** buffers,
    uint64_t* buffer_sizes,
    Query* query_r,
    Query* query_w) const {
  if (subarray != nullptr)
    std::free(subarray);
  free_buffers(buffer_num, buffers, buffer_sizes);
  delete query_r;
  delete query_w;
}

Status Consolidator::create_buffers(
    const ArraySchema* array_schema,
    void*** buffers,
    uint64_t** buffer_sizes,
    unsigned int* buffer_num) {
  // For easy reference
  auto attribute_num = array_schema->attribute_num();
  auto dense = array_schema->dense();

  // Calculate number of buffers
  *buffer_num = 0;
  for (unsigned int i = 0; i < attribute_num; ++i)
    *buffer_num += (array_schema->var_size(i)) ? 2 : 1;
  *buffer_num += (dense) ? 0 : 1;

  // Create buffers
  *buffers = (void**)std::malloc(*buffer_num * sizeof(void*));
  if (*buffers == nullptr) {
    return LOG_STATUS(Status::ConsolidationError(
        "Cannot create consolidation buffers; Memory allocation failed"));
  }
  *buffer_sizes = new uint64_t[*buffer_num];
  if (*buffer_sizes == nullptr) {
    return LOG_STATUS(Status::ConsolidationError(
        "Cannot create consolidation buffer sizes; Memory allocation failed"));
  }

  // Allocate space for each buffer
  bool error = false;
  for (unsigned int i = 0; i < *buffer_num; ++i) {
    (*buffers)[i] = std::malloc(constants::consolidation_buffer_size);
    if ((*buffers)[i] == nullptr)  // The loop should continue to
      error = true;                // allocate nullptr to each buffer
    (*buffer_sizes)[i] = constants::consolidation_buffer_size;
  }

  // Clean up upon error
  if (error) {
    free_buffers(*buffer_num, *buffers, *buffer_sizes);
    *buffers = nullptr;
    *buffer_sizes = nullptr;
    return LOG_STATUS(Status::ConsolidationError(
        "Cannot create consolidation buffers; Memory allocation failed"));
  }

  // Success
  return Status::Ok();
}

Status Consolidator::create_queries(
    Query** query_r,
    Query** query_w,
    void* subarray,
    OpenArray* open_array_for_reads,
    OpenArray* open_array_for_writes,
    uint64_t snapshot,
    void** buffers,
    uint64_t* buffer_sizes,
    unsigned int* fragment_num,
    URI* new_fragment_uri) {
  // Create read query
  RETURN_NOT_OK(
      storage_manager_->query_create(query_r, open_array_for_reads, snapshot));
  if (!(*query_r)->array_schema()->is_kv())
    RETURN_NOT_OK((*query_r)->set_layout(Layout::GLOBAL_ORDER));
  RETURN_NOT_OK(set_query_buffers(*query_r, buffers, buffer_sizes));

  // Get fragment num and terminate with success if it is <=1
  *fragment_num = (*query_r)->fragment_num();
  if (*fragment_num <= 1)
    return Status::Ok();

  // Get last fragment URI, which will be the URI of the consolidated fragment
  *new_fragment_uri = (*query_r)->last_fragment_uri();
  RETURN_NOT_OK(rename_new_fragment_uri(new_fragment_uri));

  // Create write query
  RETURN_NOT_OK(storage_manager_->query_create(
      query_w, open_array_for_writes, 0, *new_fragment_uri));
  if (!(*query_w)->array_schema()->is_kv())
    RETURN_NOT_OK((*query_w)->set_layout(Layout::GLOBAL_ORDER));
  RETURN_NOT_OK((*query_w)->set_subarray(subarray));
  RETURN_NOT_OK(set_query_buffers(*query_w, buffers, buffer_sizes));

  return Status::Ok();
}

Status Consolidator::create_subarray(
    OpenArray* open_array, uint64_t snapshot, void** subarray) const {
  assert(open_array != nullptr);
  auto array_schema = open_array->array_schema();

  // Create subarray only for the dense case
  if (array_schema->dense()) {
    *subarray = std::malloc(2 * array_schema->coords_size());
    if (*subarray == nullptr)
      return LOG_STATUS(Status::ConsolidationError(
          "Cannot create subarray; Failed to allocate memory"));
    bool is_empty;
    RETURN_NOT_OK_ELSE(
        storage_manager_->array_get_non_empty_domain(
            open_array, snapshot, *subarray, &is_empty),
        std::free(subarray));
    assert(!is_empty);
    array_schema->domain()->expand_domain(*subarray);
  }

  return Status::Ok();
}

Status Consolidator::delete_old_fragment_metadata(
    const std::vector<URI>& uris) {
  for (auto& uri : uris) {
    auto meta_uri = uri.join_path(constants::fragment_metadata_filename);
    RETURN_NOT_OK(storage_manager_->vfs()->remove_file(meta_uri));
  }

  return Status::Ok();
}

Status Consolidator::delete_old_fragments(const std::vector<URI>& uris) {
  for (auto& uri : uris)
    RETURN_NOT_OK(storage_manager_->vfs()->remove_dir(uri));

  return Status::Ok();
}

void Consolidator::free_buffers(
    unsigned int buffer_num, void** buffers, uint64_t* buffer_sizes) const {
  for (unsigned int i = 0; i < buffer_num; ++i) {
    if (buffers[i] != nullptr)
      std::free(buffers[i]);
  }
  std::free(buffers);
  delete[] buffer_sizes;
}

Status Consolidator::rename_new_fragment_uri(URI* uri) const {
  // Get timestamp
  std::string name = uri->last_path_part();
  auto timestamp_str = name.substr(name.find_last_of('_') + 1);

  // Get current time
  uint64_t ms = utils::timestamp_ms();

  // Get uuid
  std::string uuid;
  RETURN_NOT_OK(uuid::generate_uuid(&uuid, false));

  std::stringstream ss;
  ss << uri->parent().to_string() << "/__" << uuid << "_" << ms << "_"
     << timestamp_str;

  *uri = URI(ss.str());
  return Status::Ok();
}

Status Consolidator::set_query_buffers(
    Query* query, void** buffers, uint64_t* buffer_sizes) const {
  auto dense = query->array_schema()->dense();
  auto attributes = query->array_schema()->attributes();
  unsigned bid = 0;
  for (const auto& attr : attributes) {
    if (!attr->var_size()) {
      RETURN_NOT_OK(query->set_buffer(
          attr->name().c_str(), buffers[bid], &buffer_sizes[bid]));
      ++bid;
    } else {
      RETURN_NOT_OK(query->set_buffer(
          attr->name().c_str(),
          (uint64_t*)buffers[bid],
          &buffer_sizes[bid],
          buffers[bid + 1],
          &buffer_sizes[bid + 1]));
      bid += 2;
    }
  }
  if (!dense)
    RETURN_NOT_OK(query->set_buffer(
        constants::coords.c_str(), buffers[bid], &buffer_sizes[bid]));

  return Status::Ok();
}

}  // namespace sm
}  // namespace tiledb
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: fltglbls.hxx,v $
 *
 *  $Revision: 1.5 $
 *
 *  last change: $Author: kz $ $Date: 2006-11-08 13:33:40 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/
#ifndef _FLTGLBS_HXX
#define _FLTGLBS_HXX

#ifndef _STRING_HXX //autogen
#include <tools/string.hxx>
#endif
#ifndef INCLUDED_I18NPOOL_LANG_H
#include <i18npool/lang.h>
#endif
#ifndef _SVARRAY_HXX //autogen
#include <svtools/svarray.hxx>
#endif

class SfxPoolItem;
class SwDoc;
class SwPaM;
class SwTable;

class XF_Buffer;
class ColorBuffer;
class FontBuffer;
class SvNumberFormatter;

class ExcGlob;
extern ExcGlob *pExcGlob;

class LotGlob;
extern LotGlob *pLotGlob;

// ----- Basis-Klasse ----------------------------------------------------
class FilterGlobals
{
protected:
    SvPtrarr        aTblFmts;
    USHORT          nColStart;
    USHORT          nColEnd;
    USHORT          nRowStart;
    USHORT          nRowEnd;
    USHORT          nAnzCols;
    USHORT          nAnzRows;

public:
    FilterGlobals( SwDoc& rDoc, const SwPaM& rPam );
    ~FilterGlobals();

    SwDoc           *pD;
    SwPaM           *pPam;
    const SwTable   *pTable;

    SvNumberFormatter *pNumFormatter;
    LanguageType    eDefLanguage;
    ULONG           nStandard;
    ULONG           nDefFormat;     // = 0xFFFFFFFF

    void SetRange( USHORT nCS, USHORT nCE, USHORT nRS, USHORT nRE );

    BOOL IsInColRange( USHORT nCol )
                {   return ( nCol >= nColStart && nCol <= nColEnd ); }
    BOOL IsInRowRange( USHORT nRow )
                {   return ( nRow >= nRowStart && nRow <= nRowEnd ); }
    BOOL IsInRange( USHORT nCol, USHORT nRow )
                {   return IsInRowRange(nRow) && IsInColRange(nCol); }

    void NormalizeCol( USHORT &rCol )   { rCol -= nColStart; }
    void NormalizeRow( USHORT &rRow )   { rRow -= nRowStart; }
    void Normalize( USHORT &rCol, USHORT &rRow )
                        {   NormalizeCol( rCol ); NormalizeRow( rRow ); }

    USHORT AnzCols() const  { return nAnzCols; }
    USHORT AnzRows() const  { return nAnzRows; }

    BOOL ColRangeLimitter( USHORT &rCS, USHORT &rCE );

    void InsertText( USHORT nCol, USHORT nRow, const String& rStr );
    void CreateTable();
    void InsertAttr( const SfxPoolItem& rItem );

    inline void     ColLimitter( USHORT &rCol );
    inline void     RowLimitter( USHORT &rRow );
};

inline void FilterGlobals::ColLimitter( USHORT &rCol )
{
    if( rCol < nColStart )
        rCol = nColStart;
    else if( rCol > nColEnd )
        rCol = nColEnd;
}

inline void FilterGlobals::RowLimitter( USHORT &rRow )
{
    if( rRow < nRowStart )
        rRow = nRowStart;
    else if( rRow > nRowEnd )
        rRow = nRowEnd;
}

#endif
<commit_msg>INTEGRATION: CWS changefileheader (1.5.656); FILE MERGED 2008/04/01 15:57:47 thb 1.5.656.2: #i85898# Stripping all external header guards 2008/03/31 16:55:45 rt 1.5.656.1: #i87441# Change license header to LPGL v3.<commit_after>/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: fltglbls.hxx,v $
 * $Revision: 1.6 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
#ifndef _FLTGLBS_HXX
#define _FLTGLBS_HXX

#include <tools/string.hxx>
#include <i18npool/lang.h>
#include <svtools/svarray.hxx>

class SfxPoolItem;
class SwDoc;
class SwPaM;
class SwTable;

class XF_Buffer;
class ColorBuffer;
class FontBuffer;
class SvNumberFormatter;

class ExcGlob;
extern ExcGlob *pExcGlob;

class LotGlob;
extern LotGlob *pLotGlob;

// ----- Basis-Klasse ----------------------------------------------------
class FilterGlobals
{
protected:
    SvPtrarr        aTblFmts;
    USHORT          nColStart;
    USHORT          nColEnd;
    USHORT          nRowStart;
    USHORT          nRowEnd;
    USHORT          nAnzCols;
    USHORT          nAnzRows;

public:
    FilterGlobals( SwDoc& rDoc, const SwPaM& rPam );
    ~FilterGlobals();

    SwDoc           *pD;
    SwPaM           *pPam;
    const SwTable   *pTable;

    SvNumberFormatter *pNumFormatter;
    LanguageType    eDefLanguage;
    ULONG           nStandard;
    ULONG           nDefFormat;     // = 0xFFFFFFFF

    void SetRange( USHORT nCS, USHORT nCE, USHORT nRS, USHORT nRE );

    BOOL IsInColRange( USHORT nCol )
                {   return ( nCol >= nColStart && nCol <= nColEnd ); }
    BOOL IsInRowRange( USHORT nRow )
                {   return ( nRow >= nRowStart && nRow <= nRowEnd ); }
    BOOL IsInRange( USHORT nCol, USHORT nRow )
                {   return IsInRowRange(nRow) && IsInColRange(nCol); }

    void NormalizeCol( USHORT &rCol )   { rCol -= nColStart; }
    void NormalizeRow( USHORT &rRow )   { rRow -= nRowStart; }
    void Normalize( USHORT &rCol, USHORT &rRow )
                        {   NormalizeCol( rCol ); NormalizeRow( rRow ); }

    USHORT AnzCols() const  { return nAnzCols; }
    USHORT AnzRows() const  { return nAnzRows; }

    BOOL ColRangeLimitter( USHORT &rCS, USHORT &rCE );

    void InsertText( USHORT nCol, USHORT nRow, const String& rStr );
    void CreateTable();
    void InsertAttr( const SfxPoolItem& rItem );

    inline void     ColLimitter( USHORT &rCol );
    inline void     RowLimitter( USHORT &rRow );
};

inline void FilterGlobals::ColLimitter( USHORT &rCol )
{
    if( rCol < nColStart )
        rCol = nColStart;
    else if( rCol > nColEnd )
        rCol = nColEnd;
}

inline void FilterGlobals::RowLimitter( USHORT &rRow )
{
    if( rRow < nRowStart )
        rRow = nRowStart;
    else if( rRow > nRowEnd )
        rRow = nRowEnd;
}

#endif
<|endoftext|>