Hi everybody.I'm having problems understanding the Boost::Serialization
library.
I've managed to get it to save my object... but once I try to load them in
to the code doesn't compile.
It fails on the line where the ifstream writes to the object.
Basically what I'm trying to do is to save a hierarchy of objects that build
up a filesystem namespace for a virtual filesystem I'm trying to code.
It's something like this:
Filesystem_NS
--> fsFile
-->--> fsFileChunk
-->--> fsFileChunk
--> fsFile
-->--> fsFileChunk
-->--> fsFileChunk
The code isn't commented very well but it's nothing more than saving and
loading the data structures at the moment. There is no code/methods
note: There are more errors but they're so many that the forum doesn't allow
me to post all of them because of a character limit
It looks there's a problem that I'm boost::shared_ptr or something... I
don't know I've been probably looking at this for too long. The code
compiles if I remove the line where it loads the fsImage file back in to an
object.
Note that it does save properly. That is, I believe all the data shows up in
the saved archive although I can't be sure unless I'm able to restore the
state.
Could someone here give me a hand?
*hdfs.cpp*
Code:
#ifdef HAVE_CONFIG_H
#include
#endif
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <utility>
#include "FileSystem.h"
#include "fstypes.h"
#include "fsFile.h
"
#include "fsFileChunk.h"
#include
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
// string s1 = Path::normalizePath("http://asdfasd/fasdfa//asdf//");
string fsPath = "./fsImage";
FileSystem_NS * const fs = new FileSystem_NS(fsPath);
fileId_t fid = ++(*fs).nextFileId;
fsFile_Ptr ff(new fsFile(fid));
(*fs).files.insert(pair("biomynd",ff));
chunkVersion_t cv = 1;
size_t cs = 67108864;
fsFileChunk_Ptr fc(new fsFileChunk(++(*fs).nextChunkId, (*ff).fID, cv, cs));
ff->addChunk(fc);
ofstream ofs("./fsImage");
{
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << (const FileSystem_NS)(*fs);
// archive and stream closed when destructors are called
}
ofs.close();
FileSystem_NS const *newFilesystem = new FileSystem_NS("lol");
ifstream ifs("./fsImage");
{
// open the archive
boost::archive::text_iarchive ia(ifs);
// restore the schedule from the archive
ia >> (*newFilesystem);
}
ifs.close();
cout << fs->fsImage_Path << endl;
return EXIT_SUCCESS;
}
*FileSystem.h*
Code:
/************************************************************************
FileSystem.h - Copyright Hannes Baldursson
**************************************************************************/
#ifndef FILESYSTEM_H
#define FILESYSTEM_H
#include "fstypes.h"
#include "fsFile.h"
#include "fsFileChunk.h"
#include <string>
#include <map>
#include <vector>
#include
#include
#include
#include
#include
#endif
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <utility>
#include "FileSystem.h"
#include "fstypes.h"
#include "fsFile.h"
#include "fsFileChunk.h"
#include
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char *argvon/tracking.hpp>
#include
using namespace std;
// Filesystem namespace
class FileSystem_NS
{
public:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version) const {
ar & nextFileId;
ar & nextChunkId;
ar & files;
}
fileId_t nextFileId;
chunkId_t nextChunkId;
const string fsImage_Path;
FileSystem_NS(string fsImage = "./fsImage");
map files; // Maps filenames to fileIds
};
BOOST_CLASS_TRACKING(FileSystem_NS, boost::serialization::track_always)
#endif // FILESYSTEM_H
*fsFile.h*
Code:
#ifndef FSFILE_H
#define FSFILE_H
#include <string>
#include <map>
#include "fstypes.h"
#include "fsFileChunk.h"
#include
#include
using namespace std;
class fsFile {
public:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version) const
{
ar & fname;
ar & fID;
ar & chunks;
}
string fname;
fileId_t fID;
vector chunks;
// Create a fsFile
fsFile(fileId_t fileID);
void addChunk(fsFileChunk_Ptr ptr);
};
typedef boost::shared_ptr<fsFile> fsFile_Ptr;
BOOST_CLASS_TRACKING(fsFile, boost::serialization::track_always)
#endif
*fsFileChunk.h*
Code:
#ifndef FSFILE_CHUNK_H
#define FSFILE_CHUNK_H
#include <vector>
#include <string>
#include "fstypes.h"
#include
#include
#include
using namespace std;
class fsFileChunk {
public:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version) const {
ar & cID;
ar & fID;
ar & cVersion;
ar & cSize;
}
chunkId_t cID;
fileId_t fID; // This chunk belongs to file "fileID"
chunkVersion_t cVersion; // Current version
size_t cSize;
std::vector<string> hosts; // vector of hostIP:port for the datanodes
that host this chunk.
fsFileChunk(chunkId_t chunkID, fileId_t fileID, chunkVersion_t
chunkVersion, size_t chunkSize);
};
typedef boost::shared_ptr<fsFileChunk> fsFileChunk_Ptr;
BOOST_CLASS_TRACKING(fsFileChunk, boost::serialization::track_always)
#endif // FSFILE_CHUNK_H
*fstypes.h*
Code:
#ifndef COMMON_FSTYPES_H
#define COMMON_FSTYPES_H
#include
typedef unsigned long long fileId_t;
typedef unsigned long long chunkId_t;
typedef unsigned int chunkVersion_t;
#endif
These are all the header files plus the "main program".
Code:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/istream:231:
note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
_Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with
_CharT = char, _Traits = std::char_traits<char>] <near match>
/home/hannson/Projects/HDFS/src/boost/serialization/access.hpp: In
static member function 'static void
boost::serialization::access::serialize(Archive&, T&, unsigned int)
[with Archive = boost::archive::text_iarchive, T = const
std::map, boost::shared_ptr<fsFile>,
std::less >, std::allocator
, boost::shared_ptr<fsFile> > > >]':
/home/hannson/Projects/HDFS/src/boost/serialization/serialization.hpp:81:
instantiated from 'void boost::serialization::serialize(Archive&, T&,
unsigned int) [with Archive = boost::archive::text_iarchive, T = const
std::map, boost::shared_ptr<fsFile>,
std::less >, std::allocator > >]'
/home/hannson/Projects/HDFS/src/boost/serialization/serialization.hpp:140:
instantiated from 'void boost::serialization::serialize_adl(Archive&,
T&, unsigned int) [with Archive = boost::archive::text_iarchive, T =
const std::map, boost::shared_ptr<fsFile>,
std::less >, std::allocator > >]'
/home/hannson/Projects/HDFS/src/boost/archive/detail/iserializer.hpp:158:
instantiated from 'void boost::archive::detail::iserializer::load_object_data(boost::archive::detail::basic_iarchive&, void*,
unsigned int) const [with Archive = boost::archive::text_iarchive, T =
const std::map, boost::shared_ptr<fsFile>,
std::less >, std::allocator > >]'
/home/hannson/Projects/HDFS/src/hdfs.cpp:70: instantiated from here
/home/hannson/Projects/HDFS/src/boost/serialization/access.hpp:109:
error: 'const class std::map,
boost::shared_ptr<fsFile>, std::less >,
std::allocator
These are not all the error messages I get..
One important thing. I'm clearly missing something because even when I
comment out the fsFile part from the serialization (that is I'm only saving
nextFileId and nextChunkId in FileSystem_NS) it still doesn't compile when
I'm including the loading code so it seems I'm having two problems. One
problem is restoring the object in general, and the other is restoring the
objects when using a shared_ptr.
Btw, sorry about the html if you like it plain :)