Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. class Database {
  2.  public:
  3.     auto Put(const Record& record) -> bool;
  4.     auto GetById(const string& id) const -> const Record*;
  5.     auto Erase(const string& id) -> bool;
  6.  
  7.     template <typename Callback> void RangeByTimestamp(int low, int high, Callback callback) const;
  8.     template <typename Callback> void RangeByKarma(int low, int high, Callback callback) const;
  9.     template <typename Callback> void AllByUser(const string& user, Callback callback) const;
  10.  
  11.  private:
  12.     unordered_map<string, Record> mDatabase;
  13.     unordered_map<int, unordered_set<string>> mKarmaIndex;
  14.     unordered_map<int, unordered_set<string>> mTimestampIndex;
  15.     unordered_map<string, unordered_set<string>> mUserIndex;
  16. };
  17.  
  18. auto Database::Put(const Record& record) -> bool {
  19.     const auto[recordIterator, bResult] = mDatabase.insert({record.id, record});
  20.     if (bResult) {
  21.         mKarmaIndex[record.karma].insert(record.id);
  22.         mTimestampIndex[record.timestamp].insert(record.id);
  23.         mUserIndex[record.user].insert(record.id);
  24.     }
  25.     return bResult;
  26. }
  27. auto Database::GetById(const string& id) const -> const Record* {
  28.     if (mDatabase.count(id)) {
  29.         return &mDatabase.at(id);
  30.     } else {
  31.         return nullptr;
  32.     }
  33. }
  34. auto Database::Erase(const string& id) -> bool {
  35.     const auto foundRecord = mDatabase.find(id);
  36.     if (foundRecord != mDatabase.end()) {
  37.         mKarmaIndex[foundRecord->second.karma].erase(id);
  38.         mTimestampIndex[foundRecord->second.timestamp].erase(id);
  39.         mUserIndex[foundRecord->second.user].erase(id);
  40.         mDatabase.erase(id);
  41.         return true;
  42.     } else {
  43.         return false;
  44.     }
  45. }
  46. template<typename Callback> void Database::RangeByTimestamp(int low, int high, Callback callback) const {
  47.     for (int i = low; i <= high; ++i) {
  48.         try {
  49.             for (const auto& item : mTimestampIndex.at(i)) {
  50.                 if (!callback(mDatabase.at(item))) {
  51.                     return;
  52.                 }
  53.             }
  54.         } catch (exception&) {}
  55.     }
  56. }
  57. template<typename Callback> void Database::RangeByKarma(int low, int high, Callback callback) const {
  58.     for (int i = low; i <= high; ++i) {
  59.         try {
  60.             for (const auto& item : mKarmaIndex.at(i)) {
  61.                 if (!callback(mDatabase.at(item))) {
  62.                     return;
  63.                 }
  64.             }
  65.         } catch (exception&) {}
  66.     }
  67. }
  68. template<typename Callback> void Database::AllByUser(const string& user, Callback callback) const {
  69.     if (!mUserIndex.count(user)) {
  70.         return;
  71.     }
  72.     for (const auto& item : mUserIndex.at(user)) {
  73.         if (!callback(mDatabase.at(item))) {
  74.             return;
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement