Add Data::StoreToFile

This commit is contained in:
Sebastian Messmer 2014-12-05 07:42:39 +01:00
parent e585d12511
commit e32a84eb8e
4 changed files with 23 additions and 10 deletions

View File

@ -1,12 +1,16 @@
#include <blobstore/implementations/ondisk/Data.h> #include <blobstore/implementations/ondisk/Data.h>
#include <stdexcept> #include <stdexcept>
#include <fstream>
using std::ofstream;
using std::ios;
namespace blobstore { namespace blobstore {
namespace ondisk { namespace ondisk {
Data::Data(size_t size) Data::Data(size_t size)
: _data(std::malloc(size)) { : _size(size), _data(std::malloc(size)) {
if (nullptr == _data) { if (nullptr == _data) {
throw std::bad_alloc(); throw std::bad_alloc();
} }
@ -25,5 +29,14 @@ const void *Data::data() const {
return _data; return _data;
} }
size_t Data::size() const {
return _size;
}
void Data::StoreToFile(const boost::filesystem::path &filepath) const {
ofstream file(filepath.c_str(), ios::binary | ios::trunc);
file.write((const char*)_data, _size);
}
} /* namespace ondisk */ } /* namespace ondisk */
} /* namespace blobstore */ } /* namespace blobstore */

View File

@ -6,6 +6,8 @@
//TODO Move this to a more generic utils //TODO Move this to a more generic utils
#include "fspp/utils/macros.h" #include "fspp/utils/macros.h"
#include <boost/filesystem/path.hpp>
namespace blobstore { namespace blobstore {
namespace ondisk { namespace ondisk {
@ -17,13 +19,17 @@ public:
void *data(); void *data();
const void *data() const; const void *data() const;
size_t size() const;
void StoreToFile(const boost::filesystem::path &filepath) const;
private: private:
size_t _size;
void *_data; void *_data;
DISALLOW_COPY_AND_ASSIGN(Data); DISALLOW_COPY_AND_ASSIGN(Data);
}; };
} /* namespace ondisk */ } /* namespace ondisk */
} /* namespace blobstore */ } /* namespace blobstore */

View File

@ -84,12 +84,7 @@ void OnDiskBlob::_fillDataWithZeroes() {
} }
void OnDiskBlob::_storeToDisk() const { void OnDiskBlob::_storeToDisk() const {
ofstream file(_filepath.c_str(), ios::binary | ios::trunc); _data.StoreToFile(_filepath);
_storeDataToStream(file);
}
void OnDiskBlob::_storeDataToStream(ostream &stream) const {
stream.write((const char*)_data.data(), _size);
} }
} /* namespace ondisk */ } /* namespace ondisk */

View File

@ -25,8 +25,7 @@ public:
void SetFileSize(size_t size) { void SetFileSize(size_t size) {
Data data(size); Data data(size);
ofstream writer(file.path().c_str(), ios::trunc | ios::binary); data.StoreToFile(file.path());
writer.write((char*)data.data(), size);
} }
}; };