namespace AdminPolicyParser {
-CynaraAdminPolicies parse(const std::shared_ptr<std::istream> &input,
+CynaraAdminPolicies parse(std::istream &input,
std::function<PolicyType(const std::string &)> translatePolicy) {
CynaraAdminPolicies policies;
std::string line;
std::size_t lineNum = 1;
- while (std::getline(*input, line, PathConfig::StoragePath::recordSeparator)) {
+ while (std::getline(input, line, PathConfig::StoragePath::recordSeparator)) {
if (line.empty())
break;
namespace AdminPolicyParser {
-CynaraAdminPolicies parse(const std::shared_ptr<std::istream> &input,
- std::function<PolicyType(const std::string &)>);
+CynaraAdminPolicies parse(std::istream &input, std::function<PolicyType(const std::string &)>);
} /* namespace AdminPolicyParser */
try {
using Cynara::AdminPolicyParser::parse;
- auto policies = parse(input, std::bind(&PolicyTypeTranslator::translate,
+ auto policies = parse(*input, std::bind(&PolicyTypeTranslator::translate,
&m_policyTranslator, std::placeholders::_1));
auto ret = m_adminApiWrapper.cynara_admin_set_policies(m_cynaraAdmin, policies.data());
if (ret != CYNARA_API_SUCCESS)
}
try {
- auto chsStream = std::make_shared<std::ifstream>();
+ std::ifstream chsStream;
openFileStream(chsStream, chsFilename, isBackupValid);
- m_checksum.load(*chsStream);
+ m_checksum.load(chsStream);
auto indexStream = std::make_shared<std::ifstream>();
- openFileStream(indexStream, indexFilename, isBackupValid);
+ openFileStream(*indexStream, indexFilename, isBackupValid);
StorageDeserializer storageDeserializer(indexStream,
std::bind(&InMemoryStorageBackend::bucketStreamOpener, this,
void InMemoryStorageBackend::save(void) {
std::string checksumFilename = m_dbPath + m_chsFilename;
auto chsStream = std::make_shared<std::ofstream>();
- openDumpFileStream<std::ofstream>(chsStream, checksumFilename + m_backupFilenameSuffix);
+ openDumpFileStream<std::ofstream>(*chsStream, checksumFilename + m_backupFilenameSuffix);
dumpDatabase(chsStream);
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);
+ 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) {
+void InMemoryStorageBackend::openFileStream(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);
- stream->open(filename);
+ stream.open(filename);
- if (!stream->is_open()) {
+ if (!stream.is_open()) {
throw FileNotFoundException(filename);
}
- m_checksum.compare(*stream, filename, isBackupValid);
+ m_checksum.compare(stream, filename, isBackupValid);
}
std::shared_ptr<BucketDeserializer> InMemoryStorageBackend::bucketStreamOpener(
std::string bucketFilename = m_dbPath + m_bucketFilenamePrefix + bucketId + filenameSuffix;
auto bucketStream = std::make_shared<std::ifstream>();
try {
- openFileStream(bucketStream, bucketFilename, isBackupValid);
+ openFileStream(*bucketStream, bucketFilename, isBackupValid);
return std::make_shared<BucketDeserializer>(bucketStream);
} catch (const FileNotFoundException &) {
return nullptr;
auto bucketStream = std::make_shared<ChecksumStream>(m_bucketFilenamePrefix + bucketId,
chsStream);
- openDumpFileStream<ChecksumStream>(bucketStream, bucketFilename);
+ openDumpFileStream<ChecksumStream>(*bucketStream, bucketFilename);
return std::make_shared<StorageSerializer<ChecksumStream> >(bucketStream);
}
protected:
void dumpDatabase(const std::shared_ptr<std::ofstream> &chsStream);
- void openFileStream(const std::shared_ptr<std::ifstream> &stream, const std::string &filename,
- bool isBackupValid);
+ void openFileStream(std::ifstream &stream, const std::string &filename, bool isBackupValid);
std::shared_ptr<BucketDeserializer> bucketStreamOpener(const PolicyBucketId &bucketId,
const std::string &fileNameSuffix,
bool isBackupValid);
template<typename StreamType>
- void openDumpFileStream(const std::shared_ptr<StreamType> &stream,
- const std::string &filename);
+ void openDumpFileStream(StreamType &stream, const std::string &filename);
std::shared_ptr<StorageSerializer<ChecksumStream> > bucketDumpStreamOpener(
const PolicyBucketId &bucketId, const std::shared_ptr<std::ofstream> &chsStream);
};
template<typename StreamType>
-void InMemoryStorageBackend::openDumpFileStream(const std::shared_ptr<StreamType> &stream,
+void InMemoryStorageBackend::openDumpFileStream(StreamType &stream,
const std::string &filename) {
- stream->open(filename, std::ofstream::out | std::ofstream::trunc);
+ stream.open(filename, std::ofstream::out | std::ofstream::trunc);
- if (!stream->is_open()) {
+ if (!stream.is_open()) {
throw CannotCreateFileException(filename);
}
}
*input << "invalid input" << std::endl;
- ASSERT_THROW(Cynara::AdminPolicyParser::parse(input, translatePolicy),
+ ASSERT_THROW(Cynara::AdminPolicyParser::parse(*input, translatePolicy),
Cynara::BucketRecordCorruptedException);
}
Cynara::CynaraAdminPolicies expectedPolicies;
expectedPolicies.seal();
- auto policies = Cynara::AdminPolicyParser::parse(input, translatePolicy);
+ auto policies = Cynara::AdminPolicyParser::parse(*input, translatePolicy);
ASSERT_TRUE(policies.sealed());
ASSERT_THAT(policies.data(), AdmPolicyListEq(expectedPolicies.data()));
expectedPolicies.add("b", { 0, "m" }, { "c", "u", "p" });
expectedPolicies.seal();
- auto policies = Cynara::AdminPolicyParser::parse(input, translatePolicy);
+ auto policies = Cynara::AdminPolicyParser::parse(*input, translatePolicy);
ASSERT_TRUE(policies.sealed());
ASSERT_THAT(policies.data(), AdmPolicyListEq(expectedPolicies.data()));
expectedPolicies.add("b2", { 0, "m2" }, { "c2", "u2", "p2" });
expectedPolicies.seal();
- auto policies = Cynara::AdminPolicyParser::parse(input, translatePolicy);
+ auto policies = Cynara::AdminPolicyParser::parse(*input, translatePolicy);
ASSERT_TRUE(policies.sealed());
ASSERT_THAT(policies.data(), AdmPolicyListEq(expectedPolicies.data()));