void process_cache::store(string const &key,set<string> const &triggers_in,time_t timeout_in,archive const &a) { rwlock_wrlock lock(access_lock); pointer main; main=primary.find(key.c_str()); if(main!=primary.end()) delete_node(main); if(a.get().size()>memsize/20) { return; } time_t now; time(&now); // Make sure there is at least 10% avalible // And there is a block that is big enough to allocate 5% of memory for(;;) { if(process_cache_factory::mem->available() > memsize / 10) { void *p=process_cache_factory::mem->malloc(memsize/20); if(p) { process_cache_factory::mem->free(p); break; } } if(timeout.begin()->first<now) { main=timeout.begin()->second; } else { main=lru.back(); } delete_node(main); } try { pair<pointer,bool> res=primary.insert(pair<shr_string,container>(key.c_str(),container())); main=res.first; container &cont=main->second; cont.data.assign(a.get().c_str(),a.get().size()); lru.push_front(main); cont.lru=lru.begin(); cont.timeout=timeout.insert(pair<time_t,pointer>(timeout_in,main)); if(triggers_in.find(key)==triggers_in.end()){ cont.triggers.push_back(triggers.insert( pair<shr_string,pointer>(key.c_str(),main))); } set<string>::const_iterator si; for(si=triggers_in.begin();si!=triggers_in.end();si++) { cont.triggers.push_back(triggers.insert( pair<shr_string,pointer>(si->c_str(),main))); } } catch(std::bad_alloc const &e) { clear(); } }
void serialize(archive& a) const { std::size_t len = length(); a & len; if ( buffer ) { a.write(buffer, len); } }
void thread_cache::store(string const &key,set<string> const &triggers_in,time_t timeout_in,archive const &a) { rwlock_wrlock lock(access_lock); if(debug_mode) print_all(); pointer main; if(debug_mode) { string res; res=str(boost::format("Storing key [%1%], triggers:") % key); for(set<string>::iterator ps=triggers_in.begin(),pe=triggers_in.end();ps!=pe;ps++) { res+=*ps; res+=" "; } res+="\n"; write(fd,res.c_str(),res.size()); } main=primary.find(key); if(main==primary.end() && primary.size()>=limit && limit>0) { if(debug_mode) { char const *msg="Not found, size limit\n"; write(fd,msg,strlen(msg)); } time_t now; time(&now); if(timeout.begin()->first<now) { main=timeout.begin()->second; if(debug_mode) { string res; res=str(boost::format("Deleting timeout node [%1%] with " "delta of %2% seconds\n") % main->first % (now - main->second.timeout->first)); write(fd,res.c_str(),res.size()); } } else { main=lru.back(); if(debug_mode) { string res; res=str(boost::format("Deleting LRU [%1%]\n") % main->first); write(fd,res.c_str(),res.size()); } } } if(main!=primary.end()) delete_node(main); pair<pointer,bool> res=primary.insert(pair<string,container>(key,container())); main=res.first; container &cont=main->second; cont.data=a.get(); lru.push_front(main); cont.lru=lru.begin(); cont.timeout=timeout.insert(pair<time_t,pointer>(timeout_in,main)); if(triggers_in.find(key)==triggers_in.end()){ cont.triggers.push_back(triggers.insert(pair<string,pointer>(key,main))); } set<string>::const_iterator si; for(si=triggers_in.begin();si!=triggers_in.end();si++) { cont.triggers.push_back(triggers.insert(pair<string,pointer>(*si,main))); } }
bool process_cache::fetch(string const &key,archive &a,set<string> &tags) { rwlock_rdlock lock(access_lock); shr_string *r=get(key,&tags); if(!r) return false; a.set(r->c_str(),r->size()); return true; }
bool thread_cache::fetch(string const &key,archive &a,set<string> &tags) { rwlock_rdlock lock(access_lock); string *r=get(key,&tags); if(!r) return false; a.set(*r); return true; }
template <typename T, typename MemoryBlock> void load( archive & ar , std::string const & path , alps::numeric::matrix<T,MemoryBlock> & m , std::vector<std::size_t> chunk = std::vector<std::size_t>() , std::vector<std::size_t> offset = std::vector<std::size_t>() ) { using std::copy; if(ar.is_data(path + "/size1") && ar.is_scalar(path + "/size1")) { // Old matrix hdf5 format std::size_t size1(0), size2(0), reserved_size1(0); ar[path + "/size1"] >> size1; ar[path + "/size2"] >> size2; ar[path + "/reserved_size1"] >> reserved_size1; std::vector<T> data; ar[path + "/values"] >> data; alps::numeric::matrix<T,MemoryBlock> m2(reserved_size1,size2); assert(m2.capacity().first == reserved_size1); copy(data.begin(), data.end(), col(m2,0).first); m2.resize(size1,size2); swap(m, m2); return; }
template <typename T, typename MemoryBlock> void save( archive& ar , std::string const& path , alps::numeric::matrix<T, MemoryBlock> const& m , std::vector<std::size_t> size = std::vector<std::size_t>() , std::vector<std::size_t> chunk = std::vector<std::size_t>() , std::vector<std::size_t> offset = std::vector<std::size_t>() ) { using std::copy; using std::fill_n; using alps::cast; typedef typename alps::numeric::matrix<T,MemoryBlock>::const_col_element_iterator col_iterator; if (is_continuous<T>::value && m.empty()) ar.write(path, static_cast<typename scalar_type<alps::numeric::matrix<T, MemoryBlock> >::type const *>(NULL), std::vector<std::size_t>()); else if (is_continuous<T>::value) { std::vector<std::size_t> extent(get_extent(m)); copy(extent.begin(),extent.end(), std::back_inserter(size)); // We want to write one column: chunk.push_back(1); // How much memory does the column and the elements it contains need? copy(extent.begin()+1,extent.end(), std::back_inserter(chunk)); std::size_t const offset_col_index = offset.size(); fill_n(std::back_inserter(offset), extent.size(), 0); // Write column by column for(std::size_t j=0; j < num_cols(m); ++j) { offset[offset_col_index] = j; ar.write(path, get_pointer(*(col(m, j).first)), size, chunk, offset); } } else if (m.empty()) ar.write(path, static_cast<int const *>(NULL), std::vector<std::size_t>()); else if (is_vectorizable(m)) { size.push_back(num_cols(m)); size.push_back(num_rows(m)); // We want to write element by element: chunk.push_back(1); chunk.push_back(1); std::size_t const offset_col_index = offset.size(); std::size_t const offset_row_index = offset.size()+1; offset.push_back(0); offset.push_back(0); for(std::size_t j=0; j < num_cols(m); ++j) { offset[offset_col_index] = j; for(std::size_t i=0; i< num_rows(m); ++i) { offset[offset_row_index] = i; save(ar, path, m(i,j), size, chunk, offset); } } } else { if( ar.is_data(path) ) ar.delete_data(path); for(std::size_t j=0; j < num_cols(m); ++j) for(std::size_t i=0; i < num_rows(m); ++i) save(ar, ar.complete_path(path) + "/" + cast<std::string>(j) + "/" + cast<std::string>(i), m(i,j) ); } }
void serialize(archive& a) { std::size_t len; a & len; // Possibly extraneous: // Buffer might always be null when loading // since this base_str was just constructed if (buffer) { delete[] buffer; buffer = 0; } if (len) { buffer = new char[len + 1]; buffer[len] = 0; } else { buffer = 0; } if ( buffer ) { a.read(buffer, len); } }
void reflect(archive& ar) { ar.reflect("name", name, std::string("")); ar.reflect("weight", weight, 0u); ar.vreflect("bages", bages); }
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include "test.hpp" #include "archive.hpp" #include "hex.hpp" #include <boost/uuid/uuid_io.hpp> #include <boost/uuid/uuid_generators.hpp> using namespace koi; TEST_CASE("chive/basic", "just testing some basic serialization/deserialization") { std::vector<std::string> slist; slist.push_back("one"); slist.push_back("tu"); archive a; a << 1 << "hello" << "wee" << slist; a.done(); LOG_TRACE("%s\n", a.to_string().c_str()); REQUIRE(a.size() == 23); archive::iterator i = a.begin(); archive::iterator e = a.end(); REQUIRE(i != e); archive::iterator z = ++i; REQUIRE(z != i); REQUIRE(z != e);