void TripodClient::getMissedKeys(const DataMap& res, const KeySeq& keys, KeySeq& missedKeys) { for(KeySeq::const_iterator it = keys.begin(); it != keys.end(); ++it) { if(res.find(*it) == res.end()) { missedKeys.push_back(*it); } } }
TicketData TicketAdapter::queryTicket(const string& ticket) { TicketData data; if(TicketUtil::isSTicketLegal(ticket)) { KeySeq keys, missedKeys; keys.push_back(ticket); DataMap dataMap = _tripodClient->get(keys, missedKeys); DataMap::const_iterator it = dataMap.find(ticket); if(it != dataMap.end()) { string value(it->second.begin(), it->second.end()); istringstream in(value); data.ParseFromIstream(&in); } } return data; }
void FreezeScript::AssignVisitor::visitDictionary(const DictionaryDataPtr& dest) { Slice::TypePtr type = dest->getType(); DictionaryDataPtr d = DictionaryDataPtr::dynamicCast(_src); if(d && isCompatible(type, _src->getType())) { DataMap& srcMap = d->getElements(); DataMap destMap; Slice::DictionaryPtr dictType = Slice::DictionaryPtr::dynamicCast(type); assert(dictType); Slice::TypePtr keyType = dictType->keyType(); Slice::TypePtr valueType = dictType->valueType(); string typeName = typeToString(type); for(DataMap::const_iterator p = srcMap.begin(); p != srcMap.end(); ++p) { DataPtr key = _factory->create(keyType, false); Destroyer<DataPtr> keyDestroyer(key); DataPtr value = _factory->create(valueType, false); Destroyer<DataPtr> valueDestroyer(value); AssignVisitor keyVisitor(p->first, _factory, _errorReporter, _convert, typeName + " key"); key->visit(keyVisitor); AssignVisitor valueVisitor(p->second, _factory, _errorReporter, _convert, typeName + " value"); value->visit(valueVisitor); DataMap::const_iterator q = destMap.find(key); if(q != destMap.end()) { error("duplicate dictionary key in " + typeToString(dictType)); } else { destMap.insert(DataMap::value_type(key, value)); keyDestroyer.release(); valueDestroyer.release(); } } DataMap& m = dest->getElements(); m.swap(destMap); } else { typeMismatchError(type, _src->getType()); } }
long TicketAdapter::verifyTicket(const string& ticket, const IntSeq& types) { long result = -1; if(TicketUtil::isSTicketLegal(ticket)) { KeySeq keys, missedKeys; keys.push_back(ticket); DataMap dataMap = _tripodClient->get(keys, missedKeys); DataMap::const_iterator itm = dataMap.find(ticket); if(itm != dataMap.end()) { TicketData data; string value(itm->second.begin(), itm->second.end()); istringstream in(value); data.ParseFromIstream(&in); int type = data.type(); for(IntSeq::const_iterator it = types.begin(); it != types.end(); ++it) { if(type == *it) { result = data.id(); break; } } } } return result; }
void writeECLData(const UnstructuredGrid& grid, const DataMap& data, const int current_step, const double current_time, const boost::posix_time::ptime& current_date_time, const std::string& output_dir, const std::string& base_name) { ecl_file_enum file_type = ECL_UNIFIED_RESTART_FILE; // Alternatively ECL_RESTART_FILE for multiple restart files. bool fmt_file = true; char * filename = ecl_util_alloc_filename(output_dir.c_str() , base_name.c_str() , file_type , fmt_file , current_step ); int phases = ECL_OIL_PHASE + ECL_WATER_PHASE; double days = Opm::unit::convert::to(current_time, Opm::unit::day); time_t date = 0; int nx = grid.cartdims[0]; int ny = grid.cartdims[1]; int nz = grid.cartdims[2]; int nactive = grid.number_of_cells; ecl_rst_file_type * rst_file; { using namespace boost::posix_time; ptime t0( boost::gregorian::date(1970 , 1 ,1) ); time_duration::sec_type seconds = (current_date_time - t0).total_seconds(); date = time_t( seconds ); } if (current_step > 0 && file_type == ECL_UNIFIED_RESTART_FILE) rst_file = ecl_rst_file_open_append( filename ); else rst_file = ecl_rst_file_open_write( filename ); ecl_rst_file_fwrite_header( rst_file , current_step , date , days , nx , ny , nz , nactive , phases ); ecl_rst_file_start_solution( rst_file ); { DataMap::const_iterator i = data.find("pressure"); if (i != data.end()) { ecl_kw_type * pressure_kw = ecl_kw_wrapper( grid , "PRESSURE" , i->second , 0 , 1); ecl_rst_file_add_kw( rst_file , pressure_kw ); ecl_kw_free( pressure_kw ); } } { DataMap::const_iterator i = data.find("saturation"); if (i != data.end()) { if (int(i->second->size()) != 2 * grid.number_of_cells) { THROW("writeECLData() requires saturation field to have two phases."); } ecl_kw_type * swat_kw = ecl_kw_wrapper( grid , "SWAT" , i->second , 0 , 2); ecl_rst_file_add_kw( rst_file , swat_kw ); ecl_kw_free( swat_kw ); } } ecl_rst_file_end_solution( rst_file ); ecl_rst_file_close( rst_file ); free(filename); }
void FreezeScript::TransformVisitor::visitDictionary(const DictionaryDataPtr& dest) { Slice::TypePtr type = dest->getType(); if(_info->doDefaultTransform(type)) { DictionaryDataPtr d = DictionaryDataPtr::dynamicCast(_src); if(d && isCompatible(type, _src->getType())) { DataMap& srcMap = d->getElements(); DataMap destMap; Slice::DictionaryPtr dictType = Slice::DictionaryPtr::dynamicCast(type); assert(dictType); Slice::TypePtr keyType = dictType->keyType(); Slice::TypePtr valueType = dictType->valueType(); string typeName = typeToString(type); for(DataMap::const_iterator p = srcMap.begin(); p != srcMap.end(); ++p) { DataPtr key = _info->getDataFactory()->create(keyType, false); Destroyer<DataPtr> keyDestroyer(key); DataPtr value = _info->getDataFactory()->create(valueType, false); Destroyer<DataPtr> valueDestroyer(value); TransformVisitor keyVisitor(p->first, _info, typeName + " key"); key->visit(keyVisitor); try { TransformVisitor valueVisitor(p->second, _info, typeName + " value"); value->visit(valueVisitor); } catch(const ClassNotFoundException& ex) { // // If transformation of the dictionary value fails because a class // could not be found, then we invoke purgeObjects() to determine // whether we should ignore the situation (and remove the element // from the dictionary) or raise the exception again. // if(!_info->purgeObjects()) { throw; } warning("purging element of dictionary " + typeToString(dictType) + " due to missing class type " + ex.id); continue; } DataMap::const_iterator q = destMap.find(key); if(q != destMap.end()) { warning("duplicate dictionary key in " + typeToString(dictType)); } else { destMap.insert(DataMap::value_type(key, value)); keyDestroyer.release(); valueDestroyer.release(); } } DataMap& m = dest->getElements(); m.swap(destMap); } else { typeMismatchError(type, _src->getType()); } } _info->executeCustomTransform(dest, _src); }
void writeVtkData(const UnstructuredGrid& grid, const DataMap& data, std::ostream& os) { if (grid.dimensions != 3) { OPM_THROW(std::runtime_error, "Vtk output for 3d grids only"); } os.precision(12); os << "<?xml version=\"1.0\"?>\n"; PMap pm; pm["type"] = "UnstructuredGrid"; Tag vtkfiletag("VTKFile", pm, os); Tag ugtag("UnstructuredGrid", os); int num_pts = grid.number_of_nodes; int num_cells = grid.number_of_cells; pm.clear(); pm["NumberOfPoints"] = boost::lexical_cast<std::string>(num_pts); pm["NumberOfCells"] = boost::lexical_cast<std::string>(num_cells); Tag piecetag("Piece", pm, os); { Tag pointstag("Points", os); pm.clear(); pm["type"] = "Float64"; pm["Name"] = "Coordinates"; pm["NumberOfComponents"] = "3"; pm["format"] = "ascii"; Tag datag("DataArray", pm, os); for (int i = 0; i < num_pts; ++i) { Tag::indent(os); os << grid.node_coordinates[3*i + 0] << ' ' << grid.node_coordinates[3*i + 1] << ' ' << grid.node_coordinates[3*i + 2] << '\n'; } } { Tag cellstag("Cells", os); pm.clear(); pm["type"] = "Int32"; pm["NumberOfComponents"] = "1"; pm["format"] = "ascii"; std::vector<int> cell_numpts; cell_numpts.reserve(num_cells); { pm["Name"] = "connectivity"; Tag t("DataArray", pm, os); int hf = 0; for (int c = 0; c < num_cells; ++c) { std::set<int> cell_pts; for (; hf < grid.cell_facepos[c+1]; ++hf) { int f = grid.cell_faces[hf]; const int* fnbeg = grid.face_nodes + grid.face_nodepos[f]; const int* fnend = grid.face_nodes + grid.face_nodepos[f+1]; cell_pts.insert(fnbeg, fnend); } cell_numpts.push_back(cell_pts.size()); Tag::indent(os); std::copy(cell_pts.begin(), cell_pts.end(), std::ostream_iterator<int>(os, " ")); os << '\n'; } } { pm["Name"] = "offsets"; Tag t("DataArray", pm, os); int offset = 0; const int num_per_line = 10; for (int c = 0; c < num_cells; ++c) { if (c % num_per_line == 0) { Tag::indent(os); } offset += cell_numpts[c]; os << offset << ' '; if (c % num_per_line == num_per_line - 1 || c == num_cells - 1) { os << '\n'; } } } std::vector<int> cell_foffsets; cell_foffsets.reserve(num_cells); { pm["Name"] = "faces"; Tag t("DataArray", pm, os); const int* fp = grid.cell_facepos; int offset = 0; for (int c = 0; c < num_cells; ++c) { Tag::indent(os); os << fp[c+1] - fp[c] << '\n'; ++offset; for (int hf = fp[c]; hf < fp[c+1]; ++hf) { int f = grid.cell_faces[hf]; const int* np = grid.face_nodepos; int f_num_pts = np[f+1] - np[f]; Tag::indent(os); os << f_num_pts << ' '; ++offset; std::copy(grid.face_nodes + np[f], grid.face_nodes + np[f+1], std::ostream_iterator<int>(os, " ")); os << '\n'; offset += f_num_pts; } cell_foffsets.push_back(offset); } } { pm["Name"] = "faceoffsets"; Tag t("DataArray", pm, os); const int num_per_line = 10; for (int c = 0; c < num_cells; ++c) { if (c % num_per_line == 0) { Tag::indent(os); } os << cell_foffsets[c] << ' '; if (c % num_per_line == num_per_line - 1 || c == num_cells - 1) { os << '\n'; } } } { pm["type"] = "UInt8"; pm["Name"] = "types"; Tag t("DataArray", pm, os); const int num_per_line = 10; for (int c = 0; c < num_cells; ++c) { if (c % num_per_line == 0) { Tag::indent(os); } os << "42 "; if (c % num_per_line == num_per_line - 1 || c == num_cells - 1) { os << '\n'; } } } } { pm.clear(); if (data.find("saturation") != data.end()) { pm["Scalars"] = "saturation"; } else if (data.find("pressure") != data.end()) { pm["Scalars"] = "pressure"; } Tag celldatatag("CellData", pm, os); pm.clear(); pm["NumberOfComponents"] = "1"; pm["format"] = "ascii"; pm["type"] = "Float64"; for (DataMap::const_iterator dit = data.begin(); dit != data.end(); ++dit) { pm["Name"] = dit->first; const std::vector<double>& field = *(dit->second); const int num_comps = field.size()/grid.number_of_cells; pm["NumberOfComponents"] = boost::lexical_cast<std::string>(num_comps); Tag ptag("DataArray", pm, os); const int num_per_line = num_comps == 1 ? 5 : num_comps; for (int item = 0; item < num_cells*num_comps; ++item) { if (item % num_per_line == 0) { Tag::indent(os); } double value = field[item]; if (std::fabs(value) < std::numeric_limits<double>::min()) { // Avoiding denormal numbers to work around // bug in Paraview. value = 0.0; } os << value << ' '; if (item % num_per_line == num_per_line - 1 || item == num_cells - 1) { os << '\n'; } } } } }
void CsvFile::read(HeaderList& header, DataMap& data) const throw (FileException, CustomException) { std::ifstream file(filename.c_str()); if (!file) throw FileException("Cannot open file", filename, __FUNCTION_NAME__); // a buffer to store the line read std::string line; if ( ! std::getline(file, line)) throw FileException("Cannot get the first line of file", filename, __FUNCTION_NAME__); unsigned int pointer = 0; unsigned int rawnb = 0; std::string word =""; int localHostColNumber = -1, tmp=0; bool uselocalhost=false; bool useotherthanlocalhost=false; // Parse the first line (header line) while(pointer < line.size()) { while( pointer < line.size() && line[pointer] != separator) { //remove white spaces if (line[pointer] != ' ') { word.push_back(line[pointer]); } pointer++; } if( !word.empty()) { // warning if id already in if (std::find(header.begin(), header.end(), word) != header.end() ) { // Was error before, now just a warning: may be convenient to have several time the same name in a header Msg::warning("Duplicate header element '"+ word+"' in file "+filename, __FUNCTION_NAME__); // throw FileException("Duplicate header elements'"+ word+"'",filename, __FUNCTION_NAME__); } header.push_back(word); tmp++; } // if we have "localhost" in csv file, note the column number. if(word == "localhost" || word == "Localhost" || word == "127.0.0.1") localHostColNumber = tmp; pointer++; word.clear(); } // Parse the lines while (std::getline(file, line)) { rawnb++; pointer = 0; bool firstWord = true; // Are we parsing the first column unsigned int column = 0; // Which column is being scanned? std::string id; while(line.size() > pointer) { // get word word.clear(); while(line.size() > pointer && line[pointer] != separator) { if (line[pointer] != ' ') word.push_back(line[pointer]); pointer++; } pointer++; if (firstWord) { // it's the map id if ( word.empty() ) break; // forget about this line and go to parse next line id = word; if(data.find(id) != data.end()) throw CustomException("Component : " + id + " is specified twice in CSV file.", __FUNCTION_NAME__ ); // init the raw of this id with zeros. data[id].insert(data[id].begin(), header.size(),0); firstWord = false; } else { if (!word.empty()) { // store data try{ data[id].at(column) = convertTo<int>(word); if (data[id].at(column) > 0)// one component instance at least mapped { if(column == localHostColNumber-1) // we are reading in a locahost column { // Remember that we have one component mapped on a localHost. uselocalhost=true; } else { // Remember that we have one component mapped on an host different from localHost. useotherthanlocalhost=true; } if(uselocalhost && useotherthanlocalhost) throw FileException("Host name 'localhost' is used in CSV file in conjunction with other host names or IP address. Replace 'localhost' by IP address or machine name to avoid problems", filename, __FUNCTION_NAME__); } }//end try catch(const BadConversion& e) { throw FileException("Raw="+toString<unsigned int>(rawnb+1)+", col="+toString<unsigned int>(column+2)+") contains non integer value="+word,filename, __FUNCTION_NAME__); // std::cerr << "ERROR: Csv File "<<fileName<<", element (raw="<<rawnb+1<<", col="<<column+2<<") contains non integer value="<<word<< std::endl; } catch(const std::out_of_range& e) { throw FileException("Element (raw="+toString<unsigned int>(rawnb+1)+", col="+toString<unsigned int>(column+2)+") out of column (value="+word+", ignored)",filename, __FUNCTION_NAME__); // std::cerr << "WARNING : Csv File "<<fileName<<", element (raw="<<rawnb+1<<", col="<<column+2<<") out of column (value="<<word<<", ignored)"<< std::endl; } } column++; } } } };
/** sqlite3_exec的回调。 * * 向控制台打印查询的结果。 * * @param in data 传递给回调函数的数据。 * @param in n_columns sqlite3_exec执行结果集中列的数量。 * @param in col_values sqlite3_exec执行结果集中每一列的数据。 * @param in col_names sqlite3_exec执行结果集中每一列的名称。 * @return 状态码。 */ int sqlite3_exec_callback(void *data, int n_columns, char **col_values, char **col_names) { s_trade trade; long time_peroid = 0; /* for (int i = 0; i < n_columns; i++) { printf("%s:%s\t", col_names[i], col_values[i]); } */ trade.timestamp = atol(col_values[0]); trade.price = atof(col_values[1]); trade.amount = atof(col_values[2]); if(0 == strcmp((const char *)data, "5m")){ time_peroid = 5*60; } else if(0 == strcmp((const char *)data, "15m")){ time_peroid = 15*60; } else if(0 == strcmp((const char *)data, "30m")){ time_peroid = 30*60; } else if(0 == strcmp((const char *)data, "1h")){ time_peroid = 60*60; } else if(0 == strcmp((const char *)data, "4h")){ time_peroid = 60*60; } else if(0 == strcmp((const char *)data, "1d")){ time_peroid = 60*60*24; } if(0 != time_peroid){ long time = ((long)(trade.timestamp/time_peroid))*time_peroid; DataMap::iterator it = dataMap.find(time); if(it==dataMap.end()) { s_history history; history.amount = trade.amount; history.open = trade.price; history.close = trade.price; history.high = trade.price; history.low = trade.price; history.timestamp = time; history.open_timestamp = trade.timestamp; history.close_timestamp = trade.timestamp; dataMap.insert(std::make_pair(time, history)); } else { it->second.amount += trade.amount; if(trade.price > it->second.high){ it->second.high = trade.price; } if(trade.price < it->second.low){ it->second.low = trade.price; } if(trade.timestamp < it->second.open_timestamp){ it->second.open = trade.price; it->second.open_timestamp = trade.timestamp; } if(trade.timestamp > it->second.close_timestamp){ it->second.close = trade.price; it->second.close_timestamp = trade.timestamp; } } } return 0; }