disk_storage.cpp
Go to the documentation of this file.
00001 #include <megatree/disk_storage.h>
00002 
00003 #include <boost/filesystem/operations.hpp>
00004 #include <boost/filesystem/convenience.hpp>
00005 
00006 
00007 namespace megatree {
00008 
00009 
00010 
00011 
00012 void DiskStorage::get(const boost::filesystem::path &path, ByteVec &result)
00013 {
00014   assert(boost::filesystem::exists(root / path));
00015 
00016   // mmaps the file
00017   boost::iostreams::mapped_file_params params;
00018   params.path = (root / path).string();
00019   params.mode = std::ios_base::in;
00020   params.offset = 0;
00021   boost::iostreams::mapped_file file(params);
00022   
00023   // Copies data from the file
00024   result.resize(file.size());
00025   memcpy((void*)&result[0], file.const_data(), file.size());
00026 }
00027 
00028 void DiskStorage::getBatch(const std::vector<boost::filesystem::path> &paths, std::vector<ByteVec> &results)
00029 {
00030   // TODO: Execute the reads in parallel
00031   results.resize(paths.size());
00032   for (size_t i = 0; i < paths.size(); ++i)
00033     get(paths[i], results[i]);
00034 }
00035 
00036 void DiskStorage::putBatch(const std::vector<boost::filesystem::path> &paths, std::vector<ByteVec> &data)
00037 {
00038   assert(paths.size() == data.size());
00039   for (size_t i = 0; i < paths.size(); ++i)
00040   {
00041     assert(data[i].size() > 0);
00042     boost::filesystem::path abspath = root / paths[i];
00043 
00044     //make sure thate the directory exists
00045     if (!boost::filesystem::exists(abspath.parent_path()))
00046       boost::filesystem::create_directories(abspath.parent_path());
00047 
00048     // mmaps the file
00049     boost::iostreams::mapped_file_params params;
00050     params.path = abspath.string();
00051     params.mode = std::ios_base::out;
00052     params.offset = 0;
00053     params.new_file_size = data[i].size();
00054     boost::iostreams::mapped_file file(params);
00055 
00056     // Writes the data to the file
00057     memcpy(file.data(), (void*)&data[i][0], data[i].size());
00058     file.close();
00059   }
00060 }
00061 
00062 void DiskStorage::readerFunction(const boost::filesystem::path &path, GetCallback callback)
00063 {
00064   ByteVec result;
00065   get(path, result);
00066   callback(result);
00067 }
00068 
00069 void DiskStorage::getAsync(const boost::filesystem::path &path, GetCallback callback)
00070 {
00071   function_caller.addFunction(boost::bind(&DiskStorage::readerFunction, this, path, callback));
00072 }
00073 
00074 
00075 void DiskStorage::writerFunction(const boost::filesystem::path &path, const ByteVec &data, PutCallback callback)
00076 {
00077   put(path, data);
00078   callback();
00079 }
00080 
00081 void DiskStorage::putAsync(const boost::filesystem::path &path, const ByteVec &data, PutCallback callback)
00082 {
00083   function_caller.addFunction(boost::bind(&DiskStorage::writerFunction, this, path, data, callback));
00084 }
00085 
00086 
00087 
00088 }


megatree_storage
Author(s): Wim Meeussen
autogenerated on Thu Nov 28 2013 11:30:25