Skip to content
Snippets Groups Projects
sqlite_storage.hh 2.84 KiB
Newer Older
  • Learn to ignore specific revisions
  • Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    #ifndef __VEREIGN_KVSTORE_SQLITE_STORAGE_HH
    #define __VEREIGN_KVSTORE_SQLITE_STORAGE_HH
    
    #include <vereign/kvstore/storage.hh>
    #include <vereign/sqlite/connection.hh>
    
    namespace vereign::kvstore {
    
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    /**
     * Sqlite implementation of the kvstore::Storage interface.
     */
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    class SqliteStorage : public Storage {
    public:
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
      /**
       * Creates SqliteStorage instance.
       *
       * @param db_path Full path to the sqlite database file.
       */
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
      SqliteStorage(const std::string& db_path);
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    
      /**
       * Closes the connection with the database.
       */
    
      ~SqliteStorage() override;
    
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
      // disable copying
    
      SqliteStorage(const SqliteStorage&) = delete;
      auto operator=(const SqliteStorage&) -> SqliteStorage& = delete;
    
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
      /**
       * Locks the storage.
       *
       * The lock must be recursive, meaning that may be called multiple times.
       * Storage::Unlock must be called the same number of times the Storage::Lock was called.
       *
       * Use kvstore::Lock for lock guard and lock with retrials.
       *
       * @throws LockError when the lock is held by another process.
       */
    
      void Lock() override;
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    
      /**
       * Locks the storage.
       *
       * The lock must be recursive, meaning that may be called multiple times.
       * Storage::Unlock must be called the same number of times the Storage::Lock was called.
       *
       * Use kvstore::Lock for lock guard and lock with retrials.
       */
    
      void Unlock() override;
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
      /**
       * Deletes all the values in the storage.
       *
       * @throws sqlite::Error on failure.
       */
    
      void DeleteAll() override;
    
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
      /**
       * Store bytes value into the storage.
       *
       * @param key The key under which the value will be stored.
       * @param value The bytes that will be stored.
       *
       * @throws sqlite::Error on failure.
       */
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
      void PutBytes(const std::string& key, bytes::View value) override;
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    
      /**
       * Retrieve bytes value from the storage.
       *
       * @param key The key of the value that will be retrieved.
       * @param value Buffer where the value will be returned.
       *
       * @throws ValueNotFoundError when the key does not exist.
       * @throws sqlite::Error on failure.
       */
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
      void GetBytes(const std::string& key, bytes::Buffer& value) override;
    
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
      /**
       * Store int64_t value into the storage.
       *
       * @param key The key under which the value will be stored.
       * @param value The value that will be stored.
       *
       * @throws sqlite::Error on failure.
       */
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
      void PutInt64(const std::string& key, int64_t value) override;
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    
      /**
       * Retrieve int64_t value from the storage.
       *
       * @param key The key of the value that will be retrieved.
       * @param value Buffer where the value will be returned.
       *
       * @throws ValueNotFoundError when the key does not exist.
       * @throws sqlite::Error on failure.
       */
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
      auto GetInt64(const std::string& key) -> int64_t override;
    
    private:
      sqlite::Connection db_;
    
      int lock_count_ = 0;
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    };
    
    } // namespace vereign::kvstore
    
    #endif // __VEREIGN_KVSTORE_SQLITE_STORAGE_HH