LCOV - code coverage report
Current view: top level - src/libs/tools/include/merging - mergingkdb.hpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 5 8 62.5 %
Date: 2019-09-12 12:28:41 Functions: 2 4 50.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #ifndef KDBMERGE_HPP_
      10             : #define KDBMERGE_HPP_
      11             : 
      12             : #include <kdb.hpp>
      13             : #include <merging/threewaymerge.hpp>
      14             : 
      15             : namespace kdb
      16             : {
      17             : 
      18             : namespace tools
      19             : {
      20             : 
      21             : namespace merging
      22             : {
      23             : 
      24             : /**
      25             :  * Provides a merging wrapper around a KDB instance. The wrapper allows to pass
      26             :  * a three way merger instance that is used to resolve conflicts during KDB set.
      27             :  */
      28             : class MergingKDB : public KDB
      29             : {
      30             : public:
      31             :         MergingKDB ();
      32             :         explicit MergingKDB (KDB & kdb);
      33             :         virtual ~MergingKDB () throw ();
      34             : 
      35             :         /**
      36             :          * Behaves like the KDB get function
      37             :          *
      38             :          * @see KDB
      39             :          */
      40             :         int get (KeySet & returned, std::string const & keyname) override;
      41             : 
      42             :         /**
      43             :          * Behaves like the KDB get function
      44             :          *
      45             :          * @see KDB
      46             :          */
      47             :         int get (KeySet & returned, Key & parentKey) override;
      48             : 
      49             :         /**
      50             :          * Synchronizes the file with the supplied KeySet.
      51             :          * If a conflict occurs during set, the supplied merger is used to resolve the conflict.
      52             :          * If the conflict cannot be solved, an exception is thrown. If the KeySet was successfully
      53             :          * written (either by merging or due the absence of a conflict) the supplied KeySet is updated
      54             :          * with the new content of the file.
      55             :          *
      56             :          * @see KDB
      57             :          * @throws MergingKDBException
      58             :          */
      59             :         virtual int synchronize (KeySet & returned, std::string const & keyname, ThreeWayMerge & merger);
      60             : 
      61             :         /**
      62             :          * If a conflict occurs during set, the supplied merger is used to resolve the conflict.
      63             :          * If the conflict cannot be solved, an exception is thrown. If the KeySet was successfully
      64             :          * written (either by merging or due the absence of a conflict) the supplied KeySet is updated
      65             :          * with the new content of the file.
      66             :          *
      67             :          * @see KDB
      68             :          * @throws MergingKDBException
      69             :          */
      70             :         virtual int synchronize (KeySet & returned, Key & parentKey, ThreeWayMerge & merger);
      71             : 
      72             : private:
      73             :         KeySet base;
      74             : };
      75             : 
      76             : class MergingKDBException : public KDBException
      77             : {
      78             : public:
      79           8 :         MergingKDBException (Key key, KeySet conflicts) : KDBException (key), m_conflicts (conflicts)
      80             :         {
      81           2 :         }
      82             : 
      83           2 :         virtual ~MergingKDBException () throw ()
      84           4 :         {
      85           2 :         }
      86             : 
      87           0 :         virtual const char * what () const throw () override
      88             :         {
      89           0 :                 return "Exception while merging conflicting KeySets";
      90             :         }
      91             : 
      92             :         KeySet getConflicts () const
      93             :         {
      94           0 :                 return m_conflicts;
      95             :         }
      96             : 
      97             : private:
      98             :         KeySet m_conflicts;
      99             : };
     100             : } // namespace merging
     101             : } // namespace tools
     102             : } // namespace kdb
     103             : 
     104             : 
     105             : #endif /* KDBMERGE_HPP_ */

Generated by: LCOV version 1.13