Make StorageSerializer a template class
[platform/core/security/cynara.git] / src / storage / InMemoryStorageBackend.h
1 /*
2  * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file        src/storage/InMemoryStorageBackend.h
18  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
19  * @version     1.0
20  * @brief       Headers for InMemoryStorageBackend
21  */
22
23 #ifndef SRC_STORAGE_INMEMORYSTORAGEBACKEND_H_
24 #define SRC_STORAGE_INMEMORYSTORAGEBACKEND_H_
25
26 #include <fstream>
27 #include <memory>
28 #include <string>
29
30 #include <types/pointers.h>
31 #include <types/PolicyBucket.h>
32 #include <types/PolicyBucketId.h>
33 #include <types/PolicyKey.h>
34 #include <types/PolicyResult.h>
35
36 #include <storage/BucketDeserializer.h>
37 #include <storage/Buckets.h>
38 #include <storage/Integrity.h>
39 #include <storage/StorageBackend.h>
40 #include <storage/StorageSerializer.h>
41
42 namespace Cynara {
43
44 class InMemoryStorageBackend : public StorageBackend {
45 public:
46     InMemoryStorageBackend() = delete;
47     InMemoryStorageBackend(const std::string &path);
48     virtual ~InMemoryStorageBackend() {};
49
50     virtual void load(void);
51     virtual void save(void);
52
53     virtual PolicyBucket searchDefaultBucket(const PolicyKey &key);
54     virtual PolicyBucket searchBucket(const PolicyBucketId &bucketId, const PolicyKey &key);
55     virtual void insertPolicy(const PolicyBucketId &bucketId, PolicyPtr policy);
56     virtual void createBucket(const PolicyBucketId &bucketId, const PolicyResult &defaultPolicy);
57     virtual void updateBucket(const PolicyBucketId &bucketId, const PolicyResult &defaultPolicy);
58     virtual void deleteBucket(const PolicyBucketId &bucketId);
59     virtual bool hasBucket(const PolicyBucketId &bucketId);
60     virtual void deletePolicy(const PolicyBucketId &bucketId, const PolicyKey &key);
61     virtual void deleteLinking(const PolicyBucketId &bucketId);
62     virtual PolicyBucket::Policies listPolicies(const PolicyBucketId &bucketId,
63                                                 const PolicyKey &filter) const;
64     virtual void erasePolicies(const PolicyBucketId &bucketId, bool recursive,
65                                const PolicyKey &filter);
66
67 protected:
68     void openFileStream(std::shared_ptr<std::ifstream> stream, const std::string &filename);
69     std::shared_ptr<BucketDeserializer> bucketStreamOpener(const PolicyBucketId &bucketId,
70                                                            const std::string &fileNameSuffix);
71
72     virtual void openDumpFileStream(std::shared_ptr<std::ofstream> stream,
73                                     const std::string &filename);
74     std::shared_ptr<StorageSerializer<std::ofstream> > bucketDumpStreamOpener(
75             const PolicyBucketId &bucketId);
76
77     virtual void postLoadCleanup(bool isBackupValid);
78
79 private:
80     std::string m_dbPath;
81     Buckets m_buckets;
82     Integrity m_integrity;
83     static const std::string m_indexFilename;
84     static const std::string m_backupFilenameSuffix;
85     static const std::string m_bucketFilenamePrefix;
86
87 protected:
88     virtual Buckets &buckets(void) {
89         return m_buckets;
90     }
91     virtual const Buckets &buckets(void) const {
92         return m_buckets;
93     }
94 };
95
96 } /* namespace Cynara */
97
98 #endif /* SRC_STORAGE_INMEMORYSTORAGEBACKEND_H_ */