ObjectId
ObjectToIdMap::find(JSObject* obj)
{
    Table::Ptr p = table_->lookup(obj);
    if (!p)
        return ObjectId::nullId();
    return p->value();
}
JSObject*
IdToObjectMap::find(ObjectId id)
{
    Table::Ptr p = table_.lookup(id);
    if (!p)
        return nullptr;
    return p->value();
}
JSObject*
IdToObjectMap::findPreserveColor(ObjectId id)
{
    Table::Ptr p = table_.lookup(id);
    if (!p)
        return nullptr;
    return p->value().unbarrieredGet();
}
Beispiel #4
0
//-------------------------------------------------------------------------------
Table::Ptr solveOptimalMinMax(aq::verb::VerbNode::Ptr spTree, 
                              Base& BaseDesc, 
                              Settings& Settings )
{
	if( !spTree->getLeftChild() )
  {
    return NULL;
		// throw generic_error(generic_error::INVALID_QUERY, "");
  }
	aq::verb::VerbNode::Ptr verb1 = spTree->getLeftChild();
  aq::verb::VerbNode::Ptr verb2 = NULL;
	if( !verb1 ) 
    return NULL;
  if ((verb1->getVerbType() == K_MIN) || (verb1->getVerbType() == K_MAX))
    verb2 = verb1->getLeftChild();
  else if ((verb1->getVerbType() == K_AS) && ((verb1->getLeftChild()->getVerbType() == K_MIN) || (verb1->getLeftChild()->getVerbType() == K_MAX)))
    verb2 = verb1->getLeftChild()->getLeftChild();
  else
    return NULL;

	if( verb2->getVerbType() != K_PERIOD )
		return NULL;
	if( spTree->getBrother() == NULL )
		return NULL;
	aq::verb::VerbNode::Ptr spNode = spTree;
	do
	{
		if( spNode->getVerbType() == K_WHERE )
			return NULL;
		spNode = spNode->getBrother();
	} while( spNode->getBrother() );

	aq::verb::ColumnVerb::Ptr columnVerb = boost::dynamic_pointer_cast<aq::verb::ColumnVerb>( verb2 );
	Table::Ptr table = BaseDesc.getTable( columnVerb->getTableName() );
	size_t colIdx = table->getColumnIdx( columnVerb->getColumnOnlyName() );
	Column::Ptr column = table->Columns[colIdx];
	ColumnItem::Ptr minMax = NULL;
	bool min = verb1->getVerbType() == K_MIN;
	for( int partIdx = 0; ; ++partIdx )
	{
		ColumnItem::Ptr item = getMinMaxFromThesaurus( table->ID, colIdx, partIdx, min, BaseDesc, Settings );
		if( !item )
			break;
		if( !minMax )
			minMax = item;
		else
			if( min == ColumnItem::lessThan(item.get(), minMax.get(), column->Type) )
				minMax = item;
	}
	
	table.reset(new Table());
	Column::Ptr newColumn(new Column(*column));
	newColumn->Items.push_back( minMax );
	table->Columns.push_back( newColumn );
	table->TotalCount = 1;
	return table;
}
void SumAggregator::Apply(const Table::Ptr& table, const Value& row)
{
	Column column = table->GetColumn(m_SumAttr);

	Value value = column.ExtractValue(row);

	m_Sum += value;
}
Beispiel #6
0
void AvgAggregator::Apply(const Table::Ptr& table, const Value& row)
{
	Column column = table->GetColumn(m_AvgAttr);

	Value value = column.ExtractValue(row);

	m_Avg += value;
	m_AvgCount++;
}
Beispiel #7
0
void MinAggregator::Apply(const Table::Ptr& table, const Value& row)
{
	Column column = table->GetColumn(m_MinAttr);

	Value value = column.ExtractValue(row);

	if (value < m_Min)
		m_Min = value;
}
Beispiel #8
0
void StdAggregator::Apply(const Table::Ptr& table, const Value& row)
{
	Column column = table->GetColumn(m_StdAttr);

	Value value = column.ExtractValue(row);

	m_StdSum += value;
	m_StdQSum += pow(value, 2);
	m_StdCount++;
}
Beispiel #9
0
void SumAggregator::Apply(const Table::Ptr& table, const Value& row, AggregatorState **state)
{
	Column column = table->GetColumn(m_SumAttr);

	Value value = column.ExtractValue(row);

	SumAggregatorState *pstate = EnsureState(state);

	pstate->Sum += value;
}
Beispiel #10
0
void AvgAggregator::Apply(const Table::Ptr& table, const Value& row, AggregatorState **state)
{
	Column column = table->GetColumn(m_AvgAttr);

	Value value = column.ExtractValue(row);

	AvgAggregatorState *pstate = EnsureState(state);

	pstate->Avg += value;
	pstate->AvgCount++;
}
Beispiel #11
0
void StdAggregator::Apply(const Table::Ptr& table, const Value& row, AggregatorState **state)
{
	Column column = table->GetColumn(m_StdAttr);

	Value value = column.ExtractValue(row);

	StdAggregatorState *pstate = EnsureState(state);

	pstate->StdSum += value;
	pstate->StdQSum += pow(value, 2);
	pstate->StdCount++;
}
Beispiel #12
0
bool DomainObject::register_table_meta(Table::Ptr tbl)
{
#ifdef YB_DEBUG_DOMAIN_REG
    std::cerr << "register_table_meta(" << tbl->name()
              << "), init_flag_=" << init_flag_ << std::endl;
#endif
    if (init_flag_)
        return false;
    SchemaInfo *&items =
        *reinterpret_cast<SchemaInfo **> (&pending_);
    if (!items)
        items = new SchemaInfo();
    items->first.push_back(tbl);
    return true;
}
Beispiel #13
0
bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row)
{
	Column column = table->GetColumn(m_Column);

	Value value = column.ExtractValue(row);

	if (value.IsObjectType<Array>()) {
		Array::Ptr array = value;

		if (m_Operator == ">=" || m_Operator == "<") {
			bool negate = (m_Operator == "<");

			ObjectLock olock(array);
			for (const String& item : array) {
				if (item == m_Operand)
					return !negate; /* Item found in list. */
			}

			return negate; /* Item not found in list. */
		} else if (m_Operator == "=") {
			return (array->GetLength() == 0);
		} else {
			BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid operator for column '" + m_Column + "': " + m_Operator + " (expected '>=' or '=')."));
		}
	} else {
		if (m_Operator == "=") {
			if (value.GetType() == ValueNumber || value.GetType() == ValueBoolean)
				return (static_cast<double>(value) == Convert::ToDouble(m_Operand));
			else
				return (static_cast<String>(value) == m_Operand);
		} else if (m_Operator == "~") {
			bool ret;
			try {
				boost::regex expr(m_Operand.GetData());
				String operand = value;
				boost::smatch what;
				ret = boost::regex_search(operand.GetData(), what, expr);
			} catch (boost::exception&) {
				Log(LogWarning, "AttributeFilter")
					<< "Regex '" << m_Operand << " " << m_Operator << " " << value << "' error.";
				ret = false;
			}

			//Log(LogDebug, "LivestatusListener/AttributeFilter")
			//    << "Attribute filter '" << m_Operand + " " << m_Operator << " "
			//    << value << "' " << (ret ? "matches" : "doesn't match") << ".";

			return ret;
		} else if (m_Operator == "=~") {
			bool ret;
			try {
				String operand = value;
				ret = boost::iequals(operand, m_Operand.GetData());
			} catch (boost::exception&) {
				Log(LogWarning, "AttributeFilter")
					<< "Case-insensitive equality '" << m_Operand << " " << m_Operator << " " << value << "' error.";
				ret = false;
			}

			return ret;
		} else if (m_Operator == "~~") {
			bool ret;
			try {
				boost::regex expr(m_Operand.GetData(), boost::regex::icase);
				String operand = value;
				boost::smatch what;
				ret = boost::regex_search(operand.GetData(), what, expr);
			} catch (boost::exception&) {
				Log(LogWarning, "AttributeFilter")
					<< "Regex '" << m_Operand << " " << m_Operator << " " << value << "' error.";
				ret = false;
			}

			//Log(LogDebug, "LivestatusListener/AttributeFilter")
			//    << "Attribute filter '" << m_Operand << " " << m_Operator << " "
			//    << value << "' " << (ret ? "matches" : "doesn't match") << ".";

			return ret;
		} else if (m_Operator == "<") {
			if (value.GetType() == ValueNumber)
				return (static_cast<double>(value) < Convert::ToDouble(m_Operand));
			else
				return (static_cast<String>(value) < m_Operand);
		} else if (m_Operator == ">") {
			if (value.GetType() == ValueNumber)
				return (static_cast<double>(value) > Convert::ToDouble(m_Operand));
			else
				return (static_cast<String>(value) > m_Operand);
		} else if (m_Operator == "<=") {
			if (value.GetType() == ValueNumber)
				return (static_cast<double>(value) <= Convert::ToDouble(m_Operand));
			else
				return (static_cast<String>(value) <= m_Operand);
		} else if (m_Operator == ">=") {
			if (value.GetType() == ValueNumber)
				return (static_cast<double>(value) >= Convert::ToDouble(m_Operand));
			else
				return (static_cast<String>(value) >= m_Operand);
		} else {
			BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown operator for column '" + m_Column + "': " + m_Operator));
		}
	}

	return false;
}
Beispiel #14
0
void AQMatrix::writeTemporaryTable()
{
  if (this->matrix.empty())
    return;
  uint64_t packet = 0;
  char filename[1024];
  FILE * fd;
  std::vector<std::map<uint64_t, FILE*> > fds(this->matrix.size());
  uint32_t status = 11;
  uint32_t invalid = 0;
  uint32_t pos = 0;
  uint64_t grpIndex = 1;
  uint64_t size = (*this->matrix.begin()).indexes.size();
  for (uint64_t i = 0; i < size; ++i, ++grpIndex)
  {
    for (size_t c = 0; c < this->matrix.size(); ++c)
    {
      pos = static_cast<uint32_t>(this->matrix[c].indexes[i]);
      packet = pos / settings->packSize;
      pos = (pos - 1) % settings->packSize;
      auto it = fds[c].find(packet);
      if (it == fds[c].end())
      {
        sprintf(filename, "%s/B001REG%.4luTMP%.4"PRIu64"P%.12"PRIu64".TMP", this->settings->tmpPath.c_str(), this->matrix[c].table_id, this->uid, packet);
        fd = fopen(filename, "wb");
        if (fd == nullptr)
        {
          throw aq::generic_error(aq::generic_error::COULD_NOT_OPEN_FILE, "cannot create temporary table file '%s'", filename);
        }
        if (!fds[c].insert(std::make_pair(packet, fd)).second)
        {
          throw aq::generic_error(aq::generic_error::GENERIC, "MEMORY ERROR");
        }
        fwrite(&status, sizeof(uint32_t), 1, fd);
      }
      else
      {
        fd = it->second;
      }
      
      fwrite(&invalid, sizeof(uint32_t), 1, fd);
      fwrite(&pos, sizeof(uint32_t), 1, fd);
    }
  }
  
  for (size_t c = 0; c < this->matrix.size(); ++c)
  {
    for (auto& v : fds[c])
    {
      fclose(v.second);
    }
  }
  
  // FIXME : generate empty file => this is temporary
  char tableName[128];
  for (auto it = this->matrix.begin(); it != this->matrix.end(); ++it)
  {
    Table::Ptr table = this->baseDesc->getTable((*it).table_id);
    uint64_t n = table->getTotalCount() / this->settings->packSize;
    for (uint64_t i = 0; i <= n; ++i)
    {
      sprintf(filename, "%s/B001REG%.4luTMP%.4"PRIu64"P%.12"PRIu64".TMP", this->settings->tmpPath.c_str(), (*it).table_id, this->uid, i);
      FILE * fd = fopen(filename, "ab");
      fclose(fd);
    }
    sprintf(tableName, "B001REG%.4luTMP%.4"PRIu64"P%.12"PRIu64, (*it).table_id, this->uid, n + 1);
    (*it).tableName = tableName;
    (*it).baseTableName = table->getName();
  }
}
Beispiel #15
0
void Session::processSQL(std::string& sqlQuery)
{
	std::ostringstream oss;
	if (this->m_current_db_cfg)
	{

		boost::posix_time::ptime begin(boost::posix_time::microsec_clock::local_time());

		try
		{

			tnode	*pNode  = NULL;
			int	nRet;

			//
			// Prepare
			TProjectSettings settings(*m_current_db_cfg->settings);
			std::string displayFile;

			//
			// generate ident and ini file
			std::string queryIdentStr = "";
			boost::uuids::uuid queryIdent = boost::uuids::random_generator()();
			std::ostringstream queryIdentOSS;
			queryIdentOSS << queryIdent;
			queryIdentStr = queryIdentOSS.str();
			settings.changeIdent(queryIdentOSS.str());

			//
			// create directories
			std::list<fs::path> lpaths;
			lpaths.push_back(fs::path(settings.szRootPath + "/calculus/" + queryIdentOSS.str()));
			lpaths.push_back(fs::path(settings.szTempPath1));
			lpaths.push_back(fs::path(settings.szTempPath2));
			for (std::list<fs::path>::const_iterator dir = lpaths.begin(); dir != lpaths.end(); ++dir)
			{
				std::ostringstream ossDirName;
				ossDirName << *dir;
				aq::Logger::getInstance().log(AQ_DEBUG, "create directory '%s'\n", ossDirName.str().c_str());

				if (fs::exists(*dir))
				{
					aq::Logger::getInstance().log(AQ_ERROR, "directory already exist '%s'\n", ossDirName.str().c_str());
					return;
				}

				if (!fs::create_directory(*dir))
				{
					aq::Logger::getInstance().log(AQ_ERROR, "cannot create directory '%s'\n", ossDirName.str().c_str());
					return;
				}

				aq::Logger::getInstance().log(AQ_DEBUG, "directory '%s' created\n", ossDirName.str().c_str());
			}

			//
			// write ini file (it is needed for now by AQEngine)
			std::ofstream iniFile(settings.iniFile.c_str());
			settings.writeAQEngineIni(iniFile);
			iniFile.close();

			// generate answer file
			displayFile = settings.szRootPath + "/calculus/" + queryIdentStr + "/display.txt"; // TODO
			aq::Logger::getInstance().log(AQ_INFO, "save answer to %s\n", displayFile.c_str());


			//
			// Parse SQL request
			aq::Logger::getInstance().log(AQ_INFO, "parse sql request %s\n", sqlQuery.c_str());
			if ((nRet = SQLParse(sqlQuery.c_str(), &pNode)) != 0 ) 
			{
				oss << "error parsing sql query '" << sqlQuery << "'" << std::endl;
				aq::Logger::getInstance().log(AQ_ERROR, oss.str().c_str());
				throw generic_error(generic_error::INVALID_QUERY, oss.str());
			}

			//
			// Transform SQL request in prefix form
			unsigned int id = 1;
      QueryResolver queryResolver(pNode, &settings, this->m_current_db_cfg->m_aq_engine, *this->m_current_db_cfg->baseDesc.get(), id);
			aq::Logger::getInstance().log(AQ_INFO, "execute query %s\n", sqlQuery.c_str());
			queryResolver.solve();

      //
      // Generate result file
      Table::Ptr result = queryResolver.getResult();
      if (result)
      {
        aq::Timer timer;
        result->saveToAnswer(settings.szAnswerFN, settings.fieldSeparator);
        aq::Logger::getInstance().log(AQ_INFO, "Save Answer: Time Elapsed = %s\n", aq::Timer::getString(timer.getTimeElapsed()).c_str());
        std::ofstream fresult(settings.szAnswerFN, std::ios::app);
        fresult << "EOS";
        fresult.close();
      }

			//
			// read result file and deliver on the socket
			aq::Logger::getInstance().log(AQ_DEBUG, "read answer file %s\n", settings.szAnswerFN);
			std::ifstream answerFile(settings.szAnswerFN);
			std::string bloc;
			std::string line;
			while (std::getline(answerFile, line))
			{
				line += "\n";
				bloc += line;
				if (bloc.size() > 2048)
				{
					this->deliver(bloc);
					bloc = "";
				}
			}
			if (bloc.size())
				this->deliver(bloc);

			oss << "sql request successfully executed" << std::endl;
		}
		catch (const generic_error& ex)
		{
			aq::Logger::getInstance().log(AQ_ERROR, "%s\n", ex.what());
			oss << "error during sql processing: " << ex.what() << std::endl;
		}

		boost::posix_time::ptime end(boost::posix_time::microsec_clock::local_time());
		oss << "Time elapsed: " << (end - begin) << std::endl;
	}
	else
	{
		oss << "not connected to any database" << std::endl;
		aq::Logger::getInstance().log(AQ_NOTICE, oss.str().c_str());
	}
	this->deliver(oss.str());
}