Adjust InMemoryStorageBackend to ChecksumStream 57/33857/29
authorPawel Wieczorek <p.wieczorek2@samsung.com>
Thu, 15 Jan 2015 09:53:18 +0000 (10:53 +0100)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Wed, 4 Mar 2015 13:09:08 +0000 (14:09 +0100)
Now InMemoryStorageBackend uses ChecksumStream instead of std::ofstream.
New member function dumpDatabase() was introduced in order to destruct
database index stream before calling integrity mechanism.

Change-Id: I5ea943e1ec21f02cea97699993ddbd0f3eeb0a62

src/storage/InMemoryStorageBackend.cpp
src/storage/InMemoryStorageBackend.h

index 249eccf..57a4b56 100644 (file)
@@ -35,7 +35,6 @@
 #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>
@@ -105,13 +104,11 @@ void InMemoryStorageBackend::load(void) {
 }
 
 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();
@@ -230,7 +227,17 @@ void InMemoryStorageBackend::erasePolicies(const PolicyBucketId &bucketId, bool
     }
 }
 
-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);
@@ -243,15 +250,6 @@ void InMemoryStorageBackend::openFileStream(std::shared_ptr<std::ifstream> strea
     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;
@@ -266,14 +264,15 @@ std::shared_ptr<BucketDeserializer> InMemoryStorageBackend::bucketStreamOpener(
     }
 }
 
-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) {
index 65af96b..ea1f2bd 100644 (file)
@@ -27,6 +27,7 @@
 #include <memory>
 #include <string>
 
+#include <exceptions/CannotCreateFileException.h>
 #include <types/pointers.h>
 #include <types/PolicyBucket.h>
 #include <types/PolicyBucketId.h>
@@ -35,6 +36,7 @@
 
 #include <storage/BucketDeserializer.h>
 #include <storage/Buckets.h>
+#include <storage/ChecksumStream.h>
 #include <storage/ChecksumValidator.h>
 #include <storage/Integrity.h>
 #include <storage/StorageBackend.h>
@@ -66,16 +68,18 @@ public:
                                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);
 
@@ -98,6 +102,16 @@ protected:
     }
 };
 
+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_ */