bool CacheManagerI::load(const KeySeq& keys, const std::string& namespaceId, const std::string& businessId, long expiredTime, bool useMaster, const Ice::Current& current) { std::ostringstream os; os<<"Key : "; for(KeySeq::const_iterator it = keys.begin(); it != keys.end(); ++it) { os<<*it<<" "; } os<<" namespaceId :"<<namespaceId; os<<" businessId :"<<businessId; os<<" expiredTime :"<<expiredTime; os<<" useMaster :"<<useMaster; MyUtil::InvokeClient ic = MyUtil::InvokeClient::create(current, os.str(), MyUtil::InvokeClient::INVOKE_LEVEL_INFO); ProducerManagerClientPtr producer = getProducer(); if(producer == NULL) { return false; } bool sucFlag = true; KeySeq inputKeys(keys.begin(), keys.end()); KeySeq lockedKeys; KeySeq failedLockedKeys; DataMap res; while(true) { xce::tempmutext::Locks<std::string> locks(&tempMutexManager_, inputKeys, lockedKeys, failedLockedKeys); if(!lockedKeys.empty()) { DataMap singleRes = producer->produce(keys, businessId, useMaster, 1000); if(!singleRes.empty()) { res.insert(singleRes.begin(), singleRes.end()); } CacheClientPtr cache = getCache(); if(cache != NULL) { for(DataMap::const_iterator it = singleRes.begin(); it != singleRes.end(); ++it) { sucFlag &= cache->set(it->first, it->second, namespaceId, businessId, expiredTime, 1000); } } if(failedLockedKeys.empty()) { break; } } inputKeys.swap(failedLockedKeys); lockedKeys.clear(); failedLockedKeys.clear(); } return sucFlag; }
void writeout_file(char *time_peroid){ char sql[1024]; int retcode; sprintf(sql, "SELECT * FROM history"); retcode = sqlite3_exec(pDB, sql, &sqlite3_exec_callback, time_peroid, &errmsg); if(SQLITE_OK!=retcode){ printf("retcode of sqlite3_exec():%d description:%s", retcode, errmsg); sqlite3_free(errmsg); } char filename[1024]; sprintf(filename, "%s.csv", time_peroid); FILE *fp = fopen(filename, "wt"); if(NULL!=fp){ printf("\nWriting file:%s...", filename); for(DataMap::iterator it = dataMap.begin(); it != dataMap.end(); it++){ struct tm *st = localtime(&(it->second.timestamp)); fprintf(fp, "%04d.%02d.%02d,%02d:%02d,%lf,%lf,%lf,%lf,%lf\n", st->tm_year+1900, st->tm_mon+1, st->tm_mday, st->tm_hour, st->tm_min, it->second.open, it->second.high, it->second.low, it->second.close, it->second.amount); /* printf("timestamp:%s open:%lf high:%lf low:%lf close:%lf amount:%lf\n", ctime(&(it->second.timestamp)), it->second.open, it->second.high, it->second.low, it->second.close, it->second.amount); */ } printf("done", filename); fclose(fp); } dataMap.clear(); // clear all the data in map }
void CsvFile::write(const HeaderList& header, const DataMap& data) throw (FileException) { std::ofstream file(filename.c_str()); if (!file) throw FileException("Cannot open file", filename, __FUNCTION_NAME__); // Print raw of headers for (HeaderList::const_iterator it = header.begin(); it != header.end(); ++it) { file << separator << *it; } // End of line file << std::endl; // Print all data raws for (DataMap::const_iterator rawit = data.begin() ; rawit != data.end(); ++rawit) { file<< rawit->first; for (DataRaw::const_iterator it = rawit->second.begin(); it != rawit->second.end(); ++it) { if (*it > 0) file << separator << *it; else file << separator; } // End of line file << std::endl; } };
int main() { DataMap sightings; generate_n( inserter(sightings, sightings.begin()), 50, SightingGen(animals)); // Print everything: copy(sightings.begin(), sightings.end(), ostream_iterator<Sighting>(cout, "\n")); // Print sightings for selected animal: while (true) { cout << "select an animal or 'q' to quit: "; for (int i = 0; i < animals.size(); i++) cout << '[' << i << ']' << animals[i] << ' '; cout << endl; string reply; cin >> reply; if (reply.at(0) == 'q') return 0; istringstream r(reply); int i; r >> i; // Convert to int i %= animals.size(); // Iterators in "range" denote begin, one // past end of matching range: pair<DMIter, DMIter> range = sightings.equal_range(animals[i]); copy(range.first, range.second, ostream_iterator<Sighting>(cout, "\n")); } } ///:~
bool CacheManagerI::loadListCache(const std::string& key, const std::string& namespace_id, const std::string& business_id, bool use_master, const Ice::Current& current) { std::ostringstream otem; otem << "CacheManageI::loadListCache() key:" << key << "\tnamespace_id:" << namespace_id << "\tbusiness_id:" << business_id; MyUtil::InvokeClient ic = MyUtil::InvokeClient::create(current, otem.str(), MyUtil::InvokeClient::INVOKE_LEVEL_INFO); ProducerManagerClientPtr producer = getProducer(); if(producer == NULL) { return false; } KeySeq keys; keys.push_back(key); DataMap data = producer->produce(keys, business_id, use_master, 0); if (data.empty()) { return true; } std::string list_key; StrList list_value; DataMap::const_iterator iter = data.begin(); list_key = iter->second; for (; iter != data.end(); ++iter) { list_value.push_back(iter->first); } RedisCacheClientPtr redis_client = GetRedisCache(); redis_client->Set(list_key, list_value, namespace_id, business_id); return true; }
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); } } }
void writeVtkData(const std::array<int, 3>& dims, const std::array<double, 3>& cell_size, const DataMap& data, std::ostream& os) { // Dimension is hardcoded in the prototype and the next two lines, // but the rest is flexible (allows dimension == 2 or 3). int dimension = 3; int num_cells = dims[0]*dims[1]*dims[2]; assert(dimension == 2 || dimension == 3); assert(num_cells == dims[0]*dims[1]* (dimension == 2 ? 1 : dims[2])); os << "# vtk DataFile Version 2.0\n"; os << "Structured Grid\n \n"; os << "ASCII \n"; os << "DATASET STRUCTURED_POINTS\n"; os << "DIMENSIONS " << dims[0] + 1 << " " << dims[1] + 1 << " "; if (dimension == 3) { os << dims[2] + 1; } else { os << 1; } os << "\n"; os << "ORIGIN " << 0.0 << " " << 0.0 << " " << 0.0 << "\n"; os << "SPACING " << cell_size[0] << " " << cell_size[1]; if (dimension == 3) { os << " " << cell_size[2]; } else { os << " " << 0.0; } os << "\n"; os << "\nCELL_DATA " << num_cells << '\n'; for (DataMap::const_iterator dit = data.begin(); dit != data.end(); ++dit) { std::string name = dit->first; os << "SCALARS " << name << " float" << '\n'; os << "LOOKUP_TABLE " << name << "_table " << '\n'; const std::vector<double>& field = *(dit->second); // We always print only the first data item for every // cell, using 'stride'. // This is a hack to get water saturation nicely. // \TODO: Extend to properly printing vector data. const int stride = field.size()/num_cells; const int num_per_line = 5; for (int c = 0; c < num_cells; ++c) { os << field[stride*c] << ' '; if (c % num_per_line == num_per_line - 1 || c == num_cells - 1) { os << '\n'; } } } }
DataMap TripodClient::getWithMissed(const KeySeq& keys, long expiredTime, bool useMaster, const long timeout) { KeySeq missedKeys; DataMap res = get(keys, missedKeys, timeout); if(!missedKeys.empty()) { DataMap missedRes = getMissed(missedKeys, expiredTime, useMaster, timeout); res.insert(missedRes.begin(), missedRes.end()); } return res; }
map<string, MenuTripodDataPtr> MenuCacheManagerI::getWithKeys(const KeySeq& keys) { map<string, MenuTripodDataPtr> result; KeySeq missedKeys; DataMap dataMap = tripodClient_->get(keys, missedKeys); for (DataMap::const_iterator it = dataMap.begin(); it != dataMap.end(); ++it) { string byteArray(it->second.begin(), it->second.end()); MenuTripodDataPtr ptr = new MenuTripodData(byteArray); result.insert(make_pair<string, MenuTripodDataPtr>(it->first, ptr)); } return result; }
void WAbstractItemModel::copyData(const WAbstractItemModel *source, const WModelIndex& sIndex, WAbstractItemModel *destination, const WModelIndex& dIndex) { DataMap values = destination->itemData(dIndex); for (DataMap::const_iterator i = values.begin(); i != values.end(); ++i) destination->setData(dIndex, boost::any(), i->first); destination->setItemData(dIndex, source->itemData(sIndex)); }
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; }
/* *Function:SeekMaxMinYLine *Inputs:none *Outputs:none *Returns:none */ void Graph::SeekMaxMinYLine(DataMap &l) { IT_IT("Graph::SeekMaxMinYLine"); for(DataMap::iterator j = l.begin(); !(j == l.end());j++) { if(minY > (*j).second) { minY = (*j).second; } else if(maxY < (*j).second) { maxY = (*j).second; }; }; };
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()); } }
bool CacheManagerI::loadListCache(const std::string& key, const std::string& namespace_id, const std::string& biz_id, bool use_master, const Ice::Current& current) { std::ostringstream os; os << "key :" << key; os << " namespaceId :" << namespace_id; os << " businessId :" << biz_id; os << " use_master :" << use_master; MyUtil::InvokeClient ic = MyUtil::InvokeClient::create(current, os.str(), MyUtil::InvokeClient::INVOKE_LEVEL_INFO); ProducerManagerClientPtr producer = getProducer(); if(!producer) { MCE_ERROR("CacheManagerI::loadListCache() getProducer() return NULL!!!"); return false; } KeySeq input_keys; input_keys.push_back(key); KeySeq locked_keys; KeySeq failed_locked_keys; bool result = false; while(true) { xce::tempmutext::Locks<std::string> locks(&tempMutexManager_, input_keys, locked_keys, failed_locked_keys); if(!locked_keys.empty()) { DataMap data; try { data = producer->produce(locked_keys, biz_id, use_master, 10000); } catch (Ice::Exception& e) { MCE_ERROR("CacheManagerI::loadListCache() rpc call produce() " << e.what()); } DataMap::const_iterator iter = data.begin(); for (; iter != data.end(); ++iter) { if (!iter->second.empty()) { ListOrHashValue list_value; list_value.ParseFromArray(iter->second.data(), iter->second.size()); if (list_value.values_size() > 0) { StrList str_list; BOOST_FOREACH(const std::string& v, list_value.values()) { str_list.push_back(v); } RedisCacheClientPtr cache = GetRedisCache(); if (!cache) { MCE_ERROR("CacheManagerI::loadListCache() GetRedisCache() return NULL !!!"); continue; } result = cache->SetList(key, str_list, namespace_id, biz_id); } } }
bool WAbstractItemModel::setItemData(const WModelIndex& index, const DataMap& values) { bool result = true; bool wasBlocked = dataChanged().isBlocked(); dataChanged().setBlocked(true); for (DataMap::const_iterator i = values.begin(); i != values.end(); ++i) // if (i->first != EditRole) if (!setData(index, i->second, i->first)) result = false; dataChanged().setBlocked(wasBlocked); dataChanged().emit(index, index); return result; }
void WeatherScreen::newData(QString loc, units_t units, DataMap data) { (void) loc; (void) units; DataMap::iterator itr = data.begin(); while (itr != data.end()) { setValue(itr.key(), *itr); ++itr; } // This may seem like overkill, but it is necessary to actually update the // static and animated maps when they are redownloaded on an update if (!prepareScreen()) LOG(VB_GENERAL, LOG_ERR, "Theme is missing a required widget!"); emit screenReady(this); }
int main() { DataMap sightings; generate_n( inserter(sightings, sightings.begin()), 50, SightingGen(animals)); // Print everything: copy(sightings.begin(), sightings.end(), ostream_iterator<Sighting>(cout, "")); // Print sightings for selected animal: for(int count = 1; count < 10; count++) { // Use menu to get selection: // int i = menu(); // Generate randomly (for automated testing): int i = rand() % animals.size(); // Iterators in "range" denote begin, one // past end of matching range: pair<DMIter, DMIter> range = sightings.equal_range(animals[i]); copy(range.first, range.second, ostream_iterator<Sighting>(cout, "")); } } ///:~
int main(int argc, char* argv[]) { if (argc < 3) { std::cout << "Usage: TestTripodSearchGet logPath userId1 [userId2, ...]" << std::endl; return 1; } ostringstream oss; com::xiaonei::xce::ConnectionPoolManager::instance(); oss << argv[1] << "/TripodSearchGet.log"; std::vector<std::string> keys; std::vector<std::string> missedKeys; std::string logPath = oss.str(); MyUtil::init_logger("Mce", logPath, "DEBUG"); for (int i = 2; i < argc; i++) { std::string userId = boost::lexical_cast<std::string>(argv[i]); MCE_WARN("logPath:" << logPath << " userId:" << userId); keys.push_back(userId); } TripodClient* testClient = new TripodClient("UserZooKeeper1:2181,UserZooKeeper2:2181,UserZooKeeper3:2181,UserZooKeeper4:2181,UserZooKeeper5:2181/Tripod", "ne0", "SearchCache"); DataMap result; try { result = testClient->get(keys, missedKeys); } catch (Ice::Exception& e) { MCE_ERROR("get Error :" << e.what()); } MCE_WARN("result.size:" << result.size()); for (std::map<std::string, Data>::iterator it = result.begin(); it != result.end(); it++) { MCE_WARN("result key:" << it->first); TripodCacheDataPtr tmp = new TripodCacheData; string dataStr((it->second).begin(), (it->second).end()); tmp->ParseFromString(dataStr); show(it->first, tmp); std::cout << "---------------------------------------" << std::endl; } std::cout << "result.size:" << result.size() << std::endl; delete testClient; return 0; }
vector<MenuTripodDataPtr> MenuCacheManagerI::getWithUserId(int userId, const CacheTypeSeq& types) { vector<MenuTripodDataPtr> result; for (CacheTypeSeq::const_iterator it = types.begin(); it != types.end(); ++it) { int category = getObjCacheCategoryWithCacheType(*it); if (category == -1) { continue; } string cacheTypeStr = TripodHelper::getCacheTypeStr(*it); MenuTripodDataPtr ptr = ServiceI::instance().findObject<MenuTripodDataPtr>(category, userId); if (ptr != 0) { //MCE_INFO("[MenuCacheManagerI::getWithUserId] userId = " << userId << ", cacheType = " << cacheTypeStr // << " found in ObjectCache"); result.push_back(ptr); } else { TripodClient* tripodClient = getTripodClientWithCacheType(*it); KeySeq missedKeys, keys; keys.push_back(boost::lexical_cast<string>(userId)); DataMap dataMap = tripodClient->get(keys, missedKeys); if (!missedKeys.empty()) { addKeysToLoad(missedKeys, *it); } if (dataMap.empty()) { //MCE_INFO("[MenuCacheManagerI::getWithUserId] userId = " << userId << ", cacheType = " << cacheTypeStr // << ", not found in TripodCache"); result.push_back(new MenuTripodData(userId, *it)); } else { //MCE_INFO("[MenuCacheManagerI::getWithUserId] userId = " << userId << ", cacheType = " << cacheTypeStr // << ", found in TripodCache"); for (DataMap::const_iterator itx = dataMap.begin(); itx != dataMap.end(); ++itx) { string byteArray(itx->second.begin(), itx->second.end()); MenuTripodDataPtr tempPtr = new MenuTripodData(byteArray); result.push_back(tempPtr); } } } } return result; }
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; }
OP_NAMESPACE_BEGIN int Ardb::HashMultiSet(Context& ctx, ValueObject& meta, DataMap& fs) { if (meta.meta.encoding != COLLECTION_ECODING_ZIPMAP) { bool multi_write = fs.size() > 1; bool set_meta = meta.meta.len != -1; if (meta.meta.Length() > 0) { multi_write = true; } BatchWriteGuard guard(GetKeyValueEngine(), multi_write); DataMap::iterator it = fs.begin(); while (it != fs.end()) { ValueObject v(HASH_FIELD); v.element = it->second; v.key.type = HASH_FIELD; v.key.db = meta.key.db; v.key.key = meta.key.key; v.key.element = it->first; SetKeyValue(ctx, v); it++; } if (set_meta) { meta.meta.len = -1; SetKeyValue(ctx, meta); } fill_int_reply(ctx.reply, fs.size()); } else { int64 oldlen = meta.meta.Length(); DataMap::iterator it = fs.begin(); bool zipsave = true; while (it != fs.end()) { const Data& field = it->first; Data& value = it->second; meta.meta.zipmap[field] = value; it++; if (!meta.attach.force_zipsave && (field.StringLength() > m_cfg.hash_max_ziplist_value || value.StringLength() > m_cfg.hash_max_ziplist_value)) { zipsave = false; } } if (meta.meta.zipmap.size() > m_cfg.hash_max_ziplist_entries) { zipsave = false; } BatchWriteGuard guard(GetKeyValueEngine(), !zipsave); if (!zipsave) { /* * convert to non zipmap encoding */ DataMap::iterator fit = meta.meta.zipmap.begin(); while (fit != meta.meta.zipmap.end()) { ValueObject v(HASH_FIELD); v.element = fit->second; v.key.type = HASH_FIELD; v.key.db = meta.key.db; v.key.key = meta.key.key; v.key.element = fit->first; SetKeyValue(ctx, v); fit++; } meta.meta.len = meta.meta.zipmap.size(); meta.meta.zipmap.clear(); meta.meta.encoding = COLLECTION_ECODING_RAW; } SetKeyValue(ctx, meta); fill_int_reply(ctx.reply, meta.meta.Length() - oldlen); } return 0; }