#include <log/log.h>
#include <config/PathConfig.h>
#include <exceptions/BucketNotExistsException.h>
-#include <exceptions/CannotCreateFileException.h>
#include <exceptions/DatabaseException.h>
#include <exceptions/FileNotFoundException.h>
#include <exceptions/UnexpectedErrorException.h>
}
void InMemoryStorageBackend::save(void) {
- auto indexStream = std::make_shared<std::ofstream>();
- std::string indexFilename = m_dbPath + m_indexFilename;
- openDumpFileStream(indexStream, indexFilename + m_backupFilenameSuffix);
+ std::string checksumFilename = m_dbPath + m_chsFilename;
+ auto chsStream = std::make_shared<std::ofstream>();
+ openDumpFileStream<std::ofstream>(chsStream, checksumFilename + m_backupFilenameSuffix);
- StorageSerializer<std::ofstream> storageSerializer(indexStream);
- storageSerializer.dump(buckets(), std::bind(&InMemoryStorageBackend::bucketDumpStreamOpener,
- this, std::placeholders::_1));
+ dumpDatabase(chsStream);
m_integrity.syncDatabase(buckets(), true);
m_integrity.createBackupGuard();
}
}
-void InMemoryStorageBackend::openFileStream(std::shared_ptr<std::ifstream> stream,
+void InMemoryStorageBackend::dumpDatabase(const std::shared_ptr<std::ofstream> &chsStream) {
+ auto indexStream = std::make_shared<ChecksumStream>(m_indexFilename, chsStream);
+ std::string indexFilename = m_dbPath + m_indexFilename;
+ openDumpFileStream<ChecksumStream>(indexStream, indexFilename + m_backupFilenameSuffix);
+
+ StorageSerializer<ChecksumStream> storageSerializer(indexStream);
+ storageSerializer.dump(buckets(), std::bind(&InMemoryStorageBackend::bucketDumpStreamOpener,
+ this, std::placeholders::_1, chsStream));
+}
+
+void InMemoryStorageBackend::openFileStream(const std::shared_ptr<std::ifstream> &stream,
const std::string &filename, bool isBackupValid) {
// TODO: Consider adding exceptions to streams and handling them:
// stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
m_checksum.compare(*stream, filename, isBackupValid);
}
-void InMemoryStorageBackend::openDumpFileStream(std::shared_ptr<std::ofstream> stream,
- const std::string &filename) {
- stream->open(filename, std::ofstream::out | std::ofstream::trunc);
-
- if (!stream->is_open()) {
- throw CannotCreateFileException(filename);
- }
-}
-
std::shared_ptr<BucketDeserializer> InMemoryStorageBackend::bucketStreamOpener(
const PolicyBucketId &bucketId, const std::string &filenameSuffix, bool isBackupValid) {
std::string bucketFilename = m_dbPath + m_bucketFilenamePrefix + bucketId + filenameSuffix;
}
}
-std::shared_ptr<StorageSerializer<std::ofstream> > InMemoryStorageBackend::bucketDumpStreamOpener(
- const PolicyBucketId &bucketId) {
+std::shared_ptr<StorageSerializer<ChecksumStream> > InMemoryStorageBackend::bucketDumpStreamOpener(
+ const PolicyBucketId &bucketId, const std::shared_ptr<std::ofstream> &chsStream) {
std::string bucketFilename = m_dbPath + m_bucketFilenamePrefix +
bucketId + m_backupFilenameSuffix;
- auto bucketStream = std::make_shared<std::ofstream>();
+ auto bucketStream = std::make_shared<ChecksumStream>(m_bucketFilenamePrefix + bucketId,
+ chsStream);
- openDumpFileStream(bucketStream, bucketFilename);
- return std::make_shared<StorageSerializer<std::ofstream> >(bucketStream);
+ openDumpFileStream<ChecksumStream>(bucketStream, bucketFilename);
+ return std::make_shared<StorageSerializer<ChecksumStream> >(bucketStream);
}
void InMemoryStorageBackend::postLoadCleanup(bool isBackupValid) {
#include <memory>
#include <string>
+#include <exceptions/CannotCreateFileException.h>
#include <types/pointers.h>
#include <types/PolicyBucket.h>
#include <types/PolicyBucketId.h>
#include <storage/BucketDeserializer.h>
#include <storage/Buckets.h>
+#include <storage/ChecksumStream.h>
#include <storage/ChecksumValidator.h>
#include <storage/Integrity.h>
#include <storage/StorageBackend.h>
const PolicyKey &filter);
protected:
- void openFileStream(std::shared_ptr<std::ifstream> stream, const std::string &filename,
+ void dumpDatabase(const std::shared_ptr<std::ofstream> &chsStream);
+ void openFileStream(const std::shared_ptr<std::ifstream> &stream, const std::string &filename,
bool isBackupValid);
std::shared_ptr<BucketDeserializer> bucketStreamOpener(const PolicyBucketId &bucketId,
const std::string &fileNameSuffix,
bool isBackupValid);
- virtual void openDumpFileStream(std::shared_ptr<std::ofstream> stream,
- const std::string &filename);
- std::shared_ptr<StorageSerializer<std::ofstream> > bucketDumpStreamOpener(
- const PolicyBucketId &bucketId);
+ template<typename StreamType>
+ void openDumpFileStream(const std::shared_ptr<StreamType> &stream,
+ const std::string &filename);
+ std::shared_ptr<StorageSerializer<ChecksumStream> > bucketDumpStreamOpener(
+ const PolicyBucketId &bucketId, const std::shared_ptr<std::ofstream> &chsStream);
virtual void postLoadCleanup(bool isBackupValid);
}
};
+template<typename StreamType>
+void InMemoryStorageBackend::openDumpFileStream(const std::shared_ptr<StreamType> &stream,
+ const std::string &filename) {
+ stream->open(filename, std::ofstream::out | std::ofstream::trunc);
+
+ if (!stream->is_open()) {
+ throw CannotCreateFileException(filename);
+ }
+}
+
} /* namespace Cynara */
#endif /* SRC_STORAGE_INMEMORYSTORAGEBACKEND_H_ */