Skip to content

Commit

Permalink
Fix some issues identified by a linter.
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Lynch <[email protected]>
  • Loading branch information
aexoden committed Feb 3, 2019
1 parent 3bb2e26 commit 9cc1ff9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
33 changes: 15 additions & 18 deletions src/cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include "cache.hh"
#include "state.hh"

Cache::Cache() {}

Cache::~Cache() {}

std::pair<int, Milliframes> DynamicCache::get(const State & state) {
Expand Down Expand Up @@ -49,23 +47,22 @@ std::size_t FixedCache::_get_index(const std::tuple<uint64_t, uint64_t, uint64_t
}

PersistentCache::PersistentCache(const std::string & filename) {
leveldb::Options options;
options.create_if_missing = true;
options.max_open_files = 524288;
options.block_cache = leveldb::NewLRUCache(1024 * 1048576);
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
_options.create_if_missing = true;
_options.max_open_files = 524288;
_options.block_cache = leveldb::NewLRUCache(1024 * 1048576);
_options.filter_policy = leveldb::NewBloomFilterPolicy(10);

auto status{leveldb::DB::Open(options, filename, &_db)};
auto status{leveldb::DB::Open(_options, filename, &_db)};

if (!status.ok()) {
std::cerr << "WARNING: Cache error: " << status.ToString() << '\n';
}
}

PersistentCache::~PersistentCache() {
if (_db) {
delete _db;
}
delete _db;
delete _options.block_cache;
delete _options.filter_policy;
}

std::pair<int, Milliframes> PersistentCache::get(const State & state) {
Expand Down Expand Up @@ -106,9 +103,9 @@ std::string PersistentCache::_encode_key(const State & state) const {
auto [key1, key2, key3] = state.get_keys();
std::string result{sizeof(key1) + sizeof(key2) + sizeof(key3), 0, std::string::allocator_type{}};

std::copy_n(reinterpret_cast<char*>(&key1), sizeof(key1), result.begin());
std::copy_n(reinterpret_cast<char*>(&key2), sizeof(key2), result.begin() + sizeof(key1));
std::copy_n(reinterpret_cast<char*>(&key3), sizeof(key3), result.begin() + sizeof(key1) + sizeof(key2));
std::copy_n(reinterpret_cast<char*>(&key1), sizeof(key1), result.begin()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
std::copy_n(reinterpret_cast<char*>(&key2), sizeof(key2), result.begin() + sizeof(key1)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
std::copy_n(reinterpret_cast<char*>(&key3), sizeof(key3), result.begin() + sizeof(key1) + sizeof(key2)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)

return result;
}
Expand All @@ -119,8 +116,8 @@ std::string PersistentCache::_encode_value(int value, Milliframes frames) const

std::string result{sizeof(int64_t) * 2, 0, std::string::allocator_type{}};

std::copy(reinterpret_cast<char*>(&value1), reinterpret_cast<char*>(&value1) + sizeof(value1), result.begin());
std::copy(reinterpret_cast<char*>(&value2), reinterpret_cast<char*>(&value2) + sizeof(value2), result.begin() + sizeof(value1));
std::copy(reinterpret_cast<char*>(&value1), reinterpret_cast<char*>(&value1) + sizeof(value1), result.begin()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast, cppcoreguidelines-pro-bounds-pointer-arithmetic)
std::copy(reinterpret_cast<char*>(&value2), reinterpret_cast<char*>(&value2) + sizeof(value2), result.begin() + sizeof(value1)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast, cppcoreguidelines-pro-bounds-pointer-arithmetic)

return result;
}
Expand All @@ -129,8 +126,8 @@ std::pair<int, Milliframes> PersistentCache::_decode_value(const std::string & d
int64_t value1{0};
int64_t value2{0};

std::copy(data.begin(), data.begin() + sizeof(value1), reinterpret_cast<char*>(&value1));
std::copy(data.begin() + sizeof(value1), data.begin() + sizeof(value1) + sizeof(value2), reinterpret_cast<char*>(&value2));
std::copy(data.begin(), data.begin() + sizeof(value1), reinterpret_cast<char*>(&value1)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
std::copy(data.begin() + sizeof(value1), data.begin() + sizeof(value1) + sizeof(value2), reinterpret_cast<char*>(&value2)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast, cppcoreguidelines-pro-bounds-pointer-arithmetic)

return std::make_pair(static_cast<int>(value1), Milliframes{value2});
}
11 changes: 6 additions & 5 deletions src/cache.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enum class CacheType {

class Cache {
public:
Cache();
Cache() = default;
Cache(const Cache &) = delete;
Cache & operator=(const Cache &) = delete;

Expand All @@ -41,7 +41,7 @@ class DynamicCache : public Cache {

class FixedCache : public Cache {
public:
FixedCache(std::size_t size);
explicit FixedCache(std::size_t size);

std::pair<int, Milliframes> get(const State & state) override;
void set(const State & state, int value, Milliframes frames) override;
Expand All @@ -56,7 +56,7 @@ class FixedCache : public Cache {

class PersistentCache : public Cache {
public:
PersistentCache(const std::string & filename);
explicit PersistentCache(const std::string & filename);
~PersistentCache() override;

std::pair<int, Milliframes> get(const State & state) override;
Expand All @@ -67,8 +67,9 @@ class PersistentCache : public Cache {
std::string _encode_value(int value, Milliframes frames) const;
std::pair<int, Milliframes> _decode_value(const std::string & data) const;

leveldb::DB * _db;
std::size_t _states;
leveldb::Options _options{};
leveldb::DB * _db{nullptr};
std::size_t _states{0};
};

#endif // ROSA_CACHE_HH

0 comments on commit 9cc1ff9

Please sign in to comment.