| 200 | |
| 201 | template <typename T, typename FileDescType> |
| 202 | named_prog<T> getCachedFile( |
| 203 | FileDescType &file_object, |
| 204 | umap_str_shptr<T>& map ) |
| 205 | { |
| 206 | |
| 207 | // printf("INside get cached file\n"); |
| 208 | std::string name = file_object.filename; |
| 209 | |
| 210 | // Find memory cached T object |
| 211 | auto it = map.find(name); |
| 212 | if ( it != map.end()) { |
| 213 | // std::cout<<"found memory-cached file "<<name<<std::endl; |
| 214 | return std::make_pair(name, it->second); |
| 215 | } |
| 216 | else { // Find file cached T object |
| 217 | bool successful_read = false; |
| 218 | std::string serialized; |
| 219 | std::string cache_dir = getCacheDir(); |
| 220 | std::string file_name = cache_dir + "/" + name; |
| 221 | if (not cache_dir.empty() ) { |
| 222 | // TODO: Use OS-agnostic path separator here |
| 223 | // std::cout<<"looking for prog in file "<<file_name<<std::endl; |
| 224 | file_object.open(file_name.c_str(), "r"); |
| 225 | cacheFile file{file_name}; |
| 226 | serialized = file.read_file(); |
| 227 | successful_read = file.is_read_successful(); |
| 228 | // std::cout << "successful_read: " << successful_read << std::endl; |
| 229 | if(successful_read) { |
| 230 | file_object.close(); |
| 231 | // std::cout << "Just closed" << std::endl; |
| 232 | } |
| 233 | } |
| 234 | if (not successful_read) { |
| 235 | // JIT compile and write to file if possible |
| 236 | // std::cout << "not successful read. macrofying" << std::endl; |
| 237 | file_object.open(file_name.c_str(), "w"); |
| 238 | file_object.macrofy(); |
| 239 | // std::cout<<" got fresh content for "<<name<<std::endl; |
| 240 | file_object.close(); |
| 241 | |
| 242 | if (not cache_dir.empty()) { |
| 243 | // std::cout<<"writing in file "<<file_name<<std::endl; |
| 244 | cacheFile file{file_name}; |
| 245 | |
| 246 | cacheFile macrofied{name}; |
| 247 | serialized = macrofied.read_file(); |
| 248 | file.write(serialized); |
| 249 | } |
| 250 | } |
| 251 | // Add deserialized T to cache and return |
| 252 | map[name] = std::make_shared<std::string>(serialized); |
| 253 | //std::cout<<"storing file in memory "<<name<<std::endl; |
| 254 | return std::make_pair(name, map[name]); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | // GetCached |
nothing calls this directly
no test coverage detected